Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_chart.c
1476 views
1
/**
2
* @file lv_chart.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_chart.h"
10
#if USE_LV_CHART != 0
11
12
#include "../lv_draw/lv_draw.h"
13
#include "../lv_themes/lv_theme.h"
14
15
/*********************
16
* DEFINES
17
*********************/
18
#define LV_CHART_YMIN_DEF 0
19
#define LV_CHART_YMAX_DEF 100
20
#define LV_CHART_HDIV_DEF 3
21
#define LV_CHART_VDIV_DEF 5
22
#define LV_CHART_PNUM_DEF 10
23
24
/**********************
25
* TYPEDEFS
26
**********************/
27
28
/**********************
29
* STATIC PROTOTYPES
30
**********************/
31
static bool lv_chart_design(lv_obj_t * chart, const lv_area_t * mask, lv_design_mode_t mode);
32
static lv_res_t lv_chart_signal(lv_obj_t * chart, lv_signal_t sign, void * param);
33
static void lv_chart_draw_div(lv_obj_t * chart, const lv_area_t * mask);
34
static void lv_chart_draw_lines(lv_obj_t * chart, const lv_area_t * mask);
35
static void lv_chart_draw_points(lv_obj_t * chart, const lv_area_t * mask);
36
static void lv_chart_draw_cols(lv_obj_t * chart, const lv_area_t * mask);
37
static void lv_chart_draw_vertical_lines(lv_obj_t * chart, const lv_area_t * mask);
38
39
/**********************
40
* STATIC VARIABLES
41
**********************/
42
static lv_design_func_t ancestor_design_f;
43
static lv_signal_func_t ancestor_signal;
44
45
/**********************
46
* MACROS
47
**********************/
48
49
/**********************
50
* GLOBAL FUNCTIONS
51
**********************/
52
53
/**
54
* Create a chart background objects
55
* @param par pointer to an object, it will be the parent of the new chart background
56
* @param copy pointer to a chart background object, if not NULL then the new object will be copied from it
57
* @return pointer to the created chart background
58
*/
59
lv_obj_t * lv_chart_create(lv_obj_t * par, const lv_obj_t * copy)
60
{
61
LV_LOG_TRACE("chart create started");
62
63
/*Create the ancestor basic object*/
64
lv_obj_t * new_chart = lv_obj_create(par, copy);
65
lv_mem_assert(new_chart);
66
if(new_chart == NULL) return NULL;
67
68
/*Allocate the object type specific extended data*/
69
lv_chart_ext_t * ext = lv_obj_allocate_ext_attr(new_chart, sizeof(lv_chart_ext_t));
70
lv_mem_assert(ext);
71
if(ext == NULL) return NULL;
72
73
lv_ll_init(&ext->series_ll, sizeof(lv_chart_series_t));
74
ext->series.num = 0;
75
ext->ymin = LV_CHART_YMIN_DEF;
76
ext->ymax = LV_CHART_YMAX_DEF;
77
ext->hdiv_cnt = LV_CHART_HDIV_DEF;
78
ext->vdiv_cnt = LV_CHART_VDIV_DEF;
79
ext->point_cnt = LV_CHART_PNUM_DEF;
80
ext->type = LV_CHART_TYPE_LINE;
81
ext->series.opa = LV_OPA_COVER;
82
ext->series.dark = LV_OPA_50;
83
ext->series.width = 2;
84
85
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_chart);
86
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_chart);
87
88
lv_obj_set_signal_func(new_chart, lv_chart_signal);
89
lv_obj_set_design_func(new_chart, lv_chart_design);
90
91
/*Init the new chart background object*/
92
if(copy == NULL) {
93
lv_obj_set_size(new_chart, LV_HOR_RES / 3, LV_VER_RES / 3);
94
95
/*Set the default styles*/
96
lv_theme_t * th = lv_theme_get_current();
97
if(th) {
98
lv_chart_set_style(new_chart, th->chart);
99
} else {
100
lv_chart_set_style(new_chart, &lv_style_pretty);
101
}
102
103
} else {
104
lv_chart_ext_t * ext_copy = lv_obj_get_ext_attr(copy);
105
ext->type = ext_copy->type;
106
ext->ymin = ext_copy->ymin;
107
ext->ymax = ext_copy->ymax;
108
ext->hdiv_cnt = ext_copy->hdiv_cnt;
109
ext->vdiv_cnt = ext_copy->vdiv_cnt;
110
ext->point_cnt = ext_copy->point_cnt;
111
ext->series.opa = ext_copy->series.opa;
112
113
/*Refresh the style with new signal function*/
114
lv_obj_refresh_style(new_chart);
115
}
116
117
LV_LOG_INFO("chart created");
118
119
120
return new_chart;
121
}
122
123
/*======================
124
* Add/remove functions
125
*=====================*/
126
127
/**
128
* Allocate and add a data series to the chart
129
* @param chart pointer to a chart object
130
* @param color color of the data series
131
* @return pointer to the allocated data series
132
*/
133
lv_chart_series_t * lv_chart_add_series(lv_obj_t * chart, lv_color_t color)
134
{
135
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
136
lv_chart_series_t * ser = lv_ll_ins_head(&ext->series_ll);
137
lv_mem_assert(ser);
138
if(ser == NULL) return NULL;
139
140
lv_coord_t def = LV_CHART_POINT_DEF;
141
142
if(ser == NULL) return NULL;
143
144
ser->color = color;
145
146
ser->points = lv_mem_alloc(sizeof(lv_coord_t) * ext->point_cnt);
147
lv_mem_assert(ser->points);
148
if(ser->points == NULL) {
149
lv_ll_rem(&ext->series_ll, ser);
150
lv_mem_free(ser);
151
return NULL;
152
}
153
154
ser->start_point = 0;
155
156
uint16_t i;
157
lv_coord_t * p_tmp = ser->points;
158
for(i = 0; i < ext->point_cnt; i++) {
159
*p_tmp = def;
160
p_tmp++;
161
}
162
163
ext->series.num++;
164
165
return ser;
166
}
167
168
/**
169
* Clear the point of a serie
170
* @param chart pointer to a chart object
171
* @param serie pointer to the chart's serie to clear
172
*/
173
void lv_chart_clear_serie(lv_obj_t * chart, lv_chart_series_t * serie)
174
{
175
if(chart == NULL || serie == NULL)
176
return;
177
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
178
if(ext == NULL) return;
179
180
uint32_t i;
181
for(i = 0; i < ext->point_cnt; i++)
182
{
183
serie->points[i] = LV_CHART_POINT_DEF;
184
}
185
186
serie->start_point = 0;
187
188
}
189
190
/*=====================
191
* Setter functions
192
*====================*/
193
194
/**
195
* Set the number of horizontal and vertical division lines
196
* @param chart pointer to a graph background object
197
* @param hdiv number of horizontal division lines
198
* @param vdiv number of vertical division lines
199
*/
200
void lv_chart_set_div_line_count(lv_obj_t * chart, uint8_t hdiv, uint8_t vdiv)
201
{
202
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
203
if(ext->hdiv_cnt == hdiv && ext->vdiv_cnt == vdiv) return;
204
205
ext->hdiv_cnt = hdiv;
206
ext->vdiv_cnt = vdiv;
207
208
lv_obj_invalidate(chart);
209
}
210
211
/**
212
* Set the minimal and maximal y values
213
* @param chart pointer to a graph background object
214
* @param ymin y minimum value
215
* @param ymax y maximum value
216
*/
217
void lv_chart_set_range(lv_obj_t * chart, lv_coord_t ymin, lv_coord_t ymax)
218
{
219
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
220
if(ext->ymin == ymin && ext->ymax == ymax) return;
221
222
ext->ymin = ymin;
223
ext->ymax = ymax;
224
225
lv_chart_refresh(chart);
226
}
227
228
/**
229
* Set a new type for a chart
230
* @param chart pointer to a chart object
231
* @param type new type of the chart (from 'lv_chart_type_t' enum)
232
*/
233
void lv_chart_set_type(lv_obj_t * chart, lv_chart_type_t type)
234
{
235
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
236
if(ext->type == type) return;
237
238
ext->type = type;
239
240
lv_chart_refresh(chart);
241
}
242
243
/**
244
* Set the number of points on a data line on a chart
245
* @param chart pointer r to chart object
246
* @param point_cnt new number of points on the data lines
247
*/
248
void lv_chart_set_point_count(lv_obj_t * chart, uint16_t point_cnt)
249
{
250
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
251
if(ext->point_cnt == point_cnt) return;
252
253
lv_chart_series_t * ser;
254
uint16_t point_cnt_old = ext->point_cnt;
255
uint16_t i;
256
lv_coord_t def = LV_CHART_POINT_DEF;
257
258
if(point_cnt < 1) point_cnt = 1;
259
260
LL_READ_BACK(ext->series_ll, ser) {
261
if(ser->start_point != 0) {
262
lv_coord_t * new_points = lv_mem_alloc(sizeof(lv_coord_t) * point_cnt);
263
lv_mem_assert(new_points);
264
if(new_points == NULL) return;
265
266
if(point_cnt >= point_cnt_old) {
267
for(i = 0; i < point_cnt_old; i++) {
268
new_points[i] = ser->points[(i + ser->start_point) % point_cnt_old]; /*Copy old contents to new array*/
269
}
270
for(i = point_cnt_old; i < point_cnt; i++) {
271
new_points[i] = def; /*Fill up the rest with default value*/
272
}
273
} else {
274
for(i = 0; i < point_cnt; i++) {
275
new_points[i] = ser->points[(i + ser->start_point) % point_cnt_old]; /*Copy old contents to new array*/
276
}
277
}
278
279
/*Switch over pointer from old to new*/
280
lv_mem_free(ser->points);
281
ser->points = new_points;
282
} else {
283
ser->points = lv_mem_realloc(ser->points, sizeof(lv_coord_t) * point_cnt);
284
lv_mem_assert(ser->points);
285
if(ser->points == NULL) return;
286
/*Initialize the new points*/
287
if(point_cnt > point_cnt_old) {
288
for(i = point_cnt_old - 1; i < point_cnt; i++) {
289
ser->points[i] = def;
290
}
291
}
292
}
293
294
ser->start_point = 0;
295
}
296
297
ext->point_cnt = point_cnt;
298
299
lv_chart_refresh(chart);
300
}
301
302
/**
303
* Set the opacity of the data series
304
* @param chart pointer to a chart object
305
* @param opa opacity of the data series
306
*/
307
void lv_chart_set_series_opa(lv_obj_t * chart, lv_opa_t opa)
308
{
309
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
310
if(ext->series.opa == opa) return;
311
312
ext->series.opa = opa;
313
lv_obj_invalidate(chart);
314
}
315
316
/**
317
* Set the line width or point radius of the data series
318
* @param chart pointer to a chart object
319
* @param width the new width
320
*/
321
void lv_chart_set_series_width(lv_obj_t * chart, lv_coord_t width)
322
{
323
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
324
if(ext->series.width == width) return;
325
326
ext->series.width = width;
327
lv_obj_invalidate(chart);
328
}
329
/**
330
* Set the dark effect on the bottom of the points or columns
331
* @param chart pointer to a chart object
332
* @param dark_eff dark effect level (LV_OPA_TRANSP to turn off)
333
*/
334
void lv_chart_set_series_darking(lv_obj_t * chart, lv_opa_t dark_eff)
335
{
336
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
337
if(ext->series.dark == dark_eff) return;
338
339
ext->series.dark = dark_eff;
340
lv_obj_invalidate(chart);
341
}
342
343
/**
344
* Initialize all data points with a value
345
* @param chart pointer to chart object
346
* @param ser pointer to a data series on 'chart'
347
* @param y the new value for all points
348
*/
349
void lv_chart_init_points(lv_obj_t * chart, lv_chart_series_t * ser, lv_coord_t y)
350
{
351
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
352
uint16_t i;
353
for(i = 0; i < ext->point_cnt; i++) {
354
ser->points[i] = y;
355
}
356
ser->start_point = 0;
357
lv_chart_refresh(chart);
358
}
359
360
/**
361
* Set the value s of points from an array
362
* @param chart pointer to chart object
363
* @param ser pointer to a data series on 'chart'
364
* @param y_array array of 'lv_coord_t' points (with 'points count' elements )
365
*/
366
void lv_chart_set_points(lv_obj_t * chart, lv_chart_series_t * ser, lv_coord_t * y_array)
367
{
368
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
369
memcpy(ser->points, y_array, ext->point_cnt * (sizeof(lv_coord_t)));
370
ser->start_point = 0;
371
lv_chart_refresh(chart);
372
}
373
374
/**
375
* Shift all data left and set the rightmost data on a data line
376
* @param chart pointer to chart object
377
* @param ser pointer to a data series on 'chart'
378
* @param y the new value of the rightmost data
379
*/
380
void lv_chart_set_next(lv_obj_t * chart, lv_chart_series_t * ser, lv_coord_t y)
381
{
382
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
383
384
ser->points[ser->start_point] = y; /*This was the place of the former left most value, after shifting it is the rightmost*/
385
ser->start_point = (ser->start_point + 1) % ext->point_cnt;
386
387
lv_chart_refresh(chart);
388
}
389
390
/*=====================
391
* Getter functions
392
*====================*/
393
394
/**
395
* Get the type of a chart
396
* @param chart pointer to chart object
397
* @return type of the chart (from 'lv_chart_t' enum)
398
*/
399
lv_chart_type_t lv_chart_get_type(const lv_obj_t * chart)
400
{
401
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
402
return ext->type;
403
}
404
405
/**
406
* Get the data point number per data line on chart
407
* @param chart pointer to chart object
408
* @return point number on each data line
409
*/
410
uint16_t lv_chart_get_point_cnt(const lv_obj_t * chart)
411
{
412
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
413
return ext->point_cnt;
414
}
415
416
/**
417
* Get the opacity of the data series
418
* @param chart pointer to chart object
419
* @return the opacity of the data series
420
*/
421
lv_opa_t lv_chart_get_series_opa(const lv_obj_t * chart)
422
{
423
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
424
return ext->series.opa;
425
}
426
427
/**
428
* Get the data series width
429
* @param chart pointer to chart object
430
* @return the width the data series (lines or points)
431
*/
432
lv_coord_t lv_chart_get_series_width(const lv_obj_t * chart)
433
{
434
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
435
return ext->series.width;
436
}
437
438
/**
439
* Get the dark effect level on the bottom of the points or columns
440
* @param chart pointer to chart object
441
* @return dark effect level (LV_OPA_TRANSP to turn off)
442
*/
443
lv_opa_t lv_chart_get_series_darking(const lv_obj_t * chart)
444
{
445
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
446
return ext->series.dark;
447
}
448
449
/*=====================
450
* Other functions
451
*====================*/
452
453
/**
454
* Refresh a chart if its data line has changed
455
* @param chart pointer to chart object
456
*/
457
void lv_chart_refresh(lv_obj_t * chart)
458
{
459
lv_obj_invalidate(chart);
460
}
461
462
/**********************
463
* STATIC FUNCTIONS
464
**********************/
465
466
/**
467
* Handle the drawing related tasks of the chart backgrounds
468
* @param chart pointer to an object
469
* @param mask the object will be drawn only in this area
470
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
471
* (return 'true' if yes)
472
* LV_DESIGN_DRAW: draw the object (always return 'true')
473
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
474
* @param return true/false, depends on 'mode'
475
*/
476
static bool lv_chart_design(lv_obj_t * chart, const lv_area_t * mask, lv_design_mode_t mode)
477
{
478
if(mode == LV_DESIGN_COVER_CHK) {
479
/*Return false if the object is not covers the mask_p area*/
480
return ancestor_design_f(chart, mask, mode);
481
} else if(mode == LV_DESIGN_DRAW_MAIN) {
482
/*Draw the background*/
483
lv_draw_rect(&chart->coords, mask, lv_obj_get_style(chart), lv_obj_get_opa_scale(chart));
484
485
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
486
487
lv_chart_draw_div(chart, mask);
488
489
if(ext->type & LV_CHART_TYPE_LINE) lv_chart_draw_lines(chart, mask);
490
if(ext->type & LV_CHART_TYPE_COLUMN) lv_chart_draw_cols(chart, mask);
491
if(ext->type & LV_CHART_TYPE_POINT) lv_chart_draw_points(chart, mask);
492
if(ext->type & LV_CHART_TYPE_VERTICAL_LINE) lv_chart_draw_vertical_lines(chart, mask);
493
}
494
return true;
495
}
496
497
/**
498
* Signal function of the chart background
499
* @param chart pointer to a chart background object
500
* @param sign a signal type from lv_signal_t enum
501
* @param param pointer to a signal specific variable
502
*/
503
static lv_res_t lv_chart_signal(lv_obj_t * chart, lv_signal_t sign, void * param)
504
{
505
lv_res_t res;
506
507
/* Include the ancient signal function */
508
res = ancestor_signal(chart, sign, param);
509
if(res != LV_RES_OK) return res;
510
511
if(sign == LV_SIGNAL_CLEANUP) {
512
lv_coord_t ** datal;
513
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
514
LL_READ(ext->series_ll, datal) {
515
lv_mem_free(*datal);
516
}
517
lv_ll_clear(&ext->series_ll);
518
} else if(sign == LV_SIGNAL_GET_TYPE) {
519
lv_obj_type_t * buf = param;
520
uint8_t i;
521
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
522
if(buf->type[i] == NULL) break;
523
}
524
buf->type[i] = "lv_chart";
525
}
526
527
return res;
528
}
529
530
/**
531
* Draw the division lines on chart background
532
* @param chart pointer to chart object
533
* @param mask mask, inherited from the design function
534
*/
535
static void lv_chart_draw_div(lv_obj_t * chart, const lv_area_t * mask)
536
{
537
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
538
lv_style_t * style = lv_obj_get_style(chart);
539
lv_opa_t opa_scale = lv_obj_get_opa_scale(chart);
540
541
uint8_t div_i;
542
uint8_t div_i_end;
543
uint8_t div_i_start;
544
lv_point_t p1;
545
lv_point_t p2;
546
lv_coord_t w = lv_obj_get_width(chart);
547
lv_coord_t h = lv_obj_get_height(chart);
548
lv_coord_t x_ofs = chart->coords.x1;
549
lv_coord_t y_ofs = chart->coords.y1;
550
551
if(ext->hdiv_cnt != 0) {
552
/*Draw slide lines if no border*/
553
if(style->body.border.width != 0) {
554
div_i_start = 1;
555
div_i_end = ext->hdiv_cnt;
556
} else {
557
div_i_start = 0;
558
div_i_end = ext->hdiv_cnt + 1;
559
}
560
561
p1.x = 0 + x_ofs;
562
p2.x = w + x_ofs;
563
for(div_i = div_i_start; div_i <= div_i_end; div_i++) {
564
p1.y = (int32_t)((int32_t)h * div_i) / (ext->hdiv_cnt + 1);
565
p1.y += y_ofs;
566
if(div_i == div_i_start) p1.y += (style->line.width >> 1) + 1; /*The first line might not be visible*/
567
if(div_i == div_i_end) p1.y -= (style->line.width >> 1) + 1; /*The last line might not be visible*/
568
569
p2.y = p1.y;
570
lv_draw_line(&p1, &p2, mask, style, opa_scale);
571
}
572
}
573
574
if(ext->vdiv_cnt != 0) {
575
/*Draw slide lines if no border*/
576
if(style->body.border.width != 0) {
577
div_i_start = 1;
578
div_i_end = ext->vdiv_cnt;
579
} else {
580
div_i_start = 0;
581
div_i_end = ext->vdiv_cnt + 1;
582
}
583
584
p1.y = 0 + y_ofs;
585
p2.y = h + y_ofs;
586
for(div_i = div_i_start; div_i <= div_i_end; div_i ++) {
587
p1.x = (int32_t)((int32_t)w * div_i) / (ext->vdiv_cnt + 1);
588
p1.x += x_ofs;
589
if(div_i == div_i_start) p1.x += (style->line.width >> 1) + 1; /*The first line might not be visible*/
590
if(div_i == div_i_end) p1.x -= (style->line.width >> 1) + 1; /*The last line might not be visible*/
591
p2.x = p1.x;
592
lv_draw_line(&p1, &p2, mask, style, opa_scale);
593
}
594
}
595
}
596
597
/**
598
* Draw the data lines as lines on a chart
599
* @param obj pointer to chart object
600
*/
601
static void lv_chart_draw_lines(lv_obj_t * chart, const lv_area_t * mask)
602
{
603
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
604
605
uint16_t i;
606
lv_point_t p1;
607
lv_point_t p2;
608
lv_coord_t w = lv_obj_get_width(chart);
609
lv_coord_t h = lv_obj_get_height(chart);
610
lv_coord_t x_ofs = chart->coords.x1;
611
lv_coord_t y_ofs = chart->coords.y1;
612
int32_t y_tmp;
613
lv_coord_t p_prev;
614
lv_coord_t p_act;
615
lv_chart_series_t * ser;
616
lv_opa_t opa_scale = lv_obj_get_opa_scale(chart);
617
lv_style_t style;
618
lv_style_copy(&style, &lv_style_plain);
619
style.line.opa = ext->series.opa;
620
style.line.width = ext->series.width;
621
622
/*Go through all data lines*/
623
LL_READ_BACK(ext->series_ll, ser) {
624
style.line.color = ser->color;
625
626
p1.x = 0 + x_ofs;
627
p2.x = 0 + x_ofs;
628
629
p_prev = ser->start_point;
630
y_tmp = (int32_t)((int32_t) ser->points[p_prev] - ext->ymin) * h;
631
y_tmp = y_tmp / (ext->ymax - ext->ymin);
632
p2.y = h - y_tmp + y_ofs;
633
634
for(i = 1; i < ext->point_cnt; i ++) {
635
p1.x = p2.x;
636
p1.y = p2.y;
637
638
p2.x = ((w * i) / (ext->point_cnt - 1)) + x_ofs;
639
640
p_act = (ser->start_point + i) % ext->point_cnt;
641
642
y_tmp = (int32_t)((int32_t) ser->points[p_act] - ext->ymin) * h;
643
y_tmp = y_tmp / (ext->ymax - ext->ymin);
644
p2.y = h - y_tmp + y_ofs;
645
646
if(ser->points[p_prev] != LV_CHART_POINT_DEF && ser->points[p_act] != LV_CHART_POINT_DEF)
647
lv_draw_line(&p1, &p2, mask, &style, opa_scale);
648
649
p_prev = p_act;
650
}
651
}
652
}
653
654
/**
655
* Draw the data lines as points on a chart
656
* @param chart pointer to chart object
657
* @param mask mask, inherited from the design function
658
*/
659
static void lv_chart_draw_points(lv_obj_t * chart, const lv_area_t * mask)
660
{
661
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
662
663
uint16_t i;
664
lv_area_t cir_a;
665
lv_coord_t w = lv_obj_get_width(chart);
666
lv_coord_t h = lv_obj_get_height(chart);
667
lv_coord_t x_ofs = chart->coords.x1;
668
lv_coord_t y_ofs = chart->coords.y1;
669
int32_t y_tmp;
670
lv_coord_t p_act;
671
lv_chart_series_t * ser;
672
uint8_t series_cnt = 0;
673
lv_style_t style_point;
674
lv_style_copy(&style_point, &lv_style_plain);
675
676
style_point.body.border.width = 0;
677
style_point.body.empty = 0;
678
style_point.body.radius = LV_RADIUS_CIRCLE;
679
style_point.body.opa = ext->series.opa;
680
style_point.body.radius = ext->series.width;
681
682
/*Go through all data lines*/
683
LL_READ_BACK(ext->series_ll, ser) {
684
style_point.body.main_color = ser->color;
685
style_point.body.grad_color = lv_color_mix(LV_COLOR_BLACK, ser->color, ext->series.dark);
686
687
for(i = 0; i < ext->point_cnt; i ++) {
688
cir_a.x1 = ((w * i) / (ext->point_cnt - 1)) + x_ofs;
689
cir_a.x2 = cir_a.x1 + style_point.body.radius;
690
cir_a.x1 -= style_point.body.radius;
691
p_act = (ser->start_point + i) % ext->point_cnt;
692
y_tmp = (int32_t)((int32_t) ser->points[p_act] - ext->ymin) * h;
693
y_tmp = y_tmp / (ext->ymax - ext->ymin);
694
cir_a.y1 = h - y_tmp + y_ofs;
695
cir_a.y2 = cir_a.y1 + style_point.body.radius;
696
cir_a.y1 -= style_point.body.radius;
697
698
if(ser->points[p_act] != LV_CHART_POINT_DEF)
699
lv_draw_rect(&cir_a, mask, &style_point, lv_obj_get_opa_scale(chart));
700
}
701
series_cnt++;
702
}
703
}
704
705
/**
706
* Draw the data lines as columns on a chart
707
* @param chart pointer to chart object
708
* @param mask mask, inherited from the design function
709
*/
710
static void lv_chart_draw_cols(lv_obj_t * chart, const lv_area_t * mask)
711
{
712
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
713
714
uint16_t i;
715
lv_area_t col_a;
716
lv_area_t col_mask;
717
bool mask_ret;
718
lv_coord_t w = lv_obj_get_width(chart);
719
lv_coord_t h = lv_obj_get_height(chart);
720
int32_t y_tmp;
721
lv_chart_series_t * ser;
722
lv_style_t rects;
723
lv_coord_t col_w = w / ((ext->series.num + 1) * ext->point_cnt); /* Suppose + 1 series as separator*/
724
lv_coord_t x_ofs = col_w / 2; /*Shift with a half col.*/
725
726
lv_style_copy(&rects, &lv_style_plain);
727
rects.body.border.width = 0;
728
rects.body.empty = 0;
729
rects.body.radius = 0;
730
rects.body.opa = ext->series.opa;
731
732
col_a.y2 = chart->coords.y2;
733
734
lv_coord_t x_act;
735
736
/*Go through all points*/
737
for(i = 0; i < ext->point_cnt; i ++) {
738
x_act = (int32_t)((int32_t) w * i) / ext->point_cnt;
739
x_act += chart->coords.x1 + x_ofs;
740
741
/*Draw the current point of all data line*/
742
LL_READ_BACK(ext->series_ll, ser) {
743
rects.body.main_color = ser->color;
744
rects.body.grad_color = lv_color_mix(LV_COLOR_BLACK, ser->color, ext->series.dark);
745
col_a.x1 = x_act;
746
col_a.x2 = col_a.x1 + col_w;
747
x_act += col_w;
748
749
lv_coord_t p_act = (ser->start_point + i) % ext->point_cnt;
750
y_tmp = (int32_t)((int32_t) ser->points[p_act] - ext->ymin) * h;
751
y_tmp = y_tmp / (ext->ymax - ext->ymin);
752
col_a.y1 = h - y_tmp + chart->coords.y1;
753
754
mask_ret = lv_area_intersect(&col_mask, mask, &col_a);
755
if(mask_ret != false && ser->points[p_act] != LV_CHART_POINT_DEF) {
756
lv_draw_rect(&chart->coords, &col_mask, &rects, lv_obj_get_opa_scale(chart));
757
}
758
}
759
}
760
}
761
762
/**
763
* Draw the data lines as vertical lines on a chart if there is only 1px between point
764
* @param obj pointer to chart object
765
*/
766
static void lv_chart_draw_vertical_lines(lv_obj_t * chart, const lv_area_t * mask)
767
{
768
769
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
770
lv_coord_t w = lv_obj_get_width(chart);
771
/*Vertical lines works only if the width == point count. Else use the normal line type*/
772
if(ext->point_cnt != w) {
773
lv_chart_draw_lines(chart, mask);
774
return;
775
}
776
777
uint16_t i;
778
lv_point_t p1;
779
lv_point_t p2;
780
lv_coord_t h = lv_obj_get_height(chart);
781
lv_coord_t x_ofs = chart->coords.x1;
782
lv_coord_t y_ofs = chart->coords.y1;
783
int32_t y_tmp;
784
lv_chart_series_t * ser;
785
lv_opa_t opa_scale = lv_obj_get_opa_scale(chart);
786
lv_style_t style;
787
lv_style_copy(&style, &lv_style_plain);
788
style.line.opa = ext->series.opa;
789
style.line.width = ext->series.width;
790
791
/*Go through all data lines*/
792
LL_READ_BACK(ext->series_ll, ser) {
793
style.line.color = ser->color;
794
795
p1.x = 0 + x_ofs;
796
p2.x = 0 + x_ofs;
797
y_tmp = (int32_t)((int32_t) ser->points[0] - ext->ymin) * h;
798
y_tmp = y_tmp / (ext->ymax - ext->ymin);
799
p2.y = h - y_tmp + y_ofs;
800
p1.y = p2.y;
801
802
for(i = 0; i < ext->point_cnt; i++)
803
{
804
805
y_tmp = (int32_t)((int32_t) ser->points[i] - ext->ymin) * h;
806
y_tmp = y_tmp / (ext->ymax - ext->ymin);
807
p2.y = h - y_tmp + y_ofs;
808
809
if(p1.y == p2.y)
810
{
811
p2.x++;
812
}
813
814
if(ser->points[i] != LV_CHART_POINT_DEF) {
815
lv_draw_line(&p1, &p2, mask, &style, opa_scale);
816
}
817
818
p2.x = ((w * i) / (ext->point_cnt - 1)) + x_ofs;
819
p1.x = p2.x;
820
p1.y = p2.y;
821
}
822
}
823
}
824
#endif
825
826