Skip to content

Commit

Permalink
Introducing DensityMapbox
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume W. Bres <guillaume.bres@bertin.group>
  • Loading branch information
Guillaume W. Bres committed Sep 21, 2023
1 parent 3a9f7ef commit dcbee20
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
1 change: 1 addition & 0 deletions plotly/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub enum PlotType {
Ohlc,
Sankey,
Surface,
DensityMapbox,
}

#[derive(Serialize, Clone, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions plotly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub use plot::{ImageFormat, Plot, Trace};
pub use traces::{box_plot, contour, histogram, image, mesh3d, sankey, scatter_mapbox, surface};
// Bring the different trace types into the top-level scope
pub use traces::{
Bar, BoxPlot, Candlestick, Contour, HeatMap, Histogram, Image, Mesh3D, Ohlc, Sankey, Scatter,
Scatter3D, ScatterMapbox, ScatterPolar, Surface,
Bar, BoxPlot, Candlestick, Contour, DensityMapbox, HeatMap, Histogram, Image, Mesh3D, Ohlc,
Sankey, Scatter, Scatter3D, ScatterMapbox, ScatterPolar, Surface,
};

pub trait Restyle: serde::Serialize {}
Expand Down
133 changes: 133 additions & 0 deletions plotly/src/traces/density_mapbox.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//! Density mapbox scatter plot

use plotly_derive::FieldSetter;
use serde::Serialize;

use crate::common::{
color::Color, Dim, Font, HoverInfo, Label, LegendGroupTitle, Line, Marker, Mode, PlotType,
Position, Visible,
};
use crate::private::{NumOrString, NumOrStringCollection};
use crate::traces::scatter_mapbox::Fill;
use crate::Trace;

#[serde_with::skip_serializing_none]
#[derive(Serialize, Clone, Debug, FieldSetter)]
#[field_setter(box_self, kind = "trace")]
pub struct DensityMapbox<Lat, Lon, Z>
where
Lat: Serialize + Clone,
Lon: Serialize + Clone,
Z: Serialize + Clone,
{
#[field_setter(default = "PlotType::DensityMapbox")]
r#type: PlotType,
/// Sets the trace name. The trace name appear as the legend item and on
/// hover.
name: Option<String>,
/// Determines whether or not this trace is visible. If
/// `Visible::LegendOnly`, the trace is not drawn, but can appear as a
/// legend item (provided that the legend itself is visible).
visible: Option<Visible>,

/// Determines whether or not an item corresponding to this trace is shown
/// in the legend.
#[serde(rename = "showlegend")]
show_legend: Option<bool>,

/// Sets the area to fill with a solid color. Defaults to "none" unless this
/// trace is stacked, then it gets "tonexty" ("tonextx") if
/// `orientation` is "v" ("h") Use with `fillcolor` if not
/// "none". "tozerox" and "tozeroy" fill to x=0 and y=0 respectively.
/// "tonextx" and "tonexty" fill between the endpoints of this trace and
/// the endpoints of the trace before it, connecting those endpoints
/// with straight lines (to make a stacked area graph); if there is
/// no trace before it, they behave like "tozerox" and "tozeroy". "toself"
/// connects the endpoints of the trace (or each segment of the trace if
/// it has gaps) into a closed shape. "tonext" fills the space between
/// two traces if one completely encloses the other (eg consecutive
/// contour lines), and behaves like "toself" if there is no trace before
/// it. "tonext" should not be used if one trace does not enclose the
/// other. Traces in a `stackgroup` will only fill to (or be filled to)
/// other traces in the same group. With multiple `stackgroup`s or some
/// traces stacked and some not, if fill-linked traces are not
/// already consecutive, the later ones will be pushed down in the drawing
/// order.
fill: Option<Fill>,
/// Sets the fill color. Defaults to a half-transparent variant of the line
/// color, marker color, or marker line color, whichever is available.
#[serde(rename = "fillcolor")]
fill_color: Option<Box<dyn Color>>,
/// Sets the legend rank for this trace. Items and groups with smaller ranks
/// are presented on top/left side while with `"reversed"
/// `legend.trace_order` they are on bottom/right side. The default
/// legendrank is 1000, so that you can use ranks less than 1000 to
/// place certain items before all unranked items, and ranks greater
/// than 1000 to go after all unranked items.
#[serde(rename = "legendrank")]
legend_rank: Option<usize>,
/// Sets the legend group for this trace. Traces part of the same legend
/// group show/hide at the same time when toggling legend items.
#[serde(rename = "legendgroup")]
legend_group: Option<String>,
/// Set and style the title to appear for the legend group.
#[serde(rename = "legendgrouptitle")]
legend_group_title: Option<LegendGroupTitle>,

/// Line display properties.
line: Option<Line>,

lat: Option<Vec<Lat>>,
lon: Option<Vec<Lon>>,
z: Option<Vec<Z>>,

/// Sets a reference between this trace's data coordinates and a mapbox
/// subplot. If "mapbox" (the default value), the data refer to
/// `layout.mapbox`. If "mapbox2", the data refer to `layout.mapbox2`, and
/// so on.
subplot: Option<String>,

/// Determines whether or not the color domain is computed
/// with respect to the input data (here in `z`) or the bounds set
/// in `zmin` and `zmax`. Defaults to false when `zmin` and `zmax` are
/// set by the user.
zauto: Option<bool>,

/// Sets the upper bound of the color domain. Value should have the
/// same units as in `z` and if set, `zmin` must be set as well.
zmax: Option<Z>,

zmid: Option<Z>,

zmin: Option<Z>,
}

impl<Lat, Lon, Z> DensityMapbox<Lat, Lon, Z>
where
Lat: Serialize + Clone + std::default::Default, // TODO why is "+ Default" necessary?
Lon: Serialize + Clone + std::default::Default,
Z: Serialize + Clone + std::default::Default,
{
pub fn new(lat: Vec<Lat>, lon: Vec<Lon>, z: Vec<Z>) -> Box<Self> {
Box::new(Self {
lat: Some(lat),
lon: Some(lon),
z: Some(z),
..Default::default()
})
}
}

impl<Lat, Lon, Z> Trace for DensityMapbox<Lat, Lon, Z>
where
Lat: Serialize + Clone,
Lon: Serialize + Clone,
Z: Serialize + Clone,
{
fn to_json(&self) -> String {
serde_json::to_string(&self).unwrap()
}
}

#[cfg(test)]
mod tests {}
2 changes: 2 additions & 0 deletions plotly/src/traces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod bar;
pub mod box_plot;
mod candlestick;
pub mod contour;
mod density_mapbox;
mod heat_map;
pub mod histogram;
pub mod image;
Expand All @@ -20,6 +21,7 @@ pub use bar::Bar;
pub use box_plot::BoxPlot;
pub use candlestick::Candlestick;
pub use contour::Contour;
pub use density_mapbox::DensityMapbox;
pub use heat_map::HeatMap;
pub use histogram::Histogram;
pub use mesh3d::Mesh3D;
Expand Down

0 comments on commit dcbee20

Please sign in to comment.