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
/// A single data point in a lollipop chart.
pub struct LollipopPoint {
pub x: f64,
pub y: f64,
/// Optional text label rendered above (or below, when y < baseline) the dot.
pub label: Option<String>,
/// Per-point color override. `None` uses the plot-level `color`.
pub color: Option<String>,
}
/// A colored annotation band drawn behind the stems, anchored to the baseline.
///
/// Typical use: protein domain annotations below a mutation landscape, where
/// each rect covers a functional region along the sequence x-axis.
pub struct LollipopDomain {
pub x_start: f64,
pub x_end: f64,
pub label: Option<String>,
pub color: String,
/// Fill opacity. Default `0.35`.
pub opacity: f64,
}
/// Builder for a lollipop chart.
///
/// Each data point is rendered as a vertical stem (line from `baseline` to `y`)
/// topped with a filled circle. Useful for mutation landscapes, ranked discrete
/// data, and any context where bar charts feel too heavy.
///
/// Optional domain annotations (`with_domain`) draw colored rectangles behind
/// the stems — the canonical presentation for protein mutation landscapes.
///
/// # Example
///
/// ```rust,no_run
/// use kuva::prelude::*;
///
/// let lollipop = LollipopPlot::new()
/// .with_point(10.0, 3.0)
/// .with_labeled_point(25.0, 7.0, "TP53")
/// .with_colored_point(42.0, 5.0, "tomato")
/// .with_domain(1.0, 50.0, Some("Kinase"), "steelblue")
/// .with_baseline(0.0);
///
/// let plots = vec![Plot::from(lollipop)];
/// let layout = Layout::auto_from_plots(&plots)
/// .with_title("Mutation Landscape")
/// .with_x_label("Position (aa)")
/// .with_y_label("Count");
/// ```
pub struct LollipopPlot {
pub points: Vec<LollipopPoint>,
/// Default fill color for stems and dots. Default `"steelblue"`.
pub color: String,
/// Y-value at which stems originate. Default `0.0`.
pub baseline: f64,
/// Stem stroke width in pixels. Default `1.5`.
pub stem_width: f64,
/// Dot radius in pixels. Default `5.0`.
pub dot_radius: f64,
/// Dot stroke color. `None` uses the dot fill color.
pub dot_stroke: Option<String>,
/// Dot stroke width in pixels. Default `1.0`.
pub dot_stroke_width: f64,
/// Draw a horizontal line at `baseline`. Default `true`.
pub show_baseline: bool,
/// Baseline line color. Default `"#888888"`.
pub baseline_color: String,
/// Baseline line stroke width in pixels. Default `1.0`.
pub baseline_width: f64,
/// Baseline line dasharray. Default `None` (solid).
pub baseline_dash: Option<String>,
/// Domain annotation bands rendered behind stems.
pub domains: Vec<LollipopDomain>,
/// Height of domain rects in data-coordinate units below the baseline. Default `0.5`.
pub domain_height: f64,
pub legend_label: Option<String>,
}
impl Default for LollipopPlot {
fn default() -> Self {
Self::new()
}
}
impl LollipopPlot {
/// Create a lollipop plot with default settings.
pub fn new() -> Self {
Self {
points: vec![],
color: "steelblue".into(),
baseline: 0.0,
stem_width: 1.5,
dot_radius: 5.0,
dot_stroke: None,
dot_stroke_width: 1.0,
show_baseline: true,
baseline_color: "#888888".into(),
baseline_width: 1.0,
baseline_dash: None,
domains: vec![],
domain_height: 0.5,
legend_label: None,
}
}
/// Add a point at (`x`, `y`) with no label.
pub fn with_point(mut self, x: impl Into<f64>, y: impl Into<f64>) -> Self {
self.points.push(LollipopPoint {
x: x.into(),
y: y.into(),
label: None,
color: None,
});
self
}
/// Add a point with a text label rendered above (or below) the dot.
pub fn with_labeled_point(
mut self,
x: impl Into<f64>,
y: impl Into<f64>,
label: impl Into<String>,
) -> Self {
self.points.push(LollipopPoint {
x: x.into(),
y: y.into(),
label: Some(label.into()),
color: None,
});
self
}
/// Add a point with a per-point color override.
pub fn with_colored_point(
mut self,
x: impl Into<f64>,
y: impl Into<f64>,
color: impl Into<String>,
) -> Self {
self.points.push(LollipopPoint {
x: x.into(),
y: y.into(),
label: None,
color: Some(color.into()),
});
self
}
/// Add a point with both a label and a per-point color override.
pub fn with_labeled_colored_point(
mut self,
x: impl Into<f64>,
y: impl Into<f64>,
label: impl Into<String>,
color: impl Into<String>,
) -> Self {
self.points.push(LollipopPoint {
x: x.into(),
y: y.into(),
label: Some(label.into()),
color: Some(color.into()),
});
self
}
/// Add multiple unlabeled points from an iterator of `(x, y)` pairs.
pub fn with_points<T, U, I>(mut self, pts: I) -> Self
where
T: Into<f64>,
U: Into<f64>,
I: IntoIterator<Item = (T, U)>,
{
for (x, y) in pts {
self.points.push(LollipopPoint {
x: x.into(),
y: y.into(),
label: None,
color: None,
});
}
self
}
/// Set the default stem and dot color (CSS color string). Default `"steelblue"`.
pub fn with_color(mut self, color: impl Into<String>) -> Self {
self.color = color.into();
self
}
/// Set the baseline Y value (where stems originate). Default `0.0`.
pub fn with_baseline(mut self, v: f64) -> Self {
self.baseline = v;
self
}
/// Set the stem stroke width in pixels. Default `1.5`.
pub fn with_stem_width(mut self, w: f64) -> Self {
self.stem_width = w;
self
}
/// Set the dot radius in pixels. Default `5.0`.
pub fn with_dot_radius(mut self, r: f64) -> Self {
self.dot_radius = r;
self
}
/// Set the dot stroke color (CSS color string). Default is same as fill.
pub fn with_dot_stroke(mut self, color: impl Into<String>) -> Self {
self.dot_stroke = Some(color.into());
self
}
/// Set the dot stroke width in pixels. Default `1.0`.
pub fn with_dot_stroke_width(mut self, w: f64) -> Self {
self.dot_stroke_width = w;
self
}
/// Toggle the horizontal baseline line. Default `true`.
pub fn with_show_baseline(mut self, show: bool) -> Self {
self.show_baseline = show;
self
}
/// Set the baseline line color. Default `"#888888"`.
pub fn with_baseline_color(mut self, c: impl Into<String>) -> Self {
self.baseline_color = c.into();
self
}
/// Set the baseline line stroke width in pixels. Default `1.0`.
pub fn with_baseline_width(mut self, w: f64) -> Self {
self.baseline_width = w;
self
}
/// Set the baseline line dasharray (e.g. `"4,3"`). Default `None` (solid).
pub fn with_baseline_dash(mut self, d: impl Into<String>) -> Self {
self.baseline_dash = Some(d.into());
self
}
/// Add a colored domain annotation band behind the stems.
///
/// `label` is optional text centered inside the rect. `color` is a CSS color string.
pub fn with_domain(
mut self,
x_start: f64,
x_end: f64,
label: Option<&str>,
color: impl Into<String>,
) -> Self {
self.domains.push(LollipopDomain {
x_start,
x_end,
label: label.map(|s| s.to_string()),
color: color.into(),
opacity: 0.35,
});
self
}
/// Add a domain with explicit opacity.
pub fn with_domain_opacity(
mut self,
x_start: f64,
x_end: f64,
label: Option<&str>,
color: impl Into<String>,
opacity: f64,
) -> Self {
self.domains.push(LollipopDomain {
x_start,
x_end,
label: label.map(|s| s.to_string()),
color: color.into(),
opacity,
});
self
}
/// Set the domain rect height in data-coordinate units below the baseline. Default `0.5`.
pub fn with_domain_height(mut self, h: f64) -> Self {
self.domain_height = h;
self
}
/// Attach a legend label to this plot (shows a colored circle entry).
pub fn with_legend(mut self, label: impl Into<String>) -> Self {
self.legend_label = Some(label.into());
self
}
}