Bar (lv_bar)
A background with an indicator whose length represents a value within a range.
Overview
The Bar has a background and an indicator whose length represents the current value within a range. Both the start and end values can be set, so the indicator can grow from any point, and it animates between values.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_bar.c
*/
#include "../lv_examples.h"
/**
* @title Bar
* @brief Bound, range, reversed-with-gradient and vertical bars.
*
* The first bar is bound to `subject_value` and overlays a live readout. The
* second uses `mode="range"` with a `start_value` and `value` to show a span.
* The third sets `min_value` above `max_value` so it fills right-to-left, with
* a gradient indicator. The last two are vertical, the second of them also
* reversed.
*/
void lv_example_bar(void)
{
static lv_style_t style_bar;
static lv_style_t style_bar_ind;
static lv_subject_t subject_value;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_bar);
lv_style_init(&style_bar_ind);
lv_style_set_bg_color(&style_bar, lv_color_hex(0xdee2e7));
lv_style_set_bg_opa(&style_bar, (255 * 100 / 100));
lv_style_set_radius(&style_bar, 8);
lv_style_set_outline_width(&style_bar, 1);
lv_style_set_outline_color(&style_bar, lv_color_hex(0x3b82f6));
lv_style_set_outline_opa(&style_bar, (255 * 90 / 100));
lv_style_set_pad_all(&style_bar, 3);
lv_style_set_bg_color(&style_bar_ind, lv_color_hex(0x3b82f6));
lv_style_set_radius(&style_bar_ind, 6);
lv_subject_init_int(&subject_value, 50);
lv_subject_set_min_value_int(&subject_value, 0);
lv_subject_set_max_value_int(&subject_value, 100);
inited = true;
}
lv_obj_t * screen = lv_screen_active();
/* Bound, normal mode, with an overlaid readout */
lv_obj_t * lv_bar_0 = lv_bar_create(screen);
lv_obj_set_size(lv_bar_0, 240, 22);
lv_bar_bind_value(lv_bar_0, &subject_value);
lv_obj_set_align(lv_bar_0, LV_ALIGN_LEFT_MID);
lv_obj_set_y(lv_bar_0, -40);
lv_obj_set_x(lv_bar_0, 20);
lv_obj_add_style(lv_bar_0, &style_bar, 0);
lv_obj_add_style(lv_bar_0, &style_bar_ind, LV_PART_INDICATOR);
lv_obj_t * lv_label_0 = lv_label_create(lv_bar_0);
lv_obj_set_align(lv_label_0, LV_ALIGN_CENTER);
lv_label_bind_text(lv_label_0, &subject_value, "%d %%");
lv_obj_set_style_text_color(lv_label_0, lv_color_hex(0x192e4f), 0);
/* Range: indicator spans start_value..value */
lv_obj_t * lv_bar_1 = lv_bar_create(screen);
lv_obj_set_size(lv_bar_1, 240, 20);
lv_bar_set_mode(lv_bar_1, LV_BAR_MODE_RANGE);
lv_bar_set_value(lv_bar_1, 80, false);
lv_bar_set_start_value(lv_bar_1, 10, false);
lv_obj_set_align(lv_bar_1, LV_ALIGN_LEFT_MID);
lv_obj_set_x(lv_bar_1, 20);
lv_obj_add_style(lv_bar_1, &style_bar, 0);
lv_obj_add_style(lv_bar_1, &style_bar_ind, LV_PART_INDICATOR);
/* Right to left fill when max_value < min_value with gradient */
lv_obj_t * lv_bar_2 = lv_bar_create(screen);
lv_obj_set_size(lv_bar_2, 240, 20);
lv_bar_set_min_value(lv_bar_2, 100);
lv_bar_set_max_value(lv_bar_2, 0);
lv_bar_set_value(lv_bar_2, 80, false);
lv_obj_set_align(lv_bar_2, LV_ALIGN_LEFT_MID);
lv_obj_set_pos(lv_bar_2, 20, 40);
lv_obj_set_style_bg_color(lv_bar_2, lv_color_hex3(0x04f), LV_PART_INDICATOR);
lv_obj_set_style_bg_grad_color(lv_bar_2, lv_color_hex3(0xa8f), LV_PART_INDICATOR);
lv_obj_set_style_bg_grad_dir(lv_bar_2, LV_GRAD_DIR_HOR, LV_PART_INDICATOR);
lv_obj_add_style(lv_bar_2, &style_bar, 0);
lv_obj_add_style(lv_bar_2, &style_bar_ind, LV_PART_INDICATOR);
/* Vertical orientation */
lv_obj_t * lv_bar_3 = lv_bar_create(screen);
lv_obj_set_size(lv_bar_3, 20, 160);
lv_bar_set_orientation(lv_bar_3, LV_BAR_ORIENTATION_VERTICAL);
lv_bar_set_value(lv_bar_3, 65, false);
lv_obj_set_align(lv_bar_3, LV_ALIGN_RIGHT_MID);
lv_obj_set_y(lv_bar_3, 0);
lv_obj_set_x(lv_bar_3, -60);
lv_obj_add_style(lv_bar_3, &style_bar, 0);
lv_obj_add_style(lv_bar_3, &style_bar_ind, LV_PART_INDICATOR);
lv_obj_t * lv_bar_4 = lv_bar_create(screen);
lv_obj_set_size(lv_bar_4, 20, 160);
lv_bar_set_orientation(lv_bar_4, LV_BAR_ORIENTATION_VERTICAL);
lv_bar_set_min_value(lv_bar_4, 100);
lv_bar_set_max_value(lv_bar_4, 0);
lv_bar_set_value(lv_bar_4, 65, false);
lv_obj_set_align(lv_bar_4, LV_ALIGN_RIGHT_MID);
lv_obj_set_y(lv_bar_4, 0);
lv_obj_set_x(lv_bar_4, -20);
lv_obj_add_style(lv_bar_4, &style_bar, 0);
lv_obj_add_style(lv_bar_4, &style_bar_ind, LV_PART_INDICATOR);
}
<!--
@title Bar
@brief Bound, range, reversed-with-gradient and vertical bars.
The first bar is bound to `subject_value` and overlays a live readout. The
second uses `mode="range"` with a `start_value` and `value` to show a span.
The third sets `min_value` above `max_value` so it fills right-to-left, with
a gradient indicator. The last two are vertical, the second of them also
reversed.
-->
<screen>
<styles>
<style
name="style_bar"
bg_color="0xdee2e7"
bg_opa="100%"
radius="8"
outline_width="1"
outline_color="0x3b82f6"
outline_opa="90%"
pad_all="3"
/>
<style name="style_bar_ind" bg_color="0x3b82f6" radius="6" />
</styles>
<view>
<!-- Bound, normal mode, with an overlaid readout -->
<lv_bar width="240" height="22" bind_value="subject_value" align="left_mid" y="-40" x="20">
<style name="style_bar" />
<style name="style_bar_ind" selector="indicator" />
<lv_label align="center" bind_text="subject_value" bind_text-fmt="%d %%" style_text_color="0x192e4f" />
</lv_bar>
<!-- Range: indicator spans start_value..value -->
<lv_bar width="240" height="20" mode="range" value="80" start_value="10" align="left_mid" x="20">
<style name="style_bar" />
<style name="style_bar_ind" selector="indicator" />
</lv_bar>
<!-- Right to left fill when max_value < min_value with gradient -->
<lv_bar
width="240"
height="20"
min_value="100"
max_value="0"
value="80"
align="left_mid"
x="20"
y="40"
style_bg_color-indicator="0x04f"
style_bg_grad_color-indicator="0xa8f"
style_bg_grad_dir-indicator="hor"
>
<style name="style_bar" />
<style name="style_bar_ind" selector="indicator" />
</lv_bar>
<!-- Vertical orientation -->
<lv_bar width="20" height="160" orientation="vertical" value="65" align="right_mid" y="0" x="-60">
<style name="style_bar" />
<style name="style_bar_ind" selector="indicator" />
</lv_bar>
<lv_bar
width="20"
height="160"
orientation="vertical"
min_value="100"
max_value="0"
value="65"
align="right_mid"
y="0"
x="-20"
>
<style name="style_bar" />
<style name="style_bar_ind" selector="indicator" />
</lv_bar>
</view>
</screen>
Enumerations
lv_bar_mode — How the bar grows with values
| Value | Description |
|---|---|
normal | Make the bar grow from minimum toward maximum |
symmetrical | Make the bar grow symmetrically from the midpoint |
range | Make the bar show values between start and end points |
lv_bar_orientation — How the bar is oriented
| Value | Description |
|---|---|
auto | Choose orientation automatically based on widget size |
horizontal | Make the bar fill left to right |
vertical | Make the bar fill bottom to top |
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 bar's background track: background properties. padding shrinks the indicator in the respective direction. |
indicator | The filled portion that shows the value: background properties. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
min_value | int | Set the minimum value of the bar. . If min_value > max_value fills from the right. |
max_value | int | Set the maximum value of the bar. If min_value > max_value fills from the right |
value | int | Set the current value of the bar |
value-animated | bool | Animate the bar when changing its value. Reference as value-animated (default: false) |
start_value | int | Set the start value in range mode |
start_value-animated | bool | Animate the bar when changing its start value. Reference as start_value-animated (default: false) |
mode | enum:lv_bar_mode | Select how the bar grows |
orientation | enum:lv_bar_orientation | Select the bar’s orientation |
bind_value | subject | Bind the bar’s value to a subject |
Last updated on