diff --git a/asciichart.js b/asciichart.js index 799b10f..ad2a0a6 100644 --- a/asciichart.js +++ b/asciichart.js @@ -4,18 +4,41 @@ exports.plot = function (series, cfg = undefined) { + var toFixedTrailing = function(x, trailing = 2) { + if (Math.abs(x) > 1) { + var trailing = trailing || 0, + neg = x < 0, + power = Math.pow(10, trailing), + x = Math.round(x * power), + integral = String((neg ? Math.ceil : Math.floor)(x / power)), + fraction = String((neg ? -x : x) % power), + padding = new Array(Math.max(trailing - fraction.length, 0) + 1).join('0'); + + return trailing ? integral + '.' + padding + fraction : integral; + } else { + var numDigits = x !== 0 ? Math.ceil(Math.log10(Math.abs(x))) : 0; + var rounded = Math.round(x*Math.pow(10,trailing-numDigits))*Math.pow(10,numDigits-trailing); + return rounded.toFixed(Math.max(trailing-numDigits,0)); + } + } + + let min = series[0] let max = series[0] + let paddingLength = 0 for (let i = 1; i < series.length; i++) { min = Math.min (min, series[i]) max = Math.max (max, series[i]) + + var numChars = String(toFixedTrailing(series[i])).length + paddingLength = numChars > paddingLength ? numChars : paddingLength } let range = Math.abs (max - min) cfg = (typeof cfg !== 'undefined') ? cfg : {} let offset = (typeof cfg.offset !== 'undefined') ? cfg.offset : 3 - let padding = (typeof cfg.padding !== 'undefined') ? cfg.padding : ' ' + let padding = (typeof cfg.padding !== 'undefined') ? cfg.padding : Array(paddingLength).fill(" ") let height = (typeof cfg.height !== 'undefined') ? cfg.height : range let ratio = height / range let min2 = Math.round (min * ratio) @@ -23,7 +46,7 @@ let rows = Math.abs (max2 - min2) let width = series.length + offset let format = (typeof cfg.format !== 'undefined') ? cfg.format : function (x) { - return (padding + x.toFixed (2)).slice (-padding.length) + return (padding + toFixedTrailing(x)).slice (-padding.length) } let result = new Array (rows + 1) // empty space diff --git a/test.js b/test.js index 630ef51..3b03def 100644 --- a/test.js +++ b/test.js @@ -16,7 +16,6 @@ console.log (line) console.log ("configuring / scale to desired height\n") var config = { - padding: ' ', // padding string for label formatting (can be overrided) offset: 3, // axis offset from the left (min 2) height: 10, // any height you want format: function (x, i) { @@ -36,4 +35,17 @@ var s2 = new Array (width) 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)) -console.log (asciichart.plot (s2) + "\n") \ No newline at end of file +console.log (asciichart.plot (s2) + "\n") + +console.log (line) +console.log ("\ncorrect padding for widely varying label lengths\n") + +console.log ("\nrange 8000 - 12000\n") +var s3 = [] +for (var i = 0; i < width; i++) + s3[i] = (2000 * Math.cos (i * ((Math.PI * 8) / width))) + 10000 // values range from 9000 to 11000 +console.log (asciichart.plot (s3, { height: 10, width: 60 })) + +console.log ("\nrange 0.000080 - 0.00012\n") +s3 = s3.map(x => { return x / 100000000 }) +console.log (asciichart.plot (s3, { height: 10, width: 60 })) \ No newline at end of file