|
| 1 | +//! Funnel trace |
| 2 | +
|
| 3 | +use plotly_derive::FieldSetter; |
| 4 | +use serde::Serialize; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + color::Color, |
| 8 | + common::{ |
| 9 | + ConstrainText, Dim, Font, HoverInfo, Label, LegendGroupTitle, Line, Marker, Orientation, |
| 10 | + PlotType, TextAnchor, TextPosition, Visible, XAxisId, YAxisId, |
| 11 | + }, |
| 12 | + Trace, |
| 13 | +}; |
| 14 | + |
| 15 | +/// Visually connects consecutive funnel stages. |
| 16 | +#[serde_with::skip_serializing_none] |
| 17 | +#[derive(Serialize, Clone, Debug, FieldSetter)] |
| 18 | +pub struct Connector { |
| 19 | + visible: Option<bool>, |
| 20 | + line: Option<Line>, |
| 21 | + #[serde(rename = "fillcolor")] |
| 22 | + fill_color: Option<Box<dyn Color>>, |
| 23 | +} |
| 24 | + |
| 25 | +impl Connector { |
| 26 | + pub fn new() -> Self { |
| 27 | + Default::default() |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// Construct a funnel trace. |
| 32 | +/// |
| 33 | +/// # Examples |
| 34 | +/// |
| 35 | +/// ``` |
| 36 | +/// use plotly::Funnel; |
| 37 | +/// |
| 38 | +/// let x = vec![100, 60, 40]; |
| 39 | +/// let y = vec!["Visits", "Signups", "Purchases"]; |
| 40 | +/// |
| 41 | +/// let trace = Funnel::new(x, y) |
| 42 | +/// .orientation(plotly::common::Orientation::Horizontal) |
| 43 | +/// .text_info("value+percent previous"); |
| 44 | +/// |
| 45 | +/// let expected = serde_json::json!({ |
| 46 | +/// "type": "funnel", |
| 47 | +/// "x": [100, 60, 40], |
| 48 | +/// "y": ["Visits", "Signups", "Purchases"], |
| 49 | +/// "orientation": "h", |
| 50 | +/// "textinfo": "value+percent previous" |
| 51 | +/// }); |
| 52 | +/// |
| 53 | +/// assert_eq!(serde_json::to_value(trace).unwrap(), expected); |
| 54 | +/// ``` |
| 55 | +#[serde_with::skip_serializing_none] |
| 56 | +#[derive(Serialize, Debug, Clone, FieldSetter)] |
| 57 | +#[field_setter(box_self, kind = "trace")] |
| 58 | +pub struct Funnel<X, Y> |
| 59 | +where |
| 60 | + X: Serialize + Clone, |
| 61 | + Y: Serialize + Clone, |
| 62 | +{ |
| 63 | + #[field_setter(default = "PlotType::Funnel")] |
| 64 | + r#type: PlotType, |
| 65 | + x: Option<Vec<X>>, |
| 66 | + y: Option<Vec<Y>>, |
| 67 | + name: Option<String>, |
| 68 | + visible: Option<Visible>, |
| 69 | + #[serde(rename = "showlegend")] |
| 70 | + show_legend: Option<bool>, |
| 71 | + #[serde(rename = "legendgroup")] |
| 72 | + legend_group: Option<String>, |
| 73 | + #[serde(rename = "legendgrouptitle")] |
| 74 | + legend_group_title: Option<LegendGroupTitle>, |
| 75 | + opacity: Option<f64>, |
| 76 | + ids: Option<Vec<String>>, |
| 77 | + /// Sets the bar width (in position axis units). Unlike `Bar`, the funnel |
| 78 | + /// trace does not accept a per-point array here. |
| 79 | + width: Option<f64>, |
| 80 | + /// Shifts the position where the bar is drawn (in position axis units). |
| 81 | + /// Unlike `Bar`, the funnel trace does not accept a per-point array here. |
| 82 | + offset: Option<f64>, |
| 83 | + orientation: Option<Orientation>, |
| 84 | + text: Option<Dim<String>>, |
| 85 | + #[serde(rename = "textposition")] |
| 86 | + text_position: Option<Dim<TextPosition>>, |
| 87 | + /// Determines which trace information appears on the graph. Plotly.js |
| 88 | + /// expects a `+`-joined flaglist built from any of `label`, `text`, |
| 89 | + /// `percent initial`, `percent previous`, `percent total` and `value`, or |
| 90 | + /// the single value `none`. |
| 91 | + #[serde(rename = "textinfo")] |
| 92 | + text_info: Option<String>, |
| 93 | + #[serde(rename = "texttemplate")] |
| 94 | + text_template: Option<Dim<String>>, |
| 95 | + #[serde(rename = "texttemplatefallback")] |
| 96 | + text_template_fallback: Option<Dim<String>>, |
| 97 | + #[serde(rename = "textangle")] |
| 98 | + text_angle: Option<f64>, |
| 99 | + #[serde(rename = "textfont")] |
| 100 | + text_font: Option<Font>, |
| 101 | + #[serde(rename = "insidetextfont")] |
| 102 | + inside_text_font: Option<Font>, |
| 103 | + #[serde(rename = "outsidetextfont")] |
| 104 | + outside_text_font: Option<Font>, |
| 105 | + #[serde(rename = "insidetextanchor")] |
| 106 | + inside_text_anchor: Option<TextAnchor>, |
| 107 | + #[serde(rename = "constraintext")] |
| 108 | + constrain_text: Option<ConstrainText>, |
| 109 | + #[serde(rename = "cliponaxis")] |
| 110 | + clip_on_axis: Option<bool>, |
| 111 | + #[serde(rename = "hovertext")] |
| 112 | + hover_text: Option<Dim<String>>, |
| 113 | + #[serde(rename = "hoverinfo")] |
| 114 | + hover_info: Option<HoverInfo>, |
| 115 | + #[serde(rename = "hovertemplate")] |
| 116 | + hover_template: Option<Dim<String>>, |
| 117 | + #[serde(rename = "hovertemplatefallback")] |
| 118 | + hover_template_fallback: Option<Dim<String>>, |
| 119 | + #[serde(rename = "hoverlabel")] |
| 120 | + hover_label: Option<Label>, |
| 121 | + marker: Option<Marker>, |
| 122 | + connector: Option<Connector>, |
| 123 | + #[serde(rename = "xaxis")] |
| 124 | + x_axis: Option<XAxisId>, |
| 125 | + #[serde(rename = "yaxis")] |
| 126 | + y_axis: Option<YAxisId>, |
| 127 | + #[serde(rename = "alignmentgroup")] |
| 128 | + alignment_group: Option<String>, |
| 129 | + #[serde(rename = "offsetgroup")] |
| 130 | + offset_group: Option<String>, |
| 131 | +} |
| 132 | + |
| 133 | +impl<X, Y> Funnel<X, Y> |
| 134 | +where |
| 135 | + X: Serialize + Clone, |
| 136 | + Y: Serialize + Clone, |
| 137 | +{ |
| 138 | + pub fn new(x: Vec<X>, y: Vec<Y>) -> Box<Self> { |
| 139 | + Box::new(Funnel { |
| 140 | + x: Some(x), |
| 141 | + y: Some(y), |
| 142 | + ..Default::default() |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl<X, Y> Trace for Funnel<X, Y> |
| 148 | +where |
| 149 | + X: Serialize + Clone, |
| 150 | + Y: Serialize + Clone, |
| 151 | +{ |
| 152 | + fn to_json(&self) -> String { |
| 153 | + serde_json::to_string(self).unwrap() |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#[cfg(test)] |
| 158 | +mod tests { |
| 159 | + use serde_json::{json, to_value}; |
| 160 | + |
| 161 | + use super::*; |
| 162 | + use crate::common::DashType; |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn default_funnel() { |
| 166 | + let trace: Funnel<i32, i32> = Funnel::default(); |
| 167 | + let expected = json!({"type": "funnel"}).to_string(); |
| 168 | + |
| 169 | + assert_eq!(trace.to_json(), expected); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn serialize_connector() { |
| 174 | + let connector = Connector::new() |
| 175 | + .visible(true) |
| 176 | + .line(Line::new().width(2.0).dash(DashType::Dot)) |
| 177 | + .fill_color("#aabbcc"); |
| 178 | + |
| 179 | + let expected = json!({ |
| 180 | + "visible": true, |
| 181 | + "line": {"width": 2.0, "dash": "dot"}, |
| 182 | + "fillcolor": "#aabbcc", |
| 183 | + }); |
| 184 | + |
| 185 | + assert_eq!(to_value(connector).unwrap(), expected); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn serialize_funnel() { |
| 190 | + let funnel = Funnel::new(vec![1, 2], vec![3, 4]) |
| 191 | + .alignment_group("alignment_group") |
| 192 | + .clip_on_axis(true) |
| 193 | + .connector(Connector::new().visible(true)) |
| 194 | + .constrain_text(ConstrainText::Both) |
| 195 | + .hover_info(HoverInfo::All) |
| 196 | + .hover_label(Label::new()) |
| 197 | + .hover_template("tmpl") |
| 198 | + .hover_template_array(vec!["tmpl1", "tmpl2"]) |
| 199 | + .hover_text("hover_text") |
| 200 | + .hover_text_array(vec!["hover_text"]) |
| 201 | + .ids(vec!["1"]) |
| 202 | + .inside_text_anchor(TextAnchor::End) |
| 203 | + .inside_text_font(Font::new()) |
| 204 | + .legend_group("legend-group") |
| 205 | + .legend_group_title("legend-group-title") |
| 206 | + .marker(Marker::new()) |
| 207 | + .name("Funnel") |
| 208 | + .offset(5.0) |
| 209 | + .offset_group("offset_group") |
| 210 | + .opacity(0.5) |
| 211 | + .orientation(Orientation::Horizontal) |
| 212 | + .outside_text_font(Font::new()) |
| 213 | + .show_legend(false) |
| 214 | + .text("text") |
| 215 | + .text_angle(0.05) |
| 216 | + .text_array(vec!["text"]) |
| 217 | + .text_font(Font::new()) |
| 218 | + .text_info("value+percent previous") |
| 219 | + .text_position(TextPosition::Inside) |
| 220 | + .text_position_array(vec![TextPosition::Inside]) |
| 221 | + .text_template("text_template") |
| 222 | + .text_template_array(vec!["text_template"]) |
| 223 | + .visible(Visible::LegendOnly) |
| 224 | + .width(999.0) |
| 225 | + .x_axis("x3") |
| 226 | + .y_axis("y3"); |
| 227 | + |
| 228 | + let expected = json!({ |
| 229 | + "type": "funnel", |
| 230 | + "x": [1, 2], |
| 231 | + "y": [3, 4], |
| 232 | + "name": "Funnel", |
| 233 | + "visible": "legendonly", |
| 234 | + "showlegend": false, |
| 235 | + "legendgroup": "legend-group", |
| 236 | + "legendgrouptitle": {"text": "legend-group-title"}, |
| 237 | + "opacity": 0.5, |
| 238 | + "ids": ["1"], |
| 239 | + "width": 999.0, |
| 240 | + "offset": 5.0, |
| 241 | + "orientation": "h", |
| 242 | + "text": ["text"], |
| 243 | + "textposition": ["inside"], |
| 244 | + "textinfo": "value+percent previous", |
| 245 | + "texttemplate": ["text_template"], |
| 246 | + "textangle": 0.05, |
| 247 | + "textfont": {}, |
| 248 | + "insidetextfont": {}, |
| 249 | + "outsidetextfont": {}, |
| 250 | + "insidetextanchor": "end", |
| 251 | + "constraintext": "both", |
| 252 | + "cliponaxis": true, |
| 253 | + "hovertext": ["hover_text"], |
| 254 | + "hoverinfo": "all", |
| 255 | + "hovertemplate": ["tmpl1", "tmpl2"], |
| 256 | + "hoverlabel": {}, |
| 257 | + "marker": {}, |
| 258 | + "connector": {"visible": true}, |
| 259 | + "xaxis": "x3", |
| 260 | + "yaxis": "y3", |
| 261 | + "alignmentgroup": "alignment_group", |
| 262 | + "offsetgroup": "offset_group", |
| 263 | + }); |
| 264 | + |
| 265 | + assert_eq!(to_value(funnel).unwrap(), expected); |
| 266 | + } |
| 267 | + |
| 268 | + #[test] |
| 269 | + fn serialize_horizontal_funnel_in_subplot() { |
| 270 | + let funnel = Funnel::new( |
| 271 | + vec![1234567890.0, 800000000.0, 500000000.0], |
| 272 | + vec!["Spent", "Committed", "Planned"], |
| 273 | + ) |
| 274 | + .orientation(Orientation::Horizontal) |
| 275 | + .name("Pipeline funnel: totalplannedcommit -> commit_total -> spent_total") |
| 276 | + .text_info("value+percent previous") |
| 277 | + .text_position(TextPosition::Inside) |
| 278 | + .hover_text_array(vec!["a", "b", "c"]) |
| 279 | + .hover_template("%{hovertext}<extra></extra>") |
| 280 | + .marker(Marker::new().color_array(vec!["#111111", "#222222", "#333333"])) |
| 281 | + .connector(Connector::new().visible(true)) |
| 282 | + .x_axis("x3") |
| 283 | + .y_axis("y3"); |
| 284 | + |
| 285 | + let expected = json!({ |
| 286 | + "type": "funnel", |
| 287 | + "x": [1234567890.0, 800000000.0, 500000000.0], |
| 288 | + "y": ["Spent", "Committed", "Planned"], |
| 289 | + "orientation": "h", |
| 290 | + "name": "Pipeline funnel: totalplannedcommit -> commit_total -> spent_total", |
| 291 | + "textinfo": "value+percent previous", |
| 292 | + "textposition": "inside", |
| 293 | + "hovertext": ["a", "b", "c"], |
| 294 | + "hovertemplate": "%{hovertext}<extra></extra>", |
| 295 | + "marker": {"color": ["#111111", "#222222", "#333333"]}, |
| 296 | + "connector": {"visible": true}, |
| 297 | + "xaxis": "x3", |
| 298 | + "yaxis": "y3", |
| 299 | + }); |
| 300 | + |
| 301 | + assert_eq!(to_value(funnel).unwrap(), expected); |
| 302 | + } |
| 303 | +} |
0 commit comments