docs(chart): update chart example
This commit is contained in:
@@ -1,85 +1,45 @@
|
||||
#include "../../lv_examples.h"
|
||||
#if LV_USE_CHART && LV_BUILD_EXAMPLES
|
||||
|
||||
#if LV_USE_CHART && LV_DRAW_SW_COMPLEX && LV_BUILD_EXAMPLES
|
||||
|
||||
static void event_cb(lv_event_t * e)
|
||||
static void draw_event_cb(lv_event_t * e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * chart = lv_event_get_target(e);
|
||||
lv_draw_task_t * draw_task = lv_event_get_draw_task(e);
|
||||
lv_draw_dsc_base_t * base_dsc = draw_task->draw_dsc;
|
||||
|
||||
if(code == LV_EVENT_VALUE_CHANGED) {
|
||||
lv_obj_invalidate(chart);
|
||||
}
|
||||
if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
|
||||
lv_coord_t * s = lv_event_get_param(e);
|
||||
*s = LV_MAX(*s, 20);
|
||||
}
|
||||
else if(code == LV_EVENT_DRAW_POST_END) {
|
||||
int32_t id = lv_chart_get_pressed_point(chart);
|
||||
if(id == LV_CHART_POINT_NONE) return;
|
||||
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;
|
||||
|
||||
LV_LOG_USER("Selected point %d", (int)id);
|
||||
lv_obj_t * chart = lv_event_get_target(e);
|
||||
lv_coord_t * y_array = lv_chart_get_y_array(chart, lv_chart_get_series_next(chart, NULL));
|
||||
lv_coord_t v = y_array[base_dsc->id2];
|
||||
|
||||
lv_chart_series_t * ser = lv_chart_get_series_next(chart, NULL);
|
||||
while(ser) {
|
||||
lv_point_t p;
|
||||
lv_chart_get_point_pos_by_id(chart, ser, id, &p);
|
||||
|
||||
lv_coord_t * y_array = lv_chart_get_y_array(chart, ser);
|
||||
lv_coord_t value = y_array[id];
|
||||
|
||||
char buf[16];
|
||||
lv_snprintf(buf, sizeof(buf), LV_SYMBOL_DUMMY"$%d", value);
|
||||
|
||||
lv_draw_rect_dsc_t draw_rect_dsc;
|
||||
lv_draw_rect_dsc_init(&draw_rect_dsc);
|
||||
draw_rect_dsc.bg_color = lv_color_black();
|
||||
draw_rect_dsc.bg_opa = LV_OPA_50;
|
||||
draw_rect_dsc.radius = 3;
|
||||
draw_rect_dsc.bg_image_src = buf;
|
||||
draw_rect_dsc.bg_image_recolor = lv_color_white();
|
||||
|
||||
lv_area_t a;
|
||||
a.x1 = chart->coords.x1 + p.x - 20;
|
||||
a.x2 = chart->coords.x1 + p.x + 20;
|
||||
a.y1 = chart->coords.y1 + p.y - 30;
|
||||
a.y2 = chart->coords.y1 + p.y - 10;
|
||||
|
||||
lv_layer_t * layer = lv_event_get_layer(e);
|
||||
lv_draw_rect(layer, &draw_rect_dsc, &a);
|
||||
|
||||
ser = lv_chart_get_series_next(chart, ser);
|
||||
}
|
||||
}
|
||||
else if(code == LV_EVENT_RELEASED) {
|
||||
lv_obj_invalidate(chart);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the value of the pressed points
|
||||
* Recolor the bars of a chart based on their value
|
||||
*/
|
||||
void lv_example_chart_4(void)
|
||||
{
|
||||
/*Create a chart*/
|
||||
lv_obj_t * chart;
|
||||
chart = lv_chart_create(lv_screen_active());
|
||||
lv_obj_set_size(chart, 200, 150);
|
||||
/*Create a chart1*/
|
||||
lv_obj_t * chart = lv_chart_create(lv_scr_act());
|
||||
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);
|
||||
|
||||
lv_obj_add_event(chart, event_cb, LV_EVENT_ALL, NULL);
|
||||
lv_obj_refresh_ext_draw_size(chart);
|
||||
lv_chart_series_t * ser = lv_chart_add_series(chart, lv_color_hex(0xff0000), LV_CHART_AXIS_PRIMARY_Y);
|
||||
lv_obj_add_event(chart, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
|
||||
lv_obj_add_flag(chart, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
|
||||
|
||||
/*Zoom in a little in X*/
|
||||
// lv_chart_set_zoom_x(chart, 800);
|
||||
|
||||
/*Add two data series*/
|
||||
lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
|
||||
lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_GREEN), LV_CHART_AXIS_PRIMARY_Y);
|
||||
uint32_t i;
|
||||
for(i = 0; i < 10; i++) {
|
||||
lv_chart_set_next_value(chart, ser1, lv_rand(60, 90));
|
||||
lv_chart_set_next_value(chart, ser2, lv_rand(10, 40));
|
||||
for(i = 0; i < 24; i++) {
|
||||
lv_chart_set_next_value(chart, ser, lv_rand(10, 90));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user