Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
scatter3d_parser.rs

1use super::common::parse_marker;
2use super::until::{Color, must_translate_from_context};
3use crate::code_handler::parse_context::ParseContext;
4use crate::{translate_enum_with_config, translate_with_config};
5use anyhow::Result;
6use plotly::{Scatter3D, Trace};
7
8pub fn parse_scatter3d_data(
9    s3_obj: &mut serde_json::Value,
10    context: &ParseContext<'_>,
11) -> Result<Box<Scatter3D<f64, f64, f64>>> {
12    let x: Vec<f64> = must_translate_from_context(s3_obj, context, "x")?;
13    let y: Vec<f64> = must_translate_from_context(s3_obj, context, "y")?;
14    let z: Vec<f64> = must_translate_from_context(s3_obj, context, "z")?;
15    let s3 = Scatter3D::new(x, y, z);
16
17    let s3 = translate_with_config! {
18        s3,
19        s3_obj,
20        context.map(),
21        context.map_eval(),
22        (name, String),
23        (opacity, f64),
24        (ids, Vec<String>),
25        (text, String),
26        (text_array, Vec<String>),
27        (text_template, String),
28        (text_template_array, Vec<String>),
29        (hover_text, String),
30        (hover_text_array, Vec<String>),
31        (hover_template, String),
32        (hover_template_array, Vec<String>),
33        (show_legend, bool),
34        (legend_group, String),
35        (legend_rank, usize),
36        (surface_color, Color),
37        (connect_gaps, bool),
38        (scene, String),
39        (meta, String),
40    }?;
41
42    use plotly::common::{HoverInfo, Mode, Position};
43    let s3 = translate_enum_with_config! {
44        s3,
45        s3_obj,
46        context.map(),
47        context.map_eval(),
48        (mode, {
49            "lines" =>          Mode::Lines,
50            "markers" =>        Mode::Markers,
51            "text" =>           Mode::Text,
52            "linesmarkers" =>   Mode::LinesMarkers,
53            "linestext" =>      Mode::LinesText,
54            "markerstext" =>    Mode::MarkersText,
55            "linemarkerstext" =>Mode::LinesMarkersText,
56            "none" =>           Mode::None,
57        }),
58        (hover_info, {
59            "all" => HoverInfo::All,
60            "x" => HoverInfo::X,
61            "y" => HoverInfo::Y,
62            "z" => HoverInfo::Z,
63            "x+y" => HoverInfo::XAndY,
64            "x+z" => HoverInfo::XAndZ,
65            "y+z" => HoverInfo::YAndZ,
66            "x+y+z" => HoverInfo::XAndYAndZ,
67            "text" => HoverInfo::Text,
68            "name" => HoverInfo::Name,
69            "none" => HoverInfo::None,
70            "skip" => HoverInfo::Skip,
71        }),
72        (text_position, {
73            "top left" =>       Position::TopLeft,
74            "top center" =>     Position::TopCenter,
75            "top right" =>      Position::TopRight,
76            "middle left" =>    Position::MiddleLeft,
77            "middle center" =>  Position::MiddleCenter,
78            "middle right" =>   Position::MiddleRight,
79            "bottom left" =>    Position::BottomLeft,
80            "bottom center" =>  Position::BottomCenter,
81            "bottom right" =>   Position::BottomRight,
82        }),
83    }?;
84
85    use plotly::traces::scatter3d::SurfaceAxis;
86    let s3 = translate_enum_with_config! {
87        s3,
88        s3_obj,
89        context.map(),
90        context.map_eval(),
91        (surface_axis, {
92            "-1" => SurfaceAxis::MinusOne,
93            "0" =>  SurfaceAxis::Zero,
94            "1" =>  SurfaceAxis::One,
95            "2" =>  SurfaceAxis::Two,
96        }),
97    }?;
98
99    let s3 = if let Some(marker_obj) = s3_obj.get_mut("marker")
100        && marker_obj.is_object()
101    {
102        let marker = parse_marker(marker_obj, context)?;
103        s3.marker(marker)
104    } else {
105        s3
106    };
107
108    let s3 = if let Some(line_obj) = s3_obj.get_mut("line")
109        && line_obj.is_object()
110    {
111        use plotly::common::Line;
112        let line = translate_with_config! {
113            Line::new(),
114            line_obj,
115            context.map(),
116            context.map_eval(),
117            (color, Color),
118            (width, f64),
119        }?;
120        use plotly::common::DashType;
121        let line = translate_enum_with_config! {
122            line,
123            line_obj,
124            context.map(),
125            context.map_eval(),
126            (dash, {
127                "solid" => DashType::Solid,
128                "dot" => DashType::Dot,
129                "dash" => DashType::Dash,
130                "longdash" => DashType::LongDash,
131                "dashdot" => DashType::DashDot,
132                "longdashdot" => DashType::LongDashDot,
133            }),
134        }?;
135        s3.line(line)
136    } else {
137        s3
138    };
139
140    Ok(s3)
141}
142
143pub fn parse_scatter3d_trace(
144    s3_obj: &mut serde_json::Value,
145    context: &ParseContext<'_>,
146) -> Result<Box<dyn Trace>> {
147    Ok(parse_scatter3d_data(s3_obj, context)?)
148}