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
/// Determines the direction cells are filled across the grid.
///
/// The default is [`RowMajorTopLeft`](FillOrder::RowMajorTopLeft), which fills
/// left-to-right, top-to-bottom (reading order).
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FillOrder {
/// Left-to-right, top-to-bottom (reading order). Default.
RowMajorTopLeft,
/// Left-to-right, bottom-to-top (filling-up, like a progress bar).
RowMajorBottomLeft,
/// Top-to-bottom, left-to-right (column-first).
ColMajorTopLeft,
/// Bottom-to-top, left-to-right.
ColMajorBottomLeft,
}
/// Shape used to render each cell.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CellShape {
/// Filled square (default).
Square,
/// Filled circle inscribed in the cell.
Circle,
}
/// A single category in a [`WafflePlot`].
#[derive(Debug, Clone)]
pub struct WaffleCategory {
pub label: String,
pub value: f64,
/// Fill color as a CSS color string (e.g. `"steelblue"`, `"#4682b4"`).
pub color: String,
}
/// Builder for a waffle chart.
///
/// Proportions are encoded as colored cells in a rectangular grid.
/// The Largest Remainder (Hamilton) method guarantees the filled cell count
/// always equals exactly `rows × cols`, regardless of rounding.
///
/// # Example
///
/// ```rust,no_run
/// use kuva::plot::{WafflePlot, FillOrder, CellShape};
/// use kuva::render::plots::Plot;
/// use kuva::render::layout::Layout;
/// use kuva::render::render::render_multiple;
/// use kuva::backend::svg::SvgBackend;
///
/// let waffle = WafflePlot::new()
/// .with_category("Treated", 45.0, "steelblue")
/// .with_category("Partial", 30.0, "gold")
/// .with_category("Untreated", 25.0, "#e74c3c")
/// .with_legend("Status")
/// .with_show_percents();
///
/// let plots = vec![Plot::Waffle(waffle)];
/// let layout = Layout::auto_from_plots(&plots).with_title("Treatment coverage");
/// let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
/// std::fs::write("waffle.svg", svg).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct WafflePlot {
pub categories: Vec<WaffleCategory>,
/// Number of rows in the grid. Default: 10.
pub rows: usize,
/// Number of columns in the grid. Default: 10.
pub cols: usize,
/// Gap between cells as a fraction of cell size. `0.0` = no gap; `0.2`
/// gives visible gutters. Default: `0.1`.
pub gap: f64,
/// Fill direction. Default: [`FillOrder::RowMajorTopLeft`].
pub fill_order: FillOrder,
/// Cell shape. Default: [`CellShape::Square`].
pub shape: CellShape,
/// Fill color for unused / background cells. Default: `"#e8e8e8"`.
pub empty_color: String,
/// When `Some`, a legend is rendered. The string is not currently used as a
/// legend title but triggers legend entry generation in
/// [`collect_legend_entries`](crate::render::render::collect_legend_entries).
pub legend_label: Option<String>,
/// When `true`, percentage of total is appended to each legend label.
pub show_percents: bool,
/// When `true`, cell count is appended to each legend label.
pub show_counts: bool,
/// Optional annotation displayed below the grid, e.g. `"■ = 1,000 people"`.
pub unit_label: Option<String>,
}
impl Default for WafflePlot {
fn default() -> Self {
Self::new()
}
}
impl WafflePlot {
/// Create a waffle chart with default settings (10×10 grid, square cells,
/// row-major top-left fill, 10% gap, light-grey background cells).
pub fn new() -> Self {
Self {
categories: vec![],
rows: 10,
cols: 10,
gap: 0.1,
fill_order: FillOrder::RowMajorTopLeft,
shape: CellShape::Square,
empty_color: "#e8e8e8".into(),
legend_label: None,
show_percents: false,
show_counts: false,
unit_label: None,
}
}
/// Add a category with a label, proportional value, and fill color.
///
/// Values are proportional — only their relative magnitudes matter.
/// Cells are assigned using Largest Remainder rounding so the total
/// always equals exactly `rows × cols`.
///
/// ```rust,no_run
/// # use kuva::plot::WafflePlot;
/// let w = WafflePlot::new()
/// .with_category("Yes", 60.0, "steelblue")
/// .with_category("No", 40.0, "tomato");
/// ```
pub fn with_category<L, V, C>(mut self, label: L, value: V, color: C) -> Self
where
L: Into<String>,
V: Into<f64>,
C: Into<String>,
{
self.categories.push(WaffleCategory {
label: label.into(),
value: value.into(),
color: color.into(),
});
self
}
/// Add multiple categories at once.
///
/// Each item is `(label, value, color)`.
///
/// ```rust,no_run
/// # use kuva::plot::WafflePlot;
/// let w = WafflePlot::new()
/// .with_categories([
/// ("A", 50.0, "steelblue"),
/// ("B", 30.0, "gold"),
/// ("C", 20.0, "tomato"),
/// ]);
/// ```
pub fn with_categories<L, V, C>(mut self, items: impl IntoIterator<Item = (L, V, C)>) -> Self
where
L: Into<String>,
V: Into<f64>,
C: Into<String>,
{
for (l, v, c) in items {
self.categories.push(WaffleCategory {
label: l.into(),
value: v.into(),
color: c.into(),
});
}
self
}
/// Set the grid dimensions.
///
/// A 10×10 grid cleanly represents percentages (1 cell per percent).
/// A 5×20 or 4×25 grid gives more horizontal space for wide canvases.
///
/// ```rust,no_run
/// # use kuva::plot::WafflePlot;
/// // 5 rows × 20 cols = 100 cells, wide aspect ratio
/// let w = WafflePlot::new().with_grid(5, 20);
/// ```
pub fn with_grid(mut self, rows: usize, cols: usize) -> Self {
self.rows = rows.max(1);
self.cols = cols.max(1);
self
}
/// Set the number of rows. See also [`with_grid`](Self::with_grid).
pub fn with_rows(mut self, rows: usize) -> Self {
self.rows = rows.max(1);
self
}
/// Set the number of columns. See also [`with_grid`](Self::with_grid).
pub fn with_cols(mut self, cols: usize) -> Self {
self.cols = cols.max(1);
self
}
/// Set the gap between cells as a fraction of cell size.
///
/// `0.0` produces a solid grid with no gutters; `0.2` gives clearly
/// separated cells. Default: `0.1`.
pub fn with_gap(mut self, gap: f64) -> Self {
self.gap = gap.clamp(0.0, 0.5);
self
}
/// Set the fill direction.
///
/// Controls where cell 0 is placed and which direction subsequent cells
/// are assigned. Default: [`FillOrder::RowMajorTopLeft`] (reading order).
pub fn with_fill_order(mut self, order: FillOrder) -> Self {
self.fill_order = order;
self
}
/// Set the cell shape.
///
/// Default: [`CellShape::Square`]. Use [`CellShape::Circle`] for a more
/// bubbly, infographic aesthetic.
pub fn with_shape(mut self, shape: CellShape) -> Self {
self.shape = shape;
self
}
/// Set the fill color for background (unfilled) cells.
///
/// Default: `"#e8e8e8"`. Set to the canvas background color (`"white"` or
/// `"none"`) to hide empty cells entirely.
pub fn with_empty_color<C: Into<String>>(mut self, color: C) -> Self {
self.empty_color = color.into();
self
}
/// Attach a legend.
///
/// When set, [`render_multiple`](crate::render::render::render_multiple)
/// adds a colored-square legend entry for each category. Use
/// [`with_show_percents`](Self::with_show_percents) or
/// [`with_show_counts`](Self::with_show_counts) to annotate entries.
pub fn with_legend<S: Into<String>>(mut self, label: S) -> Self {
self.legend_label = Some(label.into());
self
}
/// Append the percentage of total to each legend label.
///
/// Example: `"Treated (45.0%)"`. Requires [`with_legend`](Self::with_legend).
pub fn with_show_percents(mut self) -> Self {
self.show_percents = true;
self
}
/// Append the cell count to each legend label.
///
/// Example: `"Treated (45 cells)"`. Requires [`with_legend`](Self::with_legend).
pub fn with_show_counts(mut self) -> Self {
self.show_counts = true;
self
}
/// Add a unit annotation displayed below the grid.
///
/// Useful for absolute-count data where each cell represents a fixed
/// quantity, e.g. `"■ = 100 people"`.
///
/// ```rust,no_run
/// # use kuva::plot::WafflePlot;
/// let w = WafflePlot::new()
/// .with_category("Positive", 2700.0, "steelblue")
/// .with_category("Negative", 7300.0, "#e8e8e8")
/// .with_unit_label("■ = 100 people");
/// ```
pub fn with_unit_label<S: Into<String>>(mut self, label: S) -> Self {
self.unit_label = Some(label.into());
self
}
/// Total number of cells in the grid (`rows × cols`).
pub fn total_cells(&self) -> usize {
self.rows * self.cols
}
}