Win/Loss Chart
Create a Unicode sparkline win/loss chart.
Usage
var WinLossChart = require( '@stdlib/plot/sparklines/unicode/win-loss' );
WinLossChart( [data,] [options] )
Returns a chart instance.
var chart = new WinLossChart();
The constructor accepts the following options
:
- autoRender:
boolean
indicating whether to re-render on achange
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.
- isDefined: accessor
function
indicating whether a datum is defined. - label: data label.
Writable Properties
WinLossChart.prototype.autoRender
Rendering mode. If true
, an instance renders on each 'change'
event; otherwise, rendering must be triggered manually.
var chart = new WinLossChart();
// Set:
chart.autoRender = false;
// Get:
var mode = chart.autoRender;
// returns false
WinLossChart.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 WinLossChart();
// 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.
WinLossChart.prototype.data
Chart data. When set, the value must be either array
-like or an ndarray and cannot exceed the bufferSize
.
var Int8Array = require( '@stdlib/array/int8' );
var chart = new WinLossChart();
// Set:
chart.data = new Int8Array( [ -2, 1, 2, 2, 1, -1, -1, 1 ] );
// Get:
var data = chart.data;
// returns [ -2, 1, 2, 2, 1, -1, -1, 1 ]
Note that data is copied to an internal data buffer.
WinLossChart.prototype.description
Chart description.
var chart = new WinLossChart();
// Set:
chart.description = 'Wins and losses for the past 30 days.';
// Get:
var desc = chart.description;
// returns 'Wins and losses for the past 30 days.'
WinLossChart.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 ) {
// Ignore home losses:
return ( d !== -2 );
}
var chart = new WinLossChart();
// Set:
chart.isDefined = isDefined;
// Get:
var fcn = chart.isDefined;
// returns <Function>
The default behavior is to ignore any values which are not 1
, -1
, 2
, or -2
.
WinLossChart.prototype.label
Data label.
var chart = new WinLossChart();
// Set:
chart.label = 'beep';
// Get:
var label = chart.label;
// returns 'beep'
Methods
WinLossChart.prototype.push( datum )
Appends data to a chart.
var chart = new WinLossChart( [ 1, 2, 3 ] );
var data = chart.data;
// returns [ 1, 2, 3 ]
chart.push( 4 );
data = chart.data;
// returns [ 1, 2, 3, 4 ]
WinLossChart.prototype.render()
Renders a win/loss chart sparkline.
var chart = new WinLossChart( [ -2, 1, 2, 2, 1, -1, -1, 1, -2, -2 ] );
var str = chart.render();
// returns '┌╵└┴╵╷╷╵┌┬'
Glyphs:
Value | Glyph |
---|---|
1 | ╵ |
-1 | ╷ |
2 | └ |
-2 | ┌ |
If a 2
or -2
is preceded by a 2
or -2
,
Value | Glyph |
---|---|
2 | ┴ |
-2 | ┬ |
Based on the win/loss analogy,
1
: win away-1
: loss away2
: win at home-2
: loss at home
If provided any value other than 1
, -1
, 2
, or -2
, the value is encoded as a missing value.
WinLossChart.prototype.toString()
Serializes a win/loss chart sparkline as a string
by calling the render()
method.
var chart = new WinLossChart( [ -2, 1, 2, 2, 1, -1, -1, 1, -2, -2 ] );
var str = chart.toString();
// returns '┌╵└┴╵╷╷╵┌┬'
Events
'change'
Emitted whenever a property value changes.
var chart = new WinLossChart();
chart.on( 'change', onChange );
function onChange() {
console.log( 'A property was updated.' );
}
'render'
Emitted whenever a sparkline is rendered.
var chart = new WinLossChart();
chart.on( 'render', onRender );
function onRender( str ) {
console.log( 'Rendered sparkline: %s', str );
}
Examples
var randu = require( '@stdlib/random/base/randu' );
var Int8Array = require( '@stdlib/array/int8' );
var stdout = require( '@stdlib/streams/node/stdout' );
var winlossChart = require( '@stdlib/plot/sparklines/unicode/win-loss' );
var chart;
var data;
var id;
var i;
// Based on GS Warriors winning percentages for 2015-2016...
function datum() {
var d = randu();
if ( d > 0.5 ) { // home
d = randu();
if ( d < 0.057 ) { // loss
d = -2;
} else { // win
d = 2;
}
} else { // away
d = randu();
if ( d < 0.229 ) { // loss
d = -1;
} else { // win
d = 1;
}
}
return d;
}
// Generate some random data...
data = new Int8Array( 50 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = datum();
}
// Create a new win/loss chart:
chart = winlossChart( 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;
// 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( datum() );
// 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-winloss [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/.
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-winloss --split /\r?\n/ # Escaped... $ echo -n $'1\n2\n3\n' | sparkline-winloss --split /\\r?\\n/
Examples
$ sparkline-winloss -- -2 1 2 2 1 -1 -1 1 -2 -2
┌╵└┴╵╷╷╵┌┬
$ echo -n $'-2\n1\n2\n2\n1\n-1\n-1\n1\n-2\n-2\n' | sparkline-winloss
┌╵└┴╵╷╷╵┌┬