1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/// Stroke style for a line plot.
///
/// The default is [`LineStyle::Solid`].
#[derive(Debug, Clone, Default)]
pub enum LineStyle {
#[default]
Solid,
Dashed,
Dotted,
DashDot,
/// Arbitrary SVG `stroke-dasharray` value, e.g. `"10 5 2 5"`.
Custom(String),
}
impl LineStyle {
pub fn dasharray(&self) -> Option<String> {
match self {
LineStyle::Solid => None,
LineStyle::Dashed => Some("8 4".into()),
LineStyle::Dotted => Some("2 4".into()),
LineStyle::DashDot => Some("8 4 2 4".into()),
LineStyle::Custom(s) => Some(s.clone()),
}
}
}
/// A single (x, y) data point with optional asymmetric error bars.
///
/// Error bars are stored as `(negative_half, positive_half)` — the
/// magnitude of each arm, not the absolute bounds.
#[derive(Debug, Clone, Copy)]
pub struct ScatterPoint {
pub x: f64,
pub y: f64,
pub x_err: Option<(f64, f64)>, // (negative, positive)
pub y_err: Option<(f64, f64)>,
}
impl From<&ScatterPoint> for (f64, f64) {
fn from(p: &ScatterPoint) -> (f64, f64) {
(p.x, p.y)
}
}
impl ScatterPoint {
pub fn with_y_error(mut self, err: f64) -> Self {
self.y_err = Some((err, err));
self
}
pub fn with_y_error_asymmetric(mut self, neg: f64, pos: f64) -> Self {
self.y_err = Some((neg, pos));
self
}
}
use crate::plot::band::BandPlot;
/// Builder for a line plot.
///
/// Connects (x, y) data points with a continuous line. Supports multiple
/// line styles, filled area regions, step interpolation, confidence bands,
/// and error bars.
///
/// # Example
///
/// ```rust,no_run
/// use kuva::plot::LinePlot;
/// use kuva::backend::svg::SvgBackend;
/// use kuva::render::render::render_multiple;
/// use kuva::render::layout::Layout;
/// use kuva::render::plots::Plot;
///
/// let data: Vec<(f64, f64)> = (0..=100)
/// .map(|i| { let x = i as f64 * 0.1; (x, x.sin()) })
/// .collect();
///
/// let plot = LinePlot::new()
/// .with_data(data)
/// .with_color("steelblue")
/// .with_stroke_width(2.0);
///
/// let plots = vec![Plot::Line(plot)];
/// let layout = Layout::auto_from_plots(&plots)
/// .with_title("Sine Wave")
/// .with_x_label("X")
/// .with_y_label("Y");
///
/// let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
/// std::fs::write("line.svg", svg).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct LinePlot {
pub data: Vec<ScatterPoint>,
pub color: String,
pub stroke_width: f64,
pub legend_label: Option<String>,
pub band: Option<BandPlot>,
pub line_style: LineStyle,
pub step: bool,
pub fill: bool,
pub fill_opacity: f64,
}
impl Default for LinePlot {
fn default() -> Self {
Self::new()
}
}
impl LinePlot {
/// Create a line plot with default settings.
///
/// Defaults: color `"black"`, stroke width `2.0`, [`LineStyle::Solid`],
/// no fill, no step interpolation.
pub fn new() -> Self {
Self {
data: vec![],
color: "black".into(),
stroke_width: 2.0,
legend_label: None,
band: None,
line_style: LineStyle::default(),
step: false,
fill: false,
fill_opacity: 0.3,
}
}
/// Set the (x, y) data points.
///
/// Accepts any iterator of `(T, U)` pairs where `T` and `U` implement
/// `Into<f64>`, so integer and float types all work without casting.
///
/// ```rust,no_run
/// # use kuva::plot::LinePlot;
/// // integer input
/// let plot = LinePlot::new()
/// .with_data(vec![(0_i32, 0_i32), (1, 2), (2, 1)]);
/// ```
pub fn with_data<T, U, I>(mut self, points: I) -> Self
where
I: IntoIterator<Item = (T, U)>,
T: Into<f64>,
U: Into<f64>,
{
self.data = points
.into_iter()
.map(|(x, y)| ScatterPoint {
x: x.into(),
y: y.into(),
x_err: None,
y_err: None,
})
.collect();
self
}
/// Set symmetric X error bars.
///
/// Each value is the half-width of the error bar. Must be called
/// after [`with_data`](Self::with_data).
pub fn with_x_err<T, I>(mut self, errors: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<f64> + Copy,
{
for (i, err) in errors.into_iter().enumerate() {
if i < self.data.len() {
self.data[i].x_err = Some((err.into(), err.into()));
}
}
self
}
/// Set asymmetric X error bars.
///
/// Each item is a `(negative_arm, positive_arm)` tuple. Must be
/// called after [`with_data`](Self::with_data).
pub fn with_x_err_asymmetric<T, U, I>(mut self, errors: I) -> Self
where
I: IntoIterator<Item = (T, U)>,
T: Into<f64>,
U: Into<f64>,
{
for (i, (neg, pos)) in errors.into_iter().enumerate() {
if i < self.data.len() {
self.data[i].x_err = Some((neg.into(), pos.into()));
}
}
self
}
/// Set symmetric Y error bars.
///
/// Each value is the half-height of the error bar. Must be called
/// after [`with_data`](Self::with_data).
pub fn with_y_err<T, I>(mut self, errors: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<f64> + Copy,
{
for (i, err) in errors.into_iter().enumerate() {
if i < self.data.len() {
self.data[i].y_err = Some((err.into(), err.into()));
}
}
self
}
/// Set asymmetric Y error bars.
///
/// Each item is a `(negative_arm, positive_arm)` tuple. Must be
/// called after [`with_data`](Self::with_data).
pub fn with_y_err_asymmetric<T, U, I>(mut self, errors: I) -> Self
where
I: IntoIterator<Item = (T, U)>,
T: Into<f64>,
U: Into<f64>,
{
for (i, (neg, pos)) in errors.into_iter().enumerate() {
if i < self.data.len() {
self.data[i].y_err = Some((neg.into(), pos.into()));
}
}
self
}
/// Set the line color (CSS color string, e.g. `"steelblue"`, `"#4477aa"`).
pub fn with_color<S: Into<String>>(mut self, color: S) -> Self {
self.color = color.into();
self
}
/// Set the stroke width in pixels (default `2.0`).
pub fn with_stroke_width(mut self, width: f64) -> Self {
self.stroke_width = width;
self
}
/// Attach a legend label to this series.
///
/// A legend is rendered automatically when at least one series in
/// the plot has a label.
pub fn with_legend<S: Into<String>>(mut self, label: S) -> Self {
self.legend_label = Some(label.into());
self
}
/// Attach a shaded confidence band aligned to the line's x positions.
///
/// `y_lower` and `y_upper` must have the same length as the data.
/// The band color matches the line color at 30% opacity.
///
/// ```rust,no_run
/// # use kuva::plot::LinePlot;
/// let data: Vec<(f64, f64)> = (0..=5).map(|i| (i as f64, (i as f64).sin())).collect();
/// let lower: Vec<f64> = data.iter().map(|&(_, y)| y - 0.2).collect();
/// let upper: Vec<f64> = data.iter().map(|&(_, y)| y + 0.2).collect();
///
/// let plot = LinePlot::new()
/// .with_data(data)
/// .with_color("steelblue")
/// .with_band(lower, upper);
/// ```
pub fn with_band<T, U, I1, I2>(mut self, y_lower: I1, y_upper: I2) -> Self
where
I1: IntoIterator<Item = T>,
I2: IntoIterator<Item = U>,
T: Into<f64>,
U: Into<f64>,
{
let x: Vec<f64> = self.data.iter().map(|p| p.x).collect();
let band = BandPlot::new(x, y_lower, y_upper).with_color(self.color.clone());
self.band = Some(band);
self
}
/// Set the line style explicitly.
///
/// Convenience shortcuts: [`with_dashed`](Self::with_dashed),
/// [`with_dotted`](Self::with_dotted), [`with_dashdot`](Self::with_dashdot).
pub fn with_line_style(mut self, style: LineStyle) -> Self {
self.line_style = style;
self
}
/// Set the line style to [`LineStyle::Dashed`] (`8 4` dasharray).
pub fn with_dashed(mut self) -> Self {
self.line_style = LineStyle::Dashed;
self
}
/// Set the line style to [`LineStyle::Dotted`] (`2 4` dasharray).
pub fn with_dotted(mut self) -> Self {
self.line_style = LineStyle::Dotted;
self
}
/// Set the line style to [`LineStyle::DashDot`] (`8 4 2 4` dasharray).
pub fn with_dashdot(mut self) -> Self {
self.line_style = LineStyle::DashDot;
self
}
/// Use step interpolation between data points.
///
/// Instead of a diagonal segment between (x₁, y₁) and (x₂, y₂), the
/// renderer draws a horizontal segment at y₁ to x₂, then a vertical
/// step to y₂. Useful for histograms and discrete-time series.
pub fn with_step(mut self) -> Self {
self.step = true;
self
}
/// Fill the area between the line and the x-axis (area chart).
///
/// The fill uses the line color at the opacity set by
/// [`with_fill_opacity`](Self::with_fill_opacity) (default `0.3`).
pub fn with_fill(mut self) -> Self {
self.fill = true;
self
}
/// Set the fill opacity for area charts (default `0.3`).
///
/// Has no effect unless [`with_fill`](Self::with_fill) is also called.
pub fn with_fill_opacity(mut self, opacity: f64) -> Self {
self.fill_opacity = opacity;
self
}
}