LVGL v8 Basechart
lvsf_basechart is a custom chart widget that SiFli wraps around LVGL v8’s native lv_chart. It draws one or more data series (line, bar, scatter, and so on) on the screen. While keeping every commonly used LVGL chart interface, it adds custom drawing capabilities such as colored bar segments, dual-endpoint bars, and scatter bars.
Main header: middleware/lvgl/lvsf/gui_widgets/lvsf_basechart.h
Dependent widget: LVGL chart (
LV_USE_CHART)Example project: example/multimedia/lvgl/lvgl_v8_basechart
Note
lvsf_basechart inherits from lv_chart_t (its first struct member is lv_chart_t bg). Therefore, in addition to the wrapper interfaces listed below, the widget still follows the LVGL chart styling and event model. LV_PART_ITEMS corresponds to series lines, and LV_PART_INDICATOR corresponds to data-point markers.
Features
Supports multiple chart types such as line (LINE), bar (BAR), and scatter (SCATTER), with a one-to-one mapping to the LVGL
lv_chartinterfaces.Supports multiple data series; each series can have its own color, axis, and show/hide state.
Supports colored bar segments: different colors are mapped to numeric ranges (
lv_basechart_color_t).Supports three bar forms: normal bars, dual-endpoint bars (two data values per bar), and scatter bars (connected data points merged into one bar).
Provides common chart capabilities such as cursor positioning, axis ticks, X/Y axis zoom, and grid dividers.
Supports periodic refresh via a timer (
lv_basechart_refresh_timer()), which is convenient for scroll-refreshed real-time data display.
Use Cases
Real-time data curves, such as heart rate, battery level, and sensor waveforms.
Bar statistics, such as segmented battery level and numeric-range distribution.
Dashboards and health-data charts that need coloring by numeric range.
Streaming data series that update by scrolling (used together with
set_next_valueand timer refresh).
Supported Boards
sf32lb52-lcd_n16r8
sf32lb52-lchspi-ulp
Configuration and Initialization
lvsf_basechart depends on the LVGL chart component. In menuconfig, confirm that the chart widget is enabled:
CONFIG_LV_USE_CHART=y
In the BSP, the LVSF_USE_BASECHART macro (under the lvsf configuration) controls whether this widget is compiled. The example project enables it by default in proj.conf.
Minimum flow to create and configure an object:
#include "lvsf_basechart.h"
lv_obj_t *chart = lv_basechart_create(lv_scr_act());
lv_obj_set_size(chart, 280, 180);
API Reference
Widget creation and type extensions
Function |
Description |
Parameters / Returns |
|---|---|---|
|
Creates and initializes a basechart object |
|
|
Chart refresh timer callback |
|
|
Sets the bar type |
|
|
Sets colored segments |
|
|
Adds a simple multiple |
|
Bar type enumeration (lv_basechart_column_type_t):
Value |
Meaning |
|---|---|
|
Normal bar chart |
|
Bar chart with two endpoints; each bar requires two data values |
|
Scatter points on a bar chart; connected data points are joined into one bar |
Chart property settings (wrapped from lv_chart)
Function |
Description |
Parameters / Returns |
|---|---|---|
|
Sets the chart type (line/bar/etc.) |
|
|
Sets the number of points per data series |
|
|
Sets the axis range |
|
|
Sets the chart update mode |
|
|
Sets the number of grid dividers |
|
|
Sets the X-axis zoom ratio |
|
|
Sets the Y-axis zoom ratio |
|
|
Gets the X-axis zoom ratio |
Returns the X-axis zoom ratio |
|
Gets the Y-axis zoom ratio |
Returns the Y-axis zoom ratio |
|
Sets axis ticks |
|
|
Gets the chart type |
Returns the chart type |
|
Gets the point count |
Returns the point count |
|
Gets the X-axis start-point index |
|
|
Gets the coordinate position of the point with the given ID |
|
|
Forces a chart refresh |
No return value |
Data series
Function |
Description |
Parameters / Returns |
|---|---|---|
|
Adds a data series |
|
|
Removes a data series |
|
|
Hides/shows a data series |
|
|
Sets the data series color |
|
|
Sets the X-axis start point |
|
|
Gets the next data series |
|
|
Sets all points of a series to the same value |
|
|
Pushes the value of the next point |
Commonly used for streaming scroll updates |
|
Pushes the X/Y values of the next point |
|
|
Sets the value of the point with the given ID |
|
|
Sets the X/Y values of the point with the given ID |
— |
|
Sets an external Y array |
|
|
Sets an external X array |
|
|
Gets the Y array pointer |
Returns the Y array pointer |
|
Gets the X array pointer |
Returns the X array pointer |
|
Gets the point ID pressed by the user |
Returns the pressed point ID |
Cursor
Function |
Description |
Parameters / Returns |
|---|---|---|
|
Adds a cursor |
|
|
Sets the cursor position |
|
|
Sets the data point the cursor points to |
|
|
Gets the coordinates of the point the cursor points to |
Returns the coordinates of the point the cursor points to |
Typical Usage
The following example draws two line series (red and blue), each with 10 data points, and a vertical-axis range of 0~100:
#include "lvsf_basechart.h"
void demo_basechart_init(void)
{
lv_obj_t *scr = lv_scr_act();
lv_obj_t *chart = lv_basechart_create(scr);
lv_obj_set_size(chart, 280, 180);
lv_obj_align(chart, LV_ALIGN_CENTER, 0, 10);
lv_obj_set_style_bg_color(chart, lv_color_white(), 0);
lv_obj_set_style_bg_opa(chart, LV_OPA_COVER, 0);
/* Grid color and line width */
lv_obj_set_style_line_color(chart, lv_palette_lighten(LV_PALETTE_GREY, 2), LV_PART_MAIN);
lv_obj_set_style_line_width(chart, 1, LV_PART_MAIN);
/* Series line width and data-point size */
lv_obj_set_style_line_width(chart, 3, LV_PART_ITEMS);
lv_obj_set_style_width(chart, 5, LV_PART_INDICATOR);
lv_obj_set_style_height(chart, 5, LV_PART_INDICATOR);
lv_basechart_set_type(chart, LV_CHART_TYPE_LINE);
lv_basechart_set_point_count(chart, 10);
lv_basechart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
lv_basechart_set_div_line_count(chart, 5, 6);
static const lv_coord_t v1[10] = {10, 45, 30, 70, 50, 92, 60, 80, 40, 78};
static const lv_coord_t v2[10] = {80, 60, 75, 40, 55, 20, 48, 35, 65, 30};
lv_chart_series_t *s1 = lv_basechart_add_series(chart, lv_palette_main(LV_PALETTE_RED),
LV_CHART_AXIS_PRIMARY_Y);
lv_chart_series_t *s2 = lv_basechart_add_series(chart, lv_palette_main(LV_PALETTE_BLUE),
LV_CHART_AXIS_PRIMARY_Y);
for (int i = 0; i < 10; i++)
{
lv_basechart_set_next_value(chart, s1, v1[i]);
lv_basechart_set_next_value(chart, s2, v2[i]);
}
lv_basechart_refresh(chart);
}
Demo
Run the lvgl_v8_basechart example to see the result: a white-background chart with grid lines is shown in the center of the screen, drawing two polylines (red and blue), each with 10 data points, and a vertical-axis range of 0~100.
See Also
Source path:
middleware/lvgl/lvsf/gui_widgets/lvsf_basechart.hExample path:
example/multimedia/lvgl/lvgl_v8_basechart