1 #include "../../lv_examples.h"
2 #if LV_USE_CHART && LV_BUILD_EXAMPLES
3
event_cb(lv_event_t * e)4 static void event_cb(lv_event_t * e)
5 {
6 lv_event_code_t code = lv_event_get_code(e);
7 lv_obj_t * chart = lv_event_get_target(e);
8
9 if(code == LV_EVENT_VALUE_CHANGED) {
10 lv_obj_invalidate(chart);
11 }
12 if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
13 lv_coord_t * s = lv_event_get_param(e);
14 *s = LV_MAX(*s, 20);
15 }
16 else if(code == LV_EVENT_DRAW_POST_END) {
17 int32_t id = lv_chart_get_pressed_point(chart);
18 if(id == LV_CHART_POINT_NONE) return;
19
20 LV_LOG_USER("Selected point %d", (int)id);
21
22 lv_chart_series_t * ser = lv_chart_get_series_next(chart, NULL);
23 while(ser) {
24 lv_point_t p;
25 lv_chart_get_point_pos_by_id(chart, ser, id, &p);
26
27 lv_coord_t * y_array = lv_chart_get_y_array(chart, ser);
28 lv_coord_t value = y_array[id];
29
30 char buf[16];
31 lv_snprintf(buf, sizeof(buf), LV_SYMBOL_DUMMY"$%d", value);
32
33 lv_draw_rect_dsc_t draw_rect_dsc;
34 lv_draw_rect_dsc_init(&draw_rect_dsc);
35 draw_rect_dsc.bg_color = lv_color_black();
36 draw_rect_dsc.bg_opa = LV_OPA_50;
37 draw_rect_dsc.radius = 3;
38 draw_rect_dsc.bg_img_src = buf;
39 draw_rect_dsc.bg_img_recolor = lv_color_white();
40
41 lv_area_t a;
42 a.x1 = chart->coords.x1 + p.x - 20;
43 a.x2 = chart->coords.x1 + p.x + 20;
44 a.y1 = chart->coords.y1 + p.y - 30;
45 a.y2 = chart->coords.y1 + p.y - 10;
46
47 lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
48 lv_draw_rect(draw_ctx, &draw_rect_dsc, &a);
49
50 ser = lv_chart_get_series_next(chart, ser);
51 }
52 }
53 else if(code == LV_EVENT_RELEASED) {
54 lv_obj_invalidate(chart);
55 }
56 }
57
58 /**
59 * Show the value of the pressed points
60 */
lv_example_chart_4(void)61 void lv_example_chart_4(void)
62 {
63 /*Create a chart*/
64 lv_obj_t * chart;
65 chart = lv_chart_create(lv_scr_act());
66 lv_obj_set_size(chart, 200, 150);
67 lv_obj_center(chart);
68
69 lv_obj_add_event_cb(chart, event_cb, LV_EVENT_ALL, NULL);
70 lv_obj_refresh_ext_draw_size(chart);
71
72 /*Zoom in a little in X*/
73 lv_chart_set_zoom_x(chart, 800);
74
75 /*Add two data series*/
76 lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
77 lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_GREEN), LV_CHART_AXIS_PRIMARY_Y);
78 uint32_t i;
79 for(i = 0; i < 10; i++) {
80 lv_chart_set_next_value(chart, ser1, lv_rand(60, 90));
81 lv_chart_set_next_value(chart, ser2, lv_rand(10, 40));
82 }
83 }
84
85 #endif
86