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