Skip to content

Commit dcbee20

Browse files
author
Guillaume W. Bres
committed
Introducing DensityMapbox
Signed-off-by: Guillaume W. Bres <[email protected]>
1 parent 3a9f7ef commit dcbee20

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

plotly/src/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ pub enum PlotType {
201201
Ohlc,
202202
Sankey,
203203
Surface,
204+
DensityMapbox,
204205
}
205206

206207
#[derive(Serialize, Clone, Debug)]

plotly/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ pub use plot::{ImageFormat, Plot, Trace};
3333
pub use traces::{box_plot, contour, histogram, image, mesh3d, sankey, scatter_mapbox, surface};
3434
// Bring the different trace types into the top-level scope
3535
pub use traces::{
36-
Bar, BoxPlot, Candlestick, Contour, HeatMap, Histogram, Image, Mesh3D, Ohlc, Sankey, Scatter,
37-
Scatter3D, ScatterMapbox, ScatterPolar, Surface,
36+
Bar, BoxPlot, Candlestick, Contour, DensityMapbox, HeatMap, Histogram, Image, Mesh3D, Ohlc,
37+
Sankey, Scatter, Scatter3D, ScatterMapbox, ScatterPolar, Surface,
3838
};
3939

4040
pub trait Restyle: serde::Serialize {}

