tammoippen / plotille

Plot in the terminal using braille dots.
MIT License
398 stars 17 forks source link

using plotille for a real time chart #57

Open xmnlab opened 7 months ago

xmnlab commented 7 months ago

Hey everyone!

first of all, thank you all for working on plotille, that is a very nice library!

I am trying to have a real time chart, but I am having some hard time trying to figure out how to have the time in the x axis.

This is my initial code: https://github.com/osl-incubator/sugar/blob/main/src/sugar/plugins/stats.py#L94

but now I am trying to have the x axis with the time. and it seems it is not working properly. as I saw in the source code it uses some math to calculate the ticks, so maybe it is breaking my code.

locally, in a wip, I tried x_ticks_fkt and register_label_formatter .. but it seems it is not working as I expected.

could you point me to any example that uses x axis for time? that I could use it for a real time chart?

another extra question, still about real time chart, do I need to create the figure for every iteration? because if I use plot every time, it add more lines to the chart. so for now I am recreating the chart figure every time, but it would be nice to just update the data there.

thanks!

xmnlab commented 7 months ago

btw, I managed to workaround that with:

self.fig_mem._plots.clear()
self.fig_cpu._plots.clear()

but the color of the lines change every iteration

xmnlab commented 7 months ago

I also changed to X axis to datetime.datetime.now(), and it seems it is working .. I will investiate a bit more about that.

xmnlab commented 7 months ago

I created a copy of _color_seq with tee in order to handle keep the same color always.

but if there is a better way to handle that .. I will appreciate a better approach. thanks!

tammoippen commented 7 months ago

Hi @xmnlab ,

Thank you for the kind words!

Here is an example for custom ticks: https://github.com/tammoippen/plotille/blob/master/examples/custom_ticks_example.py

The function signature is def default_tick(min_, max_): where min_ and max_ both have the type of the x- or y-axis data (e.g. datetime). They are the minimum and maximum value of the current tick. The return value will be rendered as the tick value - if there is a formatter for the value, it will be formatted. If the return value is a str, the string will be rendered.

Here are tests that use datetime on the x-axis: https://github.com/tammoippen/plotille/blob/master/tests/test_figure.py#L494-L572

When you want time on the x-axis, you have to use date, datetime or numpy.datetime, or write your own converter / formatter. See here for the default converters / formatters of date(time)s. The delta is the difference between the smallest and largest x-value, hence the formatting can be 'refined' depending on how large the difference is (you only have a certain amount of characters per tick to write the tick-value).

Regarding colors, you can either:

The parameter lc means line color and can be either color names, color codes or rgb values.

Please read this docstring for the color formatting in the terminal. To change the color mode of a figure, use this getter/setter. BTW, you can also set a background color: https://github.com/tammoippen/plotille/blob/master/plotille/_figure.py#L472 .

I hop, this helps you. I am looking forward to see how suger evolves :D.

Best, Tammo

brablc commented 2 months ago

We might be doing similar thing. I try to visualize sar/sysstat data generally in https://github.com/brablc/lazysar I use it to observe my swarm and can see that @xmnlab uses it in similar environment.

I am so far with real time data: https://asciinema.org/a/ECaK0TKivITr7kw4RtBK79RSy . It flickers, not sure if this can be avoided.

brablc commented 2 months ago

We might be doing similar thing. I try to visualize sar/sysstat data generally in https://github.com/brablc/lazysar I use it to observe my swarm and can see that @xmnlab uses it in similar environment.

I am so far with real time data: https://asciinema.org/a/ECaK0TKivITr7kw4RtBK79RSy . It flickers, not sure if this can be avoided.

In my I project I capture the data and display it using curses library to avoid flickering. Additionally I move the legend inside the chart area:

image

xmnlab commented 2 months ago

Thanks @tammoippen for the detailed explanation! Appreciate that :)

And thanks @brablc for sharing about your implementation! I am taking a look into that!

brablc commented 2 months ago

And here my (hopefully last post) demo with real-time data collected from three hosts:

lazysar-hosts

xmnlab commented 2 months ago

And here my (hopefully last post) demo with real-time data collected from three hosts:

that looks awesome! thanks for sharing!