-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconsole.chart.js
More file actions
59 lines (50 loc) · 1.99 KB
/
console.chart.js
File metadata and controls
59 lines (50 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
console.chart = ( data, _params ) => {
function intFormat( int, n ) {
let a = ( '' + int ).split( '' ).reverse();
let len = a.length;
while( a.length < n ) {
a.push( ' ' );
}
return( a.reverse().join( '' ) );
}
// defaults
let params = {
title: null,
height: 10,
color: '#000',
background: null,
min: Math.min( ...data ),
max: Math.max( ...data )
};
// extend defaults with passed options
for( let key in params ) {
if( _params && _params.hasOwnProperty( key ) ) {
params[ key ] = _params[ key ];
}
}
let len = data.map( d => '' + d ).sort( ( a, b ) => a.length < b.length )[ 0 ].length;
let chart = new Array( params.height).fill( 0 ).map( ( d, i ) =>
i == 0 ? intFormat( params.max, len ) + '| ' :
i < params.height - 1 ? intFormat( '', len ) + '| ' :
// i < params.height - 1 ? intFormat( params.min + ( params.max - params.min ) / (params.height - 1) * ( params.height - 1 - i ), len ) + '| ' :
intFormat( params.min, len ) + '|_'
);
data.forEach( d =>
chart.forEach( ( c, i ) =>
chart[ i ] += d > ( chart.length - i ) * ( ( params.max - params.min ) / params.height ) ? ( i < ( params.height - 1 ) ? '█ ' : '█_') : ( i < ( params.height - 1 ) ? ' ' : '__')
)
);
chart.forEach( ( c, i ) => chart[ i ] = ' ' + chart[ i ] + ' ' );
let empty = new Array( chart[ 0 ].length ).fill( ' ' ).join( '' );
chart.splice( 0, 0, empty );
if( params.title ) {
params.title = ' ' + params.title;
while( params.title.length < chart[ 0 ].length ) {
params.title += ' ';
}
chart.splice( 0, 0, params.title );
chart.splice( 0, 0, empty );
}
chart.push( empty );
console.log( '%c' + chart.join( '\n' ), 'color: ' + params.color + ';' + ( params.background ? 'background: ' + params.background + ';' : '' ) );
};