Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
scatter_polar_parser.rs

1use super::common::parse_marker;
2use super::until::must_translate_from_context;
3use crate::code_handler::parse_context::ParseContext;
4use crate::{translate_enum_with_config, translate_with_config};
5use anyhow::{Ok, Result};
6use plotly::{ScatterPolar, Trace};
7
8pub fn parse_scatter_polar_data(
9    sp_obj: &mut serde_json::Value,
10    context: &ParseContext<'_>,
11) -> Result<Box<ScatterPolar<f64, f64>>> {
12    let theta: Vec<f64> = must_translate_from_context(sp_obj, context, "theta")?;
13    let r: Vec<f64> = must_translate_from_context(sp_obj, context, "r")?;
14    let sp = ScatterPolar::new(theta, r);
15    let sp = translate_with_config! {
16        sp,
17        sp_obj,
18        context.map(),
19        context.map_eval(),
20        (name, String),
21        (show_legend, bool),
22        (legend_group, String),
23        (opacity, f64),
24        (text, String),
25        (text_array, Vec<String>),
26        (hover_text, String),
27        (hover_text_array, Vec<String>),
28        (hover_template, String),
29        (hover_template_array, Vec<String>),
30        (subplot, String),
31        (connect_gaps, bool),
32        (r0, f64),
33        (dr, f64),
34        (theta0, f64),
35        (dtheta, f64),
36    }?;
37
38    use plotly::common::Fill;
39    use plotly::common::Mode;
40    use plotly::common::Visible;
41    let sp = translate_enum_with_config! {
42        sp,
43        sp_obj,
44        context.map(),
45        context.map_eval(),
46        (fill, {
47            "tozeroy" => Fill::ToZeroY,
48            "tozerox" => Fill::ToZeroX,
49            "tonexty" => Fill::ToNextY,
50            "tonextx" => Fill::ToNextX,
51            "toself" =>  Fill::ToSelf,
52            "tonext" =>  Fill::ToNext,
53            "none" =>    Fill::None,
54        }),
55        (mode, {
56            "lines" =>          Mode::Lines,
57            "markers" =>        Mode::Markers,
58            "text" =>           Mode::Text,
59            "linesmarkers" =>   Mode::LinesMarkers,
60            "linestext" =>      Mode::LinesText,
61            "markerstext" =>    Mode::MarkersText,
62            "linemarkerstext" =>Mode::LinesMarkersText,
63            "none" =>           Mode::None,
64        }),
65        (visible, {
66            "true" =>       Visible::True,
67            "false" =>      Visible::False,
68            "legendonly" => Visible::LegendOnly,
69        }),
70    }?;
71
72    let sp = if let Some(marker_obj) = sp_obj.get_mut("marker")
73        && marker_obj.is_object()
74    {
75        let marker = parse_marker(marker_obj, context)?;
76        sp.marker(marker)
77    } else {
78        sp
79    };
80
81    Ok(sp)
82}
83
84pub fn parse_scatter_polar_trace(
85    sp_obj: &mut serde_json::Value,
86    context: &ParseContext<'_>,
87) -> Result<Box<dyn Trace>> {
88    Ok(parse_scatter_polar_data(sp_obj, context)?)
89}