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
/// Builder for a bar chart.
///
/// Supports three modes depending on how data is added:
///
/// - **Simple** — one bar per category via [`.with_bar()`](BarPlot::with_bar) or
/// [`.with_bars()`](BarPlot::with_bars).
/// - **Grouped** — multiple side-by-side bars per category via
/// [`.with_group()`](BarPlot::with_group).
/// - **Stacked** — bars stacked vertically via `.with_group()` +
/// [`.with_stacked()`](BarPlot::with_stacked).
///
/// # Simple example
///
/// ```rust,no_run
/// use kuva::plot::BarPlot;
/// use kuva::backend::svg::SvgBackend;
/// use kuva::render::render::render_multiple;
/// use kuva::render::layout::Layout;
/// use kuva::render::plots::Plot;
///
/// let plot = BarPlot::new()
/// .with_bars(vec![("Apples", 42.0), ("Bananas", 58.0), ("Cherries", 31.0)])
/// .with_color("steelblue");
///
/// let plots = vec![Plot::Bar(plot)];
/// let layout = Layout::auto_from_plots(&plots)
/// .with_title("Fruit Counts")
/// .with_x_label("Fruit")
/// .with_y_label("Count");
///
/// let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
/// std::fs::write("bar.svg", svg).unwrap();
/// ```
pub struct BarPlot {
pub groups: Vec<BarGroup>,
pub width: f64,
pub legend_label: Option<Vec<String>>,
pub stacked: bool,
pub show_tooltips: bool,
pub tooltip_labels: Option<Vec<String>>,
}
/// A single category group containing one or more bars.
///
/// In simple mode each group holds one `BarValue`. In grouped and stacked
/// modes each group holds one `BarValue` per series.
#[derive(Debug, Clone)]
pub struct BarGroup {
pub label: String,
pub bars: Vec<BarValue>,
}
/// A single bar segment with a value and a fill color.
#[derive(Debug, Clone)]
pub struct BarValue {
pub value: f64,
pub color: String,
}
impl Default for BarPlot {
fn default() -> Self {
Self::new()
}
}
impl BarPlot {
/// Create a bar plot with default settings.
///
/// Default bar width is `0.8` (as a fraction of the available slot).
pub fn new() -> Self {
Self {
groups: vec![],
width: 0.8,
legend_label: None,
stacked: false,
show_tooltips: false,
tooltip_labels: None,
}
}
/// Add a group of bars for one category (grouped / stacked mode).
///
/// Each item in `values` is a `(value, color)` pair — one per series.
/// Call this once per x-axis category. Pair with
/// [`.with_legend()`](Self::with_legend) to label the series.
///
/// ```rust,no_run
/// # use kuva::plot::BarPlot;
/// let plot = BarPlot::new()
/// .with_group("Jan", vec![(10.0, "steelblue"), (7.0, "crimson")])
/// .with_group("Feb", vec![(13.0, "steelblue"), (9.0, "crimson")])
/// .with_legend(vec!["Series A", "Series B"]);
/// ```
pub fn with_group<T, V, S, I>(mut self, label: T, values: I) -> Self
where
T: Into<String>,
V: Into<f64>,
S: Into<String>,
I: IntoIterator<Item = (V, S)>,
{
let bars = values
.into_iter()
.map(|(v, c)| BarValue {
value: v.into(),
color: c.into(),
})
.collect();
self.groups.push(BarGroup {
label: label.into(),
bars,
});
self
}
/// Set legend labels for each series (one per bar within a group).
///
/// Must be called after the groups are defined so the label count
/// matches the number of bars per group.
pub fn with_legend(mut self, legend: Vec<&str>) -> Self {
self.legend_label = Some(legend.into_iter().map(|l| l.into()).collect());
self
}
/// Set the bar width as a fraction of the available category slot (default `0.8`).
///
/// Values between `0.0` and `1.0`. A width of `1.0` means bars touch.
/// Complement of [`with_gap`](Self::with_gap): `width = 1.0 - gap`.
pub fn with_width(mut self, width: f64) -> Self {
self.width = width;
self
}
/// Set the gap between bars as a fraction of the category slot (default `0.2`).
///
/// Complement of [`with_width`](Self::with_width): `gap = 1.0 - width`.
/// A gap of `0.0` means bars touch; `0.5` leaves half the slot as space.
pub fn with_gap(mut self, gap: f64) -> Self {
self.width = (1.0 - gap).clamp(0.0, 1.0);
self
}
/// Add a single bar (simple mode).
///
/// The bar is colored with the library default (`"steelblue"`). Use
/// [`.with_color()`](Self::with_color) afterwards to change all bars at once.
///
/// ```rust,no_run
/// # use kuva::plot::BarPlot;
/// let plot = BarPlot::new()
/// .with_bar("A", 3.2)
/// .with_bar("B", 4.7)
/// .with_bar("C", 2.8)
/// .with_color("steelblue");
/// ```
pub fn with_bar<T: Into<String>>(mut self, label: T, value: impl Into<f64>) -> Self {
let color = self.default_color();
let l = label.into();
self.groups.push(BarGroup {
label: l.clone(),
bars: vec![BarValue {
value: value.into(),
color,
}],
});
self
}
/// Add a single bar with an explicit color (simple mode).
///
/// Use this when each bar should have its own color — for example when
/// bars represent distinct categories such as nucleotide variants.
/// Unlike [`.with_bar()`](Self::with_bar) + [`.with_color()`](Self::with_color),
/// this sets only that bar's color without touching the others.
///
/// ```rust,no_run
/// # use kuva::plot::BarPlot;
/// let plot = BarPlot::new()
/// .with_colored_bar("A2C", 42.0, "steelblue")
/// .with_colored_bar("A2G", 58.0, "seagreen")
/// .with_colored_bar("A2T", 31.0, "tomato");
/// ```
pub fn with_colored_bar<T, V, S>(mut self, label: T, value: V, color: S) -> Self
where
T: Into<String>,
V: Into<f64>,
S: Into<String>,
{
self.groups.push(BarGroup {
label: label.into(),
bars: vec![BarValue {
value: value.into(),
color: color.into(),
}],
});
self
}
/// Add multiple bars with per-bar colors at once (simple mode).
///
/// Each item is a `(label, value, color)` triple. Equivalent to calling
/// [`.with_colored_bar()`](Self::with_colored_bar) for each item.
///
/// ```rust,no_run
/// # use kuva::plot::BarPlot;
/// let variants = vec![
/// ("A2C", 42.0, "steelblue"),
/// ("A2G", 58.0, "seagreen"),
/// ("A2T", 31.0, "tomato"),
/// ("C2A", 25.0, "gold"),
/// ];
/// let plot = BarPlot::new().with_colored_bars(variants);
/// ```
pub fn with_colored_bars<I, T, V, S>(mut self, data: I) -> Self
where
I: IntoIterator<Item = (T, V, S)>,
T: Into<String>,
V: Into<f64>,
S: Into<String>,
{
for (label, value, color) in data {
self.groups.push(BarGroup {
label: label.into(),
bars: vec![BarValue {
value: value.into(),
color: color.into(),
}],
});
}
self
}
/// Add multiple bars at once (simple mode).
///
/// Equivalent to calling [`.with_bar()`](Self::with_bar) for each item.
/// Chain [`.with_color()`](Self::with_color) to set a uniform color.
///
/// ```rust,no_run
/// # use kuva::plot::BarPlot;
/// let plot = BarPlot::new()
/// .with_bars(vec![("A", 3.2), ("B", 4.7), ("C", 2.8)])
/// .with_color("steelblue");
/// ```
pub fn with_bars<T, V, I>(mut self, data: I) -> Self
where
T: Into<String>,
V: Into<f64>,
I: IntoIterator<Item = (T, V)>,
{
let color = self.default_color();
for (label, value) in data {
self.groups.push(BarGroup {
label: label.into(),
bars: vec![BarValue {
value: value.into(),
color: color.clone(),
}],
});
}
self
}
/// Set a uniform color for all bars added so far.
///
/// Overwrites the color on every existing bar. In simple mode, call
/// this after [`.with_bar()`](Self::with_bar) /
/// [`.with_bars()`](Self::with_bars). Not needed in grouped/stacked
/// mode, where colors are set per-value in
/// [`.with_group()`](Self::with_group).
pub fn with_color<S: Into<String>>(mut self, color: S) -> Self {
let c = color.into();
for group in &mut self.groups {
for bar in &mut group.bars {
bar.color = c.clone();
}
}
self
}
/// Enable stacked mode.
///
/// Instead of placing bars side-by-side, segments are stacked
/// vertically within each category. Requires groups to be defined
/// with [`.with_group()`](Self::with_group).
pub fn with_stacked(mut self) -> Self {
self.stacked = true;
self
}
fn default_color(&self) -> String {
"steelblue".into()
}
pub fn with_tooltips(mut self) -> Self {
self.show_tooltips = true;
self
}
pub fn with_tooltip_labels(
mut self,
labels: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
self.tooltip_labels = Some(labels.into_iter().map(|s| s.into()).collect());
self
}
}