1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00
lvgl/examples/widgets/chart/lv_example_chart_4.c

46 lines
1.5 KiB
C
Raw Normal View History

2021-04-08 13:07:48 +02:00
#include "../../lv_examples.h"
2021-02-08 09:53:03 +01:00
2023-10-16 23:43:27 +02:00
#if LV_USE_CHART && LV_DRAW_SW_COMPLEX && LV_BUILD_EXAMPLES
2021-02-08 09:53:03 +01:00
2023-10-16 23:43:27 +02:00
static void draw_event_cb(lv_event_t * e)
2021-02-08 09:53:03 +01:00
{
2023-10-16 23:43:27 +02:00
lv_draw_task_t * draw_task = lv_event_get_draw_task(e);
lv_draw_dsc_base_t * base_dsc = draw_task->draw_dsc;
2023-10-16 23:43:27 +02:00
if(base_dsc->part == LV_PART_ITEMS && draw_task->type == LV_DRAW_TASK_TYPE_FILL) {
lv_draw_fill_dsc_t * fill_dsc = draw_task->draw_dsc;
2021-02-08 09:53:03 +01:00
2023-10-16 23:43:27 +02:00
lv_obj_t * chart = lv_event_get_target(e);
int32_t * y_array = lv_chart_get_y_array(chart, lv_chart_get_series_next(chart, NULL));
int32_t v = y_array[base_dsc->id2];
2021-02-08 09:53:03 +01:00
2023-10-16 23:43:27 +02:00
uint32_t ratio = v * 255 / 100;
fill_dsc->color = lv_color_mix(lv_palette_main(LV_PALETTE_GREEN), lv_palette_main(LV_PALETTE_RED), ratio);
}
2021-02-08 09:53:03 +01:00
}
/**
2023-10-16 23:43:27 +02:00
* Recolor the bars of a chart based on their value
2021-02-08 09:53:03 +01:00
*/
void lv_example_chart_4(void)
{
2023-10-16 23:43:27 +02:00
/*Create a chart1*/
lv_obj_t * chart = lv_chart_create(lv_screen_active());
2023-10-16 23:43:27 +02:00
lv_chart_set_type(chart, LV_CHART_TYPE_BAR);
lv_chart_set_point_count(chart, 24);
lv_obj_set_style_pad_column(chart, 2, 0);
lv_obj_set_size(chart, 260, 160);
lv_obj_center(chart);
2021-02-08 09:53:03 +01:00
2023-10-16 23:43:27 +02:00
lv_chart_series_t * ser = lv_chart_add_series(chart, lv_color_hex(0xff0000), LV_CHART_AXIS_PRIMARY_Y);
lv_obj_add_event_cb(chart, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
2023-10-16 23:43:27 +02:00
lv_obj_add_flag(chart, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
2021-02-08 09:53:03 +01:00
uint32_t i;
2023-10-16 23:43:27 +02:00
for(i = 0; i < 24; i++) {
lv_chart_set_next_value(chart, ser, lv_rand(10, 90));
2021-02-08 09:53:03 +01:00
}
}
#endif