1 #include "../../lv_examples.h"
2 #if LV_USE_CHART && LV_BUILD_EXAMPLES
3 
draw_event_cb(lv_event_t * e)4 static void draw_event_cb(lv_event_t * e)
5 {
6     lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
7     if(dsc->part == LV_PART_ITEMS) {
8         lv_obj_t * obj = lv_event_get_target(e);
9         lv_chart_series_t * ser = lv_chart_get_series_next(obj, NULL);
10         uint32_t cnt = lv_chart_get_point_count(obj);
11         /*Make older value more transparent*/
12         dsc->rect_dsc->bg_opa = (LV_OPA_COVER *  dsc->id) / (cnt - 1);
13 
14         /*Make smaller values blue, higher values red*/
15         lv_coord_t * x_array = lv_chart_get_x_array(obj, ser);
16         lv_coord_t * y_array = lv_chart_get_y_array(obj, ser);
17         /*dsc->id is the tells drawing order, but we need the ID of the point being drawn.*/
18         uint32_t start_point = lv_chart_get_x_start_point(obj, ser);
19         uint32_t p_act = (start_point + dsc->id) % cnt; /*Consider start point to get the index of the array*/
20         lv_opa_t x_opa = (x_array[p_act] * LV_OPA_50) / 200;
21         lv_opa_t y_opa = (y_array[p_act] * LV_OPA_50) / 1000;
22 
23         dsc->rect_dsc->bg_color = lv_color_mix(lv_palette_main(LV_PALETTE_RED),
24                                                lv_palette_main(LV_PALETTE_BLUE),
25                                                x_opa + y_opa);
26     }
27 }
28 
add_data(lv_timer_t * timer)29 static void add_data(lv_timer_t * timer)
30 {
31     LV_UNUSED(timer);
32     lv_obj_t * chart = timer->user_data;
33     lv_chart_set_next_value2(chart, lv_chart_get_series_next(chart, NULL), lv_rand(0, 200), lv_rand(0, 1000));
34 }
35 
36 /**
37  * A scatter chart
38  */
lv_example_chart_7(void)39 void lv_example_chart_7(void)
40 {
41     lv_obj_t * chart = lv_chart_create(lv_scr_act());
42     lv_obj_set_size(chart, 200, 150);
43     lv_obj_align(chart, LV_ALIGN_CENTER, 0, 0);
44     lv_obj_add_event_cb(chart, draw_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);
45     lv_obj_set_style_line_width(chart, 0, LV_PART_ITEMS);   /*Remove the lines*/
46 
47     lv_chart_set_type(chart, LV_CHART_TYPE_SCATTER);
48 
49     lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_X, 5, 5, 5, 1, true, 30);
50     lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_Y, 10, 5, 6, 5, true, 50);
51 
52     lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_X, 0, 200);
53     lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 1000);
54 
55     lv_chart_set_point_count(chart, 50);
56 
57     lv_chart_series_t * ser = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
58     uint32_t i;
59     for(i = 0; i < 50; i++) {
60         lv_chart_set_next_value2(chart, ser, lv_rand(0, 200), lv_rand(0, 1000));
61     }
62 
63     lv_timer_create(add_data, 100, chart);
64 }
65 
66 #endif
67