From 5f13b49690d6fc238e755c35517ccfd548c021f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Gimeno=20Ort=C3=AD?= Date: Tue, 14 Oct 2025 03:36:30 +0200 Subject: [PATCH] plot: Add stroke style per each Y series (#1364) Enables the user to set different `stroke_style` to each Y data series, keeping retrocompatibility with the previous API. Screenshot from 2025-10-13 11-33-28 --- crates/ui/src/chart/area_chart.rs | 36 ++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/crates/ui/src/chart/area_chart.rs b/crates/ui/src/chart/area_chart.rs index 9717e568..45e7bb82 100644 --- a/crates/ui/src/chart/area_chart.rs +++ b/crates/ui/src/chart/area_chart.rs @@ -23,9 +23,9 @@ where data: Vec, x: Option X>>, y: Vec Y>>, - stroke: Vec, - stroke_style: StrokeStyle, - fill: Vec, + strokes: Vec, + stroke_styles: Vec, + fills: Vec, tick_margin: usize, } @@ -40,9 +40,9 @@ where { Self { data: data.into_iter().collect(), - stroke_style: Default::default(), - stroke: vec![], - fill: vec![], + stroke_styles: vec![], + strokes: vec![], + fills: vec![], tick_margin: 1, x: None, y: vec![], @@ -60,22 +60,27 @@ where } pub fn stroke(mut self, stroke: impl Into) -> Self { - self.stroke.push(stroke.into()); + self.strokes.push(stroke.into()); self } pub fn fill(mut self, fill: impl Into) -> Self { - self.fill.push(fill.into()); + self.fills.push(fill.into()); + self + } + + pub fn natural(mut self) -> Self { + self.stroke_styles.push(StrokeStyle::Natural); self } pub fn linear(mut self) -> Self { - self.stroke_style = StrokeStyle::Linear; + self.stroke_styles.push(StrokeStyle::Linear); self } pub fn step_after(mut self) -> Self { - self.stroke_style = StrokeStyle::StepAfter; + self.stroke_styles.push(StrokeStyle::StepAfter); self } @@ -158,11 +163,16 @@ where let y_fn = y_fn.clone(); let fill = *self - .fill + .fills .get(i) .unwrap_or(&cx.theme().chart_2.opacity(0.4).into()); - let stroke = *self.stroke.get(i).unwrap_or(&cx.theme().chart_2); + let stroke = *self.strokes.get(i).unwrap_or(&cx.theme().chart_2); + + let stroke_style = *self + .stroke_styles + .get(i) + .unwrap_or(self.stroke_styles.first().unwrap_or(&Default::default())); Area::new() .data(&self.data) @@ -170,7 +180,7 @@ where .y0(height) .y1(move |d| y.tick(&y_fn(d))) .stroke(stroke) - .stroke_style(self.stroke_style) + .stroke_style(stroke_style) .fill(fill) .paint(&bounds, window); }