kroitor / asciichart

Nice-looking lightweight console ASCII line charts ╭┈╯ for NodeJS, browsers and terminal, no dependencies
MIT License
1.84k stars 94 forks source link

Cannot plot a chart with a single repeating value (horizontal line) #10

Closed pghalliday closed 5 years ago

pghalliday commented 6 years ago

When I try to plot a chart with an array of values such as [100, 100, 100], eg.

var asciichart = require('asciichart');
asciichart.plot([100, 100, 100], {
  height: 10,
});

I get the following error

/Users/pghalliday/projects/github/pghalliday/metriculator/node_modules/asciichart/asciichart.js:29
        let result = new Array (rows + 1) // empty space
                     ^

RangeError: Invalid array length
    at Object.exports.plot (/Users/pghalliday/projects/github/pghalliday/metriculator/node_modules/asciichart/asciichart.js:29:22)
    at Object.<anonymous> (/Users/pghalliday/projects/github/pghalliday/metriculator/test.js:2:12)
    at Module._compile (module.js:662:30)
    at Object.Module._extensions..js (module.js:673:10)
    at Module.load (module.js:575:32)
    at tryModuleLoad (module.js:515:12)
    at Function.Module._load (module.js:507:3)
    at Function.Module.runMain (module.js:703:10)
    at startup (bootstrap_node.js:193:16)
    at bootstrap_node.js:660:3

This seems to be due to the way the number of rows are calculated based on the range (which in this case I presume is calculated to zero). The same error occurs if I supply an empty array or an array with just 1 value. I think a possible fix would be to define a default range if one cannot be sensibly calculated. This should probably be added to the options in some way.

This impacts me as I'm starting with an empty chart and adding values over time. For now I have worked around it by ensuring that the range is non-zero before plotting. However this leads to an odd corner case where the last n entries for a chart that only plots n entries (due to screen width) are all equal (even if previous entries are not) then the chart cannot be updated.

kroitor commented 6 years ago

Thanks for reporting! Will fix asap!

arkihillel commented 6 years ago

I'm also getting a flat line with a NaN when the numbers are really small:

[0.4000000189989805,0.20000000949949026,0.3000000142492354,0.19999995129182935,0.3000000142492354,0.10000000474974513,0.3000000142492354,0.10000000474974513,0.10000000474974513]

Result calling a simple char.plot():


    NaN ┼──────── 
sheerun commented 5 years ago

Same issue..

ourarash commented 5 years ago

I have the same problem