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
/// Display style for a [`SeriesPlot`].
///
/// Controls whether each data point is rendered as a line segment, a dot,
/// or both.
pub enum SeriesStyle {
/// Connect consecutive values with a polyline. No dots are drawn.
Line,
/// Draw a circle at each value. No connecting line is drawn. **(default)**
Point,
/// Draw both the connecting polyline and a circle at each value.
Both,
}
/// Builder for a series plot — a 1D sequence of y-values plotted against
/// their sequential index on the x-axis.
///
/// A series plot is the simplest way to visualise a time series, signal, or
/// any ordered sequence of measurements. The x-axis is assigned automatically
/// as consecutive integers `0, 1, 2, …` matching the index of each value.
///
/// Multiple `SeriesPlot` instances combined in one `plots` vector share the
/// same axes and are drawn in order, enabling overlay of several series.
///
/// # Display styles
///
/// Three rendering styles are available via the `with_*_style()` methods:
///
/// | Method | Style | Description |
/// |--------|-------|-------------|
/// | `.with_line_style()` | `Line` | Polyline connecting consecutive points |
/// | `.with_point_style()` | `Point` | Circle at each value **(default)** |
/// | `.with_line_point_style()` | `Both` | Polyline + circles |
///
/// # Example
///
/// ```rust,no_run
/// use kuva::plot::SeriesPlot;
/// 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> = (0..80)
/// .map(|i| (i as f64 * std::f64::consts::TAU / 80.0).sin())
/// .collect();
///
/// let series = SeriesPlot::new()
/// .with_data(data)
/// .with_color("steelblue")
/// .with_line_style();
///
/// let plots = vec![Plot::Series(series)];
/// let layout = Layout::auto_from_plots(&plots)
/// .with_title("Sine Wave")
/// .with_x_label("Sample")
/// .with_y_label("Amplitude");
///
/// let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
/// std::fs::write("series.svg", svg).unwrap();
/// ```
pub struct SeriesPlot {
/// Ordered y-values. The x position of value `i` is `i as f64`.
pub values: Vec<f64>,
/// CSS color used for both lines and points. Default: `"black"`.
pub color: String,
/// Rendering style. Default: [`SeriesStyle::Point`].
pub style: SeriesStyle,
/// When `Some`, a legend entry is shown with this label.
pub legend_label: Option<String>,
/// Line stroke width in pixels. Default: `2.0`.
pub stroke_width: f64,
/// Circle radius in pixels (used in `Point` and `Both` styles). Default: `3.0`.
pub point_radius: f64,
}
impl Default for SeriesPlot {
fn default() -> Self {
Self::new()
}
}
impl SeriesPlot {
/// Create a series plot with default settings.
///
/// Defaults: no data, `"black"` color, `Point` style, stroke width `2.0`,
/// point radius `3.0`, no legend.
pub fn new() -> Self {
Self {
values: vec![],
color: "black".into(),
style: SeriesStyle::Point,
legend_label: None,
stroke_width: 2.0,
point_radius: 3.0,
}
}
/// Load y-values from any iterable of numeric values.
///
/// The x position of each value is its index in the sequence (0, 1, 2, …).
///
/// ```rust,no_run
/// # use kuva::plot::SeriesPlot;
/// let data: Vec<f64> = (0..100)
/// .map(|i| (i as f64 / 10.0).sin())
/// .collect();
/// let series = SeriesPlot::new().with_data(data);
/// ```
pub fn with_data<T, I>(mut self, data: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<f64>,
{
self.values = data.into_iter().map(|x| x.into()).collect();
self
}
/// Set the color for lines and points. Default: `"black"`.
///
/// Accepts any CSS color string (`"steelblue"`, `"#4682b4"`, `"rgb(70,130,180)"`).
pub fn with_color<S: Into<String>>(mut self, color: S) -> Self {
self.color = color.into();
self
}
/// Render as a polyline only — no circles at data points.
pub fn with_line_style(mut self) -> Self {
self.style = SeriesStyle::Line;
self
}
/// Render as circles only — no connecting line. **(default)**
pub fn with_point_style(mut self) -> Self {
self.style = SeriesStyle::Point;
self
}
/// Render as a polyline with circles at each data point.
pub fn with_line_point_style(mut self) -> Self {
self.style = SeriesStyle::Both;
self
}
/// Enable a legend entry with the given label.
///
/// ```rust,no_run
/// # use kuva::plot::SeriesPlot;
/// let series = SeriesPlot::new()
/// .with_data(vec![1.0_f64, 2.0, 1.5])
/// .with_legend("sensor A");
/// ```
pub fn with_legend<S: Into<String>>(mut self, label: S) -> Self {
self.legend_label = Some(label.into());
self
}
/// Set the line stroke width in pixels. Default: `2.0`.
///
/// Only affects `Line` and `Both` styles.
pub fn with_stroke_width(mut self, width: f64) -> Self {
self.stroke_width = width;
self
}
/// Set the circle radius in pixels. Default: `3.0`.
///
/// Only affects `Point` and `Both` styles.
pub fn with_point_radius(mut self, radius: f64) -> Self {
self.point_radius = radius;
self
}
}