mdbook_plotly/code_handler/plot_obj_parser/
bar_parser.rs1use 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::{Bar, Trace};
7
8pub fn parse_bar_data(
9 bar_obj: &mut serde_json::Value,
10 context: &ParseContext<'_>,
11) -> Result<Box<Bar<f64, f64>>> {
12 let x: Vec<f64> = must_translate_from_context(bar_obj, context, "x")?;
13 let y: Vec<f64> = must_translate_from_context(bar_obj, context, "y")?;
14 let bar = Bar::new(x, y);
15 let bar = translate_with_config! {
16 bar,
17 bar_obj,
18 context.map(),
19 context.map_eval(),
20 (ids, Vec<String>),
21 (offset, f64),
22 (offset_array, Vec<f64>),
23 (text, String),
24 (text_array, Vec<String>),
25 (text_template, String),
26 (hover_template, String),
27 (hover_template_array, Vec<String>),
28 (hover_text, String),
29 (hover_text_array, Vec<String>),
30 (name, String),
31 (opacity, f64),
32 (x_axis, String),
33 (y_axis, String),
34 (alignment_group, String),
35 (offset_group, String),
36 (clip_on_axis, bool),
37 (show_legend, bool),
38 (legend_group, String),
39 (width, f64),
40 (text_angle, f64),
41 }?;
42
43 use plotly::common::Orientation;
44 let bar = translate_enum_with_config! {
45 bar,
46 bar_obj,
47 context.map(),
48 context.map_eval(),
49 (orientation, {
50 "v" => Orientation::Vertical,
51 "h" => Orientation::Horizontal,
52 }),
53 }?;
54
55 let bar = if let Some(marker_obj) = bar_obj.get_mut("marker")
56 && marker_obj.is_object()
57 {
58 let marker = parse_marker(marker_obj, context)?;
59 bar.marker(marker)
60 } else {
61 bar
62 };
63
64 Ok(bar)
65}
66
67pub fn parse_bar_trace(
68 bar_obj: &mut serde_json::Value,
69 context: &ParseContext<'_>,
70) -> Result<Box<dyn Trace>> {
71 Ok(parse_bar_data(bar_obj, context)?)
72}