Skip to content

Commit

Permalink
fix(barchart): print value when value width equal bar width
Browse files Browse the repository at this point in the history
till now if the value or the label is equal the available width, then
it will be ignored. Modify the condition to less or equal.

Signed-off-by: Ben Fekih, Hichem <hichem.f@live.de>
  • Loading branch information
karthago1 committed Sep 18, 2023
1 parent dd9a8df commit 746cf3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/widgets/barchart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ mod tests {
}

#[test]
fn test_group_label_right() {
fn test_value_label_equal_bar_width() {
let chart: BarChart<'_> = BarChart::default().data(
BarGroup::default()
.label(Line::from(Span::from("G")).alignment(Alignment::Right))
Expand All @@ -1149,4 +1149,26 @@ mod tests {
let expected = Buffer::with_lines(vec![" █", "▆ █", " G"]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_value_width_equal_bar_width() {
let group = BarGroup::default().bars(&[
Bar::default().value(123).label("B1".into()),
Bar::default().value(321).label("B2".into()),
Bar::default().value(333).label("B2".into()),
]);
let chart = BarChart::default().data(group).bar_width(3).bar_gap(1);

let mut buffer = Buffer::empty(Rect::new(0, 0, 11, 5));
chart.render(buffer.area, &mut buffer);

let expected = Buffer::with_lines(vec![
" ▆▆▆ ███",
" ███ ███",
"▃▃▃ ███ ███",
"123 321 333",
"B1 B2 B2 ",
]);
assert_buffer_eq!(buffer, expected);
}
}
2 changes: 1 addition & 1 deletion src/widgets/barchart/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a> Bar<'a> {
};

let width = value_label.len() as u16;
if width < max_width {
if width <= max_width {
buf.set_string(
x + (max_width.saturating_sub(value_label.len() as u16) >> 1),
y,
Expand Down

0 comments on commit 746cf3d

Please sign in to comment.