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
/// How bar colors are assigned in a funnel chart.
#[derive(Debug, Clone, Default)]
pub enum FunnelColorMode {
/// All bars share a single palette color. **(default)**
#[default]
Uniform,
/// Each stage gets a distinct category10 palette color.
ByStage,
/// Bars darken progressively from top to bottom (hue from the first palette color).
Gradient,
}
/// Orientation of the funnel (direction of flow).
#[derive(Debug, Clone, Default)]
pub enum FunnelOrientation {
/// Top-to-bottom: widest bar at the top. **(default)**
#[default]
Vertical,
/// Left-to-right: widest bar on the left.
Horizontal,
}
/// One stage (bar) in a [`FunnelPlot`].
#[derive(Debug, Clone)]
pub struct FunnelStage {
pub label: String,
pub value: f64,
/// Optional explicit CSS color. Falls back to the resolved color mode.
pub color: Option<String>,
}
impl FunnelStage {
pub fn new(label: impl Into<String>, value: f64) -> Self {
FunnelStage {
label: label.into(),
value,
color: None,
}
}
pub fn colored(label: impl Into<String>, value: f64, color: impl Into<String>) -> Self {
FunnelStage {
label: label.into(),
value,
color: Some(color.into()),
}
}
}
/// Funnel chart — shows attrition / conversion through ordered stages.
///
/// Each bar represents one stage; widths are proportional to stage values.
/// Trapezoidal connectors between bars make the attrition visually explicit.
///
/// # Basic usage
///
/// ```rust,no_run
/// use kuva::plot::funnel::FunnelPlot;
/// use kuva::render::{plots::Plot, layout::Layout, render::render_multiple};
/// use kuva::backend::svg::SvgBackend;
///
/// let plot = FunnelPlot::new()
/// .with_stage("Screened", 1200)
/// .with_stage("Eligible", 800)
/// .with_stage("Enrolled", 600)
/// .with_stage("Completed", 540);
///
/// let plots = vec![Plot::Funnel(plot)];
/// let layout = Layout::auto_from_plots(&plots).with_title("CONSORT Flow");
/// let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
/// std::fs::write("funnel.svg", svg).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct FunnelPlot {
/// Ordered stages, first = largest / entry point.
pub stages: Vec<FunnelStage>,
/// Orientation of the funnel (default: [`FunnelOrientation::Vertical`]).
pub orientation: FunnelOrientation,
/// Draw trapezoidal connectors between adjacent bars. Default: `true`.
pub show_connectors: bool,
/// Connector fill opacity (0–1). Default: `0.4`.
/// Named to mirror [`SankeyPlot::link_opacity`](crate::plot::sankey::SankeyPlot::link_opacity).
pub connector_opacity: f64,
/// Show absolute value labels on each bar. Default: `true`.
pub show_values: bool,
/// Show percentage-of-first-stage labels alongside value labels. Default: `false`.
pub show_percents: bool,
/// Show step-to-step conversion rate in each connector. Default: `true`.
pub show_conversion: bool,
/// Bar color mode. Default: [`FunnelColorMode::Uniform`].
pub color_mode: FunnelColorMode,
/// Gap in pixels between adjacent bars. Default: `4.0`.
/// Named to mirror [`SankeyPlot::node_gap`](crate::plot::sankey::SankeyPlot::node_gap).
pub stage_gap: f64,
/// If set, a legend entry is rendered for each stage.
/// Named to mirror [`SankeyPlot::legend_label`](crate::plot::sankey::SankeyPlot::legend_label).
pub legend_label: Option<String>,
// ── Mirror (diverging) mode ───────────────────────────────────────────────
/// Right-side stages for a back-to-back diverging funnel. `None` = standard funnel.
pub mirror: Option<Vec<FunnelStage>>,
/// Label placed above the left (standard) side in diverging mode.
pub left_label: Option<String>,
/// Label placed above the right (mirror) side in diverging mode.
pub right_label: Option<String>,
}
impl Default for FunnelPlot {
fn default() -> Self {
Self::new()
}
}
impl FunnelPlot {
/// Create a [`FunnelPlot`] with default settings.
pub fn new() -> Self {
FunnelPlot {
stages: vec![],
orientation: FunnelOrientation::Vertical,
show_connectors: true,
connector_opacity: 0.4,
show_values: true,
show_percents: false,
show_conversion: true,
color_mode: FunnelColorMode::Uniform,
stage_gap: 4.0,
legend_label: None,
mirror: None,
left_label: None,
right_label: None,
}
}
// ── Data ──────────────────────────────────────────────────────────────────
/// Add a stage using any numeric value type.
pub fn with_stage(mut self, label: impl Into<String>, value: impl Into<f64>) -> Self {
self.stages.push(FunnelStage::new(label, value.into()));
self
}
/// Add a stage with an explicit CSS color.
pub fn with_stage_color(
mut self,
label: impl Into<String>,
value: impl Into<f64>,
color: impl Into<String>,
) -> Self {
self.stages
.push(FunnelStage::colored(label, value.into(), color));
self
}
/// Add multiple stages at once from an iterator of `(label, value)`.
/// Mirrors [`SankeyPlot::with_links`](crate::plot::sankey::SankeyPlot::with_links).
pub fn with_stages<S, I>(mut self, stages: I) -> Self
where
S: Into<String>,
I: IntoIterator<Item = (S, f64)>,
{
for (label, value) in stages {
self.stages.push(FunnelStage::new(label, value));
}
self
}
// ── Mirror / diverging mode ───────────────────────────────────────────────
/// Enable diverging (back-to-back) mode with a second set of stages.
/// The right side mirrors the left side around a shared center axis.
pub fn with_mirror(mut self, stages: impl IntoIterator<Item = FunnelStage>) -> Self {
self.mirror = Some(stages.into_iter().collect());
self
}
/// Add a mirror stage using `(label, value)` — convenience wrapper for `with_mirror`.
pub fn with_mirror_stages<S, I>(mut self, stages: I) -> Self
where
S: Into<String>,
I: IntoIterator<Item = (S, f64)>,
{
let v: Vec<FunnelStage> = stages
.into_iter()
.map(|(l, v)| FunnelStage::new(l, v))
.collect();
self.mirror = Some(v);
self
}
/// Set labels for the left and right sides of a diverging funnel.
pub fn with_mirror_labels(mut self, left: impl Into<String>, right: impl Into<String>) -> Self {
self.left_label = Some(left.into());
self.right_label = Some(right.into());
self
}
// ── Appearance ────────────────────────────────────────────────────────────
/// Set funnel orientation (vertical / horizontal).
pub fn with_orientation(mut self, orientation: FunnelOrientation) -> Self {
self.orientation = orientation;
self
}
/// Show / hide trapezoidal connectors between bars.
pub fn with_connectors(mut self, show: bool) -> Self {
self.show_connectors = show;
self
}
/// Set connector fill opacity.
/// Mirrors [`SankeyPlot::with_link_opacity`](crate::plot::sankey::SankeyPlot::with_link_opacity).
pub fn with_connector_opacity(mut self, opacity: f64) -> Self {
self.connector_opacity = opacity.clamp(0.0, 1.0);
self
}
/// Show / hide absolute value labels on each bar.
pub fn with_show_values(mut self, show: bool) -> Self {
self.show_values = show;
self
}
/// Show / hide percentage-of-first-stage labels.
pub fn with_show_percents(mut self, show: bool) -> Self {
self.show_percents = show;
self
}
/// Show / hide step-to-step conversion rate in connector areas.
pub fn with_show_conversion(mut self, show: bool) -> Self {
self.show_conversion = show;
self
}
/// Set the bar color mode.
pub fn with_color_mode(mut self, mode: FunnelColorMode) -> Self {
self.color_mode = mode;
self
}
/// Set the gap in pixels between adjacent bars.
/// Mirrors [`SankeyPlot::with_node_gap`](crate::plot::sankey::SankeyPlot::with_node_gap).
pub fn with_stage_gap(mut self, gap: f64) -> Self {
self.stage_gap = gap.max(0.0);
self
}
/// Enable the legend; each stage gets one entry.
/// Mirrors [`SankeyPlot::with_legend`](crate::plot::sankey::SankeyPlot::with_legend).
pub fn with_legend(mut self, label: impl Into<String>) -> Self {
self.legend_label = Some(label.into());
self
}
// ── Internal helpers ──────────────────────────────────────────────────────
/// Number of stages (used for estimated_primitives).
pub(crate) fn stage_count(&self) -> usize {
self.stages
.len()
.max(self.mirror.as_ref().map_or(0, |m| m.len()))
}
/// Maximum value across all stages (both sides in mirror mode).
pub(crate) fn max_value(&self) -> f64 {
let left = self.stages.iter().map(|s| s.value).fold(0.0_f64, f64::max);
let right = self
.mirror
.as_ref()
.map(|m| m.iter().map(|s| s.value).fold(0.0_f64, f64::max))
.unwrap_or(0.0);
left.max(right)
}
}