1 #include "../../lv_examples.h"
2 #if LV_USE_CHART && LV_BUILD_EXAMPLES
3 
4 static lv_obj_t * chart;
5 static lv_chart_series_t * ser;
6 static lv_chart_cursor_t * cursor;
7 
event_cb(lv_event_t * e)8 static void event_cb(lv_event_t * e)
9 {
10     static int32_t last_id = -1;
11     lv_event_code_t code = lv_event_get_code(e);
12     lv_obj_t * obj = lv_event_get_target(e);
13 
14     if(code == LV_EVENT_VALUE_CHANGED) {
15         last_id = lv_chart_get_pressed_point(obj);
16         if(last_id != LV_CHART_POINT_NONE) {
17             lv_chart_set_cursor_point(obj, cursor, NULL, last_id);
18         }
19     }
20     else if(code == LV_EVENT_DRAW_PART_END) {
21         lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
22         if(!lv_obj_draw_part_check_type(dsc, &lv_chart_class, LV_CHART_DRAW_PART_CURSOR)) return;
23         if(dsc->p1 == NULL || dsc->p2 == NULL || dsc->p1->y != dsc->p2->y || last_id < 0) return;
24 
25         lv_coord_t * data_array = lv_chart_get_y_array(chart, ser);
26         lv_coord_t v = data_array[last_id];
27         char buf[16];
28         lv_snprintf(buf, sizeof(buf), "%d", v);
29 
30         lv_point_t size;
31         lv_txt_get_size(&size, buf, LV_FONT_DEFAULT, 0, 0, LV_COORD_MAX, LV_TEXT_FLAG_NONE);
32 
33         lv_area_t a;
34         a.y2 = dsc->p1->y - 5;
35         a.y1 = a.y2 - size.y - 10;
36         a.x1 = dsc->p1->x + 10;
37         a.x2 = a.x1 + size.x + 10;
38 
39         lv_draw_rect_dsc_t draw_rect_dsc;
40         lv_draw_rect_dsc_init(&draw_rect_dsc);
41         draw_rect_dsc.bg_color = lv_palette_main(LV_PALETTE_BLUE);
42         draw_rect_dsc.radius = 3;
43 
44         lv_draw_rect(dsc->draw_ctx, &draw_rect_dsc, &a);
45 
46         lv_draw_label_dsc_t draw_label_dsc;
47         lv_draw_label_dsc_init(&draw_label_dsc);
48         draw_label_dsc.color = lv_color_white();
49         a.x1 += 5;
50         a.x2 -= 5;
51         a.y1 += 5;
52         a.y2 -= 5;
53         lv_draw_label(dsc->draw_ctx, &draw_label_dsc, &a, buf, NULL);
54     }
55 }
56 
57 /**
58  * Show cursor on the clicked point
59  */
lv_example_chart_6(void)60 void lv_example_chart_6(void)
61 {
62     chart = lv_chart_create(lv_scr_act());
63     lv_obj_set_size(chart, 200, 150);
64     lv_obj_align(chart, LV_ALIGN_CENTER, 0, -10);
65 
66     lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_Y, 10, 5, 6, 5, true, 40);
67     lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_X, 10, 5, 10, 1, true, 30);
68 
69     lv_obj_add_event_cb(chart, event_cb, LV_EVENT_ALL, NULL);
70     lv_obj_refresh_ext_draw_size(chart);
71 
72     cursor = lv_chart_add_cursor(chart, lv_palette_main(LV_PALETTE_BLUE), LV_DIR_LEFT | LV_DIR_BOTTOM);
73 
74     ser = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
75     uint32_t i;
76     for(i = 0; i < 10; i++) {
77         lv_chart_set_next_value(chart, ser, lv_rand(10, 90));
78     }
79 
80     lv_chart_set_zoom_x(chart, 500);
81 
82     lv_obj_t * label = lv_label_create(lv_scr_act());
83     lv_label_set_text(label, "Click on a point");
84     lv_obj_align_to(label, chart, LV_ALIGN_OUT_TOP_MID, 0, -5);
85 }
86 
87 #endif
88