Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
sankey_parser.rs

1use super::until::{Color, Map};
2use crate::{translate, translate_enum};
3use anyhow::Result;
4use plotly::sankey::{Node, Sankey};
5
6pub fn parse_sankey_data(
7    sankey_obj: &mut serde_json::Value,
8    map: &Map,
9) -> Result<Box<Sankey<f64>>> {
10    let sankey = Sankey::new();
11
12    let sankey = if let Some(node_obj) = sankey_obj.get_mut("node")
13        && node_obj.is_object()
14    {
15        let node = translate! {
16            Node::new(),
17            node_obj,
18            map,
19            (color, Color),
20            (color_array, Vec<Color>),
21            (hover_template, String),
22            (pad, usize),
23            (thickness, usize),
24            (x, Vec<f64>),
25            (y, Vec<f64>),
26        }?;
27        sankey.node(node)
28    } else {
29        sankey
30    };
31
32    let sankey = translate! {
33        sankey,
34        sankey_obj,
35        map,
36        (name, String),
37        (visible, bool),
38        (value_format, String),
39        (value_suffix, String),
40    }?;
41
42    use plotly::common::Orientation;
43    use plotly::sankey::Arrangement;
44    let sankey = translate_enum! {
45        sankey,
46        sankey_obj,
47        map,
48        (orientation, {
49            "v" => Orientation::Vertical,
50            "h" => Orientation::Horizontal,
51        }),
52        (arrangement, {
53            "snap" =>           Arrangement::Snap,
54            "perpendicular" =>  Arrangement::Perpendicular,
55            "freeform" =>       Arrangement::Freeform,
56            "fixed" =>          Arrangement::Fixed,
57        }),
58    }?;
59
60    Ok(sankey)
61}