Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
sankey_parser.rs

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