plot(shape): Add multi-label support for Bar (#1262)

## Breaking Change

```diff
- bar.label(move |_, point| Text::new("foo", point, "#red"));
+ bar.label(move |_, point| vec![Text::new("foo", point, "#red")]);
```

Add support for multiple labels on a single bar.
This commit is contained in:
Floyd Wang 2025-09-19 14:13:51 +08:00 committed by GitHub
parent 5e7a2cb37f
commit c385da56c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 5 deletions

View file

@ -154,8 +154,7 @@ where
if let Some(label) = self.label.as_ref() {
let label = label.clone();
bar =
bar.label(move |d, p| Text::new(label(d), p, label_color).align(TextAlign::Center));
bar = bar.label(move |d, p| vec![Text::new(label(d), p, label_color)]);
}
bar.paint(&bounds, window, cx);

View file

@ -13,7 +13,7 @@ pub struct Bar<T> {
y0: f32,
y1: Box<dyn Fn(&T) -> Option<f32>>,
fill: Box<dyn Fn(&T) -> Hsla>,
label: Option<Box<dyn Fn(&T, Point<Pixels>) -> Text>>,
label: Option<Box<dyn Fn(&T, Point<Pixels>) -> Vec<Text>>>,
}
impl<T> Default for Bar<T> {
@ -87,7 +87,7 @@ impl<T> Bar<T> {
/// Set the label of the Bar.
pub fn label<F>(mut self, label: F) -> Self
where
F: Fn(&T, Point<Pixels>) -> Text + 'static,
F: Fn(&T, Point<Pixels>) -> Vec<Text> + 'static,
{
self.label = Some(Box::new(label));
self
@ -121,7 +121,7 @@ impl<T> Bar<T> {
graph.push(fill(Bounds::from_corners(p1, p2), color));
if let Some(label) = &self.label {
labels.push(label(
labels.extend(label(
v,
point(
px(x_tick + self.band_width / 2.),