Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
scatter_geo_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::Result;
6use plotly::{ScatterGeo, Trace};
7
8pub fn parse_scatter_geo_data(
9    sg_obj: &mut serde_json::Value,
10    context: &ParseContext<'_>,
11) -> Result<Box<ScatterGeo<f64, f64>>> {
12    let lat: Vec<f64> = must_translate_from_context(sg_obj, context, "lat")?;
13    let lon: Vec<f64> = must_translate_from_context(sg_obj, context, "lon")?;
14    let sg = ScatterGeo::new(lat, lon);
15    let sg = translate_with_config! {
16        sg,
17        sg_obj,
18        context.map(),
19        context.map_eval(),
20        (ids, Vec<String>),
21        (show_legend, bool),
22        (name, String),
23        (legend_group, String),
24        (legend_rank, usize),
25        (opacity, f64),
26        (text, String),
27        (text_array, Vec<String>),
28        (text_template, String),
29        (text_template_array, Vec<String>),
30        (hover_text, String),
31        (hover_text_array, Vec<String>),
32        (hover_template, String),
33        (hover_template_array, Vec<String>),
34        (connect_gaps, bool),
35        (subplot, String),
36        (below, String),
37    }?;
38
39    use plotly::common::Mode;
40    let sg = translate_enum_with_config! {
41        sg,
42        sg_obj,
43        context.map(),
44        context.map_eval(),
45        (mode, {
46            "lines" =>          Mode::Lines,
47            "markers" =>        Mode::Markers,
48            "text" =>           Mode::Text,
49            "linesmarkers" =>   Mode::LinesMarkers,
50            "linestext" =>      Mode::LinesText,
51            "markerstext" =>    Mode::MarkersText,
52            "linemarkerstext" =>Mode::LinesMarkersText,
53            "none" =>           Mode::None,
54        }),
55    }?;
56
57    let sg = if let Some(marker_obj) = sg_obj.get_mut("marker")
58        && marker_obj.is_object()
59    {
60        let marker = parse_marker(marker_obj, context)?;
61        sg.marker(marker)
62    } else {
63        sg
64    };
65
66    Ok(sg)
67}
68
69pub fn parse_scatter_geo_trace(
70    sg_obj: &mut serde_json::Value,
71    context: &ParseContext<'_>,
72) -> Result<Box<dyn Trace>> {
73    Ok(parse_scatter_geo_data(sg_obj, context)?)
74}