1 #include "../../lv_examples.h"
2 #if LV_USE_CHART && LV_BUILD_EXAMPLES
3
4 /**
5 * Use lv_scale to add ticks to a scrollable chart
6 */
lv_example_chart_2(void)7 void lv_example_chart_2(void)
8 {
9 /*Create a container*/
10 lv_obj_t * main_cont = lv_obj_create(lv_screen_active());
11 lv_obj_set_size(main_cont, 200, 150);
12 lv_obj_center(main_cont);
13
14 /*Create a transparent wrapper for the chart and the scale.
15 *Set a large width, to make it scrollable on the main container*/
16 lv_obj_t * wrapper = lv_obj_create(main_cont);
17 lv_obj_remove_style_all(wrapper);
18 lv_obj_set_size(wrapper, lv_pct(300), lv_pct(100));
19 lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
20
21 /*Create a chart on the wrapper
22 *Set it's width to 100% to fill the large wrapper*/
23 lv_obj_t * chart = lv_chart_create(wrapper);
24 lv_obj_set_width(chart, lv_pct(100));
25 lv_obj_set_flex_grow(chart, 1);
26 lv_chart_set_type(chart, LV_CHART_TYPE_BAR);
27 lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
28 lv_chart_set_range(chart, LV_CHART_AXIS_SECONDARY_Y, 0, 400);
29 lv_chart_set_point_count(chart, 12);
30 lv_obj_set_style_radius(chart, 0, 0);
31
32 /*Create a scale also with 100% width*/
33 lv_obj_t * scale_bottom = lv_scale_create(wrapper);
34 lv_scale_set_mode(scale_bottom, LV_SCALE_MODE_HORIZONTAL_BOTTOM);
35 lv_obj_set_size(scale_bottom, lv_pct(100), 25);
36 lv_scale_set_total_tick_count(scale_bottom, 12);
37 lv_scale_set_major_tick_every(scale_bottom, 1);
38 lv_obj_set_style_pad_hor(scale_bottom, lv_chart_get_first_point_center_offset(chart), 0);
39
40 static const char * month[] = {"Jan", "Febr", "March", "Apr", "May", "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec", NULL};
41 lv_scale_set_text_src(scale_bottom, month);
42
43 /*Add two data series*/
44 lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_lighten(LV_PALETTE_GREEN, 2), LV_CHART_AXIS_PRIMARY_Y);
45 lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_darken(LV_PALETTE_GREEN, 2), LV_CHART_AXIS_PRIMARY_Y);
46
47 /*Set the next points on 'ser1'*/
48 uint32_t i;
49 for(i = 0; i < 12; i++) {
50 lv_chart_set_next_value(chart, ser1, lv_rand(10, 60));
51 lv_chart_set_next_value(chart, ser2, lv_rand(50, 90));
52 }
53 lv_chart_refresh(chart); /*Required after direct set*/
54 }
55
56 #endif
57