kuva 0.1.4

Scientific plotting library in Rust with various backends.
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/// Marker shape used to render individual scatter points.
///
/// The default is [`MarkerShape::Circle`].
#[derive(Debug, Clone, Copy, Default)]
pub enum MarkerShape {
    #[default]
    Circle,
    Square,
    Triangle,
    Diamond,
    Cross,
    Plus,
}


/// Trend line variant to overlay on a scatter plot.
#[derive(Debug, Clone, Copy)]
pub enum TrendLine {
    /// Ordinary least-squares linear fit: y = mx + b.
    Linear,
    // Polynomial(u8),
    // Exponential,
}

/// 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)
    }
}


use crate::plot::band::BandPlot;

/// Builder for a scatter plot.
///
/// Constructs a scatter plot from (x, y) data. Supports error bars,
/// trend lines, confidence bands, variable point sizes (bubble plots),
/// per-point colors, and six marker shapes.
///
/// # Coloring points
///
/// Two coloring modes are available and can be combined:
///
/// | Method | Effect |
/// |--------|--------|
/// | `.with_color(c)` | Uniform color for all points (default `"black"`) |
/// | `.with_colors(iter)` | Per-point colors; falls back to `.with_color` for out-of-range indices |
///
/// `with_colors` is useful when your data already carries a group label encoded
/// as an index or category string, and you want to avoid splitting into multiple
/// `ScatterPlot` instances. Note that the legend is not automatically updated —
/// if you need a legend, use one `ScatterPlot` per color group with
/// `.with_legend()` on each, or supply custom entries via
/// `Layout::with_legend_entries` (planned).
///
/// # Example
///
/// ```rust,no_run
/// use kuva::plot::scatter::ScatterPlot;
/// use kuva::backend::svg::SvgBackend;
/// use kuva::render::render::render_multiple;
/// use kuva::render::layout::Layout;
/// use kuva::render::plots::Plot;
///
/// let data = vec![(1.0_f64, 2.0_f64), (3.0, 5.0), (5.0, 4.0)];
///
/// let plot = ScatterPlot::new()
///     .with_data(data)
///     .with_color("steelblue")
///     .with_size(5.0);
///
/// let plots = vec![Plot::Scatter(plot)];
/// let layout = Layout::auto_from_plots(&plots)
///     .with_title("My Scatter")
///     .with_x_label("X")
///     .with_y_label("Y");
///
/// let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
/// std::fs::write("scatter.svg", svg).unwrap();
/// ```
pub struct ScatterPlot {
    pub data: Vec<ScatterPoint>,
    pub color: String,
    pub size: f64,
    pub legend_label: Option<String>,
    pub trend: Option<TrendLine>,
    pub trend_color: String,
    pub show_equation: bool,
    pub show_correlation: bool,
    pub trend_width: f64,
    pub band: Option<BandPlot>,
    pub marker: MarkerShape,
    pub sizes: Option<Vec<f64>>,
    pub colors: Option<Vec<String>>,
    /// Fill opacity for markers (0.0 = transparent, 1.0 = solid). `None` = fully opaque.
    pub marker_opacity: Option<f64>,
    /// Stroke (outline) width for markers. `None` = no stroke. Stroke color matches fill.
    pub marker_stroke_width: Option<f64>,
}


impl Default for ScatterPlot {
    fn default() -> Self { Self::new() }
}

impl ScatterPlot {
    /// Create a scatter plot with default settings.
    ///
    /// Defaults: color `"black"`, size `3.0`, [`MarkerShape::Circle`],
    /// no trend line, no legend label.
    pub fn new() -> Self {
        Self {
            data: vec![],
            color: "black".into(),
            size: 3.0,
            legend_label: None,
            trend: None,
            trend_color: "black".into(),
            show_equation: false,
            show_correlation: false,
            trend_width: 1.0,
            band: None,
            marker: MarkerShape::default(),
            sizes: None,
            colors: None,
            marker_opacity: None,
            marker_stroke_width: None,
        }
    }

