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
//! Pie chart builder methods.
//!
//! Provides a fluent builder API for configuring [`PieArtist`] instances.
//! Each method returns `&mut Self`, allowing calls to be chained together
//! for concise, readable chart construction.
use crate::artist::PieArtist;
use crate::primitives::Color;
impl PieArtist {
/// Sets the wedge labels displayed next to each slice.
///
/// The number of labels should match the number of wedge sizes. If
/// fewer labels are provided, extra wedges will have no label.
///
/// # Arguments
///
/// * `labels` - A vector of label strings, one per wedge.
///
/// # Examples
///
/// ```
/// # use plotkit_core::artist::PieArtist;
/// let mut pie = PieArtist {
/// sizes: vec![1.0, 2.0, 3.0],
/// labels: None,
/// colors: None,
/// explode: None,
/// autopct: false,
/// start_angle: 90.0,
/// radius: 1.0,
/// label: None,
/// color: plotkit_core::primitives::Color::TAB_BLUE,
/// };
/// pie.labels(vec!["A", "B", "C"]);
/// assert_eq!(pie.labels.as_ref().unwrap().len(), 3);
/// ```
pub fn labels(&mut self, labels: Vec<&str>) -> &mut Self {
self.labels = Some(labels.into_iter().map(String::from).collect());
self
}
/// Sets custom colors for each wedge.
///
/// When not set, the theme color cycle is used automatically.
///
/// # Arguments
///
/// * `colors` - A vector of [`Color`] values, one per wedge.
///
/// # Examples
///
/// ```
/// # use plotkit_core::artist::PieArtist;
/// # use plotkit_core::primitives::Color;
/// let mut pie = PieArtist {
/// sizes: vec![1.0, 2.0],
/// labels: None,
/// colors: None,
/// explode: None,
/// autopct: false,
/// start_angle: 90.0,
/// radius: 1.0,
/// label: None,
/// color: Color::TAB_BLUE,
/// };
/// pie.colors(vec![Color::TAB_RED, Color::TAB_GREEN]);
/// assert_eq!(pie.colors.as_ref().unwrap().len(), 2);
/// ```
pub fn colors(&mut self, colors: Vec<Color>) -> &mut Self {
self.colors = Some(colors);
self
}
/// Sets the explode offsets for each wedge.
///
/// Each value represents the fraction of the radius by which the
/// corresponding wedge is offset from the center. A value of `0.0`
/// means no offset; `0.1` pushes the wedge outward by 10% of the
/// radius.
///
/// # Arguments
///
/// * `explode` - A vector of offset fractions, one per wedge.
///
/// # Examples
///
/// ```
/// # use plotkit_core::artist::PieArtist;
/// # use plotkit_core::primitives::Color;
/// let mut pie = PieArtist {
/// sizes: vec![1.0, 2.0, 3.0],
/// labels: None,
/// colors: None,
/// explode: None,
/// autopct: false,
/// start_angle: 90.0,
/// radius: 1.0,
/// label: None,
/// color: Color::TAB_BLUE,
/// };
/// pie.explode(vec![0.1, 0.0, 0.0]);
/// assert_eq!(pie.explode.as_ref().unwrap()[0], 0.1);
/// ```
pub fn explode(&mut self, explode: Vec<f64>) -> &mut Self {
self.explode = Some(explode);
self
}
/// Enables or disables automatic percentage labels on each wedge.
///
/// When enabled, each wedge displays its percentage of the total as
/// text positioned at the midpoint of the wedge arc.
///
/// # Arguments
///
/// * `show` - `true` to show percentage labels, `false` to hide them.
///
/// # Examples
///
/// ```
/// # use plotkit_core::artist::PieArtist;
/// # use plotkit_core::primitives::Color;
/// let mut pie = PieArtist {
/// sizes: vec![1.0, 2.0],
/// labels: None,
/// colors: None,
/// explode: None,
/// autopct: false,
/// start_angle: 90.0,
/// radius: 1.0,
/// label: None,
/// color: Color::TAB_BLUE,
/// };
/// pie.autopct(true);
/// assert!(pie.autopct);
/// ```
pub fn autopct(&mut self, show: bool) -> &mut Self {
self.autopct = show;
self
}
/// Sets the starting angle for the first wedge in degrees.
///
/// The default is `90.0` (top of the circle). Angles increase
/// counter-clockwise.
///
/// # Arguments
///
/// * `angle` - The starting angle in degrees.
///
/// # Examples
///
/// ```
/// # use plotkit_core::artist::PieArtist;
/// # use plotkit_core::primitives::Color;
/// let mut pie = PieArtist {
/// sizes: vec![1.0, 2.0],
/// labels: None,
/// colors: None,
/// explode: None,
/// autopct: false,
/// start_angle: 90.0,
/// radius: 1.0,
/// label: None,
/// color: Color::TAB_BLUE,
/// };
/// pie.start_angle(0.0);
/// assert!((pie.start_angle - 0.0).abs() < f64::EPSILON);
/// ```
pub fn start_angle(&mut self, angle: f64) -> &mut Self {
self.start_angle = angle;
self
}
/// Sets the radius of the pie chart in data-space units.
///
/// The default radius is `1.0`. The pie is drawn in a square
/// coordinate space that accommodates the radius plus any explode
/// offsets.
///
/// # Arguments
///
/// * `radius` - The radius of the pie.
///
/// # Examples
///
/// ```
/// # use plotkit_core::artist::PieArtist;
/// # use plotkit_core::primitives::Color;
/// let mut pie = PieArtist {
/// sizes: vec![1.0, 2.0],
/// labels: None,
/// colors: None,
/// explode: None,
/// autopct: false,
/// start_angle: 90.0,
/// radius: 1.0,
/// label: None,
/// color: Color::TAB_BLUE,
/// };
/// pie.radius(0.8);
/// assert!((pie.radius - 0.8).abs() < f64::EPSILON);
/// ```
pub fn radius(&mut self, radius: f64) -> &mut Self {
self.radius = radius;
self
}
/// Sets the legend label for the pie chart.
///
/// When a legend is displayed on the figure, this label will appear
/// next to the color swatch for this pie chart.
///
/// # Arguments
///
/// * `label` - A string slice that will be stored as the legend entry.
///
/// # Examples
///
/// ```
/// # use plotkit_core::artist::PieArtist;
/// # use plotkit_core::primitives::Color;
/// let mut pie = PieArtist {
/// sizes: vec![1.0, 2.0],
/// labels: None,
/// colors: None,
/// explode: None,
/// autopct: false,
/// start_angle: 90.0,
/// radius: 1.0,
/// label: None,
/// color: Color::TAB_BLUE,
/// };
/// pie.label("pie chart");
/// assert_eq!(pie.label.as_deref(), Some("pie chart"));
/// ```
pub fn label(&mut self, label: &str) -> &mut Self {
self.label = Some(label.to_string());
self
}
}
#[cfg(test)]
mod tests {
use crate::artist::PieArtist;
use crate::primitives::Color;
fn sample_pie() -> PieArtist {
PieArtist {
sizes: vec![35.0, 25.0, 20.0, 15.0, 5.0],
labels: None,
colors: None,
explode: None,
autopct: false,
start_angle: 90.0,
radius: 1.0,
label: None,
color: Color::TAB_BLUE,
}
}
#[test]
fn builder_labels() {
let mut p = sample_pie();
p.labels(vec!["A", "B", "C", "D", "E"]);
assert_eq!(p.labels.as_ref().unwrap().len(), 5);
assert_eq!(p.labels.as_ref().unwrap()[0], "A");
}
#[test]
fn builder_colors() {
let mut p = sample_pie();
p.colors(vec![Color::TAB_RED, Color::TAB_GREEN]);
assert_eq!(p.colors.as_ref().unwrap().len(), 2);
}
#[test]
fn builder_explode() {
let mut p = sample_pie();
p.explode(vec![0.1, 0.0, 0.0, 0.0, 0.0]);
assert!((p.explode.as_ref().unwrap()[0] - 0.1).abs() < f64::EPSILON);
assert!((p.explode.as_ref().unwrap()[1] - 0.0).abs() < f64::EPSILON);
}
#[test]
fn builder_autopct() {
let mut p = sample_pie();
assert!(!p.autopct);
p.autopct(true);
assert!(p.autopct);
}
#[test]
fn builder_start_angle() {
let mut p = sample_pie();
p.start_angle(45.0);
assert!((p.start_angle - 45.0).abs() < f64::EPSILON);
}
#[test]
fn builder_radius() {
let mut p = sample_pie();
p.radius(0.5);
assert!((p.radius - 0.5).abs() < f64::EPSILON);
}
#[test]
fn builder_label() {
let mut p = sample_pie();
p.label("my pie");
assert_eq!(p.label.as_deref(), Some("my pie"));
}
#[test]
fn builder_chaining() {
let mut p = sample_pie();
p.labels(vec!["A", "B", "C", "D", "E"])
.autopct(true)
.explode(vec![0.05, 0.0, 0.0, 0.0, 0.0])
.start_angle(0.0)
.radius(0.9)
.label("chained");
assert!(p.autopct);
assert!((p.start_angle - 0.0).abs() < f64::EPSILON);
assert!((p.radius - 0.9).abs() < f64::EPSILON);
assert_eq!(p.label.as_deref(), Some("chained"));
assert_eq!(p.labels.as_ref().unwrap()[0], "A");
}
#[test]
fn data_bounds_default() {
let p = sample_pie();
let (xmin, xmax, ymin, ymax) = p.data_bounds();
assert!((xmin - (-1.1)).abs() < f64::EPSILON);
assert!((xmax - 1.1).abs() < f64::EPSILON);
assert!((ymin - (-1.1)).abs() < f64::EPSILON);
assert!((ymax - 1.1).abs() < f64::EPSILON);
}
#[test]
fn data_bounds_custom_radius() {
let mut p = sample_pie();
p.radius(2.0);
let (xmin, xmax, ymin, ymax) = p.data_bounds();
assert!((xmin - (-2.2)).abs() < f64::EPSILON);
assert!((xmax - 2.2).abs() < f64::EPSILON);
assert!((ymin - (-2.2)).abs() < f64::EPSILON);
assert!((ymax - 2.2).abs() < f64::EPSILON);
}
#[test]
fn data_bounds_with_explode() {
let mut p = sample_pie();
p.explode(vec![0.2, 0.0, 0.0, 0.0, 0.0]);
let (xmin, xmax, ymin, ymax) = p.data_bounds();
// max_explode = 0.2, extent = 1.0 * (1.0 + 0.2) + 0.1 = 1.3
assert!((xmin - (-1.3)).abs() < f64::EPSILON);
assert!((xmax - 1.3).abs() < f64::EPSILON);
assert!((ymin - (-1.3)).abs() < f64::EPSILON);
assert!((ymax - 1.3).abs() < f64::EPSILON);
}
#[test]
fn data_bounds_empty_sizes() {
let p = PieArtist {
sizes: vec![],
labels: None,
colors: None,
explode: None,
autopct: false,
start_angle: 90.0,
radius: 1.0,
label: None,
color: Color::TAB_BLUE,
};
let (xmin, xmax, ymin, ymax) = p.data_bounds();
assert!((xmin - (-1.1)).abs() < f64::EPSILON);
assert!((xmax - 1.1).abs() < f64::EPSILON);
assert!((ymin - (-1.1)).abs() < f64::EPSILON);
assert!((ymax - 1.1).abs() < f64::EPSILON);
}
}