1 /**
2  * @file lv_chart.h
3  *
4  */
5 
6 #ifndef LV_CHART_H
7 #define LV_CHART_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../../../lvgl.h"
17 
18 #if LV_USE_CHART != 0
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 
24 /**Default value of points. Can be used to not draw a point*/
25 #if LV_USE_LARGE_COORD
26 #define LV_CHART_POINT_NONE (INT32_MAX)
27 #else
28 #define LV_CHART_POINT_NONE (INT16_MAX)
29 #endif
30 LV_EXPORT_CONST_INT(LV_CHART_POINT_NONE);
31 
32 /**********************
33  *      TYPEDEFS
34  **********************/
35 
36 /**
37  * Chart types
38  */
39 enum {
40     LV_CHART_TYPE_NONE,     /**< Don't draw the series*/
41     LV_CHART_TYPE_LINE,     /**< Connect the points with lines*/
42     LV_CHART_TYPE_BAR,      /**< Draw columns*/
43     LV_CHART_TYPE_SCATTER,  /**< Draw points and lines in 2D (x,y coordinates)*/
44 };
45 typedef uint8_t lv_chart_type_t;
46 
47 /**
48  * Chart update mode for `lv_chart_set_next`
49  */
50 enum {
51     LV_CHART_UPDATE_MODE_SHIFT,     /**< Shift old data to the left and add the new one the right*/
52     LV_CHART_UPDATE_MODE_CIRCULAR,  /**< Add the new data in a circular way*/
53 };
54 typedef uint8_t lv_chart_update_mode_t;
55 
56 /**
57  * Enumeration of the axis'
58  */
59 enum {
60     LV_CHART_AXIS_PRIMARY_Y     = 0x00,
61     LV_CHART_AXIS_SECONDARY_Y   = 0x01,
62     LV_CHART_AXIS_PRIMARY_X     = 0x02,
63     LV_CHART_AXIS_SECONDARY_X   = 0x04,
64     _LV_CHART_AXIS_LAST
65 };
66 typedef uint8_t lv_chart_axis_t;
67 
68 /**
69  * Descriptor a chart series
70  */
71 typedef struct {
72     lv_coord_t * x_points;
73     lv_coord_t * y_points;
74     lv_color_t color;
75     uint16_t start_point;
76     uint8_t hidden : 1;
77     uint8_t x_ext_buf_assigned : 1;
78     uint8_t y_ext_buf_assigned : 1;
79     uint8_t x_axis_sec : 1;
80     uint8_t y_axis_sec : 1;
81 } lv_chart_series_t;
82 
83 typedef struct {
84     lv_point_t pos;
85     lv_coord_t point_id;
86     lv_color_t color;
87     lv_chart_series_t * ser;
88     lv_dir_t dir;
89     uint8_t pos_set: 1; /*1: pos is set; 0: point_id is set*/
90 } lv_chart_cursor_t;
91 
92 typedef struct {
93     lv_coord_t major_len;
94     lv_coord_t minor_len;
95     lv_coord_t draw_size;
96     uint32_t minor_cnt : 15;
97     uint32_t major_cnt : 15;
98     uint32_t label_en  : 1;
99 } lv_chart_tick_dsc_t;
100 
101 typedef struct {
102     lv_obj_t obj;
103     lv_ll_t series_ll;     /**< Linked list for the series (stores lv_chart_series_t)*/
104     lv_ll_t cursor_ll;     /**< Linked list for the cursors (stores lv_chart_cursor_t)*/
105     lv_chart_tick_dsc_t tick[4];
106     lv_coord_t ymin[2];
107     lv_coord_t ymax[2];
108     lv_coord_t xmin[2];
109     lv_coord_t xmax[2];
110     lv_coord_t pressed_point_id;
111     uint16_t hdiv_cnt;      /**< Number of horizontal division lines*/
112     uint16_t vdiv_cnt;      /**< Number of vertical division lines*/
113     uint16_t point_cnt;    /**< Point number in a data line*/
114     uint16_t zoom_x;
115     uint16_t zoom_y;
116     lv_chart_type_t type  : 3; /**< Line or column chart*/
117     lv_chart_update_mode_t update_mode : 1;
118 } lv_chart_t;
119 
120 extern const lv_obj_class_t lv_chart_class;
121 
122 /**
123  * `type` field in `lv_obj_draw_part_dsc_t` if `class_p = lv_chart_class`
124  * Used in `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END`
125  */
126 typedef enum {
127     LV_CHART_DRAW_PART_DIV_LINE_INIT,    /**< Used before/after drawn the div lines*/
128     LV_CHART_DRAW_PART_DIV_LINE_HOR,     /**< Used for each horizontal division lines*/
129     LV_CHART_DRAW_PART_DIV_LINE_VER,     /**< Used for each vertical division lines*/
130     LV_CHART_DRAW_PART_LINE_AND_POINT,   /**< Used on line and scatter charts for lines and points*/
131     LV_CHART_DRAW_PART_BAR,              /**< Used on bar charts for the rectangles*/
132     LV_CHART_DRAW_PART_CURSOR,           /**< Used on cursor lines and points*/
133     LV_CHART_DRAW_PART_TICK_LABEL,       /**< Used on tick lines and labels*/
134 } lv_chart_draw_part_type_t;
135 
136 /**********************
137  * GLOBAL PROTOTYPES
138  **********************/
139 
140 /**
141  * Create a chart object
142  * @param parent    pointer to an object, it will be the parent of the new chart
143  * @return          pointer to the created chart
144  */
145 lv_obj_t * lv_chart_create(lv_obj_t * parent);
146 
147 /**
148  * Set a new type for a chart
149  * @param obj       pointer to a chart object
150  * @param type      new type of the chart (from 'lv_chart_type_t' enum)
151  */
152 void lv_chart_set_type(lv_obj_t * obj, lv_chart_type_t type);
153 /**
154  * Set the number of points on a data line on a chart
155  * @param obj       pointer to a chart object
156  * @param cnt       new number of points on the data lines
157  */
158 void lv_chart_set_point_count(lv_obj_t * obj, uint16_t cnt);
159 
160 /**
161  * Set the minimal and maximal y values on an axis
162  * @param obj       pointer to a chart object
163  * @param axis      `LV_CHART_AXIS_PRIMARY_Y` or `LV_CHART_AXIS_SECONDARY_Y`
164  * @param min       minimum value of the y axis
165  * @param max       maximum value of the y axis
166  */
167 void lv_chart_set_range(lv_obj_t * obj, lv_chart_axis_t axis, lv_coord_t min, lv_coord_t max);
168 
169 /**
170  * Set update mode of the chart object. Affects
171  * @param obj       pointer to a chart object
172  * @param mode      the update mode
173  */
174 void lv_chart_set_update_mode(lv_obj_t * obj, lv_chart_update_mode_t update_mode);
175 
176 /**
177  * Set the number of horizontal and vertical division lines
178  * @param obj       pointer to a chart object
179  * @param hdiv      number of horizontal division lines
180  * @param vdiv      number of vertical division lines
181  */
182 void lv_chart_set_div_line_count(lv_obj_t * obj, uint8_t hdiv, uint8_t vdiv);
183 
184 /**
185  * Zoom into the chart in X direction
186  * @param obj       pointer to a chart object
187  * @param zoom_x    zoom in x direction. LV_ZOOM_NONE or 256 for no zoom, 512 double zoom
188  */
189 void lv_chart_set_zoom_x(lv_obj_t * obj, uint16_t zoom_x);
190 
191 /**
192  * Zoom into the chart in Y direction
193  * @param obj       pointer to a chart object
194  * @param zoom_y    zoom in y direction. LV_ZOOM_NONE or 256 for no zoom, 512 double zoom
195  */
196 void lv_chart_set_zoom_y(lv_obj_t * obj, uint16_t zoom_y);
197 
198 /**
199  * Get X zoom of a chart
200  * @param obj       pointer to a chart object
201  * @return          the X zoom value
202  */
203 uint16_t lv_chart_get_zoom_x(const lv_obj_t * obj);
204 
205 /**
206  * Get Y zoom of a chart
207  * @param obj       pointer to a chart object
208  * @return          the Y zoom value
209  */
210 uint16_t lv_chart_get_zoom_y(const lv_obj_t * obj);
211 
212 /**
213  * Set the number of tick lines on an axis
214  * @param obj           pointer to a chart object
215  * @param axis          an axis which ticks count should be set
216  * @param major_len     length of major ticks
217  * @param minor_len     length of minor ticks
218  * @param major_cnt     number of major ticks on the axis
219  * @param minor_cnt     number of minor ticks between two major ticks
220  * @param label_en      true: enable label drawing on major ticks
221  * @param draw_size     extra size required to draw the tick and labels
222  *                      (start with 20 px and increase if the ticks/labels are clipped)
223  */
224 void lv_chart_set_axis_tick(lv_obj_t * obj, lv_chart_axis_t axis, lv_coord_t major_len, lv_coord_t minor_len,
225                             lv_coord_t major_cnt, lv_coord_t minor_cnt, bool label_en, lv_coord_t draw_size);
226 
227 /**
228  * Get the type of a chart
229  * @param obj       pointer to chart object
230  * @return          type of the chart (from 'lv_chart_t' enum)
231  */
232 lv_chart_type_t lv_chart_get_type(const lv_obj_t * obj);
233 
234 /**
235  * Get the data point number per data line on chart
236  * @param chart     pointer to chart object
237  * @return          point number on each data line
238  */
239 uint16_t lv_chart_get_point_count(const lv_obj_t * obj);
240 
241 /**
242  * Get the current index of the x-axis start point in the data array
243  * @param chart     pointer to a chart object
244  * @param ser       pointer to a data series on 'chart'
245  * @return          the index of the current x start point in the data array
246  */
247 uint16_t lv_chart_get_x_start_point(const lv_obj_t * obj, lv_chart_series_t * ser);
248 
249 /**
250  * Get the position of a point to the chart.
251  * @param chart     pointer to a chart object
252  * @param ser       pointer to series
253  * @param id        the index.
254  * @param p_out     store the result position here
255  */
256 void lv_chart_get_point_pos_by_id(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id, lv_point_t * p_out);
257 
258 /**
259  * Refresh a chart if its data line has changed
260  * @param   chart pointer to chart object
261  */
262 void lv_chart_refresh(lv_obj_t * obj);
263 
264 /*======================
265  * Series
266  *=====================*/
267 
268 /**
269  * Allocate and add a data series to the chart
270  * @param obj       pointer to a chart object
271  * @param color     color of the data series
272  * @param axis      the y axis to which the series should be attached (::LV_CHART_AXIS_PRIMARY_Y or ::LV_CHART_AXIS_SECONDARY_Y)
273  * @return          pointer to the allocated data series
274  */
275 lv_chart_series_t * lv_chart_add_series(lv_obj_t * obj, lv_color_t color, lv_chart_axis_t axis);
276 
277 /**
278  * Deallocate and remove a data series from a chart
279  * @param chart     pointer to a chart object
280  * @param series    pointer to a data series on 'chart'
281  */
282 void lv_chart_remove_series(lv_obj_t * obj, lv_chart_series_t * series);
283 
284 /**
285  * Hide/Unhide a single series of a chart.
286  * @param obj       pointer to a chart object.
287  * @param series    pointer to a series object
288  * @param hide      true: hide the series
289  */
290 void lv_chart_hide_series(lv_obj_t * chart, lv_chart_series_t * series, bool hide);
291 
292 /**
293  * Change the color of a series
294  * @param obj       pointer to a chart object.
295  * @param series    pointer to a series object
296  * @param color     the new color of the series
297  */
298 void lv_chart_set_series_color(lv_obj_t * chart, lv_chart_series_t * series, lv_color_t color);
299 
300 /**
301  * Set the index of the x-axis start point in the data array.
302  * This point will be considers the first (left) point and the other points will be drawn after it.
303  * @param obj       pointer to a chart object
304  * @param ser       pointer to a data series on 'chart'
305  * @param id        the index of the x point in the data array
306  */
307 void lv_chart_set_x_start_point(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id);
308 
309 /**
310  * Get the next series.
311  * @param chart     pointer to a chart
312  * @param ser      the previous series or NULL to get the first
313  * @return          the next series or NULL if there is no more.
314  */
315 lv_chart_series_t * lv_chart_get_series_next(const lv_obj_t * chart, const lv_chart_series_t * ser);
316 
317 /*=====================
318  * Cursor
319  *====================*/
320 
321 /**
322  * Add a cursor with a given color
323  * @param obj       pointer to chart object
324  * @param color     color of the cursor
325  * @param dir       direction of the cursor. `LV_DIR_RIGHT/LEFT/TOP/DOWN/HOR/VER/ALL`. OR-ed values are possible
326  * @return          pointer to the created cursor
327  */
328 lv_chart_cursor_t  * lv_chart_add_cursor(lv_obj_t * obj, lv_color_t color, lv_dir_t dir);
329 
330 /**
331  * Set the coordinate of the cursor with respect to the paddings
332  * @param obj       pointer to a chart object
333  * @param cursor    pointer to the cursor
334  * @param pos       the new coordinate of cursor relative to the chart
335  */
336 void lv_chart_set_cursor_pos(lv_obj_t * chart, lv_chart_cursor_t * cursor, lv_point_t * pos);
337 
338 /**
339  * Stick the cursor to a point
340  * @param obj       pointer to a chart object
341  * @param cursor    pointer to the cursor
342  * @param ser       pointer to a series
343  * @param point_id  the point's index or `LV_CHART_POINT_NONE` to not assign to any points.
344  */
345 void lv_chart_set_cursor_point(lv_obj_t * chart, lv_chart_cursor_t * cursor, lv_chart_series_t * ser,
346                                uint16_t point_id);
347 
348 /**
349  * Get the coordinate of the cursor with respect to the paddings
350  * @param obj       pointer to a chart object
351  * @param cursor    pointer to cursor
352  * @return          coordinate of the cursor as lv_point_t
353  */
354 lv_point_t lv_chart_get_cursor_point(lv_obj_t * chart, lv_chart_cursor_t * cursor);
355 
356 /*=====================
357  * Set/Get value(s)
358  *====================*/
359 
360 /**
361  * Initialize all data points of a series with a value
362  * @param obj       pointer to chart object
363  * @param ser       pointer to a data series on 'chart'
364  * @param value     the new value for all points. `LV_CHART_POINT_NONE` can be used to hide the points.
365  */
366 void lv_chart_set_all_value(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t value);
367 
368 /**
369  * Set the next point's Y value according to the update mode policy.
370  * @param obj       pointer to chart object
371  * @param ser       pointer to a data series on 'chart'
372  * @param value     the new value of the next data
373  */
374 void lv_chart_set_next_value(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t value);
375 
376 /**
377  * Set the next point's X and Y value according to the update mode policy.
378  * @param obj       pointer to chart object
379  * @param ser       pointer to a data series on 'chart'
380  * @param x_value   the new X value of the next data
381  * @param y_value   the new Y value of the next data
382  */
383 void lv_chart_set_next_value2(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t x_value, lv_coord_t y_value);
384 
385 /**
386  * Set an individual point's y value of a chart's series directly based on its index
387  * @param obj     pointer to a chart object
388  * @param ser     pointer to a data series on 'chart'
389  * @param id      the index of the x point in the array
390  * @param value   value to assign to array point
391  */
392 void lv_chart_set_value_by_id(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id, lv_coord_t value);
393 
394 /**
395  * Set an individual point's x and y value of a chart's series directly based on its index
396  * Can be used only with `LV_CHART_TYPE_SCATTER`.
397  * @param obj       pointer to chart object
398  * @param ser       pointer to a data series on 'chart'
399  * @param id        the index of the x point in the array
400  * @param x_value   the new X value of the next data
401  * @param y_value   the new Y value of the next data
402  */
403 void lv_chart_set_value_by_id2(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id, lv_coord_t x_value,
404                                lv_coord_t y_value);
405 
406 /**
407  * Set an external array for the y data points to use for the chart
408  * NOTE: It is the users responsibility to make sure the `point_cnt` matches the external array size.
409  * @param obj       pointer to a chart object
410  * @param ser       pointer to a data series on 'chart'
411  * @param array     external array of points for chart
412  */
413 void lv_chart_set_ext_y_array(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t array[]);
414 
415 /**
416  * Set an external array for the x data points to use for the chart
417  * NOTE: It is the users responsibility to make sure the `point_cnt` matches the external array size.
418  * @param obj       pointer to a chart object
419  * @param ser       pointer to a data series on 'chart'
420  * @param array     external array of points for chart
421  */
422 void lv_chart_set_ext_x_array(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t array[]);
423 
424 /**
425  * Get the array of y values of a series
426  * @param obj   pointer to a chart object
427  * @param ser   pointer to a data series on 'chart'
428  * @return      the array of values with 'point_count' elements
429  */
430 lv_coord_t * lv_chart_get_y_array(const lv_obj_t * obj, lv_chart_series_t * ser);
431 
432 /**
433  * Get the array of x values of a series
434  * @param obj   pointer to a chart object
435  * @param ser   pointer to a data series on 'chart'
436  * @return      the array of values with 'point_count' elements
437  */
438 lv_coord_t * lv_chart_get_x_array(const lv_obj_t * obj, lv_chart_series_t * ser);
439 
440 /**
441  * Get the index of the currently pressed point. It's the same for every series.
442  * @param obj       pointer to a chart object
443  * @return          the index of the point [0 .. point count] or LV_CHART_POINT_ID_NONE if no point is being pressed
444  */
445 uint32_t lv_chart_get_pressed_point(const lv_obj_t * obj);
446 
447 /**********************
448  *      MACROS
449  **********************/
450 
451 #endif /*LV_USE_CHART*/
452 
453 #ifdef __cplusplus
454 } /*extern "C"*/
455 #endif
456 
457 #endif /*LV_CHART_H*/
458