Chart Shift Direction

I’m not sure what the specific goal was, but if it is to make a simple automatically-shifting chart go from left-to-right instead of the default right-to-left…it’s actually pretty simple.

My work is on LVGL 8.4.0, but this should apply to the newer version as well.

Firstly, I noticed the code in “lv_chart_set_next_value” ( lvgl/src/widgets/chart/lv_chart.c at 20bbe4a8ac33b2c8d8f44015aa1838c9e17f40cb · lvgl/lvgl · GitHub )

All it really does is round-robin the data through the chart (setting the Series Start point to avoid having to shift data through the entire buffer).

That means that it is very simple to reverse the direction of this function without changing literally anything else. Instead of calling “lv_chart_set_next_value”, roll the functionality yourself like this:

if (ser->start_point == 0) {ser->start_point = NUM_CHART_POINTS - 1;} else {ser->start_point--;}
ser->y_points[ser->start_point] = value;
lv_obj_invalidate(chart);

where “ser” = chart series, and “chart” = chart object. “NUM_CHART_POINTS” is self explanatory; the original routine could pull max points out of the chart object, but for some reason that property doesn’t seem available to the end user.
The last line is basically what the internal “invalidate_point” does with a full shift anyway.

Easy peasy :wink:

2 Likes