    /// 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::scatter::ScatterPlot;
    /// // integer input
    /// let plot = ScatterPlot::new()
    ///     .with_data(vec![(1_i32, 5_i32), (2, 8), (3, 6)]);
    /// ```
    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 (i.e. the bar
    /// extends ± value from the point). 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 point 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 uniform point radius in pixels (default `3.0`).
    ///
    /// For per-point radii use [`with_sizes`](Self::with_sizes).
    pub fn with_size(mut self, size: f64) -> Self {
        self.size = size;
        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
    }

    /// Overlay a trend line computed from the scatter data.
    pub fn with_trend(mut self, trend: TrendLine) -> Self {
        self.trend = Some(trend);
        self
    }

    /// Set the trend line color (default `"black"`).
    pub fn with_trend_color<S: Into<String>>(mut self, color: S) -> Self {
        self.trend_color = color.into();
        self
    }

    /// Annotate the plot with the regression equation (y = mx + b).
    ///
    /// Requires a trend line to be set via [`with_trend`](Self::with_trend).
    pub fn with_equation(mut self) -> Self {
        self.show_equation = true;
        self
    }

    /// Annotate the plot with the Pearson R² value.
    ///
    /// Requires a trend line to be set via [`with_trend`](Self::with_trend).
    pub fn with_correlation(mut self) -> Self {
        self.show_correlation = true;
        self
    }

    /// Set the trend line stroke width in pixels (default `1.0`).
    pub fn with_trend_width(mut self, width: f64) -> Self {
        self.trend_width = width;
        self
    }

    /// Attach a shaded confidence band aligned to the scatter x positions.
    ///
    /// `y_lower` and `y_upper` must have the same length as the data.
    /// The band color matches the scatter series color.
    ///
    /// ```rust,no_run
    /// # use kuva::plot::scatter::ScatterPlot;
    /// let data = vec![(1.0_f64, 2.0_f64), (2.0, 4.0), (3.0, 6.0)];
    /// let lower = vec![1.5_f64, 3.5, 5.5];
    /// let upper = vec![2.5_f64, 4.5, 6.5];
    ///
    /// let plot = ScatterPlot::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 marker shape (default [`MarkerShape::Circle`]).
    pub fn with_marker(mut self, marker: MarkerShape) -> Self {
        self.marker = marker;
        self
    }

    /// Set per-point radii for a bubble plot.
    ///
    /// Values are radii in pixels. When set, the uniform `size` value
    /// from [`with_size`](Self::with_size) is ignored.
    ///
    /// ```rust,no_run
    /// # use kuva::plot::scatter::ScatterPlot;
    /// let data = vec![(1.0_f64, 2.0_f64), (3.0, 4.0), (5.0, 3.0)];
    /// let sizes = vec![5.0_f64, 12.0, 8.0];
    ///
    /// let plot = ScatterPlot::new()
    ///     .with_data(data)
    ///     .with_sizes(sizes)
    ///     .with_color("steelblue");
    /// ```
    pub fn with_sizes<T, I>(mut self, sizes: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<f64>,
    {
        self.sizes = Some(sizes.into_iter().map(|s| s.into()).collect());
        self
    }

    /// Set per-point colors.
    ///
    /// Colors are matched to points by index. If the list is shorter than the
    /// data, the uniform color from [`with_color`](Self::with_color) is used as
    /// a fallback for the remaining points.
    ///
    /// This is the single-series equivalent of splitting data into multiple
    /// `ScatterPlot` instances — useful when color encodes a pre-computed
    /// group label rather than a separate series.
    ///
    /// ```rust,no_run
    /// # use kuva::plot::scatter::ScatterPlot;
    /// let data = vec![(1.0_f64, 1.0), (2.0, 2.0), (3.0, 3.0), (4.0, 4.0)];
    /// let colors = vec!["red", "red", "blue", "blue"];
    ///
    /// let plot = ScatterPlot::new()
    ///     .with_data(data)
    ///     .with_colors(colors);
    /// ```
    pub fn with_colors<S, I>(mut self, colors: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.colors = Some(colors.into_iter().map(|s| s.into()).collect());
        self
    }

    /// Set the fill opacity for all markers (0.0 = fully transparent, 1.0 = fully opaque).
    ///
    /// Combine with [`with_marker_stroke_width`](Self::with_marker_stroke_width) for the
    /// classic "open circle" look where overlapping points show density.
    pub fn with_marker_opacity(mut self, opacity: f64) -> Self {
        self.marker_opacity = Some(opacity.clamp(0.0, 1.0));
        self
    }

    /// Draw a solid outline around each marker at the given stroke width.
    ///
    /// The stroke color matches the fill color. Pair with a low
    /// [`with_marker_opacity`](Self::with_marker_opacity) to make individual
    /// points visible even in dense regions.
    pub fn with_marker_stroke_width(mut self, width: f64) -> Self {
        self.marker_stroke_width = Some(width);
        self
    }
}