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

Question - How to plot a live data stream #39

Closed alextran1502 closed 4 years ago

alextran1502 commented 4 years ago

Hello,

First of all, thank you for your wonderful job. This is a very good library.

I am trying to graph a stream of data, can you help to point me in the right direction.

Thanks

kroitor commented 4 years ago

Hi! What's your programming language?

alextran1502 commented 4 years ago

hello @kroitor I am using Javascript

kroitor commented 4 years ago

@alextran1502 assuming you can read the data points from your stream, you can add them to the set of most recent maxLength elements in your array of data upon reading, and then redraw the chart using the most recent elements. In the simplest form it would look like:

const maxLength = 80
const numbers = []
while (true) {
    const number = readFromStream () // your reading logic here
    if (numbers.length >= maxLength) {
        numbers.shift ()
    }
    numbers.push (number)
    console.log (plot (numbers))
}

Let me know if that does not answer the question. Feel free to reopen this if needed or just ask further questions, if any.

NightMachinery commented 3 years ago

@kroitor commented on Feb 28, 2020, 10:13 AM GMT+3:30:

@alextran1502 assuming you can read the data points from your stream, you can add them to the set of most recent maxLength elements in your array of data upon reading, and then redraw the chart using the most recent elements. In the simplest form it would look like:

const maxLength = 80
const numbers = []
while (true) {
    const number = readFromStream () // your reading logic here
    if (numbers.length >= maxLength) {
        numbers.shift ()
    }
    numbers.push (number)
    console.log (plot (numbers))
}

Let me know if that does not answer the question. Feel free to reopen this if needed or just ask further questions, if any.

I want to just plot the streaming data (each new line being the updated value) from stdin. How do I do that?

kroitor commented 3 years ago

@NightMachinary you could have the chat in a separate blessed window and redraw it when your data changes. In other words, you need to locate your chart in your output and redraw it, by overwriting it.