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(!lv_obj_draw_part_check_type(dsc, &lv_chart_class, LV_CHART_DRAW_PART_TICK_LABEL)) return;
8
9 if(dsc->id == LV_CHART_AXIS_PRIMARY_X && dsc->text) {
10 const char * month[] = {"Jan", "Febr", "March", "Apr", "May", "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
11 lv_snprintf(dsc->text, dsc->text_length, "%s", month[dsc->value]);
12 }
13 }
14
15 /**
16 * Add ticks and labels to the axis and demonstrate scrolling
17 */
lv_example_chart_3(void)18 void lv_example_chart_3(void)
19 {
20 /*Create a chart*/
21 lv_obj_t * chart;
22 chart = lv_chart_create(lv_scr_act());
23 lv_obj_set_size(chart, 200, 150);
24 lv_obj_center(chart);
25 lv_chart_set_type(chart, LV_CHART_TYPE_BAR);
26 lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
27 lv_chart_set_range(chart, LV_CHART_AXIS_SECONDARY_Y, 0, 400);
28 lv_chart_set_point_count(chart, 12);
29 lv_obj_add_event_cb(chart, draw_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);
30
31 /*Add ticks and label to every axis*/
32 lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_X, 10, 5, 12, 3, true, 40);
33 lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_Y, 10, 5, 6, 2, true, 50);
34 lv_chart_set_axis_tick(chart, LV_CHART_AXIS_SECONDARY_Y, 10, 5, 3, 4, true, 50);
35
36 /*Zoom in a little in X*/
37 lv_chart_set_zoom_x(chart, 800);
38
39 /*Add two data series*/
40 lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_lighten(LV_PALETTE_GREEN, 2), LV_CHART_AXIS_PRIMARY_Y);
41 lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_darken(LV_PALETTE_GREEN, 2),
42 LV_CHART_AXIS_SECONDARY_Y);
43
44 /*Set the next points on 'ser1'*/
45 lv_chart_set_next_value(chart, ser1, 31);
46 lv_chart_set_next_value(chart, ser1, 66);
47 lv_chart_set_next_value(chart, ser1, 10);
48 lv_chart_set_next_value(chart, ser1, 89);
49 lv_chart_set_next_value(chart, ser1, 63);
50 lv_chart_set_next_value(chart, ser1, 56);
51 lv_chart_set_next_value(chart, ser1, 32);
52 lv_chart_set_next_value(chart, ser1, 35);
53 lv_chart_set_next_value(chart, ser1, 57);
54 lv_chart_set_next_value(chart, ser1, 85);
55 lv_chart_set_next_value(chart, ser1, 22);
56 lv_chart_set_next_value(chart, ser1, 58);
57
58 lv_coord_t * ser2_array = lv_chart_get_y_array(chart, ser2);
59 /*Directly set points on 'ser2'*/
60 ser2_array[0] = 92;
61 ser2_array[1] = 71;
62 ser2_array[2] = 61;
63 ser2_array[3] = 15;
64 ser2_array[4] = 21;
65 ser2_array[5] = 35;
66 ser2_array[6] = 35;
67 ser2_array[7] = 58;
68 ser2_array[8] = 31;
69 ser2_array[9] = 53;
70 ser2_array[10] = 33;
71 ser2_array[11] = 73;
72
73 lv_chart_refresh(chart); /*Required after direct set*/
74 }
75
76 #endif
77