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
/// Radar / spider chart — multivariate data plotted on radial axes.
///
/// Each axis represents one variable; values are mapped to radial distance
/// from the centre. Multiple series (observations or groups) can be overlaid
/// as filled or stroked polygons.
///
/// # Example
/// ```
/// use kuva::plot::radar::RadarPlot;
///
/// let plot = RadarPlot::new(vec!["Speed", "Power", "Agility", "Stamina", "Technique"])
/// .with_series_labeled(vec![0.8_f64, 0.6, 0.9, 0.7, 0.75], "Group A")
/// .with_series_labeled(vec![0.6_f64, 0.9, 0.5, 0.8, 0.70], "Group B")
/// .with_filled(true)
/// .with_legend(true);
/// ```
#[derive(Debug, Clone)]
pub struct RadarPlot {
/// Names of the axes, in order (rendered clockwise from the start angle).
pub axes: Vec<String>,
/// Data series to plot.
pub series: Vec<RadarSeries>,
/// Reference polygons drawn as dashed overlays (e.g. a baseline or target).
pub references: Vec<RadarReference>,
/// Fill each polygon (default `false`).
pub filled: bool,
/// Fill opacity when `filled` is `true` (default `0.25`).
pub opacity: f64,
/// Shared value range `(min, max)`. `None` = auto from data.
pub range: Option<(f64, f64)>,
/// Per-axis value ranges. Index must match axis position; `None` entries use the shared range.
pub axis_ranges: Vec<Option<(f64, f64)>>,
/// Per-axis inversion flags. When `true` for axis `i`, high values plot near the centre.
pub inverted_axes: Vec<bool>,
/// Number of concentric grid rings (default `5`).
pub grid_lines: usize,
/// Draw grid rings and axis lines (default `true`).
pub show_grid: bool,
/// Draw grid rings as circles instead of polygons (default `false`).
pub circular_grid: bool,
/// Show a legend box (default `false`).
pub show_legend: bool,
/// Draw dots at each vertex (radius in px; `None` = no dots).
pub dot_size: Option<f64>,
/// Series polygon stroke width (default `1.5`).
pub stroke_width: f64,
/// Normalise each axis independently to \[0, 1\] before plotting.
/// Useful when axes have different units. Grid labels become percentages.
pub normalize: bool,
/// Show the data value as a label at each polygon vertex (default `false`).
pub vertex_labels: bool,
/// Angle of the first axis in degrees, measured clockwise from north (default `−90` = top).
pub start_angle_deg: f64,
/// Draw short tick marks on each axis at every grid ring (default `false`).
pub axis_ticks: bool,
}
/// One series (polygon) in a [`RadarPlot`].
#[derive(Debug, Clone)]
pub struct RadarSeries {
/// One value per axis; length must equal `RadarPlot::axes.len()`.
pub values: Vec<f64>,
/// Optional display label (used in legend and tooltip).
pub label: Option<String>,
/// Override colour; `None` = palette-assigned.
pub color: Option<String>,
/// Per-axis ±error used to draw a shaded band around the polygon.
pub errors: Option<Vec<f64>>,
/// SVG `stroke-dasharray` for this series line (e.g. `"6,3"`).
pub dasharray: Option<String>,
}
/// A reference polygon drawn as a dashed overlay in a [`RadarPlot`].
#[derive(Debug, Clone)]
pub struct RadarReference {
/// One value per axis; length must equal `RadarPlot::axes.len()`.
pub values: Vec<f64>,
/// Label shown in the legend when `show_legend` is `true`.
pub label: Option<String>,
/// Override colour; `None` = `"#999999"`.
pub color: Option<String>,
}
impl RadarPlot {
/// Create a new radar plot with the given axis names.
pub fn new(axes: impl IntoIterator<Item = impl Into<String>>) -> Self {
Self {
axes: axes.into_iter().map(Into::into).collect(),
series: Vec::new(),
references: Vec::new(),
filled: false,
opacity: 0.25,
range: None,
axis_ranges: Vec::new(),
inverted_axes: Vec::new(),
grid_lines: 5,
show_grid: true,
circular_grid: false,
show_legend: false,
dot_size: None,
stroke_width: 1.5,
normalize: false,
vertex_labels: false,
start_angle_deg: -90.0,
axis_ticks: false,
}
}
/// Add an unlabelled series.
pub fn with_series(mut self, values: impl IntoIterator<Item = impl Into<f64>>) -> Self {
self.series.push(RadarSeries {
values: values.into_iter().map(Into::into).collect(),
label: None,
color: None,
errors: None,
dasharray: None,
});
self
}
/// Add a labelled series (label appears in legend).
pub fn with_series_labeled(
mut self,
values: impl IntoIterator<Item = impl Into<f64>>,
label: impl Into<String>,
) -> Self {
self.series.push(RadarSeries {
values: values.into_iter().map(Into::into).collect(),
label: Some(label.into()),
color: None,
errors: None,
dasharray: None,
});
self
}
/// Add a labelled series with an explicit colour.
pub fn with_series_color(
mut self,
values: impl IntoIterator<Item = impl Into<f64>>,
label: impl Into<String>,
color: impl Into<String>,
) -> Self {
self.series.push(RadarSeries {
values: values.into_iter().map(Into::into).collect(),
label: Some(label.into()),
color: Some(color.into()),
errors: None,
dasharray: None,
});
self
}
/// Attach per-axis ±errors to the **most recently added** series.
///
/// A shaded band is drawn between `value − error` and `value + error` on each axis.
/// Call this immediately after `with_series*`.
pub fn with_series_errors(mut self, errors: impl IntoIterator<Item = impl Into<f64>>) -> Self {
if let Some(last) = self.series.last_mut() {
last.errors = Some(errors.into_iter().map(Into::into).collect());
}
self
}
/// Set a custom SVG `stroke-dasharray` on the **most recently added** series.
///
/// Example: `"6,3"` for dashes, `"2,2"` for dots. Call immediately after `with_series*`.
pub fn with_series_dasharray(mut self, dasharray: impl Into<String>) -> Self {
if let Some(last) = self.series.last_mut() {
last.dasharray = Some(dasharray.into());
}
self
}
/// Add a reference polygon drawn as a dashed overlay.
pub fn with_reference(
mut self,
values: impl IntoIterator<Item = impl Into<f64>>,
label: impl Into<String>,
) -> Self {
self.references.push(RadarReference {
values: values.into_iter().map(Into::into).collect(),
label: Some(label.into()),
color: None,
});
self
}
/// Add a reference polygon with an explicit colour.
pub fn with_reference_color(
mut self,
values: impl IntoIterator<Item = impl Into<f64>>,
label: impl Into<String>,
color: impl Into<String>,
) -> Self {
self.references.push(RadarReference {
values: values.into_iter().map(Into::into).collect(),
label: Some(label.into()),
color: Some(color.into()),
});
self
}
/// Fill each polygon with a semi-transparent colour.
pub fn with_filled(mut self, filled: bool) -> Self {
self.filled = filled;
self
}
/// Set fill opacity (only used when `filled` is `true`).
pub fn with_opacity(mut self, opacity: impl Into<f64>) -> Self {
self.opacity = opacity.into();
self
}
/// Set a shared value range for all axes.
pub fn with_range(mut self, min: impl Into<f64>, max: impl Into<f64>) -> Self {
self.range = Some((min.into(), max.into()));
self
}
/// Override the value range for a single axis (by index).
///
/// Per-axis ranges take priority over `with_range` and `with_normalize`.
pub fn with_axis_range(
mut self,
axis: usize,
min: impl Into<f64>,
max: impl Into<f64>,
) -> Self {
if self.axis_ranges.len() <= axis {
self.axis_ranges.resize(axis + 1, None);
}
self.axis_ranges[axis] = Some((min.into(), max.into()));
self
}
/// Mark a single axis as inverted: high values plot near the centre, low values at the rim.
pub fn with_inverted_axis(mut self, axis: usize) -> Self {
if self.inverted_axes.len() <= axis {
self.inverted_axes.resize(axis + 1, false);
}
self.inverted_axes[axis] = true;
self
}
/// Mark multiple axes as inverted (by index).
pub fn with_inverted_axes(mut self, indices: impl IntoIterator<Item = usize>) -> Self {
for axis in indices {
if self.inverted_axes.len() <= axis {
self.inverted_axes.resize(axis + 1, false);
}
self.inverted_axes[axis] = true;
}
self
}
/// Set the number of concentric grid rings.
pub fn with_grid_lines(mut self, n: usize) -> Self {
self.grid_lines = n;
self
}
/// Show or hide grid rings and radial axis lines.
pub fn with_grid(mut self, show: bool) -> Self {
self.show_grid = show;
self
}
/// Draw grid rings as circles rather than polygons.
pub fn with_circular_grid(mut self, circular: bool) -> Self {
self.circular_grid = circular;
self
}
/// Show a legend.
pub fn with_legend(mut self, show: bool) -> Self {
self.show_legend = show;
self
}
/// Draw dots at polygon vertices with the given radius in px.
pub fn with_dot_size(mut self, size: impl Into<f64>) -> Self {
self.dot_size = Some(size.into());
self
}
/// Set the series polygon stroke width.
pub fn with_stroke_width(mut self, w: impl Into<f64>) -> Self {
self.stroke_width = w.into();
self
}
/// Normalise each axis independently to \[0, 1\] (ignores `range`).
/// Useful when axes have different units or scales. Grid labels become percentages.
pub fn with_normalize(mut self, normalize: bool) -> Self {
self.normalize = normalize;
self
}
/// Show the data value as a small label at each polygon vertex.
pub fn with_vertex_labels(mut self, show: bool) -> Self {
self.vertex_labels = show;
self
}
/// Set the starting angle (in degrees, clockwise from north) for the first axis.
///
/// Default is `−90°` (axis 0 points straight up). Use `0°` for axis 0 pointing right.
pub fn with_start_angle(mut self, deg: impl Into<f64>) -> Self {
self.start_angle_deg = deg.into();
self
}
/// Place axis `k` at the top (north) position.
///
/// Equivalent to `with_start_angle(−90 − k × 360/n)`.
pub fn with_start_axis(mut self, k: usize) -> Self {
use std::f64::consts::PI;
let n = self.axes.len().max(1);
self.start_angle_deg = (-PI / 2.0 - k as f64 * 2.0 * PI / n as f64).to_degrees();
self
}
/// Draw short tick marks on each axis at every grid ring intersection.
pub fn with_axis_ticks(mut self, show: bool) -> Self {
self.axis_ticks = show;
self
}
}