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