mdbook_plotly/code_handler/plot_obj_parser/
heat_map_parser.rs1use super::common::parse_color_bar;
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::{HeatMap, Trace};
7
8pub fn parse_heat_map_data(
9 hm_obj: &mut serde_json::Value,
10 context: &ParseContext<'_>,
11) -> Result<Box<HeatMap<f64, f64, Vec<f64>>>> {
12 let z: Vec<Vec<f64>> = must_translate_from_context(hm_obj, context, "z")?;
13 let heat_map = if hm_obj.get("x").is_some() {
14 let x: Vec<f64> = must_translate_from_context(hm_obj, context, "x")?;
15 let y: Vec<f64> = must_translate_from_context(hm_obj, context, "y")?;
16 HeatMap::new(x, y, z)
17 } else {
18 HeatMap::new_z(z)
19 };
20
21 let heat_map = translate_with_config! {
22 heat_map,
23 hm_obj,
24 context.map(),
25 context.map_eval(),
26 (name, String),
27 (opacity, f64),
28 (hover_template, String),
29 (hover_template_array, Vec<String>),
30 (hover_text, String),
31 (hover_text_array, Vec<String>),
32 (hover_text_matrix, Vec<Vec<String>>),
33 (text, String),
34 (text_array, Vec<String>),
35 (text_matrix, Vec<Vec<String>>),
36 (show_legend, bool),
37 (legend_group, String),
38 (x_axis, String),
39 (y_axis, String),
40 (connect_gaps, bool),
41 (transpose, bool),
42 (auto_color_scale, bool),
43 (reverse_scale, bool),
44 (show_scale, bool),
45 (zauto, bool),
46 (zmax, f64),
47 (zmin, f64),
48 (zmid, f64),
49 (x_gap, usize),
50 (y_gap, usize),
51 }?;
52
53 use plotly::common::HoverInfo;
54 let heat_map = translate_enum_with_config! {
55 heat_map,
56 hm_obj,
57 context.map(),
58 context.map_eval(),
59 (hover_info, {
60 "all" => HoverInfo::All,
61 "x" => HoverInfo::X,
62 "y" => HoverInfo::Y,
63 "z" => HoverInfo::Z,
64 "x+y" => HoverInfo::XAndY,
65 "x+z" => HoverInfo::XAndZ,
66 "y+z" => HoverInfo::YAndZ,
67 "x+y+z" => HoverInfo::XAndYAndZ,
68 "text" => HoverInfo::Text,
69 "name" => HoverInfo::Name,
70 "none" => HoverInfo::None,
71 "skip" => HoverInfo::Skip,
72 }),
73 }?;
74
75 let heat_map = if let Some(color_bar_obj) = hm_obj.get_mut("color_bar")
76 && color_bar_obj.is_object()
77 {
78 let color_bar = parse_color_bar(color_bar_obj, context)?;
79 heat_map.color_bar(color_bar)
80 } else {
81 heat_map
82 };
83
84 let heat_map = if hm_obj.get("color_scale").is_some() {
85 use plotly::common::{ColorScale, ColorScalePalette};
86 let color_scale_str: String = must_translate_from_context(hm_obj, context, "color_scale")?;
87 let palette = match color_scale_str.to_lowercase().as_str() {
88 "greys" => ColorScalePalette::Greys,
89 "ylgnbu" => ColorScalePalette::YlGnBu,
90 "greens" => ColorScalePalette::Greens,
91 "ylorrd" => ColorScalePalette::YlOrRd,
92 "bluered" => ColorScalePalette::Bluered,
93 "rdbu" => ColorScalePalette::RdBu,
94 "reds" => ColorScalePalette::Reds,
95 "blues" => ColorScalePalette::Blues,
96 "picnic" => ColorScalePalette::Picnic,
97 "rainbow" => ColorScalePalette::Rainbow,
98 "portland" => ColorScalePalette::Portland,
99 "jet" => ColorScalePalette::Jet,
100 "hot" => ColorScalePalette::Hot,
101 "blackbody" => ColorScalePalette::Blackbody,
102 "earth" => ColorScalePalette::Earth,
103 "electric" => ColorScalePalette::Electric,
104 "viridis" => ColorScalePalette::Viridis,
105 "cividis" => ColorScalePalette::Cividis,
106 _ => return Err(anyhow::anyhow!("unknown color_scale: {}", color_scale_str)),
107 };
108 heat_map.color_scale(ColorScale::Palette(palette))
109 } else {
110 heat_map
111 };
112
113 Ok(heat_map)
114}
115
116pub fn parse_heat_map_trace(
117 hm_obj: &mut serde_json::Value,
118 context: &ParseContext<'_>,
119) -> Result<Box<dyn Trace>> {
120 Ok(parse_heat_map_data(hm_obj, context)?)
121}