Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
scatter_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::{Scatter, Trace};
7
8pub fn parse_scatter_data(
9    sc_obj: &mut serde_json::Value,
10    context: &ParseContext<'_>,
11) -> Result<Box<Scatter<f64, f64>>> {
12    let x: Vec<f64> = must_translate_from_context(sc_obj, context, "x")?;
13    let y: Vec<f64> = must_translate_from_context(sc_obj, context, "y")?;
14    let sc = Scatter::new(x, y);
15    let sc = translate_with_config! {
16        sc,
17        sc_obj,
18        context.map(),
19        context.map_eval(),
20        (web_gl_mode, bool),
21        (x0, f64),
22        (dx, f64),
23        (y0, f64),
24        (dy, f64),
25        (ids, Vec<String>),
26        (text, String),
27        (text_array, Vec<String>),
28        (text_template, String),
29        (hover_template, String),
30        (hover_template_array, Vec<String>),
31        (hover_text, String),
32        (hover_text_array, Vec<String>),
33        (name, String),
34        (opacity, f64),
35        (meta, String),
36        (x_axis, String),
37        (y_axis, String),
38        (stack_group, String),
39        (clip_on_axis, bool),
40        (connect_gaps, bool),
41        (fill_color, Color),
42        (show_legend, bool),
43        (legend_group, String),
44    }?;
45
46    use plotly::common::Fill;
47    use plotly::common::Mode;
48    let sc = translate_enum_with_config! {
49        sc,
50        sc_obj,
51        context.map(),
52        context.map_eval(),
53        (fill, {
54            "tozeroy" => Fill::ToZeroY,
55            "tozerox" => Fill::ToZeroX,
56            "tonexty" => Fill::ToNextY,
57            "tonextx" => Fill::ToNextX,
58            "toself" =>  Fill::ToSelf,
59            "tonext" =>  Fill::ToNext,
60            "none" =>    Fill::None,
61        }),
62        (mode, {
63            "lines" =>          Mode::Lines,
64            "markers" =>        Mode::Markers,
65            "text" =>           Mode::Text,
66            "linesmarkers" =>   Mode::LinesMarkers,
67            "linestext" =>      Mode::LinesText,
68            "markerstext" =>    Mode::MarkersText,
69            "linemarkerstext" =>Mode::LinesMarkersText,
70            "none" =>           Mode::None,
71        }),
72    }?;
73
74    let sc = if let Some(marker_obj) = sc_obj.get_mut("marker")
75        && marker_obj.is_object()
76    {
77        let marker = parse_marker(marker_obj, context)?;
78        sc.marker(marker)
79    } else {
80        sc
81    };
82
83    Ok(sc)
84}
85
86pub fn parse_scatter_trace(
87    sc_obj: &mut serde_json::Value,
88    context: &ParseContext<'_>,
89) -> Result<Box<dyn Trace>> {
90    Ok(parse_scatter_data(sc_obj, context)?)
91}