1 /**
2 * @file lv_chart.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_chart.h"
10 #if LV_USE_CHART != 0
11
12 #include "../../../misc/lv_assert.h"
13
14 /*********************
15 * DEFINES
16 *********************/
17 #define MY_CLASS &lv_chart_class
18
19 #define LV_CHART_HDIV_DEF 3
20 #define LV_CHART_VDIV_DEF 5
21 #define LV_CHART_POINT_CNT_DEF 10
22 #define LV_CHART_LABEL_MAX_TEXT_LENGTH 16
23
24 /**********************
25 * TYPEDEFS
26 **********************/
27
28 /**********************
29 * STATIC PROTOTYPES
30 **********************/
31 static void lv_chart_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
32 static void lv_chart_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
33 static void lv_chart_event(const lv_obj_class_t * class_p, lv_event_t * e);
34
35 static void draw_div_lines(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx);
36 static void draw_series_line(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx);
37 static void draw_series_bar(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx);
38 static void draw_series_scatter(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx);
39 static void draw_cursors(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx);
40 static void draw_axes(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx);
41 static uint32_t get_index_from_x(lv_obj_t * obj, lv_coord_t x);
42 static void invalidate_point(lv_obj_t * obj, uint16_t i);
43 static void new_points_alloc(lv_obj_t * obj, lv_chart_series_t * ser, uint32_t cnt, lv_coord_t ** a);
44 lv_chart_tick_dsc_t * get_tick_gsc(lv_obj_t * obj, lv_chart_axis_t axis);
45
46 /**********************
47 * STATIC VARIABLES
48 **********************/
49 const lv_obj_class_t lv_chart_class = {
50 .constructor_cb = lv_chart_constructor,
51 .destructor_cb = lv_chart_destructor,
52 .event_cb = lv_chart_event,
53 .width_def = LV_PCT(100),
54 .height_def = LV_DPI_DEF * 2,
55 .instance_size = sizeof(lv_chart_t),
56 .base_class = &lv_obj_class
57 };
58
59 /**********************
60 * MACROS
61 **********************/
62
63 /**********************
64 * GLOBAL FUNCTIONS
65 **********************/
66
lv_chart_create(lv_obj_t * parent)67 lv_obj_t * lv_chart_create(lv_obj_t * parent)
68 {
69 LV_LOG_INFO("begin");
70 lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
71 lv_obj_class_init_obj(obj);
72 return obj;
73 }
74
lv_chart_set_type(lv_obj_t * obj,lv_chart_type_t type)75 void lv_chart_set_type(lv_obj_t * obj, lv_chart_type_t type)
76 {
77 LV_ASSERT_OBJ(obj, MY_CLASS);
78
79 lv_chart_t * chart = (lv_chart_t *)obj;
80 if(chart->type == type) return;
81
82 if(chart->type == LV_CHART_TYPE_SCATTER) {
83 lv_chart_series_t * ser;
84 _LV_LL_READ_BACK(&chart->series_ll, ser) {
85 lv_mem_free(ser->x_points);
86 ser->x_points = NULL;
87 }
88 }
89
90 if(type == LV_CHART_TYPE_SCATTER) {
91 lv_chart_series_t * ser;
92 _LV_LL_READ_BACK(&chart->series_ll, ser) {
93 ser->x_points = lv_mem_alloc(sizeof(lv_point_t) * chart->point_cnt);
94 LV_ASSERT_MALLOC(ser->x_points);
95 if(ser->x_points == NULL) return;
96 }
97 }
98
99 chart->type = type;
100
101 lv_chart_refresh(obj);
102 }
103
lv_chart_set_point_count(lv_obj_t * obj,uint16_t cnt)104 void lv_chart_set_point_count(lv_obj_t * obj, uint16_t cnt)
105 {
106 LV_ASSERT_OBJ(obj, MY_CLASS);
107
108 lv_chart_t * chart = (lv_chart_t *)obj;
109 if(chart->point_cnt == cnt) return;
110
111 lv_chart_series_t * ser;
112
113 if(cnt < 1) cnt = 1;
114
115 _LV_LL_READ_BACK(&chart->series_ll, ser) {
116 if(chart->type == LV_CHART_TYPE_SCATTER) {
117 if(!ser->x_ext_buf_assigned) new_points_alloc(obj, ser, cnt, &ser->x_points);
118 }
119 if(!ser->y_ext_buf_assigned) new_points_alloc(obj, ser, cnt, &ser->y_points);
120 ser->start_point = 0;
121 }
122
123 chart->point_cnt = cnt;
124
125 lv_chart_refresh(obj);
126 }
127
lv_chart_set_range(lv_obj_t * obj,lv_chart_axis_t axis,lv_coord_t min,lv_coord_t max)128 void lv_chart_set_range(lv_obj_t * obj, lv_chart_axis_t axis, lv_coord_t min, lv_coord_t max)
129 {
130 LV_ASSERT_OBJ(obj, MY_CLASS);
131
132 max = max == min ? max + 1 : max;
133
134 lv_chart_t * chart = (lv_chart_t *)obj;
135 switch(axis) {
136 case LV_CHART_AXIS_PRIMARY_Y:
137 chart->ymin[0] = min;
138 chart->ymax[0] = max;
139 break;
140 case LV_CHART_AXIS_SECONDARY_Y:
141 chart->ymin[1] = min;
142 chart->ymax[1] = max;
143 break;
144 case LV_CHART_AXIS_PRIMARY_X:
145 chart->xmin[0] = min;
146 chart->xmax[0] = max;
147 break;
148 case LV_CHART_AXIS_SECONDARY_X:
149 chart->xmin[1] = min;
150 chart->xmax[1] = max;
151 break;
152 default:
153 LV_LOG_WARN("Invalid axis: %d", axis);
154 return;
155 }
156
157 lv_chart_refresh(obj);
158 }
159
lv_chart_set_update_mode(lv_obj_t * obj,lv_chart_update_mode_t update_mode)160 void lv_chart_set_update_mode(lv_obj_t * obj, lv_chart_update_mode_t update_mode)
161 {
162 LV_ASSERT_OBJ(obj, MY_CLASS);
163
164 lv_chart_t * chart = (lv_chart_t *)obj;
165 if(chart->update_mode == update_mode) return;
166
167 chart->update_mode = update_mode;
168 lv_obj_invalidate(obj);
169 }
170
lv_chart_set_div_line_count(lv_obj_t * obj,uint8_t hdiv,uint8_t vdiv)171 void lv_chart_set_div_line_count(lv_obj_t * obj, uint8_t hdiv, uint8_t vdiv)
172 {
173 LV_ASSERT_OBJ(obj, MY_CLASS);
174
175 lv_chart_t * chart = (lv_chart_t *)obj;
176 if(chart->hdiv_cnt == hdiv && chart->vdiv_cnt == vdiv) return;
177
178 chart->hdiv_cnt = hdiv;
179 chart->vdiv_cnt = vdiv;
180
181 lv_obj_invalidate(obj);
182 }
183
184
lv_chart_set_zoom_x(lv_obj_t * obj,uint16_t zoom_x)185 void lv_chart_set_zoom_x(lv_obj_t * obj, uint16_t zoom_x)
186 {
187 LV_ASSERT_OBJ(obj, MY_CLASS);
188
189 lv_chart_t * chart = (lv_chart_t *)obj;
190 if(chart->zoom_x == zoom_x) return;
191
192 chart->zoom_x = zoom_x;
193 lv_obj_refresh_self_size(obj);
194 /*Be the chart doesn't remain scrolled out*/
195 lv_obj_readjust_scroll(obj, LV_ANIM_OFF);
196 lv_obj_invalidate(obj);
197 }
198
lv_chart_set_zoom_y(lv_obj_t * obj,uint16_t zoom_y)199 void lv_chart_set_zoom_y(lv_obj_t * obj, uint16_t zoom_y)
200 {
201 LV_ASSERT_OBJ(obj, MY_CLASS);
202
203 lv_chart_t * chart = (lv_chart_t *)obj;
204 if(chart->zoom_y == zoom_y) return;
205
206 chart->zoom_y = zoom_y;
207 lv_obj_refresh_self_size(obj);
208 /*Be the chart doesn't remain scrolled out*/
209 lv_obj_readjust_scroll(obj, LV_ANIM_OFF);
210 lv_obj_invalidate(obj);
211 }
212
lv_chart_get_zoom_x(const lv_obj_t * obj)213 uint16_t lv_chart_get_zoom_x(const lv_obj_t * obj)
214 {
215 LV_ASSERT_OBJ(obj, MY_CLASS);
216
217 lv_chart_t * chart = (lv_chart_t *)obj;
218 return chart->zoom_x;
219 }
220
lv_chart_get_zoom_y(const lv_obj_t * obj)221 uint16_t lv_chart_get_zoom_y(const lv_obj_t * obj)
222 {
223 LV_ASSERT_OBJ(obj, MY_CLASS);
224
225 lv_chart_t * chart = (lv_chart_t *)obj;
226 return chart->zoom_y;
227 }
228
lv_chart_set_axis_tick(lv_obj_t * obj,lv_chart_axis_t axis,lv_coord_t major_len,lv_coord_t minor_len,lv_coord_t major_cnt,lv_coord_t minor_cnt,bool label_en,lv_coord_t draw_size)229 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,
230 lv_coord_t major_cnt, lv_coord_t minor_cnt, bool label_en, lv_coord_t draw_size)
231 {
232 LV_ASSERT_OBJ(obj, MY_CLASS);
233
234 lv_chart_tick_dsc_t * t = get_tick_gsc(obj, axis);
235 t->major_len = major_len;
236 t->minor_len = minor_len;
237 t->minor_cnt = minor_cnt;
238 t->major_cnt = major_cnt;
239 t->label_en = label_en;
240 t->draw_size = draw_size;
241
242 lv_obj_refresh_ext_draw_size(obj);
243 lv_obj_invalidate(obj);
244 }
245
lv_chart_get_type(const lv_obj_t * obj)246 lv_chart_type_t lv_chart_get_type(const lv_obj_t * obj)
247 {
248 LV_ASSERT_OBJ(obj, MY_CLASS);
249
250 lv_chart_t * chart = (lv_chart_t *)obj;
251 return chart->type;
252 }
253
lv_chart_get_point_count(const lv_obj_t * obj)254 uint16_t lv_chart_get_point_count(const lv_obj_t * obj)
255 {
256 LV_ASSERT_OBJ(obj, MY_CLASS);
257
258 lv_chart_t * chart = (lv_chart_t *)obj;
259 return chart->point_cnt;
260 }
261
lv_chart_get_x_start_point(const lv_obj_t * obj,lv_chart_series_t * ser)262 uint16_t lv_chart_get_x_start_point(const lv_obj_t * obj, lv_chart_series_t * ser)
263 {
264 LV_UNUSED(obj);
265 LV_ASSERT_NULL(ser);
266
267 return ser->start_point;
268 }
269
lv_chart_get_point_pos_by_id(lv_obj_t * obj,lv_chart_series_t * ser,uint16_t id,lv_point_t * p_out)270 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)
271 {
272 LV_ASSERT_NULL(obj);
273 LV_ASSERT_NULL(ser);
274 LV_ASSERT_OBJ(obj, MY_CLASS);
275
276 lv_chart_t * chart = (lv_chart_t *)obj;
277 if(id >= chart->point_cnt) {
278 LV_LOG_WARN("Invalid index: %d", id);
279 p_out->x = 0;
280 p_out->y = 0;
281 return;
282 }
283
284 lv_coord_t w = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
285 lv_coord_t h = ((int32_t)lv_obj_get_content_height(obj) * chart->zoom_y) >> 8;
286
287 if(chart->type == LV_CHART_TYPE_LINE) {
288 p_out->x = (w * id) / (chart->point_cnt - 1);
289 }
290 else if(chart->type == LV_CHART_TYPE_SCATTER) {
291 p_out->x = lv_map(ser->x_points[id], chart->xmin[ser->x_axis_sec], chart->xmax[ser->x_axis_sec], 0, w);
292 }
293 else if(chart->type == LV_CHART_TYPE_BAR) {
294 uint32_t ser_cnt = _lv_ll_get_len(&chart->series_ll);
295 int32_t ser_gap = ((int32_t)lv_obj_get_style_pad_column(obj,
296 LV_PART_ITEMS) * chart->zoom_x) >> 8; /*Gap between the column on the ~same X*/
297 int32_t block_gap = ((int32_t)lv_obj_get_style_pad_column(obj,
298 LV_PART_MAIN) * chart->zoom_x) >> 8; /*Gap between the column on ~adjacent X*/
299 lv_coord_t block_w = (w - ((chart->point_cnt - 1) * block_gap)) / chart->point_cnt;
300 lv_coord_t col_w = block_w / ser_cnt;
301
302 p_out->x = (int32_t)((int32_t)w * id) / chart->point_cnt;
303
304 lv_chart_series_t * ser_i = NULL;
305 _LV_LL_READ_BACK(&chart->series_ll, ser_i) {
306 if(ser_i == ser) break;
307 p_out->x += col_w;
308 }
309
310 p_out->x += (col_w - ser_gap) / 2;
311 }
312
313 lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
314 p_out->x += lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + border_width;
315 p_out->x -= lv_obj_get_scroll_left(obj);
316
317 int32_t temp_y = 0;
318 temp_y = (int32_t)((int32_t)ser->y_points[id] - chart->ymin[ser->y_axis_sec]) * h;
319 temp_y = temp_y / (chart->ymax[ser->y_axis_sec] - chart->ymin[ser->y_axis_sec]);
320 p_out->y = h - temp_y;
321 p_out->y += lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + border_width;
322 p_out->y -= lv_obj_get_scroll_top(obj);
323 }
324
lv_chart_refresh(lv_obj_t * obj)325 void lv_chart_refresh(lv_obj_t * obj)
326 {
327 LV_ASSERT_OBJ(obj, MY_CLASS);
328
329 lv_obj_invalidate(obj);
330 }
331
332 /*======================
333 * Series
334 *=====================*/
335
lv_chart_add_series(lv_obj_t * obj,lv_color_t color,lv_chart_axis_t axis)336 lv_chart_series_t * lv_chart_add_series(lv_obj_t * obj, lv_color_t color, lv_chart_axis_t axis)
337 {
338 LV_LOG_INFO("begin");
339
340 LV_ASSERT_OBJ(obj, MY_CLASS);
341
342 lv_chart_t * chart = (lv_chart_t *)obj;
343 lv_chart_series_t * ser = _lv_ll_ins_head(&chart->series_ll);
344 LV_ASSERT_MALLOC(ser);
345 if(ser == NULL) return NULL;
346
347 lv_coord_t def = LV_CHART_POINT_NONE;
348
349 ser->color = color;
350 ser->y_points = lv_mem_alloc(sizeof(lv_coord_t) * chart->point_cnt);
351 LV_ASSERT_MALLOC(ser->y_points);
352
353 if(chart->type == LV_CHART_TYPE_SCATTER) {
354 ser->x_points = lv_mem_alloc(sizeof(lv_coord_t) * chart->point_cnt);
355 LV_ASSERT_MALLOC(ser->x_points);
356 }
357 if(ser->y_points == NULL) {
358 _lv_ll_remove(&chart->series_ll, ser);
359 lv_mem_free(ser);
360 return NULL;
361 }
362
363 ser->start_point = 0;
364 ser->y_ext_buf_assigned = false;
365 ser->hidden = 0;
366 ser->x_axis_sec = axis & LV_CHART_AXIS_SECONDARY_X ? 1 : 0;
367 ser->y_axis_sec = axis & LV_CHART_AXIS_SECONDARY_Y ? 1 : 0;
368
369 uint16_t i;
370 lv_coord_t * p_tmp = ser->y_points;
371 for(i = 0; i < chart->point_cnt; i++) {
372 *p_tmp = def;
373 p_tmp++;
374 }
375
376 return ser;
377 }
378
lv_chart_remove_series(lv_obj_t * obj,lv_chart_series_t * series)379 void lv_chart_remove_series(lv_obj_t * obj, lv_chart_series_t * series)
380 {
381 LV_ASSERT_OBJ(obj, MY_CLASS);
382 LV_ASSERT_NULL(series);
383
384 lv_chart_t * chart = (lv_chart_t *)obj;
385 if(!series->y_ext_buf_assigned && series->y_points) lv_mem_free(series->y_points);
386
387 _lv_ll_remove(&chart->series_ll, series);
388 lv_mem_free(series);
389
390 return;
391 }
392
lv_chart_hide_series(lv_obj_t * chart,lv_chart_series_t * series,bool hide)393 void lv_chart_hide_series(lv_obj_t * chart, lv_chart_series_t * series, bool hide)
394 {
395 LV_ASSERT_OBJ(chart, MY_CLASS);
396 LV_ASSERT_NULL(series);
397
398 series->hidden = hide ? 1 : 0;
399 lv_chart_refresh(chart);
400 }
401
402
lv_chart_set_series_color(lv_obj_t * chart,lv_chart_series_t * series,lv_color_t color)403 void lv_chart_set_series_color(lv_obj_t * chart, lv_chart_series_t * series, lv_color_t color)
404 {
405 LV_ASSERT_OBJ(chart, MY_CLASS);
406 LV_ASSERT_NULL(series);
407
408 series->color = color;
409 lv_chart_refresh(chart);
410 }
411
lv_chart_set_x_start_point(lv_obj_t * obj,lv_chart_series_t * ser,uint16_t id)412 void lv_chart_set_x_start_point(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id)
413 {
414 LV_ASSERT_OBJ(obj, MY_CLASS);
415 LV_ASSERT_NULL(ser);
416
417 lv_chart_t * chart = (lv_chart_t *)obj;
418 if(id >= chart->point_cnt) return;
419 ser->start_point = id;
420 }
421
lv_chart_get_series_next(const lv_obj_t * obj,const lv_chart_series_t * ser)422 lv_chart_series_t * lv_chart_get_series_next(const lv_obj_t * obj, const lv_chart_series_t * ser)
423 {
424 LV_ASSERT_OBJ(obj, MY_CLASS);
425
426 lv_chart_t * chart = (lv_chart_t *)obj;
427 if(ser == NULL) return _lv_ll_get_head(&chart->series_ll);
428 else return _lv_ll_get_next(&chart->series_ll, ser);
429 }
430
431 /*=====================
432 * Cursor
433 *====================*/
434
435 /**
436 * Add a cursor with a given color
437 * @param chart pointer to chart object
438 * @param color color of the cursor
439 * @param dir direction of the cursor. `LV_DIR_RIGHT/LEFT/TOP/DOWN/HOR/VER/ALL`. OR-ed values are possible
440 * @return pointer to the created cursor
441 */
lv_chart_add_cursor(lv_obj_t * obj,lv_color_t color,lv_dir_t dir)442 lv_chart_cursor_t * lv_chart_add_cursor(lv_obj_t * obj, lv_color_t color, lv_dir_t dir)
443 {
444 LV_ASSERT_OBJ(obj, MY_CLASS);
445
446 lv_chart_t * chart = (lv_chart_t *)obj;
447 lv_chart_cursor_t * cursor = _lv_ll_ins_head(&chart->cursor_ll);
448 LV_ASSERT_MALLOC(cursor);
449 if(cursor == NULL) return NULL;
450
451 cursor->pos.x = LV_CHART_POINT_NONE;
452 cursor->pos.y = LV_CHART_POINT_NONE;
453 cursor->point_id = LV_CHART_POINT_NONE;
454 cursor->pos_set = 0;
455 cursor->color = color;
456 cursor->dir = dir;
457
458 return cursor;
459 }
460
461 /**
462 * Set the coordinate of the cursor with respect
463 * to the origin of series area of the chart.
464 * @param chart pointer to a chart object.
465 * @param cursor pointer to the cursor.
466 * @param pos the new coordinate of cursor relative to the series area
467 */
lv_chart_set_cursor_pos(lv_obj_t * chart,lv_chart_cursor_t * cursor,lv_point_t * pos)468 void lv_chart_set_cursor_pos(lv_obj_t * chart, lv_chart_cursor_t * cursor, lv_point_t * pos)
469 {
470 LV_ASSERT_NULL(cursor);
471 LV_UNUSED(chart);
472
473 cursor->pos.x = pos->x;
474 cursor->pos.y = pos->y;
475 cursor->pos_set = 1;
476 lv_chart_refresh(chart);
477 }
478
479
480 /**
481 * Set the coordinate of the cursor with respect
482 * to the origin of series area of the chart.
483 * @param chart pointer to a chart object.
484 * @param cursor pointer to the cursor.
485 * @param pos the new coordinate of cursor relative to the series area
486 */
lv_chart_set_cursor_point(lv_obj_t * chart,lv_chart_cursor_t * cursor,lv_chart_series_t * ser,uint16_t point_id)487 void lv_chart_set_cursor_point(lv_obj_t * chart, lv_chart_cursor_t * cursor, lv_chart_series_t * ser, uint16_t point_id)
488 {
489 LV_ASSERT_NULL(cursor);
490 LV_UNUSED(chart);
491
492 cursor->point_id = point_id;
493 cursor->pos_set = 0;
494 if(ser == NULL) ser = lv_chart_get_series_next(chart, NULL);
495 cursor->ser = ser;
496 lv_chart_refresh(chart);
497 }
498 /**
499 * Get the coordinate of the cursor with respect
500 * to the origin of series area of the chart.
501 * @param chart pointer to a chart object
502 * @param cursor pointer to cursor
503 * @return coordinate of the cursor as lv_point_t
504 */
lv_chart_get_cursor_point(lv_obj_t * chart,lv_chart_cursor_t * cursor)505 lv_point_t lv_chart_get_cursor_point(lv_obj_t * chart, lv_chart_cursor_t * cursor)
506 {
507 LV_ASSERT_NULL(cursor);
508 LV_UNUSED(chart);
509
510 return cursor->pos;
511 }
512
513 /*=====================
514 * Set/Get value(s)
515 *====================*/
516
517
lv_chart_set_all_value(lv_obj_t * obj,lv_chart_series_t * ser,lv_coord_t value)518 void lv_chart_set_all_value(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t value)
519 {
520 LV_ASSERT_OBJ(obj, MY_CLASS);
521 LV_ASSERT_NULL(ser);
522
523 lv_chart_t * chart = (lv_chart_t *)obj;
524 uint16_t i;
525 for(i = 0; i < chart->point_cnt; i++) {
526 ser->y_points[i] = value;
527 }
528 ser->start_point = 0;
529 lv_chart_refresh(obj);
530 }
531
lv_chart_set_next_value(lv_obj_t * obj,lv_chart_series_t * ser,lv_coord_t value)532 void lv_chart_set_next_value(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t value)
533 {
534 LV_ASSERT_OBJ(obj, MY_CLASS);
535 LV_ASSERT_NULL(ser);
536
537 lv_chart_t * chart = (lv_chart_t *)obj;
538 ser->y_points[ser->start_point] = value;
539 invalidate_point(obj, ser->start_point);
540 ser->start_point = (ser->start_point + 1) % chart->point_cnt;
541 invalidate_point(obj, ser->start_point);
542 lv_chart_refresh(obj);
543 }
544
lv_chart_set_next_value2(lv_obj_t * obj,lv_chart_series_t * ser,lv_coord_t x_value,lv_coord_t y_value)545 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)
546 {
547 LV_ASSERT_OBJ(obj, MY_CLASS);
548 LV_ASSERT_NULL(ser);
549
550 lv_chart_t * chart = (lv_chart_t *)obj;
551
552 if(chart->type != LV_CHART_TYPE_SCATTER) {
553 LV_LOG_WARN("Type must be LV_CHART_TYPE_SCATTER");
554 return;
555 }
556
557 ser->x_points[ser->start_point] = x_value;
558 ser->y_points[ser->start_point] = y_value;
559 invalidate_point(obj, ser->start_point);
560 ser->start_point = (ser->start_point + 1) % chart->point_cnt;
561 invalidate_point(obj, ser->start_point);
562 lv_chart_refresh(obj);
563
564 }
565
lv_chart_set_value_by_id(lv_obj_t * obj,lv_chart_series_t * ser,uint16_t id,lv_coord_t value)566 void lv_chart_set_value_by_id(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id, lv_coord_t value)
567 {
568 LV_ASSERT_OBJ(obj, MY_CLASS);
569 LV_ASSERT_NULL(ser);
570 lv_chart_t * chart = (lv_chart_t *)obj;
571
572 if(id >= chart->point_cnt) return;
573 ser->y_points[id] = value;
574 lv_chart_refresh(obj);
575 }
576
lv_chart_set_value_by_id2(lv_obj_t * obj,lv_chart_series_t * ser,uint16_t id,lv_coord_t x_value,lv_coord_t y_value)577 void lv_chart_set_value_by_id2(lv_obj_t * obj, lv_chart_series_t * ser, uint16_t id, lv_coord_t x_value,
578 lv_coord_t y_value)
579 {
580 LV_ASSERT_OBJ(obj, MY_CLASS);
581 LV_ASSERT_NULL(ser);
582 lv_chart_t * chart = (lv_chart_t *)obj;
583
584 if(chart->type != LV_CHART_TYPE_SCATTER) {
585 LV_LOG_WARN("Type must be LV_CHART_TYPE_SCATTER");
586 return;
587 }
588
589 if(id >= chart->point_cnt) return;
590 ser->x_points[id] = x_value;
591 ser->y_points[id] = y_value;
592 lv_chart_refresh(obj);
593 }
594
lv_chart_set_ext_y_array(lv_obj_t * obj,lv_chart_series_t * ser,lv_coord_t array[])595 void lv_chart_set_ext_y_array(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t array[])
596 {
597 LV_ASSERT_OBJ(obj, MY_CLASS);
598 LV_ASSERT_NULL(ser);
599
600 if(!ser->y_ext_buf_assigned && ser->y_points) lv_mem_free(ser->y_points);
601 ser->y_ext_buf_assigned = true;
602 ser->y_points = array;
603 lv_obj_invalidate(obj);
604 }
605
lv_chart_set_ext_x_array(lv_obj_t * obj,lv_chart_series_t * ser,lv_coord_t array[])606 void lv_chart_set_ext_x_array(lv_obj_t * obj, lv_chart_series_t * ser, lv_coord_t array[])
607 {
608 LV_ASSERT_OBJ(obj, MY_CLASS);
609 LV_ASSERT_NULL(ser);
610
611 if(!ser->x_ext_buf_assigned && ser->x_points) lv_mem_free(ser->x_points);
612 ser->x_ext_buf_assigned = true;
613 ser->x_points = array;
614 lv_obj_invalidate(obj);
615 }
616
lv_chart_get_y_array(const lv_obj_t * obj,lv_chart_series_t * ser)617 lv_coord_t * lv_chart_get_y_array(const lv_obj_t * obj, lv_chart_series_t * ser)
618 {
619 LV_UNUSED(obj);
620 LV_ASSERT_OBJ(obj, MY_CLASS);
621 LV_ASSERT_NULL(ser);
622 return ser->y_points;
623 }
624
lv_chart_get_x_array(const lv_obj_t * obj,lv_chart_series_t * ser)625 lv_coord_t * lv_chart_get_x_array(const lv_obj_t * obj, lv_chart_series_t * ser)
626 {
627 LV_UNUSED(obj);
628 LV_ASSERT_OBJ(obj, MY_CLASS);
629 LV_ASSERT_NULL(ser);
630 return ser->x_points;
631 }
632
lv_chart_get_pressed_point(const lv_obj_t * obj)633 uint32_t lv_chart_get_pressed_point(const lv_obj_t * obj)
634 {
635 lv_chart_t * chart = (lv_chart_t *)obj;
636 return chart->pressed_point_id;
637 }
638
639 /**********************
640 * STATIC FUNCTIONS
641 **********************/
642
lv_chart_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)643 static void lv_chart_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
644 {
645 LV_UNUSED(class_p);
646 LV_TRACE_OBJ_CREATE("begin");
647
648 lv_chart_t * chart = (lv_chart_t *)obj;
649
650 _lv_ll_init(&chart->series_ll, sizeof(lv_chart_series_t));
651 _lv_ll_init(&chart->cursor_ll, sizeof(lv_chart_cursor_t));
652
653 chart->ymin[0] = 0;
654 chart->xmin[0] = 0;
655 chart->ymin[1] = 0;
656 chart->xmin[1] = 0;
657 chart->ymax[0] = 100;
658 chart->xmax[0] = 100;
659 chart->ymax[1] = 100;
660 chart->xmax[1] = 100;
661
662 chart->hdiv_cnt = LV_CHART_HDIV_DEF;
663 chart->vdiv_cnt = LV_CHART_VDIV_DEF;
664 chart->point_cnt = LV_CHART_POINT_CNT_DEF;
665 chart->pressed_point_id = LV_CHART_POINT_NONE;
666 chart->type = LV_CHART_TYPE_LINE;
667 chart->update_mode = LV_CHART_UPDATE_MODE_SHIFT;
668 chart->zoom_x = LV_IMG_ZOOM_NONE;
669 chart->zoom_y = LV_IMG_ZOOM_NONE;
670
671 LV_TRACE_OBJ_CREATE("finished");
672 }
673
lv_chart_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)674 static void lv_chart_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
675 {
676 LV_UNUSED(class_p);
677 LV_TRACE_OBJ_CREATE("begin");
678
679 lv_chart_t * chart = (lv_chart_t *)obj;
680 lv_chart_series_t * ser;
681 while(chart->series_ll.head) {
682 ser = _lv_ll_get_head(&chart->series_ll);
683
684 if(!ser->y_ext_buf_assigned) lv_mem_free(ser->y_points);
685
686 _lv_ll_remove(&chart->series_ll, ser);
687 lv_mem_free(ser);
688 }
689 _lv_ll_clear(&chart->series_ll);
690
691 lv_chart_cursor_t * cur;
692 while(chart->cursor_ll.head) {
693 cur = _lv_ll_get_head(&chart->cursor_ll);
694 _lv_ll_remove(&chart->cursor_ll, cur);
695 lv_mem_free(cur);
696 }
697 _lv_ll_clear(&chart->cursor_ll);
698
699 LV_TRACE_OBJ_CREATE("finished");
700 }
701
lv_chart_event(const lv_obj_class_t * class_p,lv_event_t * e)702 static void lv_chart_event(const lv_obj_class_t * class_p, lv_event_t * e)
703 {
704 LV_UNUSED(class_p);
705
706 /*Call the ancestor's event handler*/
707 lv_res_t res;
708
709 res = lv_obj_event_base(MY_CLASS, e);
710 if(res != LV_RES_OK) return;
711
712 lv_event_code_t code = lv_event_get_code(e);
713 lv_obj_t * obj = lv_event_get_target(e);
714
715 lv_chart_t * chart = (lv_chart_t *)obj;
716 if(code == LV_EVENT_PRESSED) {
717 lv_indev_t * indev = lv_indev_get_act();
718 lv_point_t p;
719 lv_indev_get_point(indev, &p);
720
721 p.x -= obj->coords.x1;
722 uint32_t id = get_index_from_x(obj, p.x + lv_obj_get_scroll_left(obj));
723 if(id != chart->pressed_point_id) {
724 invalidate_point(obj, id);
725 invalidate_point(obj, chart->pressed_point_id);
726 chart->pressed_point_id = id;
727 lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
728 }
729 }
730 else if(code == LV_EVENT_RELEASED) {
731 invalidate_point(obj, chart->pressed_point_id);
732 chart->pressed_point_id = LV_CHART_POINT_NONE;
733 }
734 else if(code == LV_EVENT_SIZE_CHANGED) {
735 lv_obj_refresh_self_size(obj);
736 }
737 else if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
738 lv_event_set_ext_draw_size(e, LV_MAX4(chart->tick[0].draw_size, chart->tick[1].draw_size, chart->tick[2].draw_size,
739 chart->tick[3].draw_size));
740 }
741 else if(code == LV_EVENT_GET_SELF_SIZE) {
742 lv_point_t * p = lv_event_get_param(e);
743 p->x = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
744 p->y = ((int32_t)lv_obj_get_content_height(obj) * chart->zoom_y) >> 8;
745 }
746 else if(code == LV_EVENT_DRAW_MAIN) {
747 lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
748 draw_div_lines(obj, draw_ctx);
749 draw_axes(obj, draw_ctx);
750
751 if(_lv_ll_is_empty(&chart->series_ll) == false) {
752 if(chart->type == LV_CHART_TYPE_LINE) draw_series_line(obj, draw_ctx);
753 else if(chart->type == LV_CHART_TYPE_BAR) draw_series_bar(obj, draw_ctx);
754 else if(chart->type == LV_CHART_TYPE_SCATTER) draw_series_scatter(obj, draw_ctx);
755 }
756
757 draw_cursors(obj, draw_ctx);
758 }
759 }
760
draw_div_lines(lv_obj_t * obj,lv_draw_ctx_t * draw_ctx)761 static void draw_div_lines(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx)
762 {
763 lv_chart_t * chart = (lv_chart_t *)obj;
764
765 lv_area_t series_clip_area;
766 bool mask_ret = _lv_area_intersect(&series_clip_area, &obj->coords, draw_ctx->clip_area);
767 if(mask_ret == false) return;
768
769 const lv_area_t * clip_area_ori = draw_ctx->clip_area;
770 draw_ctx->clip_area = &series_clip_area;
771
772 int16_t i;
773 int16_t i_start;
774 int16_t i_end;
775 lv_point_t p1;
776 lv_point_t p2;
777 lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
778 lv_coord_t pad_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + border_width;
779 lv_coord_t pad_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + border_width;
780 lv_coord_t w = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
781 lv_coord_t h = ((int32_t)lv_obj_get_content_height(obj) * chart->zoom_y) >> 8;
782
783 lv_draw_line_dsc_t line_dsc;
784 lv_draw_line_dsc_init(&line_dsc);
785 lv_obj_init_draw_line_dsc(obj, LV_PART_MAIN, &line_dsc);
786
787 lv_obj_draw_part_dsc_t part_draw_dsc;
788 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
789 part_draw_dsc.part = LV_PART_MAIN;
790 part_draw_dsc.class_p = MY_CLASS;
791 part_draw_dsc.type = LV_CHART_DRAW_PART_DIV_LINE_INIT;
792 part_draw_dsc.line_dsc = &line_dsc;
793 part_draw_dsc.id = 0xFFFFFFFF;
794 part_draw_dsc.p1 = NULL;
795 part_draw_dsc.p2 = NULL;
796 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
797
798 lv_opa_t border_opa = lv_obj_get_style_border_opa(obj, LV_PART_MAIN);
799 lv_coord_t border_w = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
800 lv_border_side_t border_side = lv_obj_get_style_border_side(obj, LV_PART_MAIN);
801
802 lv_coord_t scroll_left = lv_obj_get_scroll_left(obj);
803 lv_coord_t scroll_top = lv_obj_get_scroll_top(obj);
804 if(chart->hdiv_cnt != 0) {
805 lv_coord_t y_ofs = obj->coords.y1 + pad_top - scroll_top;
806 p1.x = obj->coords.x1;
807 p2.x = obj->coords.x2;
808
809 i_start = 0;
810 i_end = chart->hdiv_cnt;
811 if(border_opa > LV_OPA_MIN && border_w > 0) {
812 if((border_side & LV_BORDER_SIDE_TOP) && (lv_obj_get_style_pad_top(obj, LV_PART_MAIN) == 0)) i_start++;
813 if((border_side & LV_BORDER_SIDE_BOTTOM) && (lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN) == 0)) i_end--;
814 }
815
816 for(i = i_start; i < i_end; i++) {
817 p1.y = (int32_t)((int32_t)h * i) / (chart->hdiv_cnt - 1);
818 p1.y += y_ofs;
819 p2.y = p1.y;
820
821 part_draw_dsc.class_p = MY_CLASS;
822 part_draw_dsc.type = LV_CHART_DRAW_PART_DIV_LINE_HOR;
823 part_draw_dsc.p1 = &p1;
824 part_draw_dsc.p2 = &p2;
825 part_draw_dsc.id = i;
826
827 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
828 lv_draw_line(draw_ctx, &line_dsc, &p1, &p2);
829 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
830 }
831 }
832
833 if(chart->vdiv_cnt != 0) {
834 lv_coord_t x_ofs = obj->coords.x1 + pad_left - scroll_left;
835 p1.y = obj->coords.y1;
836 p2.y = obj->coords.y2;
837 i_start = 0;
838 i_end = chart->vdiv_cnt;
839 if(border_opa > LV_OPA_MIN && border_w > 0) {
840 if((border_side & LV_BORDER_SIDE_LEFT) && (lv_obj_get_style_pad_left(obj, LV_PART_MAIN) == 0)) i_start++;
841 if((border_side & LV_BORDER_SIDE_RIGHT) && (lv_obj_get_style_pad_right(obj, LV_PART_MAIN) == 0)) i_end--;
842 }
843
844 for(i = i_start; i < i_end; i++) {
845 p1.x = (int32_t)((int32_t)w * i) / (chart->vdiv_cnt - 1);
846 p1.x += x_ofs;
847 p2.x = p1.x;
848
849 part_draw_dsc.class_p = MY_CLASS;
850 part_draw_dsc.type = LV_CHART_DRAW_PART_DIV_LINE_VER;
851 part_draw_dsc.p1 = &p1;
852 part_draw_dsc.p2 = &p2;
853 part_draw_dsc.id = i;
854
855 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
856 lv_draw_line(draw_ctx, &line_dsc, &p1, &p2);
857 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
858 }
859 }
860
861 part_draw_dsc.id = 0xFFFFFFFF;
862 part_draw_dsc.p1 = NULL;
863 part_draw_dsc.p2 = NULL;
864 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
865
866 draw_ctx->clip_area = clip_area_ori;
867 }
868
draw_series_line(lv_obj_t * obj,lv_draw_ctx_t * draw_ctx)869 static void draw_series_line(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx)
870 {
871 lv_area_t clip_area;
872 if(_lv_area_intersect(&clip_area, &obj->coords, draw_ctx->clip_area) == false) return;
873
874 const lv_area_t * clip_area_ori = draw_ctx->clip_area;
875 draw_ctx->clip_area = &clip_area;
876
877 lv_chart_t * chart = (lv_chart_t *)obj;
878 if(chart->point_cnt < 2) return;
879
880 uint16_t i;
881 lv_point_t p1;
882 lv_point_t p2;
883 lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
884 lv_coord_t pad_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + border_width;
885 lv_coord_t pad_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + border_width;
886 lv_coord_t w = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
887 lv_coord_t h = ((int32_t)lv_obj_get_content_height(obj) * chart->zoom_y) >> 8;
888 lv_coord_t x_ofs = obj->coords.x1 + pad_left - lv_obj_get_scroll_left(obj);
889 lv_coord_t y_ofs = obj->coords.y1 + pad_top - lv_obj_get_scroll_top(obj);
890 lv_chart_series_t * ser;
891
892 lv_area_t series_clip_area;
893 bool mask_ret = _lv_area_intersect(&series_clip_area, &obj->coords, draw_ctx->clip_area);
894 if(mask_ret == false) return;
895
896 lv_draw_line_dsc_t line_dsc_default;
897 lv_draw_line_dsc_init(&line_dsc_default);
898 lv_obj_init_draw_line_dsc(obj, LV_PART_ITEMS, &line_dsc_default);
899
900 lv_draw_rect_dsc_t point_dsc_default;
901 lv_draw_rect_dsc_init(&point_dsc_default);
902 lv_obj_init_draw_rect_dsc(obj, LV_PART_INDICATOR, &point_dsc_default);
903
904 lv_coord_t point_w = lv_obj_get_style_width(obj, LV_PART_INDICATOR) / 2;
905 lv_coord_t point_h = lv_obj_get_style_height(obj, LV_PART_INDICATOR) / 2;
906
907 /*Do not bother with line ending is the point will over it*/
908 if(LV_MIN(point_w, point_h) > line_dsc_default.width / 2) line_dsc_default.raw_end = 1;
909 if(line_dsc_default.width == 1) line_dsc_default.raw_end = 1;
910
911 /*If there are mire points than pixels draw only vertical lines*/
912 bool crowded_mode = chart->point_cnt >= w ? true : false;
913
914 /*Go through all data lines*/
915 _LV_LL_READ_BACK(&chart->series_ll, ser) {
916 if(ser->hidden) continue;
917 line_dsc_default.color = ser->color;
918 point_dsc_default.bg_color = ser->color;
919
920 lv_coord_t start_point = chart->update_mode == LV_CHART_UPDATE_MODE_SHIFT ? ser->start_point : 0;
921
922 p1.x = x_ofs;
923 p2.x = x_ofs;
924
925 lv_coord_t p_act = start_point;
926 lv_coord_t p_prev = start_point;
927 int32_t y_tmp = (int32_t)((int32_t)ser->y_points[p_prev] - chart->ymin[ser->y_axis_sec]) * h;
928 y_tmp = y_tmp / (chart->ymax[ser->y_axis_sec] - chart->ymin[ser->y_axis_sec]);
929 p2.y = h - y_tmp + y_ofs;
930
931 lv_obj_draw_part_dsc_t part_draw_dsc;
932 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
933 part_draw_dsc.class_p = MY_CLASS;
934 part_draw_dsc.type = LV_CHART_DRAW_PART_LINE_AND_POINT;
935 part_draw_dsc.part = LV_PART_ITEMS;
936 part_draw_dsc.line_dsc = &line_dsc_default;
937 part_draw_dsc.rect_dsc = &point_dsc_default;
938 part_draw_dsc.sub_part_ptr = ser;
939
940 lv_coord_t y_min = p2.y;
941 lv_coord_t y_max = p2.y;
942
943 for(i = 0; i < chart->point_cnt; i++) {
944 p1.x = p2.x;
945 p1.y = p2.y;
946
947 if(p1.x > clip_area_ori->x2 + point_w + 1) break;
948 p2.x = ((w * i) / (chart->point_cnt - 1)) + x_ofs;
949
950 p_act = (start_point + i) % chart->point_cnt;
951
952 y_tmp = (int32_t)((int32_t)ser->y_points[p_act] - chart->ymin[ser->y_axis_sec]) * h;
953 y_tmp = y_tmp / (chart->ymax[ser->y_axis_sec] - chart->ymin[ser->y_axis_sec]);
954 p2.y = h - y_tmp + y_ofs;
955
956 if(p2.x < clip_area_ori->x1 - point_w - 1) {
957 p_prev = p_act;
958 continue;
959 }
960
961 /*Don't draw the first point. A second point is also required to draw the line*/
962 if(i != 0) {
963 if(crowded_mode) {
964 if(ser->y_points[p_prev] != LV_CHART_POINT_NONE && ser->y_points[p_act] != LV_CHART_POINT_NONE) {
965 /*Draw only one vertical line between the min and max y-values on the same x-value*/
966 y_max = LV_MAX(y_max, p2.y);
967 y_min = LV_MIN(y_min, p2.y);
968 if(p1.x != p2.x) {
969 lv_coord_t y_cur = p2.y;
970 p2.x--; /*It's already on the next x value*/
971 p1.x = p2.x;
972 p1.y = y_min;
973 p2.y = y_max;
974 if(p1.y == p2.y) p2.y++; /*If they are the same no line will be drawn*/
975 lv_draw_line(draw_ctx, &line_dsc_default, &p1, &p2);
976 p2.x++; /*Compensate the previous x--*/
977 y_min = y_cur; /*Start the line of the next x from the current last y*/
978 y_max = y_cur;
979 }
980 }
981 }
982 else {
983 lv_area_t point_area;
984 point_area.x1 = p1.x - point_w;
985 point_area.x2 = p1.x + point_w;
986 point_area.y1 = p1.y - point_h;
987 point_area.y2 = p1.y + point_h;
988
989 part_draw_dsc.id = i - 1;
990 part_draw_dsc.p1 = ser->y_points[p_prev] != LV_CHART_POINT_NONE ? &p1 : NULL;
991 part_draw_dsc.p2 = ser->y_points[p_act] != LV_CHART_POINT_NONE ? &p2 : NULL;
992 part_draw_dsc.draw_area = &point_area;
993 part_draw_dsc.value = ser->y_points[p_prev];
994
995 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
996
997 if(ser->y_points[p_prev] != LV_CHART_POINT_NONE && ser->y_points[p_act] != LV_CHART_POINT_NONE) {
998 lv_draw_line(draw_ctx, &line_dsc_default, &p1, &p2);
999 }
1000
1001 if(point_w && point_h && ser->y_points[p_prev] != LV_CHART_POINT_NONE) {
1002 lv_draw_rect(draw_ctx, &point_dsc_default, &point_area);
1003 }
1004
1005 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1006 }
1007
1008 }
1009 p_prev = p_act;
1010 }
1011
1012 /*Draw the last point*/
1013 if(!crowded_mode && i == chart->point_cnt) {
1014
1015 if(ser->y_points[p_act] != LV_CHART_POINT_NONE) {
1016 lv_area_t point_area;
1017 point_area.x1 = p2.x - point_w;
1018 point_area.x2 = p2.x + point_w;
1019 point_area.y1 = p2.y - point_h;
1020 point_area.y2 = p2.y + point_h;
1021
1022 part_draw_dsc.id = i - 1;
1023 part_draw_dsc.p1 = NULL;
1024 part_draw_dsc.p2 = NULL;
1025 part_draw_dsc.draw_area = &point_area;
1026 part_draw_dsc.value = ser->y_points[p_act];
1027
1028 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1029 lv_draw_rect(draw_ctx, &point_dsc_default, &point_area);
1030 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1031 }
1032 }
1033 }
1034
1035 draw_ctx->clip_area = clip_area_ori;
1036 }
1037
draw_series_scatter(lv_obj_t * obj,lv_draw_ctx_t * draw_ctx)1038 static void draw_series_scatter(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx)
1039 {
1040
1041 lv_area_t clip_area;
1042 if(_lv_area_intersect(&clip_area, &obj->coords, draw_ctx->clip_area) == false) return;
1043
1044 const lv_area_t * clip_area_ori = draw_ctx->clip_area;
1045 draw_ctx->clip_area = &clip_area;
1046
1047 lv_chart_t * chart = (lv_chart_t *)obj;
1048
1049 uint16_t i;
1050 lv_point_t p1;
1051 lv_point_t p2;
1052 lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
1053 lv_coord_t pad_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1054 lv_coord_t pad_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
1055 lv_coord_t w = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
1056 lv_coord_t h = ((int32_t)lv_obj_get_content_height(obj) * chart->zoom_y) >> 8;
1057 lv_coord_t x_ofs = obj->coords.x1 + pad_left + border_width - lv_obj_get_scroll_left(obj);
1058 lv_coord_t y_ofs = obj->coords.y1 + pad_top + border_width - lv_obj_get_scroll_top(obj);
1059 lv_chart_series_t * ser;
1060
1061 lv_draw_line_dsc_t line_dsc_default;
1062 lv_draw_line_dsc_init(&line_dsc_default);
1063 lv_obj_init_draw_line_dsc(obj, LV_PART_ITEMS, &line_dsc_default);
1064
1065 lv_draw_rect_dsc_t point_dsc_default;
1066 lv_draw_rect_dsc_init(&point_dsc_default);
1067 lv_obj_init_draw_rect_dsc(obj, LV_PART_INDICATOR, &point_dsc_default);
1068
1069 lv_coord_t point_w = lv_obj_get_style_width(obj, LV_PART_INDICATOR) / 2;
1070 lv_coord_t point_h = lv_obj_get_style_height(obj, LV_PART_INDICATOR) / 2;
1071
1072 /*Do not bother with line ending is the point will over it*/
1073 if(LV_MIN(point_w, point_h) > line_dsc_default.width / 2) line_dsc_default.raw_end = 1;
1074 if(line_dsc_default.width == 1) line_dsc_default.raw_end = 1;
1075
1076 /*Go through all data lines*/
1077 _LV_LL_READ_BACK(&chart->series_ll, ser) {
1078 if(ser->hidden) continue;
1079 line_dsc_default.color = ser->color;
1080 point_dsc_default.bg_color = ser->color;
1081
1082 lv_coord_t start_point = chart->update_mode == LV_CHART_UPDATE_MODE_SHIFT ? ser->start_point : 0;
1083
1084 p1.x = x_ofs;
1085 p2.x = x_ofs;
1086
1087 lv_coord_t p_act = start_point;
1088 lv_coord_t p_prev = start_point;
1089 if(ser->y_points[p_act] != LV_CHART_POINT_CNT_DEF) {
1090 p2.x = lv_map(ser->x_points[p_act], chart->xmin[ser->x_axis_sec], chart->xmax[ser->x_axis_sec], 0, w);
1091 p2.x += x_ofs;
1092
1093 p2.y = lv_map(ser->y_points[p_act], chart->ymin[ser->y_axis_sec], chart->ymax[ser->y_axis_sec], 0, h);
1094 p2.y = h - p2.y;
1095 p2.y += y_ofs;
1096 }
1097 else {
1098 p2.x = LV_COORD_MIN;
1099 p2.y = LV_COORD_MIN;
1100 }
1101
1102 lv_obj_draw_part_dsc_t part_draw_dsc;
1103 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
1104 part_draw_dsc.part = LV_PART_ITEMS;
1105 part_draw_dsc.class_p = MY_CLASS;
1106 part_draw_dsc.type = LV_CHART_DRAW_PART_LINE_AND_POINT;
1107 part_draw_dsc.line_dsc = &line_dsc_default;
1108 part_draw_dsc.rect_dsc = &point_dsc_default;
1109 part_draw_dsc.sub_part_ptr = ser;
1110
1111 for(i = 0; i < chart->point_cnt; i++) {
1112 p1.x = p2.x;
1113 p1.y = p2.y;
1114
1115 p_act = (start_point + i) % chart->point_cnt;
1116 if(ser->y_points[p_act] != LV_CHART_POINT_NONE) {
1117 p2.y = lv_map(ser->y_points[p_act], chart->ymin[ser->y_axis_sec], chart->ymax[ser->y_axis_sec], 0, h);
1118 p2.y = h - p2.y;
1119 p2.y += y_ofs;
1120
1121 p2.x = lv_map(ser->x_points[p_act], chart->xmin[ser->x_axis_sec], chart->xmax[ser->x_axis_sec], 0, w);
1122 p2.x += x_ofs;
1123 }
1124 else {
1125 p_prev = p_act;
1126 continue;
1127 }
1128
1129 /*Don't draw the first point. A second point is also required to draw the line*/
1130 if(i != 0) {
1131 lv_area_t point_area;
1132 point_area.x1 = p1.x - point_w;
1133 point_area.x2 = p1.x + point_w;
1134 point_area.y1 = p1.y - point_h;
1135 point_area.y2 = p1.y + point_h;
1136
1137 part_draw_dsc.id = i - 1;
1138 part_draw_dsc.p1 = ser->y_points[p_prev] != LV_CHART_POINT_NONE ? &p1 : NULL;
1139 part_draw_dsc.p2 = ser->y_points[p_act] != LV_CHART_POINT_NONE ? &p2 : NULL;
1140 part_draw_dsc.draw_area = &point_area;
1141 part_draw_dsc.value = ser->y_points[p_prev];
1142
1143 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1144
1145 if(ser->y_points[p_prev] != LV_CHART_POINT_NONE && ser->y_points[p_act] != LV_CHART_POINT_NONE) {
1146 lv_draw_line(draw_ctx, &line_dsc_default, &p1, &p2);
1147 if(point_w && point_h) {
1148 lv_draw_rect(draw_ctx, &point_dsc_default, &point_area);
1149 }
1150 }
1151
1152 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1153 }
1154 p_prev = p_act;
1155 }
1156
1157 /*Draw the last point*/
1158 if(i == chart->point_cnt) {
1159
1160 if(ser->y_points[p_act] != LV_CHART_POINT_NONE) {
1161 lv_area_t point_area;
1162 point_area.x1 = p2.x - point_w;
1163 point_area.x2 = p2.x + point_w;
1164 point_area.y1 = p2.y - point_h;
1165 point_area.y2 = p2.y + point_h;
1166
1167 part_draw_dsc.id = i - 1;
1168 part_draw_dsc.p1 = NULL;
1169 part_draw_dsc.p2 = NULL;
1170 part_draw_dsc.draw_area = &point_area;
1171 part_draw_dsc.value = ser->y_points[p_act];
1172 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1173 lv_draw_rect(draw_ctx, &point_dsc_default, &point_area);
1174 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1175 }
1176 }
1177 }
1178 draw_ctx->clip_area = clip_area_ori;
1179 }
1180
draw_series_bar(lv_obj_t * obj,lv_draw_ctx_t * draw_ctx)1181 static void draw_series_bar(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx)
1182 {
1183 lv_area_t clip_area;
1184 if(_lv_area_intersect(&clip_area, &obj->coords, draw_ctx->clip_area) == false) return;
1185
1186 const lv_area_t * clip_area_ori = draw_ctx->clip_area;
1187 draw_ctx->clip_area = &clip_area;
1188
1189
1190 lv_chart_t * chart = (lv_chart_t *)obj;
1191
1192 uint16_t i;
1193 lv_area_t col_a;
1194 lv_coord_t pad_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1195 lv_coord_t pad_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
1196 lv_coord_t w = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
1197 lv_coord_t h = ((int32_t)lv_obj_get_content_height(obj) * chart->zoom_y) >> 8;
1198 int32_t y_tmp;
1199 lv_chart_series_t * ser;
1200 uint32_t ser_cnt = _lv_ll_get_len(&chart->series_ll);
1201 int32_t block_gap = ((int32_t)lv_obj_get_style_pad_column(obj,
1202 LV_PART_MAIN) * chart->zoom_x) >> 8; /*Gap between the column on ~adjacent X*/
1203 lv_coord_t block_w = (w - ((chart->point_cnt - 1) * block_gap)) / chart->point_cnt;
1204 lv_coord_t col_w = block_w / ser_cnt;
1205 int32_t ser_gap = ((int32_t)lv_obj_get_style_pad_column(obj,
1206 LV_PART_ITEMS) * chart->zoom_x) >> 8; /*Gap between the column on the ~same X*/
1207 lv_coord_t x_ofs = pad_left - lv_obj_get_scroll_left(obj);
1208 lv_coord_t y_ofs = pad_top - lv_obj_get_scroll_top(obj);
1209
1210 lv_draw_rect_dsc_t col_dsc;
1211 lv_draw_rect_dsc_init(&col_dsc);
1212 lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &col_dsc);
1213 col_dsc.bg_grad.dir = LV_GRAD_DIR_NONE;
1214 col_dsc.bg_opa = LV_OPA_COVER;
1215
1216 /*Make the cols longer with `radius` to clip the rounding from the bottom*/
1217 col_a.y2 = obj->coords.y2 + col_dsc.radius;
1218
1219 lv_obj_draw_part_dsc_t part_draw_dsc;
1220 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
1221 part_draw_dsc.part = LV_PART_ITEMS;
1222 part_draw_dsc.class_p = MY_CLASS;
1223 part_draw_dsc.type = LV_CHART_DRAW_PART_BAR;
1224
1225 /*Go through all points*/
1226 for(i = 0; i < chart->point_cnt; i++) {
1227 lv_coord_t x_act = (int32_t)((int32_t)(w + block_gap) * i) / (chart->point_cnt) + obj->coords.x1 + x_ofs;
1228
1229 part_draw_dsc.id = i;
1230
1231 /*Draw the current point of all data line*/
1232 _LV_LL_READ_BACK(&chart->series_ll, ser) {
1233 if(ser->hidden) continue;
1234 lv_coord_t start_point = chart->update_mode == LV_CHART_UPDATE_MODE_SHIFT ? ser->start_point : 0;
1235
1236 col_a.x1 = x_act;
1237 col_a.x2 = col_a.x1 + col_w - ser_gap - 1;
1238 x_act += col_w;
1239
1240 if(col_a.x2 < clip_area.x1) continue;
1241 if(col_a.x1 > clip_area.x2) break;
1242
1243 col_dsc.bg_color = ser->color;
1244
1245 lv_coord_t p_act = (start_point + i) % chart->point_cnt;
1246 y_tmp = (int32_t)((int32_t)ser->y_points[p_act] - chart->ymin[ser->y_axis_sec]) * h;
1247 y_tmp = y_tmp / (chart->ymax[ser->y_axis_sec] - chart->ymin[ser->y_axis_sec]);
1248 col_a.y1 = h - y_tmp + obj->coords.y1 + y_ofs;
1249
1250 if(ser->y_points[p_act] != LV_CHART_POINT_NONE) {
1251 part_draw_dsc.draw_area = &col_a;
1252 part_draw_dsc.rect_dsc = &col_dsc;
1253 part_draw_dsc.sub_part_ptr = ser;
1254 part_draw_dsc.value = ser->y_points[p_act];
1255 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1256 lv_draw_rect(draw_ctx, &col_dsc, &col_a);
1257 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1258 }
1259 }
1260 }
1261 draw_ctx->clip_area = clip_area_ori;
1262 }
1263
draw_cursors(lv_obj_t * obj,lv_draw_ctx_t * draw_ctx)1264 static void draw_cursors(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx)
1265 {
1266 LV_ASSERT_OBJ(obj, MY_CLASS);
1267
1268 lv_chart_t * chart = (lv_chart_t *)obj;
1269 if(_lv_ll_is_empty(&chart->cursor_ll)) return;
1270
1271 lv_area_t clip_area;
1272 if(!_lv_area_intersect(&clip_area, draw_ctx->clip_area, &obj->coords)) return;
1273
1274 const lv_area_t * clip_area_ori = draw_ctx->clip_area;
1275 draw_ctx->clip_area = &clip_area;
1276
1277 lv_point_t p1;
1278 lv_point_t p2;
1279 lv_chart_cursor_t * cursor;
1280
1281 lv_draw_line_dsc_t line_dsc_ori;
1282 lv_draw_line_dsc_init(&line_dsc_ori);
1283 lv_obj_init_draw_line_dsc(obj, LV_PART_CURSOR, &line_dsc_ori);
1284
1285 lv_draw_rect_dsc_t point_dsc_ori;
1286 lv_draw_rect_dsc_init(&point_dsc_ori);
1287 point_dsc_ori.bg_opa = line_dsc_ori.opa;
1288 point_dsc_ori.radius = LV_RADIUS_CIRCLE;
1289
1290 lv_draw_line_dsc_t line_dsc_tmp;
1291 lv_draw_rect_dsc_t point_dsc_tmp;
1292
1293 lv_coord_t point_w = lv_obj_get_style_width(obj, LV_PART_CURSOR) / 2;
1294 lv_coord_t point_h = lv_obj_get_style_width(obj, LV_PART_CURSOR) / 2;
1295
1296 lv_obj_draw_part_dsc_t part_draw_dsc;
1297 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
1298 part_draw_dsc.line_dsc = &line_dsc_tmp;
1299 part_draw_dsc.rect_dsc = &point_dsc_tmp;
1300 part_draw_dsc.part = LV_PART_CURSOR;
1301 part_draw_dsc.class_p = MY_CLASS;
1302 part_draw_dsc.type = LV_CHART_DRAW_PART_CURSOR;
1303
1304 /*Go through all cursor lines*/
1305 _LV_LL_READ_BACK(&chart->cursor_ll, cursor) {
1306 lv_memcpy(&line_dsc_tmp, &line_dsc_ori, sizeof(lv_draw_line_dsc_t));
1307 lv_memcpy(&point_dsc_tmp, &point_dsc_ori, sizeof(lv_draw_rect_dsc_t));
1308 line_dsc_tmp.color = cursor->color;
1309 point_dsc_tmp.bg_color = cursor->color;
1310
1311 part_draw_dsc.p1 = &p1;
1312 part_draw_dsc.p2 = &p2;
1313
1314 lv_coord_t cx;
1315 lv_coord_t cy;
1316 if(cursor->pos_set) {
1317 cx = cursor->pos.x;
1318 cy = cursor->pos.y;
1319 }
1320 else {
1321 if(cursor->point_id == LV_CHART_POINT_NONE) continue;
1322 lv_point_t p;
1323 lv_chart_get_point_pos_by_id(obj, cursor->ser, cursor->point_id, &p);
1324 cx = p.x;
1325 cy = p.y;
1326 }
1327
1328 cx += obj->coords.x1;
1329 cy += obj->coords.y1;
1330
1331 lv_area_t point_area;
1332 if(point_w && point_h) {
1333 point_area.x1 = cx - point_w;
1334 point_area.x2 = cx + point_w;
1335 point_area.y1 = cy - point_h;
1336 point_area.y2 = cy + point_h;
1337
1338 part_draw_dsc.draw_area = &point_area;
1339 }
1340 else {
1341 part_draw_dsc.draw_area = NULL;
1342 }
1343
1344 if(cursor->dir & LV_DIR_HOR) {
1345 p1.x = cursor->dir & LV_DIR_LEFT ? obj->coords.x1 : cx;
1346 p1.y = cy;
1347 p2.x = cursor->dir & LV_DIR_RIGHT ? obj->coords.x2 : cx;
1348 p2.y = p1.y;
1349
1350 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1351 lv_draw_line(draw_ctx, &line_dsc_tmp, &p1, &p2);
1352 lv_draw_rect(draw_ctx, &point_dsc_tmp, &point_area);
1353 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1354 }
1355
1356 if(cursor->dir & LV_DIR_VER) {
1357 p1.x = cx;
1358 p1.y = cursor->dir & LV_DIR_TOP ? obj->coords.y1 : cy;
1359 p2.x = p1.x;
1360 p2.y = cursor->dir & LV_DIR_BOTTOM ? obj->coords.y2 : cy;
1361
1362 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1363 lv_draw_line(draw_ctx, &line_dsc_tmp, &p1, &p2);
1364 lv_draw_rect(draw_ctx, &point_dsc_tmp, &point_area);
1365 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1366 }
1367 }
1368
1369 draw_ctx->clip_area = clip_area_ori;
1370 }
1371
draw_y_ticks(lv_obj_t * obj,lv_draw_ctx_t * draw_ctx,lv_chart_axis_t axis)1372 static void draw_y_ticks(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx, lv_chart_axis_t axis)
1373 {
1374 lv_chart_t * chart = (lv_chart_t *)obj;
1375
1376 lv_chart_tick_dsc_t * t = get_tick_gsc(obj, axis);
1377
1378 if(t->major_cnt <= 1) return;
1379 if(!t->label_en && !t->major_len && !t->minor_len) return;
1380
1381 uint8_t sec_axis = axis == LV_CHART_AXIS_PRIMARY_Y ? 0 : 1;
1382
1383 uint32_t i;
1384
1385 lv_point_t p1;
1386 lv_point_t p2;
1387
1388 lv_coord_t border_width = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
1389 lv_coord_t pad_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
1390 lv_coord_t h = ((int32_t)lv_obj_get_content_height(obj) * chart->zoom_y) >> 8;
1391 lv_coord_t y_ofs = obj->coords.y1 + pad_top + border_width - lv_obj_get_scroll_top(obj);
1392
1393 lv_coord_t label_gap;
1394 lv_coord_t x_ofs;
1395 if(axis == LV_CHART_AXIS_PRIMARY_Y) {
1396 label_gap = lv_obj_get_style_pad_left(obj, LV_PART_TICKS);
1397 x_ofs = obj->coords.x1;
1398 }
1399 else {
1400 label_gap = lv_obj_get_style_pad_right(obj, LV_PART_TICKS);
1401 x_ofs = obj->coords.x2;
1402 }
1403
1404 lv_coord_t major_len = t->major_len;
1405 lv_coord_t minor_len = t->minor_len;
1406 /*tick lines on secondary y axis are drawn in other direction*/
1407 if(axis == LV_CHART_AXIS_SECONDARY_Y) {
1408 major_len *= -1;
1409 minor_len *= -1;
1410 }
1411
1412 lv_draw_line_dsc_t line_dsc;
1413 lv_draw_line_dsc_init(&line_dsc);
1414 lv_obj_init_draw_line_dsc(obj, LV_PART_TICKS, &line_dsc);
1415
1416 lv_draw_label_dsc_t label_dsc;
1417 lv_draw_label_dsc_init(&label_dsc);
1418 lv_obj_init_draw_label_dsc(obj, LV_PART_TICKS, &label_dsc);
1419
1420 lv_obj_draw_part_dsc_t part_draw_dsc;
1421 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
1422 part_draw_dsc.class_p = MY_CLASS;
1423 part_draw_dsc.type = LV_CHART_DRAW_PART_TICK_LABEL;
1424 part_draw_dsc.id = axis;
1425 part_draw_dsc.part = LV_PART_TICKS;
1426 part_draw_dsc.line_dsc = &line_dsc;
1427 part_draw_dsc.label_dsc = &label_dsc;
1428
1429 uint32_t total_tick_num = (t->major_cnt - 1) * (t->minor_cnt);
1430 for(i = 0; i <= total_tick_num; i++) {
1431 /*draw a line at moving y position*/
1432 p2.y = p1.y = y_ofs + (int32_t)((int32_t)(h - line_dsc.width) * i) / total_tick_num;
1433
1434 /*first point of the tick*/
1435 p1.x = x_ofs;
1436
1437 /*move extra pixel out of chart boundary*/
1438 if(axis == LV_CHART_AXIS_PRIMARY_Y) p1.x--;
1439 else p1.x++;
1440
1441 /*second point of the tick*/
1442 bool major = false;
1443 if(i % t->minor_cnt == 0) major = true;
1444
1445 if(major) p2.x = p1.x - major_len; /*major tick*/
1446 else p2.x = p1.x - minor_len; /*minor tick*/
1447
1448 part_draw_dsc.p1 = &p1;
1449 part_draw_dsc.p2 = &p2;
1450
1451 int32_t tick_value = lv_map(total_tick_num - i, 0, total_tick_num, chart->ymin[sec_axis], chart->ymax[sec_axis]);
1452 part_draw_dsc.value = tick_value;
1453
1454 /*add text only to major tick*/
1455 if(major && t->label_en) {
1456 char buf[LV_CHART_LABEL_MAX_TEXT_LENGTH];
1457 lv_snprintf(buf, sizeof(buf), "%" LV_PRId32, tick_value);
1458 part_draw_dsc.label_dsc = &label_dsc;
1459 part_draw_dsc.text = buf;
1460 part_draw_dsc.text_length = LV_CHART_LABEL_MAX_TEXT_LENGTH;
1461 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1462
1463 /*reserve appropriate area*/
1464 lv_point_t size;
1465 lv_txt_get_size(&size, part_draw_dsc.text, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX,
1466 LV_TEXT_FLAG_NONE);
1467
1468 /*set the area at some distance of the major tick len left of the tick*/
1469 lv_area_t a;
1470 a.y1 = p2.y - size.y / 2;
1471 a.y2 = p2.y + size.y / 2;
1472
1473 if(!sec_axis) {
1474 a.x1 = p2.x - size.x - label_gap;
1475 a.x2 = p2.x - label_gap;
1476 }
1477 else {
1478 a.x1 = p2.x + label_gap;
1479 a.x2 = p2.x + size.x + label_gap;
1480 }
1481
1482 if(a.y2 >= obj->coords.y1 &&
1483 a.y1 <= obj->coords.y2) {
1484 lv_draw_label(draw_ctx, &label_dsc, &a, part_draw_dsc.text, NULL);
1485 }
1486 }
1487 else {
1488 part_draw_dsc.label_dsc = NULL;
1489 part_draw_dsc.text = NULL;
1490 part_draw_dsc.text_length = 0;
1491 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1492 }
1493
1494 if(p1.y + line_dsc.width / 2 >= obj->coords.y1 &&
1495 p2.y - line_dsc.width / 2 <= obj->coords.y2) {
1496 lv_draw_line(draw_ctx, &line_dsc, &p1, &p2);
1497 }
1498
1499 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1500 }
1501 }
1502
draw_x_ticks(lv_obj_t * obj,lv_draw_ctx_t * draw_ctx,lv_chart_axis_t axis)1503 static void draw_x_ticks(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx, lv_chart_axis_t axis)
1504 {
1505 lv_chart_t * chart = (lv_chart_t *)obj;
1506
1507 lv_chart_tick_dsc_t * t = get_tick_gsc(obj, axis);
1508 if(t->major_cnt <= 1) return;
1509 if(!t->label_en && !t->major_len && !t->minor_len) return;
1510
1511 uint32_t i;
1512 lv_point_t p1;
1513 lv_point_t p2;
1514
1515 lv_coord_t pad_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + lv_obj_get_style_border_width(obj, LV_PART_MAIN);
1516 lv_coord_t w = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
1517
1518
1519 lv_draw_label_dsc_t label_dsc;
1520 lv_draw_label_dsc_init(&label_dsc);
1521 lv_obj_init_draw_label_dsc(obj, LV_PART_TICKS, &label_dsc);
1522
1523 lv_coord_t x_ofs = obj->coords.x1 + pad_left - lv_obj_get_scroll_left(obj);
1524 lv_coord_t y_ofs;
1525 lv_coord_t label_gap;
1526 if(axis == LV_CHART_AXIS_PRIMARY_X) {
1527 label_gap = t->label_en ? lv_obj_get_style_pad_bottom(obj, LV_PART_TICKS) : 0;
1528 y_ofs = obj->coords.y2;
1529 }
1530 else {
1531 label_gap = t->label_en ? lv_obj_get_style_pad_top(obj, LV_PART_TICKS) : 0;
1532 y_ofs = obj->coords.y1;
1533 }
1534
1535 if(axis == LV_CHART_AXIS_PRIMARY_X) {
1536 if(y_ofs > draw_ctx->clip_area->y2) return;
1537 if(y_ofs + label_gap + label_dsc.font->line_height + t->major_len < draw_ctx->clip_area->y1) return;
1538 }
1539
1540 lv_draw_line_dsc_t line_dsc;
1541 lv_draw_line_dsc_init(&line_dsc);
1542 lv_obj_init_draw_line_dsc(obj, LV_PART_TICKS, &line_dsc);
1543 line_dsc.dash_gap = 0;
1544 line_dsc.dash_width = 0;
1545
1546 lv_obj_draw_part_dsc_t part_draw_dsc;
1547 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
1548 part_draw_dsc.class_p = MY_CLASS;
1549 part_draw_dsc.type = LV_CHART_DRAW_PART_TICK_LABEL;
1550 part_draw_dsc.id = LV_CHART_AXIS_PRIMARY_X;
1551 part_draw_dsc.part = LV_PART_TICKS;
1552 part_draw_dsc.label_dsc = &label_dsc;
1553 part_draw_dsc.line_dsc = &line_dsc;
1554
1555 uint8_t sec_axis = axis == LV_CHART_AXIS_PRIMARY_X ? 0 : 1;
1556
1557 /*The columns ticks should be aligned to the center of blocks*/
1558 if(chart->type == LV_CHART_TYPE_BAR) {
1559 int32_t block_gap = ((int32_t)lv_obj_get_style_pad_column(obj,
1560 LV_PART_MAIN) * chart->zoom_x) >> 8; /*Gap between the columns on ~adjacent X*/
1561 lv_coord_t block_w = (w + block_gap) / (chart->point_cnt);
1562 x_ofs += (block_w - block_gap) / 2;
1563 w -= block_w - block_gap;
1564 }
1565
1566 p1.y = y_ofs;
1567 uint32_t total_tick_num = (t->major_cnt - 1) * t->minor_cnt;
1568 for(i = 0; i <= total_tick_num; i++) { /*one extra loop - it may not exist in the list, empty label*/
1569 bool major = false;
1570 if(i % t->minor_cnt == 0) major = true;
1571
1572 /*draw a line at moving x position*/
1573 p2.x = p1.x = x_ofs + (int32_t)((int32_t)(w - line_dsc.width) * i) / total_tick_num;
1574
1575 if(sec_axis) p2.y = p1.y - (major ? t->major_len : t->minor_len);
1576 else p2.y = p1.y + (major ? t->major_len : t->minor_len);
1577
1578 part_draw_dsc.p1 = &p1;
1579 part_draw_dsc.p2 = &p2;
1580
1581 /*add text only to major tick*/
1582 int32_t tick_value;
1583 if(chart->type == LV_CHART_TYPE_SCATTER) {
1584 tick_value = lv_map(i, 0, total_tick_num, chart->xmin[sec_axis], chart->xmax[sec_axis]);
1585 }
1586 else {
1587 tick_value = i / t->minor_cnt;
1588 }
1589 part_draw_dsc.value = tick_value;
1590
1591 if(major && t->label_en) {
1592 char buf[LV_CHART_LABEL_MAX_TEXT_LENGTH];
1593 lv_snprintf(buf, sizeof(buf), "%" LV_PRId32, tick_value);
1594 part_draw_dsc.label_dsc = &label_dsc;
1595 part_draw_dsc.text = buf;
1596 part_draw_dsc.text_length = LV_CHART_LABEL_MAX_TEXT_LENGTH;
1597
1598 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1599
1600 /*reserve appropriate area*/
1601 lv_point_t size;
1602 lv_txt_get_size(&size, part_draw_dsc.text, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX,
1603 LV_TEXT_FLAG_NONE);
1604
1605 /*set the area at some distance of the major tick len under of the tick*/
1606 lv_area_t a;
1607 a.x1 = (p2.x - size.x / 2);
1608 a.x2 = (p2.x + size.x / 2);
1609 if(sec_axis) {
1610 a.y2 = p2.y - label_gap;
1611 a.y1 = a.y2 - size.y;
1612 }
1613 else {
1614 a.y1 = p2.y + label_gap;
1615 a.y2 = a.y1 + size.y;
1616 }
1617
1618 if(a.x2 >= obj->coords.x1 &&
1619 a.x1 <= obj->coords.x2) {
1620 lv_draw_label(draw_ctx, &label_dsc, &a, part_draw_dsc.text, NULL);
1621 }
1622 }
1623 else {
1624 part_draw_dsc.label_dsc = NULL;
1625 part_draw_dsc.text = NULL;
1626 part_draw_dsc.text_length = 0;
1627 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
1628 }
1629
1630
1631 if(p1.x + line_dsc.width / 2 >= obj->coords.x1 &&
1632 p2.x - line_dsc.width / 2 <= obj->coords.x2) {
1633 lv_draw_line(draw_ctx, &line_dsc, &p1, &p2);
1634 }
1635
1636 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
1637 }
1638 }
1639
draw_axes(lv_obj_t * obj,lv_draw_ctx_t * draw_ctx)1640 static void draw_axes(lv_obj_t * obj, lv_draw_ctx_t * draw_ctx)
1641 {
1642 draw_y_ticks(obj, draw_ctx, LV_CHART_AXIS_PRIMARY_Y);
1643 draw_y_ticks(obj, draw_ctx, LV_CHART_AXIS_SECONDARY_Y);
1644 draw_x_ticks(obj, draw_ctx, LV_CHART_AXIS_PRIMARY_X);
1645 draw_x_ticks(obj, draw_ctx, LV_CHART_AXIS_SECONDARY_X);
1646 }
1647
1648 /**
1649 * Get the nearest index to an X coordinate
1650 * @param chart pointer to a chart object
1651 * @param coord the coordination of the point relative to the series area.
1652 * @return the found index
1653 */
get_index_from_x(lv_obj_t * obj,lv_coord_t x)1654 static uint32_t get_index_from_x(lv_obj_t * obj, lv_coord_t x)
1655 {
1656 lv_chart_t * chart = (lv_chart_t *)obj;
1657 lv_coord_t w = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
1658 lv_coord_t pad_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1659 x -= pad_left;
1660
1661 if(x < 0) return 0;
1662 if(x > w) return chart->point_cnt - 1;
1663 if(chart->type == LV_CHART_TYPE_LINE) return (x * (chart->point_cnt - 1) + w / 2) / w;
1664 if(chart->type == LV_CHART_TYPE_BAR) return (x * chart->point_cnt) / w;
1665
1666 return 0;
1667 }
1668
invalidate_point(lv_obj_t * obj,uint16_t i)1669 static void invalidate_point(lv_obj_t * obj, uint16_t i)
1670 {
1671 lv_chart_t * chart = (lv_chart_t *)obj;
1672 if(i >= chart->point_cnt) return;
1673
1674 lv_coord_t w = ((int32_t)lv_obj_get_content_width(obj) * chart->zoom_x) >> 8;
1675 lv_coord_t scroll_left = lv_obj_get_scroll_left(obj);
1676
1677 /*In shift mode the whole chart changes so the whole object*/
1678 if(chart->update_mode == LV_CHART_UPDATE_MODE_SHIFT) {
1679 lv_obj_invalidate(obj);
1680 return;
1681 }
1682
1683 if(chart->type == LV_CHART_TYPE_LINE) {
1684 lv_coord_t bwidth = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
1685 lv_coord_t pleft = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1686 lv_coord_t x_ofs = obj->coords.x1 + pleft + bwidth - scroll_left;
1687 lv_coord_t line_width = lv_obj_get_style_line_width(obj, LV_PART_ITEMS);
1688 lv_coord_t point_w = lv_obj_get_style_width(obj, LV_PART_INDICATOR);
1689
1690 lv_area_t coords;
1691 lv_area_copy(&coords, &obj->coords);
1692 coords.y1 -= line_width + point_w;
1693 coords.y2 += line_width + point_w;
1694
1695 if(i < chart->point_cnt - 1) {
1696 coords.x1 = ((w * i) / (chart->point_cnt - 1)) + x_ofs - line_width - point_w;
1697 coords.x2 = ((w * (i + 1)) / (chart->point_cnt - 1)) + x_ofs + line_width + point_w;
1698 lv_obj_invalidate_area(obj, &coords);
1699 }
1700
1701 if(i > 0) {
1702 coords.x1 = ((w * (i - 1)) / (chart->point_cnt - 1)) + x_ofs - line_width - point_w;
1703 coords.x2 = ((w * i) / (chart->point_cnt - 1)) + x_ofs + line_width + point_w;
1704 lv_obj_invalidate_area(obj, &coords);
1705 }
1706 }
1707 else if(chart->type == LV_CHART_TYPE_BAR) {
1708 lv_area_t col_a;
1709 int32_t block_gap = ((int32_t)lv_obj_get_style_pad_column(obj,
1710 LV_PART_MAIN) * chart->zoom_x) >> 8; /*Gap between the column on ~adjacent X*/
1711 lv_coord_t block_w = (w + block_gap) / chart->point_cnt;
1712
1713 lv_coord_t x_act;
1714 x_act = (int32_t)((int32_t)(block_w) * i) ;
1715 x_act += obj->coords.x1 + lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
1716
1717 lv_obj_get_coords(obj, &col_a);
1718 col_a.x1 = x_act - scroll_left;
1719 col_a.x2 = col_a.x1 + block_w;
1720 col_a.x1 -= block_gap;
1721
1722 lv_obj_invalidate_area(obj, &col_a);
1723 }
1724 else if(chart->type == LV_CHART_TYPE_SCATTER) {
1725 lv_obj_invalidate(obj);
1726 }
1727 else {
1728 lv_obj_invalidate(obj);
1729 }
1730 }
1731
new_points_alloc(lv_obj_t * obj,lv_chart_series_t * ser,uint32_t cnt,lv_coord_t ** a)1732 static void new_points_alloc(lv_obj_t * obj, lv_chart_series_t * ser, uint32_t cnt, lv_coord_t ** a)
1733 {
1734 if((*a) == NULL) return;
1735
1736 lv_chart_t * chart = (lv_chart_t *) obj;
1737 uint32_t point_cnt_old = chart->point_cnt;
1738 uint32_t i;
1739
1740 if(ser->start_point != 0) {
1741 lv_coord_t * new_points = lv_mem_alloc(sizeof(lv_coord_t) * cnt);
1742 LV_ASSERT_MALLOC(new_points);
1743 if(new_points == NULL) return;
1744
1745 if(cnt >= point_cnt_old) {
1746 for(i = 0; i < point_cnt_old; i++) {
1747 new_points[i] =
1748 (*a)[(i + ser->start_point) % point_cnt_old]; /*Copy old contents to new array*/
1749 }
1750 for(i = point_cnt_old; i < cnt; i++) {
1751 new_points[i] = LV_CHART_POINT_NONE; /*Fill up the rest with default value*/
1752 }
1753 }
1754 else {
1755 for(i = 0; i < cnt; i++) {
1756 new_points[i] =
1757 (*a)[(i + ser->start_point) % point_cnt_old]; /*Copy old contents to new array*/
1758 }
1759 }
1760
1761 /*Switch over pointer from old to new*/
1762 lv_mem_free((*a));
1763 (*a) = new_points;
1764 }
1765 else {
1766 (*a) = lv_mem_realloc((*a), sizeof(lv_coord_t) * cnt);
1767 LV_ASSERT_MALLOC((*a));
1768 if((*a) == NULL) return;
1769 /*Initialize the new points*/
1770 if(cnt > point_cnt_old) {
1771 for(i = point_cnt_old - 1; i < cnt; i++) {
1772 (*a)[i] = LV_CHART_POINT_NONE;
1773 }
1774 }
1775 }
1776 }
1777
get_tick_gsc(lv_obj_t * obj,lv_chart_axis_t axis)1778 lv_chart_tick_dsc_t * get_tick_gsc(lv_obj_t * obj, lv_chart_axis_t axis)
1779 {
1780 lv_chart_t * chart = (lv_chart_t *) obj;
1781 switch(axis) {
1782 case LV_CHART_AXIS_PRIMARY_Y:
1783 return &chart->tick[0];
1784 case LV_CHART_AXIS_PRIMARY_X:
1785 return &chart->tick[1];
1786 case LV_CHART_AXIS_SECONDARY_Y:
1787 return &chart->tick[2];
1788 case LV_CHART_AXIS_SECONDARY_X:
1789 return &chart->tick[3];
1790 default:
1791 return NULL;
1792 }
1793 }
1794
1795
1796 #endif
1797