1 #include "../../lv_examples.h"
2 
3 #if LV_USE_CHART && LV_DRAW_SW_COMPLEX && LV_BUILD_EXAMPLES
4 
draw_event_cb(lv_event_t * e)5 static void draw_event_cb(lv_event_t * e)
6 {
7     lv_draw_task_t * draw_task = lv_event_get_draw_task(e);
8     lv_draw_dsc_base_t * base_dsc = lv_draw_task_get_draw_dsc(draw_task);
9 
10     if(base_dsc->part != LV_PART_ITEMS) {
11         return;
12     }
13 
14     lv_draw_fill_dsc_t * fill_dsc = lv_draw_task_get_fill_dsc(draw_task);
15     if(fill_dsc) {
16         lv_obj_t * chart = lv_event_get_target(e);
17         int32_t * y_array = lv_chart_get_y_array(chart, lv_chart_get_series_next(chart, NULL));
18         int32_t v = y_array[base_dsc->id2];
19 
20         uint32_t ratio = v * 255 / 100;
21         fill_dsc->color = lv_color_mix(lv_palette_main(LV_PALETTE_GREEN), lv_palette_main(LV_PALETTE_RED), ratio);
22     }
23 }
24 
25 /**
26  * Recolor the bars of a chart based on their value
27  */
lv_example_chart_4(void)28 void lv_example_chart_4(void)
29 {
30     /*Create a chart1*/
31     lv_obj_t * chart = lv_chart_create(lv_screen_active());
32     lv_chart_set_type(chart, LV_CHART_TYPE_BAR);
33     lv_chart_set_point_count(chart, 24);
34     lv_obj_set_style_pad_column(chart, 2, 0);
35     lv_obj_set_size(chart, 260, 160);
36     lv_obj_center(chart);
37 
38     lv_chart_series_t * ser = lv_chart_add_series(chart, lv_color_hex(0xff0000), LV_CHART_AXIS_PRIMARY_Y);
39     lv_obj_add_event_cb(chart, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
40     lv_obj_add_flag(chart, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
41 
42     uint32_t i;
43     for(i = 0; i < 24; i++) {
44         lv_chart_set_next_value(chart, ser, lv_rand(10, 90));
45     }
46 }
47 
48 #endif
49