1def draw_event_cb(e):
2
3    obj = e.get_target()
4
5    # Add the faded area before the lines are drawn
6    dsc = lv.obj_draw_part_dsc_t.__cast__(e.get_param())
7    if dsc.part != lv.PART.ITEMS:
8        return
9    if not dsc.p1 or not dsc.p2:
10        return
11
12    # Add a line mask that keeps the area below the line
13    line_mask_param = lv.draw_mask_line_param_t()
14    line_mask_param.points_init(dsc.p1.x, dsc.p1.y, dsc.p2.x, dsc.p2.y, lv.DRAW_MASK_LINE_SIDE.BOTTOM)
15    # line_mask_id = line_mask_param.draw_mask_add(None)
16    line_mask_id = lv.draw_mask_add(line_mask_param, None)
17    # Add a fade effect: transparent bottom covering top
18    h = obj.get_height()
19    fade_mask_param = lv.draw_mask_fade_param_t()
20    coords = lv.area_t()
21    obj.get_coords(coords)
22    fade_mask_param.init(coords, lv.OPA.COVER, coords.y1 + h // 8, lv.OPA.TRANSP,coords.y2)
23    fade_mask_id = lv.draw_mask_add(fade_mask_param,None)
24
25    # Draw a rectangle that will be affected by the mask
26    draw_rect_dsc = lv.draw_rect_dsc_t()
27    draw_rect_dsc.init()
28    draw_rect_dsc.bg_opa = lv.OPA._20
29    draw_rect_dsc.bg_color = dsc.line_dsc.color
30
31    a = lv.area_t()
32    a.x1 = dsc.p1.x
33    a.x2 = dsc.p2.x - 1
34    a.y1 = min(dsc.p1.y, dsc.p2.y)
35    coords = lv.area_t()
36    obj.get_coords(coords)
37    a.y2 = coords.y2
38    dsc.draw_ctx.rect(draw_rect_dsc, a)
39
40    # Remove the masks
41    lv.draw_mask_remove_id(line_mask_id)
42    lv.draw_mask_remove_id(fade_mask_id)
43
44
45def add_data(timer):
46    # LV_UNUSED(timer);
47    cnt = 0
48    chart1.set_next_value(ser1, lv.rand(20, 90))
49
50    if cnt % 4 == 0:
51        chart1.set_next_value(ser2, lv.rand(40, 60))
52
53    cnt +=1
54
55#
56# Add a faded area effect to the line chart
57#
58
59# Create a chart1
60chart1 = lv.chart(lv.scr_act())
61chart1.set_size(200, 150)
62chart1.center()
63chart1.set_type(lv.chart.TYPE.LINE)    # Show lines and points too
64
65chart1.add_event_cb(draw_event_cb, lv.EVENT.DRAW_PART_BEGIN, None)
66chart1.set_update_mode(lv.chart.UPDATE_MODE.CIRCULAR)
67
68# Add two data series
69ser1 = chart1.add_series(lv.palette_main(lv.PALETTE.RED), lv.chart.AXIS.PRIMARY_Y)
70ser2 = chart1.add_series(lv.palette_main(lv.PALETTE.BLUE), lv.chart.AXIS.SECONDARY_Y)
71
72for i in range(10):
73    chart1.set_next_value(ser1, lv.rand(20, 90))
74    chart1.set_next_value(ser2, lv.rand(30, 70))
75
76timer = lv.timer_create(add_data, 200, None)
77