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

[Enhancement] Max width parameter #30

Open neighthan opened 5 years ago

neighthan commented 5 years ago

It would be nice to be able to specify the maximum width of a plot as well as the height. The width is determined by both the data given and the y axis (which depends on the format string used). Though this could be handled externally, by only passing in the appropriate amount of data, I think it might be nice to do this internally so that users don't have to factor in the offset + axis when determining how much data to pass in. I think the simplest approach would be to just keep the last n data points where n is the largest number such that the plot isn't too wide. One could also think of trying to plot every kth point where k is as small as possible such that the plot isn't too wide, but that's more complicated, so I wouldn't bother unless somebody cares enough to submit a PR for it. I can go ahead and do a PR for this in Python if you're interested.

vjpr commented 3 years ago

Workaround

Quick and dirty. Take the kth point of n / width.

asciichart.plot([sampleData(a.data, width)])

function sampleData(oldArr, width) {
  const factor = Math.round(oldArr.length / width)
  return oldArr.filter(function (value, index, Arr) {
    return index % factor == 0
  })
}