Skip to content

[BUG] Scale widget: horizontal/vertical section line draws edge-to-edge (missing x/y coordinate storage) #10226

@coreywilner

Description

@coreywilner

LVGL version

9.2.0

Platform

Infineon Modus Toolbox 3.7, PSOC Edge E84 MCU, EEZ Studio generated UI
file: src/widgets/scale/lv_scale.c

What happened?

What do you want to achieve?

Use lv_scale_add_section() with a LV_PART_MAIN style on a horizontal scale (LV_SCALE_MODE_HORIZONTAL_BOTTOM) to highlight a range (e.g., 25–75) with a colored line segment.

What happened instead?

The section's main line renders from edge to edge of the screen instead of being bounded to the section's tick range. The same bug affects LV_SCALE_MODE_HORIZONTAL_TOP and vertical modes (for the .x and .y coordinates respectively, depending on orientation).

How to reproduce?

lv_obj_t *scale = lv_scale_create(lv_screen_active());
lv_obj_set_pos(scale, 100, 100);
lv_obj_set_size(scale, 240, 40);
lv_scale_set_mode(scale, LV_SCALE_MODE_HORIZONTAL_BOTTOM);
lv_scale_set_range(scale, 0, 100);
lv_scale_set_total_tick_count(scale, 41);
lv_scale_set_major_tick_every(scale, 10);
lv_scale_set_label_show(scale, true);

lv_scale_section_t *section = lv_scale_add_section(scale);
lv_scale_section_set_range(section, 25, 75);

static lv_style_t section_style;
lv_style_init(&section_style);
lv_style_set_line_width(&section_style, 10);
lv_style_set_line_color(&section_style, lv_color_hex(0x0000FF));
lv_scale_section_set_style(section, LV_PART_MAIN, &section_style);

...The blue section line will render across the entire screen width instead of only between ticks 25 and 75.

Root cause

In src/widgets/scale/lv_scale.c, the function scale_store_main_line_tick_width_compensation() (around line 1454) only stores the .y coordinate of the tick positions:

section->first_tick_in_section.y = tick_point_a->y;  // line 1462
section->last_tick_in_section.y = tick_point_a->y;   // line 1482

The .x coordinate is never stored, so it remains 0. When the section line is drawn for horizontal scales (around line 840), it reads:

section_point_a.x = section->first_tick_in_section.x - first_tick_width_halved;  // = 0
section_point_b.x = section->last_tick_in_section.x + last_tick_width_halved;    // = 0

This results in the line being drawn at x≈0 on both ends, spanning the screen.

Suggested fix

Store both coordinates:

-            section->first_tick_in_section.y = tick_point_a->y;
+            section->first_tick_in_section.x = tick_point_a->x;
+            section->first_tick_in_section.y = tick_point_a->y;
-            section->last_tick_in_section.y = tick_point_a->y;
+            section->last_tick_in_section.x = tick_point_a->x;
+            section->last_tick_in_section.y = tick_point_a->y;

This fixes both horizontal scales (which need .x) and ensures vertical scales (which need .y) continue to work correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions