From cc2e8316265ad79f806f74288758a1c6e5bc1b2a Mon Sep 17 00:00:00 2001 From: CSenshi Date: Wed, 15 Apr 2020 18:43:28 +0400 Subject: [PATCH 01/11] implement multiple series plotting --- asciichart.js | 57 ++++++++++++++++++++++++++++++--------------------- test.js | 33 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/asciichart.js b/asciichart.js index ffaea31..fe21910 100644 --- a/asciichart.js +++ b/asciichart.js @@ -3,13 +3,20 @@ (function (exports) { exports.plot = function (series, cfg = undefined) { + // Function takes oth one array and array of arrays. + // if user passes array of numbers we transform it to array of 1 array containing numbers + if (typeof(series[0]) == "number"){ + series = [series] + } + + let min = series[0][0] + let max = series[0][0] - let min = series[0] - let max = series[0] - - for (let i = 1; i < series.length; i++) { - min = Math.min (min, series[i]) - max = Math.max (max, series[i]) + for (let j = 0; j < series.length; j++) { + for (let i = 0; i < series[j].length; i++) { + min = Math.min(min, series[j][i]) + max = Math.max(max, series[j][i]) + } } let defaultSymbols = [ '┼', '┤', '╶', '╴', '─', '╰', '╭', '╮', '╯', '│' ] @@ -22,7 +29,10 @@ let min2 = Math.round (min * ratio) let max2 = Math.round (max * ratio) let rows = Math.abs (max2 - min2) - let width = series.length + offset + let width = 0 + for (let i = 0; i < series.length; i++) + width = Math.max(width, series[i].length) + width = width + offset let symbols = (typeof cfg.symbols !== 'undefined') ? cfg.symbols : defaultSymbols let format = (typeof cfg.format !== 'undefined') ? cfg.format : function (x) { return (padding + x.toFixed (2)).slice (-padding.length) @@ -40,26 +50,27 @@ result[y - min2][Math.max (offset - label.length, 0)] = label result[y - min2][offset - 1] = (y == 0) ? symbols[0] : symbols[1] } + + for (let j = 0; j < series.length; j++) { + let y0 = Math.round (series[j][0] * ratio) - min2 + result[rows - y0][offset - 1] = symbols[0] // first value - let y0 = Math.round (series[0] * ratio) - min2 - result[rows - y0][offset - 1] = symbols[0] // first value - - for (let x = 0; x < series.length - 1; x++) { // plot the line - let y0 = Math.round (series[x + 0] * ratio) - min2 - let y1 = Math.round (series[x + 1] * ratio) - min2 - if (y0 == y1) { - result[rows - y0][x + offset] = symbols[4] - } else { - result[rows - y1][x + offset] = (y0 > y1) ? symbols[5] : symbols[6] - result[rows - y0][x + offset] = (y0 > y1) ? symbols[7] : symbols[8] - let from = Math.min (y0, y1) - let to = Math.max (y0, y1) - for (let y = from + 1; y < to; y++) { - result[rows - y][x + offset] = symbols[9] + for (let x = 0; x < series[j].length - 1; x++) { // plot the line + let y0 = Math.round (series[j][x + 0] * ratio) - min2 + let y1 = Math.round (series[j][x + 1] * ratio) - min2 + if (y0 == y1) { + result[rows - y0][x + offset] = symbols[4] + } else { + result[rows - y1][x + offset] = (y0 > y1) ? symbols[5] : symbols[6] + result[rows - y0][x + offset] = (y0 > y1) ? symbols[7] : symbols[8] + let from = Math.min (y0, y1) + let to = Math.max (y0, y1) + for (let y = from + 1; y < to; y++) { + result[rows - y][x + offset] = symbols[9] + } } } } - return result.map (function (x) { return x.join ('') }).join ('\n') } diff --git a/test.js b/test.js index cd046f0..1b43e7d 100644 --- a/test.js +++ b/test.js @@ -45,3 +45,36 @@ var s3 = new Array (width) for (i = 0; i < width; i++) s3[i] = 1.0 console.log (asciichart.plot (s3) + "\n") + +// test multiple +console.log (line) +console.log ("multiple disjoint array test\n") + + +var arr1 = new Array(width) +for (var i = 0; i < arr1.length; i++) + arr1[i] = 5 * Math.sin(i * ((Math.PI * 4) / arr1.length)) + +var arr2 = new Array(width) +for (var i = 0; i < arr2.length; i++) + arr2[i] = arr1[i] + 2 + +console.log(asciichart.plot([arr1, arr2])) + + +// test multiple +console.log (line) +console.log ("multiple intersecting arrays test\n") + + +var arr1 = new Array(width) +for (var i = 0; i < arr1.length; i++) + arr1[i] = 5 * Math.sin(i * ((Math.PI * 4) / arr1.length)) + +var arr2 = new Array(width) +for (var i = 0; i < arr2.length; i++) + arr2[i] = 5 * Math.sin(Math.PI + i * ((Math.PI * 4) / arr2.length)) + + +console.log(asciichart.plot([arr1, arr2])) + From b7a303fe5d37a12d439cbe92ecbe02fb82164187 Mon Sep 17 00:00:00 2001 From: CSenshi Date: Wed, 15 Apr 2020 20:43:17 +0400 Subject: [PATCH 02/11] add color support for graphs and tests --- asciichart.js | 16 ++++++++++++---- colors.js | 22 ++++++++++++++++++++++ test.js | 24 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 colors.js diff --git a/asciichart.js b/asciichart.js index fe21910..96cfce7 100644 --- a/asciichart.js +++ b/asciichart.js @@ -1,5 +1,11 @@ "use strict"; +function colored(char, clr){ + var colors = require('./colors') + clr = colors[clr] === undefined ? colors['Reset'] : colors[clr] + return clr + char + colors['Reset'] +} + (function (exports) { exports.plot = function (series, cfg = undefined) { @@ -25,6 +31,7 @@ let offset = (typeof cfg.offset !== 'undefined') ? cfg.offset : 3 let padding = (typeof cfg.padding !== 'undefined') ? cfg.padding : ' ' let height = (typeof cfg.height !== 'undefined') ? cfg.height : range + let colors = (typeof cfg.colors !== 'undefined') ? cfg.colors : ['Reset'] let ratio = range !== 0 ? height / range : 1; let min2 = Math.round (min * ratio) let max2 = Math.round (max * ratio) @@ -52,6 +59,7 @@ } for (let j = 0; j < series.length; j++) { + let currentColor = colors[j % colors.length] let y0 = Math.round (series[j][0] * ratio) - min2 result[rows - y0][offset - 1] = symbols[0] // first value @@ -59,14 +67,14 @@ let y0 = Math.round (series[j][x + 0] * ratio) - min2 let y1 = Math.round (series[j][x + 1] * ratio) - min2 if (y0 == y1) { - result[rows - y0][x + offset] = symbols[4] + result[rows - y0][x + offset] = colored(symbols[4], currentColor) } else { - result[rows - y1][x + offset] = (y0 > y1) ? symbols[5] : symbols[6] - result[rows - y0][x + offset] = (y0 > y1) ? symbols[7] : symbols[8] + result[rows - y1][x + offset] = colored((y0 > y1) ? symbols[5] : symbols[6], currentColor) + result[rows - y0][x + offset] = colored((y0 > y1) ? symbols[7] : symbols[8], currentColor) let from = Math.min (y0, y1) let to = Math.max (y0, y1) for (let y = from + 1; y < to; y++) { - result[rows - y][x + offset] = symbols[9] + result[rows - y][x + offset] = colored( symbols[9], currentColor) } } } diff --git a/colors.js b/colors.js new file mode 100644 index 0000000..a6e8f72 --- /dev/null +++ b/colors.js @@ -0,0 +1,22 @@ +colors = { + 'Reset': "\x1b[0m", + 'Blink': "\x1b[5m", + 'FgBlack': "\x1b[30m", + 'FgRed': "\x1b[31m", + 'FgGreen': "\x1b[32m", + 'FgYellow': "\x1b[33m", + 'FgBlue': "\x1b[34m", + 'FgMagenta': "\x1b[35m", + 'FgCyan': "\x1b[36m", + 'FgWhite': "\x1b[37m", + 'BgBlack': "\x1b[40m", + 'BgRed': "\x1b[41m", + 'BgGreen': "\x1b[42m", + 'BgYellow': "\x1b[43m", + 'BgBlue': "\x1b[44m", + 'BgMagenta': "\x1b[45m", + 'BgCyan': "\x1b[46m", + 'BgWhite': "\x1b[47m", +} + +module.exports = colors diff --git a/test.js b/test.js index 1b43e7d..aaa7f1b 100644 --- a/test.js +++ b/test.js @@ -78,3 +78,27 @@ for (var i = 0; i < arr2.length; i++) console.log(asciichart.plot([arr1, arr2])) +// test multiple colored +console.log(line) +console.log("multiple intersecting arrays with colors test\n") + +var arr1 = new Array(width) +for (var i = 0; i < arr1.length; i++) + arr1[i] = 5 * Math.sin(i * ((Math.PI * 4) / arr1.length)) + +var arr2 = new Array(width) +for (var i = 0; i < arr2.length; i++) + arr2[i] = 5 * Math.sin(Math.PI + i * ((Math.PI * 4) / arr2.length)) + +var arr3 = new Array(width) +for (var i = 0; i < arr3.length; i++) + arr3[i] = 5 - i * 0.2 + +var arr4 = new Array(width) +for (var i = 0; i < arr4.length; i++) + arr4[i] = 10 + 5 * Math.cos(i * ((Math.PI * 4) / arr1.length)) + +var config = { + colors: ['FgYellow', 'Reset', 'FgMagenta'] +} +console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) From db3217158b9e8a41b9c68d2cc9fd621202c3639c Mon Sep 17 00:00:00 2001 From: CSenshi Date: Thu, 16 Apr 2020 01:54:08 +0400 Subject: [PATCH 03/11] create Color class for better usage --- asciichart.js | 13 +++++++------ colors.js | 46 ++++++++++++++++++++++++++-------------------- test.js | 11 ++++++++++- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/asciichart.js b/asciichart.js index 96cfce7..0bd7a15 100644 --- a/asciichart.js +++ b/asciichart.js @@ -1,12 +1,13 @@ "use strict"; -function colored(char, clr){ +function colored(char, clr) { var colors = require('./colors') - clr = colors[clr] === undefined ? colors['Reset'] : colors[clr] - return clr + char + colors['Reset'] + clr = clr === undefined ? colors.Reset : clr + return clr + char + colors.Reset } (function (exports) { + exports.Colors = require('./colors') exports.plot = function (series, cfg = undefined) { // Function takes oth one array and array of arrays. @@ -14,7 +15,7 @@ function colored(char, clr){ if (typeof(series[0]) == "number"){ series = [series] } - + let min = series[0][0] let max = series[0][0] @@ -31,7 +32,7 @@ function colored(char, clr){ let offset = (typeof cfg.offset !== 'undefined') ? cfg.offset : 3 let padding = (typeof cfg.padding !== 'undefined') ? cfg.padding : ' ' let height = (typeof cfg.height !== 'undefined') ? cfg.height : range - let colors = (typeof cfg.colors !== 'undefined') ? cfg.colors : ['Reset'] + let colors = (typeof cfg.colors !== 'undefined') ? cfg.colors : [] let ratio = range !== 0 ? height / range : 1; let min2 = Math.round (min * ratio) let max2 = Math.round (max * ratio) @@ -61,7 +62,7 @@ function colored(char, clr){ for (let j = 0; j < series.length; j++) { let currentColor = colors[j % colors.length] let y0 = Math.round (series[j][0] * ratio) - min2 - result[rows - y0][offset - 1] = symbols[0] // first value + result[rows - y0][offset - 1] = colored(symbols[0], currentColor) // first value for (let x = 0; x < series[j].length - 1; x++) { // plot the line let y0 = Math.round (series[j][x + 0] * ratio) - min2 diff --git a/colors.js b/colors.js index a6e8f72..c346a85 100644 --- a/colors.js +++ b/colors.js @@ -1,22 +1,28 @@ -colors = { - 'Reset': "\x1b[0m", - 'Blink': "\x1b[5m", - 'FgBlack': "\x1b[30m", - 'FgRed': "\x1b[31m", - 'FgGreen': "\x1b[32m", - 'FgYellow': "\x1b[33m", - 'FgBlue': "\x1b[34m", - 'FgMagenta': "\x1b[35m", - 'FgCyan': "\x1b[36m", - 'FgWhite': "\x1b[37m", - 'BgBlack': "\x1b[40m", - 'BgRed': "\x1b[41m", - 'BgGreen': "\x1b[42m", - 'BgYellow': "\x1b[43m", - 'BgBlue': "\x1b[44m", - 'BgMagenta': "\x1b[45m", - 'BgCyan': "\x1b[46m", - 'BgWhite': "\x1b[47m", +var Color = { + BG: { + Black: "\x1b[40m", + Red: "\x1b[41m", + Green: "\x1b[42m", + Yellow: "\x1b[43m", + Blue: "\x1b[44m", + Magenta: "\x1b[45m", + Cyan: "\x1b[46m", + White: "\x1b[47m", + }, + + FG: { + Black: "\x1b[30m", + Red: "\x1b[31m", + Green: "\x1b[32m", + Yellow: "\x1b[33m", + Blue: "\x1b[34m", + Magenta: "\x1b[35m", + Cyan: "\x1b[36m", + White: "\x1b[37m", + }, + Reset: "\x1b[0m", + Blink: "\x1b[5m", + } -module.exports = colors +module.exports = Color \ No newline at end of file diff --git a/test.js b/test.js index aaa7f1b..b878f22 100644 --- a/test.js +++ b/test.js @@ -99,6 +99,15 @@ for (var i = 0; i < arr4.length; i++) arr4[i] = 10 + 5 * Math.cos(i * ((Math.PI * 4) / arr1.length)) var config = { - colors: ['FgYellow', 'Reset', 'FgMagenta'] + colors: [asciichart.Colors.FG.Blue, asciichart.Colors.FG.Green, asciichart.Colors.FG.Magenta, asciichart.Colors.FG.Red] +} +console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) + +// test multiple colored background +console.log(line) +console.log("multiple intersecting arrays with colors test\n") + +var config = { + colors: [asciichart.Colors.BG.Blue, asciichart.Colors.BG.Green, asciichart.Colors.BG.Magenta, asciichart.Colors.BG.Red] } console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) From 222525d2759b57d6dd470c81ae2cf6f354e770df Mon Sep 17 00:00:00 2001 From: Saba Pochkhua Date: Thu, 16 Apr 2020 03:30:08 +0400 Subject: [PATCH 04/11] Update README.md --- README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/README.md b/README.md index a303ad5..878f103 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,57 @@ console.log (asciichart.plot (s2)) Console ASCII Line charts in pure Javascript (for NodeJS and browsers) +### Multiple Series Plotting + +```javascript +var s2 = new Array (120) +s2[0] = Math.round (Math.random () * 15) +for (i = 1; i < s2.length; i++) + s2[i] = s2[i - 1] + Math.round (Math.random () * (Math.random () > 0.5 ? 2 : -2)) + +var s3 = new Array (120) +s3[0] = Math.round (Math.random () * 15) +for (i = 1; i < s3.length; i++) + s3[i] = s3[i - 1] + Math.round (Math.random () * (Math.random () > 0.5 ? 2 : -2)) + +console.log (asciichart.plot ([s2, s3])) +``` + +Console ASCII Line charts in pure Javascript (for NodeJS and browsers) + +### Multiple Series Plotting (Coloring) + +```javascript +var arr1 = new Array(120) +arr1[0] = Math.round(Math.random() * 15) +for (i = 1; i < arr1.length; i++) + arr1[i] = arr1[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) + +var arr2 = new Array(120) +arr2[0] = Math.round(Math.random() * 15) +for (i = 1; i < arr2.length; i++) + arr2[i] = arr2[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) + +var arr3 = new Array(120) +arr3[0] = Math.round(Math.random() * 15) +for (i = 1; i < arr3.length; i++) + arr3[i] = arr3[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) + +var arr4 = new Array(120) +arr4[0] = Math.round(Math.random() * 15) +for (i = 1; i < arr4.length; i++) + arr4[i] = arr4[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) + +var config = { + colors: [asciichart.Colors.FG.Blue, asciichart.Colors.FG.Green, + asciichart.Colors.FG.Magenta, asciichart.Colors.FG.Red] +} + +console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) +``` + +Console ASCII Line charts in pure Javascript (for NodeJS and browsers) + ### See Also A util by [madnight](https://github.com/madnight) for drawing Bitcoin/Ether/altcoin charts in command-line console: [bitcoin-chart-cli](https://github.com/madnight/bitcoin-chart-cli). From 7d66221b89a35487a62a81408578732ca08253e4 Mon Sep 17 00:00:00 2001 From: Igor Kroitor Date: Thu, 23 Apr 2020 10:13:36 +0300 Subject: [PATCH 05/11] coloring edits --- asciichart.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/asciichart.js b/asciichart.js index 0bd7a15..d3b6434 100644 --- a/asciichart.js +++ b/asciichart.js @@ -1,17 +1,23 @@ "use strict"; -function colored(char, clr) { - var colors = require('./colors') - clr = clr === undefined ? colors.Reset : clr - return clr + char + colors.Reset -} - (function (exports) { - exports.Colors = require('./colors') + + let colors = require ('./colors') + + function colored (char, color) { + // do not color it if color is not specified + let openingColor = (color === undefined) ? '' : color + let closingColor = (color === undefined) ? '' : colors.Reset + return openingColor + char + closingColor + } + + exports.colors = colors + exports.colored = colored exports.plot = function (series, cfg = undefined) { - // Function takes oth one array and array of arrays. - // if user passes array of numbers we transform it to array of 1 array containing numbers + // this function takes oth one array and array of arrays + // if an array of numbers is passed it is transfored to + // an array of exactly one array with numbers if (typeof(series[0]) == "number"){ series = [series] } @@ -38,8 +44,9 @@ function colored(char, clr) { let max2 = Math.round (max * ratio) let rows = Math.abs (max2 - min2) let width = 0 - for (let i = 0; i < series.length; i++) + for (let i = 0; i < series.length; i++) { width = Math.max(width, series[i].length) + } width = width + offset let symbols = (typeof cfg.symbols !== 'undefined') ? cfg.symbols : defaultSymbols let format = (typeof cfg.format !== 'undefined') ? cfg.format : function (x) { @@ -75,7 +82,7 @@ function colored(char, clr) { let from = Math.min (y0, y1) let to = Math.max (y0, y1) for (let y = from + 1; y < to; y++) { - result[rows - y][x + offset] = colored( symbols[9], currentColor) + result[rows - y][x + offset] = colored(symbols[9], currentColor) } } } From 180737ed4eddaa4c2eb863fec17ad2e2c921fb85 Mon Sep 17 00:00:00 2001 From: Igor Kroitor Date: Thu, 23 Apr 2020 10:15:59 +0300 Subject: [PATCH 06/11] =?UTF-8?q?README.md=20asciichart.Colors=20=E2=86=92?= =?UTF-8?q?=20asciichart.colors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 878f103..527e485 100644 --- a/README.md +++ b/README.md @@ -138,8 +138,8 @@ for (i = 1; i < arr4.length; i++) arr4[i] = arr4[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) var config = { - colors: [asciichart.Colors.FG.Blue, asciichart.Colors.FG.Green, - asciichart.Colors.FG.Magenta, asciichart.Colors.FG.Red] + colors: [asciichart.colors.FG.Blue, asciichart.colors.FG.Green, + asciichart.colors.FG.Magenta, asciichart.colors.FG.Red] } console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) From 3a80b270b52ff89add11a51cc6e07c35dce6cb66 Mon Sep 17 00:00:00 2001 From: Igor Kroitor Date: Thu, 23 Apr 2020 10:17:45 +0300 Subject: [PATCH 07/11] =?UTF-8?q?test.js=20asciichart.Colors=20=E2=86=92?= =?UTF-8?q?=C2=A0asciichart.colors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index b878f22..264e9a0 100644 --- a/test.js +++ b/test.js @@ -99,7 +99,7 @@ for (var i = 0; i < arr4.length; i++) arr4[i] = 10 + 5 * Math.cos(i * ((Math.PI * 4) / arr1.length)) var config = { - colors: [asciichart.Colors.FG.Blue, asciichart.Colors.FG.Green, asciichart.Colors.FG.Magenta, asciichart.Colors.FG.Red] + colors: [asciichart.colors.FG.Blue, asciichart.colors.FG.Green, asciichart.colors.FG.Magenta, asciichart.colors.FG.Red] } console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) @@ -108,6 +108,6 @@ console.log(line) console.log("multiple intersecting arrays with colors test\n") var config = { - colors: [asciichart.Colors.BG.Blue, asciichart.Colors.BG.Green, asciichart.Colors.BG.Magenta, asciichart.Colors.BG.Red] + colors: [asciichart.colors.BG.Blue, asciichart.colors.BG.Green, asciichart.colors.BG.Magenta, asciichart.colors.BG.Red] } console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) From 1dfc4109c9f7df32ebf51737df34493c8cae773f Mon Sep 17 00:00:00 2001 From: Igor Kroitor Date: Thu, 23 Apr 2020 11:24:59 +0300 Subject: [PATCH 08/11] coloring edits --- asciichart.js | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/asciichart.js b/asciichart.js index d3b6434..68b4c47 100644 --- a/asciichart.js +++ b/asciichart.js @@ -1,22 +1,49 @@ "use strict"; (function (exports) { - - let colors = require ('./colors') + + exports.black = 0 + exports.red = 1, + exports.green = 2 + exports.yellow = 3 + exports.blue = 4 + exports.magenta = 5 + exports.cyan = 6 + exports.lightgray = 7 + exports.default = 9 + exports.darkgray = 60 + exports.lightred = 61 + exports.lightgreen = 62 + exports.lightyellow = 63 + exports.lightblue = 64 + exports.lightmagenta = 65 + exports.lightcyan = 66 + exports.white = 67 + exports.escape = "\x1b[" + exports.end = "m" + exports.reset = exports.escape + "0" + exports.end function colored (char, color) { // do not color it if color is not specified - let openingColor = (color === undefined) ? '' : color - let closingColor = (color === undefined) ? '' : colors.Reset - return openingColor + char + closingColor + if (color === undefined) { + return char; + } else if (Array.isArray (color)) { + let foreground = 30 + ((color[0] === undefined) ? exports.default : color[0]) + let background = 40 + ((color[1] === undefined) ? exports.default : color[1]) + foreground = foreground.toString () + background = background.toString () + return exports.escape + foreground + ';' + background + exports.end + char + exports.reset + } else { + let foreground = (30 + color).toString () + return exports.escape + foreground + exports.end + char + exports.reset + } } - - exports.colors = colors + exports.colored = colored exports.plot = function (series, cfg = undefined) { // this function takes oth one array and array of arrays - // if an array of numbers is passed it is transfored to + // if an array of numbers is passed it is transfored to // an array of exactly one array with numbers if (typeof(series[0]) == "number"){ series = [series] @@ -65,8 +92,8 @@ result[y - min2][Math.max (offset - label.length, 0)] = label result[y - min2][offset - 1] = (y == 0) ? symbols[0] : symbols[1] } - - for (let j = 0; j < series.length; j++) { + + for (let j = 0; j < series.length; j++) { let currentColor = colors[j % colors.length] let y0 = Math.round (series[j][0] * ratio) - min2 result[rows - y0][offset - 1] = colored(symbols[0], currentColor) // first value From ee16a329d4e25bfb38d43fcd9712540436863dc8 Mon Sep 17 00:00:00 2001 From: Igor Kroitor Date: Thu, 23 Apr 2020 11:25:49 +0300 Subject: [PATCH 09/11] test.js colors --- test.js | 67 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/test.js b/test.js index 264e9a0..4359fbf 100644 --- a/test.js +++ b/test.js @@ -27,7 +27,7 @@ var config = { var s = [] for (var i = 0; i < width; i++) s[i] = 15 * Math.cos (i * ((Math.PI * 8) / width)) // values range from -15 to +15 -console.log (asciichart.plot (s, config)) // this rescales the graph to ±3 lines +console.log (asciichart.plot (s, config)) // this rescales the graph to ±3 lines console.log (line) console.log ("auto-range\n") @@ -51,63 +51,74 @@ console.log (line) console.log ("multiple disjoint array test\n") -var arr1 = new Array(width) +var arr1 = new Array (width) for (var i = 0; i < arr1.length; i++) - arr1[i] = 5 * Math.sin(i * ((Math.PI * 4) / arr1.length)) + arr1[i] = 5 * Math.sin (i * ((Math.PI * 4) / arr1.length)) -var arr2 = new Array(width) +var arr2 = new Array (width) for (var i = 0; i < arr2.length; i++) arr2[i] = arr1[i] + 2 - -console.log(asciichart.plot([arr1, arr2])) +console.log (asciichart.plot ([ arr1, arr2 ])) // test multiple console.log (line) console.log ("multiple intersecting arrays test\n") - -var arr1 = new Array(width) +var arr1 = new Array (width) for (var i = 0; i < arr1.length; i++) - arr1[i] = 5 * Math.sin(i * ((Math.PI * 4) / arr1.length)) + arr1[i] = 5 * Math.sin (i * ((Math.PI * 4) / arr1.length)) -var arr2 = new Array(width) +var arr2 = new Array (width) for (var i = 0; i < arr2.length; i++) - arr2[i] = 5 * Math.sin(Math.PI + i * ((Math.PI * 4) / arr2.length)) + arr2[i] = 5 * Math.sin (Math.PI + i * ((Math.PI * 4) / arr2.length)) + - -console.log(asciichart.plot([arr1, arr2])) +console.log (asciichart.plot ([ arr1, arr2 ])) // test multiple colored -console.log(line) -console.log("multiple intersecting arrays with colors test\n") +console.log (line) +console.log ("multiple intersecting arrays with colors test\n") -var arr1 = new Array(width) +var arr1 = new Array (width) for (var i = 0; i < arr1.length; i++) - arr1[i] = 5 * Math.sin(i * ((Math.PI * 4) / arr1.length)) + arr1[i] = 5 * Math.sin (i * ((Math.PI * 4) / arr1.length)) -var arr2 = new Array(width) +var arr2 = new Array (width) for (var i = 0; i < arr2.length; i++) - arr2[i] = 5 * Math.sin(Math.PI + i * ((Math.PI * 4) / arr2.length)) + arr2[i] = 5 * Math.sin (Math.PI + i * ((Math.PI * 4) / arr2.length)) -var arr3 = new Array(width) +var arr3 = new Array (width) for (var i = 0; i < arr3.length; i++) arr3[i] = 5 - i * 0.2 -var arr4 = new Array(width) +var arr4 = new Array (width) for (var i = 0; i < arr4.length; i++) - arr4[i] = 10 + 5 * Math.cos(i * ((Math.PI * 4) / arr1.length)) + arr4[i] = 10 + 5 * Math.cos (i * ((Math.PI * 4) / arr1.length)) var config = { - colors: [asciichart.colors.FG.Blue, asciichart.colors.FG.Green, asciichart.colors.FG.Magenta, asciichart.colors.FG.Red] + colors: [ + asciichart.blue, + asciichart.green, + asciichart.magenta, + asciichart.red + ] } -console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) + +var series = [ arr1, arr2, arr3, arr4 ] + +console.log (asciichart.plot (series, config)) // test multiple colored background -console.log(line) -console.log("multiple intersecting arrays with colors test\n") +console.log (line) +console.log ("multiple intersecting arrays with colors test\n") var config = { - colors: [asciichart.colors.BG.Blue, asciichart.colors.BG.Green, asciichart.colors.BG.Magenta, asciichart.colors.BG.Red] + colors: [ + [ asciichart.default, asciichart.lightblue ], + [ undefined, asciichart.white ], + [ asciichart.lightgreen, undefined ], + [ asciichart.red, asciichart.default ] + ], } -console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) +console.log (asciichart.plot (series, config)) From 512b79bca2ea4af9db02b7dd62521db862f69681 Mon Sep 17 00:00:00 2001 From: Igor Kroitor Date: Thu, 23 Apr 2020 11:33:34 +0300 Subject: [PATCH 10/11] README.md colors --- README.md | 58 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 527e485..efbc653 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,6 @@ console.log (asciichart.plot (s, { height: 6 })) // this rescales the graph Console ASCII Line charts in pure Javascript (for NodeJS and browsers) - ### Auto-range ```javascript @@ -96,7 +95,7 @@ console.log (asciichart.plot (s2)) Console ASCII Line charts in pure Javascript (for NodeJS and browsers) -### Multiple Series Plotting +### Multiple Series ```javascript var s2 = new Array (120) @@ -109,40 +108,59 @@ s3[0] = Math.round (Math.random () * 15) for (i = 1; i < s3.length; i++) s3[i] = s3[i - 1] + Math.round (Math.random () * (Math.random () > 0.5 ? 2 : -2)) -console.log (asciichart.plot ([s2, s3])) +console.log (asciichart.plot ([ s2, s3 ])) ``` Console ASCII Line charts in pure Javascript (for NodeJS and browsers) -### Multiple Series Plotting (Coloring) +### Colors ```javascript -var arr1 = new Array(120) -arr1[0] = Math.round(Math.random() * 15) +var arr1 = new Array (120) +arr1[0] = Math.round (Math.random () * 15) for (i = 1; i < arr1.length; i++) - arr1[i] = arr1[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) + arr1[i] = arr1[i - 1] + Math.round (Math.random () * (Math.random () > 0.5 ? 2 : -2)) -var arr2 = new Array(120) -arr2[0] = Math.round(Math.random() * 15) +var arr2 = new Array (120) +arr2[0] = Math.round (Math.random () * 15) for (i = 1; i < arr2.length; i++) - arr2[i] = arr2[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) + arr2[i] = arr2[i - 1] + Math.round (Math.random () * (Math.random () > 0.5 ? 2 : -2)) -var arr3 = new Array(120) -arr3[0] = Math.round(Math.random() * 15) +var arr3 = new Array (120) +arr3[0] = Math.round (Math.random () * 15) for (i = 1; i < arr3.length; i++) - arr3[i] = arr3[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) + arr3[i] = arr3[i - 1] + Math.round (Math.random () * (Math.random () > 0.5 ? 2 : -2)) -var arr4 = new Array(120) -arr4[0] = Math.round(Math.random() * 15) +var arr4 = new Array (120) +arr4[0] = Math.round (Math.random () * 15) for (i = 1; i < arr4.length; i++) - arr4[i] = arr4[i - 1] + Math.round(Math.random() * (Math.random() > 0.5 ? 2 : -2)) + arr4[i] = arr4[i - 1] + Math.round (Math.random () * (Math.random () > 0.5 ? 2 : -2)) + +var config = { + colors: [ + asciichart.blue, + asciichart.green, + asciichart.default, // default color + undefined, // equivalent to default + ] +} +console.log (asciichart.plot([ arr1, arr2, arr3, arr4 ], config)) +``` + +### Foreground / Background + +```JavaScript var config = { - colors: [asciichart.colors.FG.Blue, asciichart.colors.FG.Green, - asciichart.colors.FG.Magenta, asciichart.colors.FG.Red] + colors: [ + [ asciichart.blue, asciichart.default ], // foreground, default + [ asciichart.green, asciichart.darkgray ], // foreground, background + [ undefined, asciichart.red ], // default foreground color + [ asciichart.magenta, undefined ], // default background color + ] } -console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) +console.log (asciichart.plot([ arr1, arr2, arr3, arr4 ], config)) ``` Console ASCII Line charts in pure Javascript (for NodeJS and browsers) @@ -150,7 +168,7 @@ console.log(asciichart.plot([arr1, arr2, arr3, arr4], config)) ### See Also A util by [madnight](https://github.com/madnight) for drawing Bitcoin/Ether/altcoin charts in command-line console: [bitcoin-chart-cli](https://github.com/madnight/bitcoin-chart-cli). - + ![bitcoin-chart-cli](https://camo.githubusercontent.com/494806efd925c4cd56d8370c1d4e8b751812030a/68747470733a2f2f692e696d6775722e636f6d2f635474467879362e706e67) ### Ports From ee42b17952f1079e984d368f9a736a5239a5313e Mon Sep 17 00:00:00 2001 From: Igor Kroitor Date: Thu, 23 Apr 2020 11:35:13 +0300 Subject: [PATCH 11/11] Delete colors.js --- colors.js | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 colors.js diff --git a/colors.js b/colors.js deleted file mode 100644 index c346a85..0000000 --- a/colors.js +++ /dev/null @@ -1,28 +0,0 @@ -var Color = { - BG: { - Black: "\x1b[40m", - Red: "\x1b[41m", - Green: "\x1b[42m", - Yellow: "\x1b[43m", - Blue: "\x1b[44m", - Magenta: "\x1b[45m", - Cyan: "\x1b[46m", - White: "\x1b[47m", - }, - - FG: { - Black: "\x1b[30m", - Red: "\x1b[31m", - Green: "\x1b[32m", - Yellow: "\x1b[33m", - Blue: "\x1b[34m", - Magenta: "\x1b[35m", - Cyan: "\x1b[36m", - White: "\x1b[37m", - }, - Reset: "\x1b[0m", - Blink: "\x1b[5m", - -} - -module.exports = Color \ No newline at end of file