Line Chart

Create a Unicode sparkline line chart.

Usage

var LineChart = require( '@stdlib/plot/sparklines/unicode/line' );

LineChart( [data,] [options] )

Returns a chart instance.

var chart = new LineChart();

The constructor accepts the following options:

  • autoRender: boolean indicating whether to re-render on a change event.
  • bufferSize: data buffer size. If provided, data is kept in a first-in first-out (FIFO) buffer which cannot exceed the buffer size. Default: +infinity.
  • data: chart data.
  • description: chart description.
  • infinities: boolean flag indicating whether to encode infinite values. Default: false.
  • isDefined: accessor function indicating whether a datum is defined.
  • label: data label.
  • yMax: maximum value of the y-axis domain. If set to null, the maximum value is computed from the data.
  • yMin: minimum value of the y-axis domain. If set to null, the minimum value is computed from the data.

Writable Properties

LineChart.prototype.autoRender

Rendering mode. If true, an instance renders on each 'change' event; otherwise, rendering must be triggered manually.

var chart = new LineChart();

// Set:
chart.autoRender = false;

// Get:
var mode = chart.autoRender;
// returns false

LineChart.prototype.bufferSize

Data buffer size. If set, this specifies the maximum number of data elements which can be rendered. Once the data buffer is full, each new datum results in the oldest datum being removed.

var chart = new LineChart();

// Set:
chart.bufferSize = 3;

// Get:
var size = chart.bufferSize;
// returns 3

chart.data = [ 1, 2, 3 ];

var data = chart.data;
// returns [ 1, 2, 3 ]

chart.push( 4 );

data = chart.data;
// returns [ 2, 3, 4 ]

Setting a data buffer size is useful when rendering data streams.

LineChart.prototype.data

Chart data. When set, the value must be either array-like or an ndarray and cannot exceed the bufferSize.

var Float64Array = require( '@stdlib/array/float64' );

var chart = new LineChart();

// Set:
chart.data = new Float64Array( [ 3.14, 5.0, -3.14, -1.0 ] );

// Get:
var data = chart.data;
// returns [ 3.14, 5.0, -3.14, -1.0 ]

Note that data is copied to an internal data buffer.

LineChart.prototype.description

Chart description.

var chart = new LineChart();

// Set:
chart.description = 'Daily stock prices for the past 30 days.';

// Get:
var desc = chart.description;
// returns 'Daily stock prices for the past 30 days.'

LineChart.prototype.infinities

Boolean flag indicating whether to render infinite values. When set to false, infinite values are considered missing values. When set to true, both positive and negative infinity are encoded as .

var chart = new LineChart();

chart.infinities = true;

chart.data = [
    1.0,
    5.0,
    NaN,
    Infinity,
    4.0,
    -Infinity,
    3.0
];

var str = chart.render();
// returns '⡈⠉ ∞⢂∞⠒'

LineChart.prototype.isDefined( d, i )

An accessor function which defines whether a datum is defined. This accessor is used to define how missing values are encoded. When invoked, the function is provided two arguments:

  • d: datum
  • i: datum index
function isDefined( d ) {
    return ( d !== null );
}

var chart = new LineChart();

// Set:
chart.isDefined = isDefined;

// Get:
var fcn = chart.isDefined;
// returns <Function>

The default behavior is to ignore values which are NaN.

LineChart.prototype.label

Data label.

var chart = new LineChart();

// Set:
chart.label = 'beep';

// Get:
var label = chart.label;
// returns 'beep'

LineChart.prototype.yMin

Minimum value of the y-axis domain. If this value is set to a value other than null, the y-axis lower bound is fixed; otherwise, the minimum value is computed from the chart data.

var chart = new LineChart( [ -1.0, 5.0, -3.0, 2.0, -4.0, 4.0, 3.0 ] );

chart.yMin = 0.0;

var str = chart.render();
// returns '⡈⢁⡠⢄⡐⠒⠒'

