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.

<img width="1300" height="411" alt="Screenshot from 2025-10-13 11-33-28"
src="https://github.com/user-attachments/assets/d665b87c-0d79-479c-9156-15a0f1c8ca10"
/>
This commit is contained in:
Martí Gimeno Ortí 2025-10-14 03:36:30 +02:00 committed by GitHub
parent 5b2fed0ccb
commit 5f13b49690
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -23,9 +23,9 @@ where
data: Vec<T>, data: Vec<T>,
x: Option<Rc<dyn Fn(&T) -> X>>, x: Option<Rc<dyn Fn(&T) -> X>>,
y: Vec<Rc<dyn Fn(&T) -> Y>>, y: Vec<Rc<dyn Fn(&T) -> Y>>,
stroke: Vec<Hsla>, strokes: Vec<Hsla>,
stroke_style: StrokeStyle, stroke_styles: Vec<StrokeStyle>,
fill: Vec<Background>, fills: Vec<Background>,
tick_margin: usize, tick_margin: usize,
} }
@ -40,9 +40,9 @@ where
{ {
Self { Self {
data: data.into_iter().collect(), data: data.into_iter().collect(),
stroke_style: Default::default(), stroke_styles: vec![],
stroke: vec![], strokes: vec![],
fill: vec![], fills: vec![],
tick_margin: 1, tick_margin: 1,
x: None, x: None,
y: vec![], y: vec![],
@ -60,22 +60,27 @@ where
} }
pub fn stroke(mut self, stroke: impl Into<Hsla>) -> Self { pub fn stroke(mut self, stroke: impl Into<Hsla>) -> Self {
self.stroke.push(stroke.into()); self.strokes.push(stroke.into());
self self
} }
pub fn fill(mut self, fill: impl Into<Background>) -> Self { pub fn fill(mut self, fill: impl Into<Background>) -> 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 self
} }
pub fn linear(mut self) -> Self { pub fn linear(mut self) -> Self {
self.stroke_style = StrokeStyle::Linear; self.stroke_styles.push(StrokeStyle::Linear);
self self
} }
pub fn step_after(mut self) -> Self { pub fn step_after(mut self) -> Self {
self.stroke_style = StrokeStyle::StepAfter; self.stroke_styles.push(StrokeStyle::StepAfter);
self self
} }
@ -158,11 +163,16 @@ where
let y_fn = y_fn.clone(); let y_fn = y_fn.clone();
let fill = *self let fill = *self
.fill .fills
.get(i) .get(i)
.unwrap_or(&cx.theme().chart_2.opacity(0.4).into()); .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() Area::new()
.data(&self.data) .data(&self.data)
@ -170,7 +180,7 @@ where
.y0(height) .y0(height)
.y1(move |d| y.tick(&y_fn(d))) .y1(move |d| y.tick(&y_fn(d)))
.stroke(stroke) .stroke(stroke)
.stroke_style(self.stroke_style) .stroke_style(stroke_style)
.fill(fill) .fill(fill)
.paint(&bounds, window); .paint(&bounds, window);
} }