plotly/src/traces/density_mapbox.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//! Density mapbox scatter plot
2+
3+
use plotly_derive::FieldSetter;
4+
use serde::Serialize;
5+
6+
use crate::common::{
7+
color::Color, Dim, Font, HoverInfo, Label, LegendGroupTitle, Line, Marker, Mode, PlotType,
8+
Position, Visible,
9+
};
10+
use crate::private::{NumOrString, NumOrStringCollection};
11+
use crate::traces::scatter_mapbox::Fill;
12+
use crate::Trace;
13+
14+
#[serde_with::skip_serializing_none]
15+
#[derive(Serialize, Clone, Debug, FieldSetter)]
16+
#[field_setter(box_self, kind = "trace")]
17+
pub struct DensityMapbox<Lat, Lon, Z>
18+
where
19+
Lat: Serialize + Clone,
20+
Lon: Serialize + Clone,
21+
Z: Serialize + Clone,
22+
{
23+
#[field_setter(default = "PlotType::DensityMapbox")]
24+
r#type: PlotType,
25+
/// Sets the trace name. The trace name appear as the legend item and on
26+
/// hover.
27+
name: Option<String>,
28+
/// Determines whether or not this trace is visible. If
29+
/// `Visible::LegendOnly`, the trace is not drawn, but can appear as a
30+
/// legend item (provided that the legend itself is visible).
31+
visible: Option<Visible>,
32+
33+
/// Determines whether or not an item corresponding to this trace is shown
34+
/// in the legend.
35+
#[serde(rename = "showlegend")]
36+
show_legend: Option<bool>,
37+
38+
/// Sets the area to fill with a solid color. Defaults to "none" unless this
39+
/// trace is stacked, then it gets "tonexty" ("tonextx") if
40+
/// `orientation` is "v" ("h") Use with `fillcolor` if not
41+
/// "none". "tozerox" and "tozeroy" fill to x=0 and y=0 respectively.
42+
/// "tonextx" and "tonexty" fill between the endpoints of this trace and
43+
/// the endpoints of the trace before it, connecting those endpoints
44+
/// with straight lines (to make a stacked area graph); if there is
45+
/// no trace before it, they behave like "tozerox" and "tozeroy". "toself"
46+
/// connects the endpoints of the trace (or each segment of the trace if
47+
/// it has gaps) into a closed shape. "tonext" fills the space between
48+
/// two traces if one completely encloses the other (eg consecutive
49+
/// contour lines), and behaves like "toself" if there is no trace before
50+
/// it. "tonext" should not be used if one trace does not enclose the
51+
/// other. Traces in a `stackgroup` will only fill to (or be filled to)
52+
/// other traces in the same group. With multiple `stackgroup`s or some
53+
/// traces stacked and some not, if fill-linked traces are not
54+
/// already consecutive, the later ones will be pushed down in the drawing
55+
/// order.
56+
fill: Option<Fill>,
57+
/// Sets the fill color. Defaults to a half-transparent variant of the line
58+
/// color, marker color, or marker line color, whichever is available.
59+
#[serde(rename = "fillcolor")]
60+
fill_color: Option<Box<dyn Color>>,
61+
/// Sets the legend rank for this trace. Items and groups with smaller ranks
62+
/// are presented on top/left side while with `"reversed"
63+
/// `legend.trace_order` they are on bottom/right side. The default
64+
/// legendrank is 1000, so that you can use ranks less than 1000 to
65+
/// place certain items before all unranked items, and ranks greater
66+
/// than 1000 to go after all unranked items.
67+
#[serde(rename = "legendrank")]
68+
legend_rank: Option<usize>,
69+
/// Sets the legend group for this trace. Traces part of the same legend
70+
/// group show/hide at the same time when toggling legend items.
71+
#[serde(rename = "legendgroup")]
72+
legend_group: Option<String>,
73+
/// Set and style the title to appear for the legend group.
74+
#[serde(rename = "legendgrouptitle")]
75+
legend_group_title: Option<LegendGroupTitle>,
76+
77+
/// Line display properties.
78+
line: Option<Line>,
79+
80+
lat: Option<Vec<Lat>>,
81+
lon: Option<Vec<Lon>>,
82+
z: Option<Vec<Z>>,
83+
84+
/// Sets a reference between this trace's data coordinates and a mapbox
85+
/// subplot. If "mapbox" (the default value), the data refer to
86+
/// `layout.mapbox`. If "mapbox2", the data refer to `layout.mapbox2`, and
87+
/// so on.
88+
subplot: Option<String>,
89+
90+
/// Determines whether or not the color domain is computed
91+
/// with respect to the input data (here in `z`) or the bounds set
92+
/// in `zmin` and `zmax`. Defaults to false when `zmin` and `zmax` are
93+
/// set by the user.
94+
zauto: Option<bool>,
95+
96+
/// Sets the upper bound of the color domain. Value should have the
97+
/// same units as in `z` and if set, `zmin` must be set as well.
98+
zmax: Option<Z>,
99+
100+
zmid: Option<Z>,
101+
102+
zmin: Option<Z>,
103+
}
104+
105+
impl<Lat, Lon, Z> DensityMapbox<Lat, Lon, Z>
106+
where
107+
Lat: Serialize + Clone + std::default::Default, // TODO why is "+ Default" necessary?
108+
Lon: Serialize + Clone + std::default::Default,
109+
Z: Serialize + Clone + std::default::Default,
110+
{
111+
pub fn new(lat: Vec<Lat>, lon: Vec<Lon>, z: Vec<Z>) -> Box<Self> {
112+
Box::new(Self {
113+
lat: Some(lat),
114+
lon: Some(lon),
115+
z: Some(z),
116+
..Default::default()
117+
})
118+
}
119+
}
120+
121+
impl<Lat, Lon, Z> Trace for DensityMapbox<Lat, Lon, Z>
122+
where
123+
Lat: Serialize + Clone,
124+
Lon: Serialize + Clone,
125+
Z: Serialize + Clone,
126+
{
127+
fn to_json(&self) -> String {
128+
serde_json::to_string(&self).unwrap()
129+
}
130+
}
131+
132+
#[cfg(test)]
133+
mod tests {}

plotly/src/traces/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod bar;
44
pub mod box_plot;
55
mod candlestick;
66
pub mod contour;
7+
mod density_mapbox;
78
mod heat_map;
89
pub mod histogram;
910
pub mod image;
@@ -20,6 +21,7 @@ pub use bar::Bar;
2021
pub use box_plot::BoxPlot;
2122
pub use candlestick::Candlestick;
2223
pub use contour::Contour;
24+
pub use density_mapbox::DensityMapbox;
2325
pub use heat_map::HeatMap;
2426
pub use histogram::Histogram;
2527
pub use mesh3d::Mesh3D;

0 commit comments

Comments
 (0)