Charts · Round 1.28" touch watch
Widget gallery 3 - Charts: line, area, bar, scatter (swipe between them)
You’ll learn: line, bar and scatter charts
Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Widget gallery. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.
# Widget gallery 3 - Charts: line, area, bar, scatter (swipe between them)
import math
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
BG = 0x05110A
PANEL = 0x0C1A12
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
BLUE = 0x6CB8FF
AMBER = 0xF2B84B
VIOLET = 0xB593FF
keep = [] # styles, buffers and callbacks LVGL uses must stay referenced
FONTS = {12: lv.font_montserrat_12, 14: lv.font_montserrat_14, 16: lv.font_montserrat_16}
def label(parent, text="", color=TEXT, font=16, align=lv.ALIGN.CENTER, x=0, y=0, zoom=0):
"""lv.label with a colour and a Montserrat 12/14/16 font. zoom=2 draws it twice as big."""
lb = lv.label(parent)
lb.set_text(text)
lb.set_style_text_color(lv.color_hex(color), 0)
lb.set_style_text_font(FONTS.get(font, lv.font_montserrat_16), 0)
if zoom:
lb.set_style_transform_scale(int(256 * zoom), 0) # 256 = 100 %
lb.update_layout()
lb.set_style_transform_pivot_x(lb.get_width() // 2, 0)
lb.set_style_transform_pivot_y(lb.get_height() // 2, 0)
lb.align(align, int(x), int(y))
return lb
def anim(obj, start, end, ms, setter, path=None, repeat=1, playback=0, delay=0, done=None):
"""Animate start -> end over ms, calling setter(obj, value). repeat=-1 loops forever."""
a = lv.anim_t()
a.init()
a.set_var(obj)
a.set_values(start, end)
a.set_duration(ms)
a.set_delay(delay)
a.set_custom_exec_cb(lambda _a, v: setter(obj, v))
a.set_path_cb(path or lv.anim_t.path_ease_in_out)
if repeat < 0:
a.set_repeat_count(lv.ANIM_REPEAT_INFINITE)
elif repeat > 1:
a.set_repeat_count(repeat)
if playback:
a.set_reverse_duration(playback) # play back to start
if done:
a.set_completed_cb(lambda _a: done())
a.start()
keep.append(a)
return a
scr = lv.screen_active() # the screen LVGL is showing
scr.clean() # remove what the last program drew
scr.set_style_bg_color(lv.color_hex(BG), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)
tv = lv.tileview(scr)
tv.set_style_bg_opa(lv.OPA.TRANSP, 0)
C = lv.chart
def make_chart(tile, kind, title):
label(tile, title, MUTED, 12, y=-82)
ch = lv.chart(tile)
ch.set_size(180, 120)
ch.align(lv.ALIGN.CENTER, 0, 6)
ch.set_type(kind)
ch.set_style_bg_color(lv.color_hex(PANEL), 0)
ch.set_style_border_width(0, 0)
ch.set_style_line_color(lv.color_hex(0x1E3A2A), 0) # division lines
ch.set_div_line_count(4, 5)
return ch
# 1. Line chart with smooth sine waves that scroll
ch1 = make_chart(tv.add_tile(0, 0, lv.DIR.RIGHT), C.TYPE.LINE, "line chart (live)")
ch1.set_point_count(30)
ch1.set_axis_range(C.AXIS.PRIMARY_Y, -100, 100)
ch1.set_update_mode(C.UPDATE_MODE.SHIFT)
ch1.set_style_size(0, 0, lv.PART.INDICATOR)
ch1.set_style_line_width(3, lv.PART.ITEMS)
s1 = ch1.add_series(lv.color_hex(ACCENT), C.AXIS.PRIMARY_Y)
s2 = ch1.add_series(lv.color_hex(BLUE), C.AXIS.PRIMARY_Y)
phase = [0.0]
def wave(_):
phase[0] += 0.35
ch1.set_next_value(s1, int(math.sin(phase[0]) * 80))
ch1.set_next_value(s2, int(math.cos(phase[0] * 0.7) * 50))
wave_timer = lv.timer_create(wave, 80, None)
# 2. Bar chart (weekly steps) that grows in
ch2 = make_chart(tv.add_tile(1, 0, lv.DIR.LEFT | lv.DIR.RIGHT), C.TYPE.BAR, "bar chart")
ch2.set_point_count(7)
ch2.set_axis_range(C.AXIS.PRIMARY_Y, 0, 100)
bars = ch2.add_series(lv.color_hex(AMBER), C.AXIS.PRIMARY_Y)
steps = [45, 62, 38, 90, 74, 55, 81]
def grow(o, v):
for i, s in enumerate(steps):
ch2.set_series_value_by_id(bars, i, s * v // 100) # LVGL 9.4 name
ch2.refresh()
anim(ch2, 0, 100, 1200, grow, path=lv.anim_t.path_ease_out)
# 3. Scatter chart: points on a circle with noise
ch3 = make_chart(tv.add_tile(2, 0, lv.DIR.LEFT), C.TYPE.SCATTER, "scatter chart")
ch3.set_point_count(40)
ch3.set_axis_range(C.AXIS.PRIMARY_X, -100, 100)
ch3.set_axis_range(C.AXIS.PRIMARY_Y, -100, 100)
ch3.set_style_line_width(0, lv.PART.ITEMS)
ch3.set_style_size(5, 5, lv.PART.INDICATOR)
sc = ch3.add_series(lv.color_hex(VIOLET), C.AXIS.PRIMARY_X)
k = [0]
def scatter(_):
k[0] += 1
a = k[0] * 0.5
r = 60 + lv.rand(0, 30)
ch3.set_next_value2(sc, int(math.cos(a) * r), int(math.sin(a) * r))
scatter_timer = lv.timer_create(scatter, 60, None)
print("Swipe: live line chart, animated bar chart, scatter chart.")
Comments