Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
contour_parser.rs

1use super::until::{Color, Map, must_translate};
2use crate::{translate, translate_enum};
3use anyhow::Result;
4use plotly::Contour;
5
6pub fn parse_contour_data(
7    ct_obj: &mut serde_json::Value,
8    map: &Map,
9) -> Result<Box<Contour<Vec<f64>, f64, f64>>> {
10    let z: Vec<Vec<f64>> = must_translate(ct_obj, map, "z")?;
11    let contour = if ct_obj.get("x").is_some() {
12        let x: Vec<f64> = must_translate(ct_obj, map, "x")?;
13        let y: Vec<f64> = must_translate(ct_obj, map, "y")?;
14        Contour::new(x, y, z)
15    } else {
16        Contour::new_z(z)
17    };
18
19    let contour = translate! {
20        contour,
21        ct_obj,
22        map,
23        (x0, f64),
24        (dx, f64),
25        (y0, f64),
26        (dy, f64),
27        (opacity, f64),
28        (n_contours, usize),
29        (connect_gaps, bool),
30        (hover_on_gaps, bool),
31        (show_legend, bool),
32        (transpose, bool),
33        (auto_contour, bool),
34        (auto_color_scale, bool),
35        (reverse_scale, bool),
36        (show_scale, bool),
37        (zauto, bool),
38        (fill_color, Color),
39    }?;
40
41    use plotly::common::HoverInfo;
42    let contour = translate_enum! {
43        contour,
44        ct_obj,
45        map,
46        (hover_info, {
47            "all" => HoverInfo::All,
48            "x" => HoverInfo::X,
49            "y" => HoverInfo::Y,
50            "z" => HoverInfo::Z,
51            "x+y" => HoverInfo::XAndY,
52            "x+z" => HoverInfo::XAndZ,
53            "y+z" => HoverInfo::YAndZ,
54            "x+y+z" => HoverInfo::XAndYAndZ,
55            "text" => HoverInfo::Text,
56            "name" => HoverInfo::Name,
57            "none" => HoverInfo::None,
58            "skip" => HoverInfo::Skip,
59        }),
60    }?;
61
62    let contour = if let Some(contours_obj) = ct_obj.get_mut("contours")
63        && contours_obj.is_object()
64    {
65        use plotly::traces::contour::Contours;
66        let contours = translate! {
67            Contours::new(),
68            contours_obj,
69            map,
70            (start, f64),
71            (end, f64),
72            (size, f64),
73        }?;
74
75        use plotly::traces::contour::Coloring;
76        let contours = translate_enum! {
77            contours,
78            contours_obj,
79            map,
80            (coloring, {
81                "fill"        => Coloring::Fill,
82                "heatmap"     => Coloring::HeatMap,
83                "lines"       => Coloring::Lines,
84                "none"        => Coloring::None,
85            }),
86        }?;
87        contour.contours(contours)
88    } else {
89        contour
90    };
91
92    let contour = if let Some(line_obj) = ct_obj.get_mut("line")
93        && line_obj.is_object()
94    {
95        use plotly::common::Line;
96        let line = translate! {
97            Line::new(),
98            line_obj,
99            map,
100            (color, Color),
101            (width, f64),
102        }?;
103        use plotly::common::DashType;
104        let line = translate_enum! {
105            line,
106            line_obj,
107            map,
108            (dash, {
109                "solid" => DashType::Solid,
110                "dot" => DashType::Dot,
111                "dash" => DashType::Dash,
112                "longdash" => DashType::LongDash,
113                "dashdot" => DashType::DashDot,
114                "longdashdot" => DashType::LongDashDot,
115            }),
116        }?;
117        contour.line(line)
118    } else {
119        contour
120    };
121
122    let contour = if let Some(color_bar_obj) = ct_obj.get_mut("color_bar")
123        && color_bar_obj.is_object()
124    {
125        use plotly::common::ColorBar;
126        let color_bar = translate! {
127            ColorBar::new(),
128            color_bar_obj,
129            map,
130            (thickness, usize),
131            (len, usize),
132            (x, f64),
133            (y, f64),
134            (title, String),
135        }?;
136        contour.color_bar(color_bar)
137    } else {
138        contour
139    };
140
141    let contour = if ct_obj.get("color_scale").is_some() {
142        use plotly::common::{ColorScale, ColorScalePalette};
143        let color_scale_str: String = must_translate(ct_obj, map, "color_scale")?;
144        let palette = match color_scale_str.to_lowercase().as_str() {
145            "greys" => ColorScalePalette::Greys,
146            "ylgnbu" => ColorScalePalette::YlGnBu,
147            "greens" => ColorScalePalette::Greens,
148            "ylorrd" => ColorScalePalette::YlOrRd,
149            "bluered" => ColorScalePalette::Bluered,
150            "rdbu" => ColorScalePalette::RdBu,
151            "reds" => ColorScalePalette::Reds,
152            "blues" => ColorScalePalette::Blues,
153            "picnic" => ColorScalePalette::Picnic,
154            "rainbow" => ColorScalePalette::Rainbow,
155            "portland" => ColorScalePalette::Portland,
156            "jet" => ColorScalePalette::Jet,
157            "hot" => ColorScalePalette::Hot,
158            "blackbody" => ColorScalePalette::Blackbody,
159            "earth" => ColorScalePalette::Earth,
160            "electric" => ColorScalePalette::Electric,
161            "viridis" => ColorScalePalette::Viridis,
162            "cividis" => ColorScalePalette::Cividis,
163            _ => return Err(anyhow::anyhow!("unknown color_scale: {}", color_scale_str)),
164        };
165        contour.color_scale(ColorScale::Palette(palette))
166    } else {
167        contour
168    };
169
170    Ok(contour)
171}