plotly/traces/
density_mapbox.rs1use plotly_derive::FieldSetter;
4use serde::Serialize;
5
6use crate::common::{LegendGroupTitle, Line, PlotType, Visible};
7use crate::Trace;
8
9#[serde_with::skip_serializing_none]
10#[derive(Serialize, Clone, Debug, FieldSetter)]
11#[field_setter(box_self, kind = "trace")]
12pub struct DensityMapbox<Lat, Lon, Z>
13where
14 Lat: Serialize + Clone,
15 Lon: Serialize + Clone,
16 Z: Serialize + Clone,
17{
18 #[field_setter(default = "PlotType::DensityMapbox")]
19 r#type: PlotType,
20 name: Option<String>,
23 visible: Option<Visible>,
27
28 #[serde(rename = "showlegend")]
31 show_legend: Option<bool>,
32
33 #[serde(rename = "legendrank")]
40 legend_rank: Option<usize>,
41 #[serde(rename = "legendgroup")]
44 legend_group: Option<String>,
45 #[serde(rename = "legendgrouptitle")]
47 legend_group_title: Option<LegendGroupTitle>,
48
49 line: Option<Line>,
51
52 lat: Option<Vec<Lat>>,
53 lon: Option<Vec<Lon>>,
54 z: Option<Vec<Z>>,
55
56 opacity: Option<f64>,
58
59 subplot: Option<String>,
64
65 zauto: Option<bool>,
70
71 zmax: Option<Z>,
74
75 zmid: Option<Z>,
76
77 zmin: Option<Z>,
78
79 zoom: Option<u8>,
80
81 radius: Option<u8>,
82 }
85
86impl<Lat, Lon, Z> DensityMapbox<Lat, Lon, Z>
87where
88 Lat: Serialize + Clone + std::default::Default, Lon: Serialize + Clone + std::default::Default,
90 Z: Serialize + Clone + std::default::Default,
91{
92 pub fn new(lat: Vec<Lat>, lon: Vec<Lon>, z: Vec<Z>) -> Box<Self> {
93 Box::new(Self {
94 lat: Some(lat),
95 lon: Some(lon),
96 z: Some(z),
97 ..Default::default()
98 })
99 }
100}
101
102impl<Lat, Lon, Z> Trace for DensityMapbox<Lat, Lon, Z>
103where
104 Lat: Serialize + Clone,
105 Lon: Serialize + Clone,
106 Z: Serialize + Clone,
107{
108 fn to_json(&self) -> String {
109 serde_json::to_string(&self).unwrap()
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use serde_json::{json, to_value};
116
117 use super::*;
118
119 #[test]
120 fn serialize_density_mapbox() {
121 let density_mapbox = DensityMapbox::new(vec![45.5017], vec![-73.5673], vec![1.0])
122 .name("name")
123 .visible(Visible::True)
124 .show_legend(true)
125 .legend_rank(1000)
126 .legend_group("legend group")
127 .zoom(5)
128 .radius(20)
129 .opacity(0.5);
130 let expected = json!({
131 "type": "densitymapbox",
132 "lat": [45.5017],
133 "lon": [-73.5673],
134 "z": [1.0],
135 "name": "name",
136 "visible": true,
137 "showlegend": true,
138 "legendrank": 1000,
139 "legendgroup": "legend group",
140 "opacity": 0.5,
141 "zoom": 5,
142 "radius": 20,
143 });
144 assert_eq!(to_value(density_mapbox.clone()).unwrap(), expected);
145 }
146}