Scale (lv_scale)
A linear or circular scale with ticks, labels, ranges and sections.
Overview
The Scale draws a linear or circular scale with a configurable range, tick counts and placement, optional labels, and styled sections for highlighting sub-ranges.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_scale.c
*/
#include "../lv_examples.h"
/**
* @title Scale
* @brief A round and a horizontal scale, each with a coloured danger section.
*
* The first scale uses `mode="round_inner"` to draw a circular scale; the
* second uses `mode="horizontal_bottom"`. `total_tick_count`, `major_tick_every`
* (and `angle_range` on the round one) shape the ticks, and `label_show` prints
* values. On each, a `<lv_scale-section>` restyles the 80–100 range via named
* styles to mark a danger zone.
*/
void lv_example_scale(void)
{
static lv_style_t style_scale_main;
static lv_style_t style_scale_minor_ticks;
static lv_style_t style_scale_major_ticks;
static lv_style_t style_danger_line;
static lv_style_t style_danger_ind;
static lv_style_t style_danger_items;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_scale_main);
lv_style_init(&style_scale_minor_ticks);
lv_style_init(&style_scale_major_ticks);
lv_style_init(&style_danger_line);
lv_style_init(&style_danger_ind);
lv_style_init(&style_danger_items);
lv_style_set_bg_color(&style_scale_main, lv_color_hex(0x0f172a));
lv_style_set_bg_opa(&style_scale_main, (255 * 100 / 100));
lv_style_set_pad_all(&style_scale_main, 14);
lv_style_set_arc_color(&style_scale_main, lv_color_hex(0x64748b));
lv_style_set_line_color(&style_scale_main, lv_color_hex(0x64748b));
lv_style_set_text_color(&style_scale_main, lv_color_hex(0xcbd5e1));
lv_style_set_line_color(&style_scale_minor_ticks, lv_color_hex(0x64748b));
lv_style_set_length(&style_scale_minor_ticks, 6);
lv_style_set_line_color(&style_scale_major_ticks, lv_color_hex(0x3577d3));
lv_style_set_line_width(&style_scale_major_ticks, 3);
lv_style_set_length(&style_scale_major_ticks, 10);
lv_style_set_line_color(&style_danger_line, lv_color_hex(0xef4444));
lv_style_set_arc_color(&style_danger_line, lv_color_hex(0xef4444));
lv_style_set_line_width(&style_danger_line, 4);
lv_style_set_line_color(&style_danger_ind, lv_color_hex(0xef4444));
lv_style_set_line_width(&style_danger_ind, 4);
lv_style_set_text_color(&style_danger_ind, lv_color_hex(0xef4444));
lv_style_set_line_color(&style_danger_items, lv_color_hex(0xf87171));
lv_style_set_line_width(&style_danger_items, 3);
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_t * lv_scale_0 = lv_scale_create(screen);
lv_obj_set_size(lv_scale_0, 170, 170);
lv_obj_set_y(lv_scale_0, -40);
lv_obj_set_align(lv_scale_0, LV_ALIGN_CENTER);
lv_scale_set_mode(lv_scale_0, LV_SCALE_MODE_ROUND_INNER);
lv_scale_set_min_value(lv_scale_0, 0);
lv_scale_set_max_value(lv_scale_0, 100);
lv_scale_set_total_tick_count(lv_scale_0, 21);
lv_scale_set_major_tick_every(lv_scale_0, 5);
lv_scale_set_angle_range(lv_scale_0, 270);
lv_scale_set_rotation(lv_scale_0, 135);
lv_scale_set_label_show(lv_scale_0, true);
lv_obj_set_style_radius(lv_scale_0, 100, 0);
lv_obj_add_style(lv_scale_0, &style_scale_main, 0);
lv_obj_add_style(lv_scale_0, &style_scale_minor_ticks, LV_PART_ITEMS);
lv_obj_add_style(lv_scale_0, &style_scale_major_ticks, LV_PART_INDICATOR);
lv_scale_section_t * lv_scale_section_0 = lv_scale_add_section(lv_scale_0);
lv_scale_set_section_min_value(lv_scale_0, lv_scale_section_0, 80);
lv_scale_set_section_max_value(lv_scale_0, lv_scale_section_0, 100);
lv_scale_set_section_style_main(lv_scale_0, lv_scale_section_0, &style_danger_line);
lv_scale_set_section_style_indicator(lv_scale_0, lv_scale_section_0, &style_danger_ind);
lv_scale_set_section_style_items(lv_scale_0, lv_scale_section_0, &style_danger_items);
lv_obj_t * lv_scale_1 = lv_scale_create(screen);
lv_obj_set_size(lv_scale_1, 220, 60);
lv_obj_set_y(lv_scale_1, 80);
lv_obj_set_align(lv_scale_1, LV_ALIGN_CENTER);
lv_scale_set_mode(lv_scale_1, LV_SCALE_MODE_HORIZONTAL_BOTTOM);
lv_scale_set_min_value(lv_scale_1, 0);
lv_scale_set_max_value(lv_scale_1, 100);
lv_scale_set_total_tick_count(lv_scale_1, 21);
lv_scale_set_major_tick_every(lv_scale_1, 5);
lv_scale_set_label_show(lv_scale_1, true);
lv_obj_set_style_radius(lv_scale_1, 10, 0);
lv_obj_set_style_transform_rotation(lv_scale_1, 450, LV_PART_INDICATOR);
lv_obj_set_style_pad_bottom(lv_scale_1, 6, LV_PART_INDICATOR);
lv_obj_add_style(lv_scale_1, &style_scale_main, 0);
lv_obj_add_style(lv_scale_1, &style_scale_minor_ticks, LV_PART_ITEMS);
lv_obj_add_style(lv_scale_1, &style_scale_major_ticks, LV_PART_INDICATOR);
lv_scale_section_t * lv_scale_section_1 = lv_scale_add_section(lv_scale_1);
lv_scale_set_section_min_value(lv_scale_1, lv_scale_section_1, 80);
lv_scale_set_section_max_value(lv_scale_1, lv_scale_section_1, 100);
lv_scale_set_section_style_main(lv_scale_1, lv_scale_section_1, &style_danger_line);
lv_scale_set_section_style_indicator(lv_scale_1, lv_scale_section_1, &style_danger_ind);
lv_scale_set_section_style_items(lv_scale_1, lv_scale_section_1, &style_danger_items);
}
<!--
@title Scale
@brief A round and a horizontal scale, each with a coloured danger section.
The first scale uses `mode="round_inner"` to draw a circular scale; the
second uses `mode="horizontal_bottom"`. `total_tick_count`, `major_tick_every`
(and `angle_range` on the round one) shape the ticks, and `label_show` prints
values. On each, a `<lv_scale-section>` restyles the 80–100 range via named
styles to mark a danger zone.
-->
<screen>
<styles>
<style
name="style_scale_main"
bg_color="0x0f172a"
bg_opa="100%"
pad_all="14"
arc_color="0x64748b"
line_color="0x64748b"
text_color="0xcbd5e1"
/>
<style name="style_scale_minor_ticks" line_color="0x64748b" length="6" />
<style name="style_scale_major_ticks" line_color="0x3577d3" line_width="3" length="10" />
<!-- Danger section styling -->
<style name="style_danger_line" line_color="0xef4444" arc_color="0xef4444" line_width="4" />
<style name="style_danger_ind" line_color="0xef4444" line_width="4" text_color="0xef4444" />
<style name="style_danger_items" line_color="0xf87171" line_width="3" />
</styles>
<view>
<lv_scale
width="170"
height="170"
y="-40"
align="center"
mode="round_inner"
min_value="0"
max_value="100"
total_tick_count="21"
major_tick_every="5"
angle_range="270"
rotation="135"
label_show="true"
style_radius="100"
>
<style name="style_scale_main" />
<style name="style_scale_minor_ticks" selector="items" />
<style name="style_scale_major_ticks" selector="indicator" />
<lv_scale-section
min_value="80"
max_value="100"
style_main="style_danger_line"
style_indicator="style_danger_ind"
style_items="style_danger_items"
/>
</lv_scale>
<lv_scale
width="220"
height="60"
y="80"
align="center"
mode="horizontal_bottom"
min_value="0"
max_value="100"
total_tick_count="21"
major_tick_every="5"
label_show="true"
style_radius="10"
style_transform_rotation-indicator="450"
style_pad_bottom-indicator="6"
>
<style name="style_scale_main" />
<style name="style_scale_minor_ticks" selector="items" />
<style name="style_scale_major_ticks" selector="indicator" />
<lv_scale-section
min_value="80"
max_value="100"
style_main="style_danger_line"
style_indicator="style_danger_ind"
style_items="style_danger_items"
/>
</lv_scale>
</view>
</screen>
Enumerations
lv_scale_mode — How the scale is drawn
| Value | Description |
|---|---|
horizontal_top | Ticks on top |
horizontal_bottom | Ticks on bottom |
vertical_left | Ticks on left |
vertical_right | Ticks on right |
round_inner | Circular, ticks inside |
round_outer | Circular, ticks outside |
Properties
Parts
Style parts of the widget with local style (e.g. style_bg_color-knob="0xff0000") or with style sheets (e.g. <style name="style_knob" selector="knob">). See Styles to learn more.
| Part | Description |
|---|---|
main | The main rail: on straight scales a line (line_color, line_width, line_opa, line_rounded); on round scales an arc (arc_color, arc_width, arc_opa, arc_rounded, arc_image_src). |
items | The minor ticks (always drawn as lines): line properties; length sets the tick length and radial_offset shifts them on round scales. |
indicator | The major ticks and their labels: line properties for the ticks (length, radial_offset) plus text properties for the labels; pad_radial sets the label gap. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
mode | enum:lv_scale_mode | Set the scale mode |
total_tick_count | int | Set number of ticks |
major_tick_every | int | Set frequency of major ticks |
label_show | bool | Show or hide labels |
min_value | int | Set minimum value |
max_value | int | Set maximum value |
angle_range | deg | Set angular range (degrees) |
rotation | deg | Rotate the scale (degrees) |
text_src | string[NULL] | Set custom labels from text |
post_draw | bool | Enable post-draw events |
draw_ticks_on_top | bool | Draw ticks above other content |
<lv_scale-section>
Add a styled section
access add · returns lv_scale_section
| Name | Kind | Type | Description |
|---|---|---|---|
min_value | prop | int | Set section min |
max_value | prop | int | Set section max |
style_main | prop | style | Set main line style |
style_indicator | prop | style | Set major line and text style |
style_items | prop | style | Set minor ticks' style |
bind_min_value | prop | subject | Bind section min to subject |
bind_max_value | prop | subject | Bind section max to subject |
Last updated on