LineChart.prototype.yMax

Maximum value of the y-axis domain. If this value is set to a value other than null, the y-axis upper bound is fixed; otherwise, the maximum value is computed based on the chart data.

var chart = new LineChart( [ -1.0, 5.0, -3.0, 2.0, -4.0, 4.0, 3.0 ] );

chart.yMax = 10.0;

var str = chart.render();
// returns '⠔⢂⡠⢄⡐⠒⠒'

Methods

LineChart.prototype.push( datum )

Appends data to a chart.

var chart = new LineChart( [ 1, 2, 3 ] );

var data = chart.data;
// returns [ 1, 2, 3 ]

chart.push( 4 );

data = chart.data;
// returns [ 1, 2, 3, 4 ]

LineChart.prototype.render()

Renders a line chart sparkline.

var chart = new LineChart( [ 1.0, 5.0, 3.0, 2.0, 4.0, 4.0, 3.0 ] );

var str = chart.render();
// returns '⡈⠑⠢⠔⠒⠒⠒'

LineChart.prototype.toString()

Serializes a line chart sparkline as a string by calling the render() method.

var chart = new LineChart( [ 1.0, 5.0, 3.0, 2.0, 4.0, 4.0, 3.0 ] );

var str = chart.toString();
// returns '⡈⠑⠢⠔⠒⠒⠒'

Events

'change'

Emitted whenever a property value changes.

var chart = new LineChart();

chart.on( 'change', onChange );

function onChange() {
    console.log( 'A property was updated.' );
}

'render'

Emitted whenever a sparkline is rendered.

var chart = new LineChart();

chart.on( 'render', onRender );

function onRender( str ) {
    console.log( 'Rendered sparkline: %s', str );
}

Examples

var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var stdout = require( '@stdlib/streams/node/stdout' );
var lineChart = require( '@stdlib/plot/sparklines/unicode/line' );

var chart;
var data;
var id;
var i;

// Generate some random data...
data = new Float64Array( 50 );
for ( i = 0; i < data.length; i++ ) {
    data[ i ] = randu() * 100.0;
}

// Create a new line chart:
chart = lineChart( data );

// Hide the terminal cursor:
stdout.write( '\u001b[?25l' );

// Render the chart in the terminal:
stdout.write( chart.render() );

// Configure the chart to support streaming data:
chart.bufferSize = data.length;
chart.yMin = 0.0;
chart.yMax = 100.0;

// Update the terminal chart with new data every second:
id = setInterval( update, 1000 );

// After some time, stop updating and close:
setTimeout( stop, 20000 );

function update() {
    // Update the chart with new data:
    chart.push( randu()*100.0 );

    // Clear the terminal line and render the chart:
    stdout.write( '\r\u001b[2K' + chart.render() );
}

function stop() {
    clearInterval( id );

    // Restore the terminal cursor:
    stdout.write( '\u001b[?25h' );

    stdout.write( '\n' );
}

CLI

Usage

Usage: sparkline-line [options] <number> <number> ...

Options:

  -h, --help             Print this message.
  -V, --version          Print the package version.
      --split sep        Separator used to split stdin data. Default: /\\r?\\n/.
      --ymin min         Minimum value of y-axis domain.
      --ymax max         Maximum value of y-axis domain.
      --infinities       Encode infinite values.

Notes

  • If the split separator is a regular expression, ensure that the split option is properly escaped.

    # Not escaped...
    $ echo -n $'1\n2\n3\n' | sparkline-line --split /\r?\n/
    
    # Escaped...
    $ echo -n $'1\n2\n3\n' | sparkline-line --split /\\r?\\n/
    

Examples

$ sparkline-line 1 2 3 4 5 6
⡠⠤⠔⠒⠊⠉
$ echo -n $'1\n2\n3\n4\n5\n6\n' | sparkline-line --ymax 3
⡐⠊⠉⠉⠉⠉
Did you find this page helpful?