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
use crate::plot::scatter::{MarkerShape, ScatterPlot, TrendLine};
/// Marginal distribution style for `JointPlot`.
#[derive(Clone, Debug, Default)]
pub enum MarginalType {
/// Histogram bars. **Default.**
#[default]
Histogram,
/// Kernel density estimate curve (filled area).
Density,
}
/// One data group within a `JointPlot`.
///
/// Wraps a [`ScatterPlot`] so every scatter feature (error bars, trend lines,
/// confidence bands, per-point colors/sizes, marker shapes, tooltips, …) is
/// available. The x/y values are accessed via [`x_values`](Self::x_values) /
/// [`y_values`](Self::y_values) for marginal computation.
#[derive(Clone, Debug)]
pub struct JointGroup {
pub scatter: ScatterPlot,
}
impl JointGroup {
/// Create a group from x and y data iterables.
pub fn new(
x: impl IntoIterator<Item = impl Into<f64>>,
y: impl IntoIterator<Item = impl Into<f64>>,
) -> Self {
let xv: Vec<f64> = x.into_iter().map(|v| v.into()).collect();
let yv: Vec<f64> = y.into_iter().map(|v| v.into()).collect();
let scatter = ScatterPlot::new().with_data(xv.iter().copied().zip(yv.iter().copied()));
Self { scatter }
}
/// Create a group from a pre-built [`ScatterPlot`].
pub fn from_scatter(scatter: ScatterPlot) -> Self {
Self { scatter }
}
/// X values (for marginal histogram/density computation).
pub fn x_values(&self) -> Vec<f64> {
self.scatter.data.iter().map(|p| p.x).collect()
}
/// Y values (for marginal histogram/density computation).
pub fn y_values(&self) -> Vec<f64> {
self.scatter.data.iter().map(|p| p.y).collect()
}
// ── Forwarding builders (delegate to ScatterPlot) ──────────────────────
/// Set the group label (shown in the legend).
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.scatter = self.scatter.with_legend(label);
self
}
/// Set the marker color.
pub fn with_color(mut self, color: impl Into<String>) -> Self {
self.scatter = self.scatter.with_color(color);
self
}
/// Set the marker shape.
pub fn with_marker(mut self, marker: MarkerShape) -> Self {
self.scatter = self.scatter.with_marker(marker);
self
}
/// Set a uniform marker size (radius in pixels).
pub fn with_marker_size(mut self, size: f64) -> Self {
self.scatter = self.scatter.with_size(size);
self
}
/// Set the marker fill opacity (0.0–1.0).
pub fn with_marker_opacity(mut self, opacity: f64) -> Self {
self.scatter = self.scatter.with_marker_opacity(opacity);
self
}
/// Set the marker stroke (outline) width in pixels.
pub fn with_marker_stroke_width(mut self, width: f64) -> Self {
self.scatter = self.scatter.with_marker_stroke_width(width);
self
}
/// Set per-point sizes. Must have the same length as the data.
pub fn with_sizes(mut self, sizes: impl IntoIterator<Item = impl Into<f64>>) -> Self {
self.scatter = self.scatter.with_sizes(sizes);
self
}
/// Set per-point colors. Must have the same length as the data.
pub fn with_colors(mut self, colors: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.scatter = self.scatter.with_colors(colors);
self
}
/// Add symmetric X error bars (±value).
pub fn with_x_err(mut self, errors: impl IntoIterator<Item = impl Into<f64> + Copy>) -> Self {
self.scatter = self.scatter.with_x_err(errors);
self
}
/// Add asymmetric X error bars `(negative_arm, positive_arm)`.
pub fn with_x_err_asymmetric(
mut self,
errors: impl IntoIterator<Item = (impl Into<f64>, impl Into<f64>)>,
) -> Self {
self.scatter = self.scatter.with_x_err_asymmetric(errors);
self
}
/// Add symmetric Y error bars (±value).
pub fn with_y_err(mut self, errors: impl IntoIterator<Item = impl Into<f64> + Copy>) -> Self {
self.scatter = self.scatter.with_y_err(errors);
self
}
/// Add asymmetric Y error bars `(negative_arm, positive_arm)`.
pub fn with_y_err_asymmetric(
mut self,
errors: impl IntoIterator<Item = (impl Into<f64>, impl Into<f64>)>,
) -> Self {
self.scatter = self.scatter.with_y_err_asymmetric(errors);
self
}
/// Add a trend line (`TrendLine::Linear`).
pub fn with_trend(mut self, trend: TrendLine) -> Self {
self.scatter = self.scatter.with_trend(trend);
self
}
/// Set the trend line color.
pub fn with_trend_color(mut self, color: impl Into<String>) -> Self {
self.scatter = self.scatter.with_trend_color(color);
self
}
/// Show the regression equation on the plot.
pub fn with_equation(mut self) -> Self {
self.scatter = self.scatter.with_equation();
self
}
/// Show the Pearson correlation coefficient on the plot.
pub fn with_correlation(mut self) -> Self {
self.scatter = self.scatter.with_correlation();
self
}
/// Set the trend line stroke width.
pub fn with_trend_width(mut self, width: f64) -> Self {
self.scatter = self.scatter.with_trend_width(width);
self
}
/// Add a confidence band. `y_lower` and `y_upper` must align with the data points.
pub fn with_band(
mut self,
y_lower: impl IntoIterator<Item = impl Into<f64>>,
y_upper: impl IntoIterator<Item = impl Into<f64>>,
) -> Self {
self.scatter = self.scatter.with_band(y_lower, y_upper);
self
}
/// Enable SVG tooltip overlays showing `(x, y)` on hover.
pub fn with_tooltips(mut self) -> Self {
self.scatter = self.scatter.with_tooltips();
self
}
/// Set custom per-point tooltip labels.
pub fn with_tooltip_labels(
mut self,
labels: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
self.scatter = self.scatter.with_tooltip_labels(labels);
self
}
}
/// A scatter plot with marginal distribution panels on the top and/or right edges.
///
/// Render with [`render_jointplot`](crate::render::render::render_jointplot).
///
/// # Example
/// ```rust,no_run
/// use kuva::prelude::*;
/// let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
/// let y = vec![2.0, 3.5, 2.5, 4.0, 3.0];
/// let joint = JointPlot::new()
/// .with_xy(x, y)
/// .with_x_label("Feature A")
/// .with_y_label("Feature B");
/// let layout = Layout::new((0.5, 5.5), (1.5, 4.5)).with_title("Joint Plot");
/// let scene = render_jointplot(joint, layout);
/// ```
#[derive(Clone, Debug)]
pub struct JointPlot {
/// Data groups (one or more). Each wraps a full `ScatterPlot`.
pub groups: Vec<JointGroup>,
/// Marginal distribution style (Histogram or Density). Default: `Histogram`.
pub marginal_type: MarginalType,
/// Show a marginal panel above the scatter plot. Default: `true`.
pub show_top: bool,
/// Show a marginal panel to the right of the scatter plot. Default: `true`.
pub show_right: bool,
/// Pixel height (top) or width (right) of each marginal panel. Default: `80.0`.
pub marginal_size: f64,
/// Gap in pixels between the marginal panel and the scatter plot. Default: `4.0`.
pub marginal_gap: f64,
/// Number of histogram bins. Default: `20`.
pub bins: usize,
/// KDE bandwidth. `None` = Silverman's rule of thumb.
pub bandwidth: Option<f64>,
/// Opacity of marginal bars/fill. Default: `0.6`.
pub marginal_alpha: f64,
/// X-axis label.
pub x_label: Option<String>,
/// Y-axis label.
pub y_label: Option<String>,
/// Default scatter marker radius applied by `with_xy` / `with_group`. Default: `4.0`.
pub marker_size: f64,
/// Default scatter marker opacity applied by `with_xy` / `with_group`. Default: `0.8`.
pub marker_opacity: f64,
}
impl Default for JointPlot {
fn default() -> Self {
Self::new()
}
}
impl JointPlot {
/// Create a `JointPlot` with default settings.
pub fn new() -> Self {
Self {
groups: Vec::new(),
marginal_type: MarginalType::Histogram,
show_top: true,
show_right: true,
marginal_size: 80.0,
marginal_gap: 4.0,
bins: 20,
bandwidth: None,
marginal_alpha: 0.6,
x_label: None,
y_label: None,
marker_size: 4.0,
marker_opacity: 0.8,
}
}
/// Add a single (unlabeled) data group. Applies `marker_size` and `marker_opacity` defaults.
pub fn with_xy(
mut self,
x: impl IntoIterator<Item = impl Into<f64>>,
y: impl IntoIterator<Item = impl Into<f64>>,
) -> Self {
let g = JointGroup::new(x, y)
.with_marker_size(self.marker_size)
.with_marker_opacity(self.marker_opacity);
self.groups.push(g);
self
}
/// Add a named and colored data group. Applies `marker_size` and `marker_opacity` defaults.
pub fn with_group(
mut self,
label: impl Into<String>,
x: impl IntoIterator<Item = impl Into<f64>>,
y: impl IntoIterator<Item = impl Into<f64>>,
color: impl Into<String>,
) -> Self {
let g = JointGroup::new(x, y)
.with_label(label)
.with_color(color)
.with_marker_size(self.marker_size)
.with_marker_opacity(self.marker_opacity);
self.groups.push(g);
self
}
/// Add a fully-configured [`JointGroup`] (maximum control).
///
/// Use this to pass a group with error bars, trend lines, per-point colors, etc.
pub fn with_joint_group(mut self, group: JointGroup) -> Self {
self.groups.push(group);
self
}
/// Set the marginal distribution style.
pub fn with_marginal_type(mut self, t: MarginalType) -> Self {
self.marginal_type = t;
self
}
/// Show/hide the top marginal panel (default `true`).
pub fn with_top_marginal(mut self, v: bool) -> Self {
self.show_top = v;
self
}
/// Show/hide the right marginal panel (default `true`).
pub fn with_right_marginal(mut self, v: bool) -> Self {
self.show_right = v;
self
}
/// Set marginal panel size in pixels (height for top, width for right). Default `80.0`.
pub fn with_marginal_size(mut self, s: f64) -> Self {
self.marginal_size = s;
self
}
/// Set the gap in pixels between each marginal panel and the scatter. Default `4.0`.
pub fn with_marginal_gap(mut self, g: f64) -> Self {
self.marginal_gap = g;
self
}
/// Set the number of histogram bins. Default `20`.
pub fn with_bins(mut self, n: usize) -> Self {
self.bins = n;
self
}
/// Set the KDE bandwidth (`None` = Silverman's rule of thumb).
pub fn with_bandwidth(mut self, bw: f64) -> Self {
self.bandwidth = Some(bw);
self
}
/// Set the opacity of marginal bars/density fill. Default `0.6`.
pub fn with_marginal_alpha(mut self, a: f64) -> Self {
self.marginal_alpha = a;
self
}
/// Set the x-axis label.
pub fn with_x_label(mut self, s: impl Into<String>) -> Self {
self.x_label = Some(s.into());
self
}
/// Set the y-axis label.
pub fn with_y_label(mut self, s: impl Into<String>) -> Self {
self.y_label = Some(s.into());
self
}
/// Set the default scatter marker radius applied by `with_xy`/`with_group`. Default `4.0`.
pub fn with_marker_size(mut self, r: f64) -> Self {
self.marker_size = r;
self
}
/// Set the default scatter marker opacity applied by `with_xy`/`with_group`. Default `0.8`.
pub fn with_marker_opacity(mut self, a: f64) -> Self {
self.marker_opacity = a;
self
}
/// Returns `(x_min, x_max)` across all groups. Returns `(0.0, 1.0)` if no data.
pub fn x_range(&self) -> (f64, f64) {
let all_x: Vec<f64> = self.groups.iter().flat_map(|g| g.x_values()).collect();
if all_x.is_empty() {
return (0.0, 1.0);
}
let min = all_x.iter().cloned().fold(f64::INFINITY, f64::min);
let max = all_x.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
if (max - min).abs() < 1e-12 {
(min - 1.0, max + 1.0)
} else {
(min, max)
}
}
/// Returns `(y_min, y_max)` across all groups. Returns `(0.0, 1.0)` if no data.
pub fn y_range(&self) -> (f64, f64) {
let all_y: Vec<f64> = self.groups.iter().flat_map(|g| g.y_values()).collect();
if all_y.is_empty() {
return (0.0, 1.0);
}
let min = all_y.iter().cloned().fold(f64::INFINITY, f64::min);
let max = all_y.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
if (max - min).abs() < 1e-12 {
(min - 1.0, max + 1.0)
} else {
(min, max)
}
}
}