Skip to content

Commit d9a9a6f

Browse files
jqnatividadclaude
andcommitted
feat: add hover and color-scale support to DensityMapbox
Extend the DensityMapbox trace with the hover, data-array, and color-control attributes it was missing relative to the plotly.js densitymapbox reference: - hover/text: text, hovertext, hoverinfo, hovertemplate, hovertemplatefallback, hoverlabel - data/misc: ids, meta, customdata, uirevision, below - color: colorscale, autocolorscale, reversescale, showscale, colorbar, coloraxis The FieldSetter derive generates the scalar and *_array builder setters (e.g. hover_text/hover_text_array), so per-point hover text and shared colorscales are now expressible. Existing fields are unchanged. Extends the serialize_density_mapbox test to cover the new keys. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent d11094e commit d9a9a6f

2 files changed

Lines changed: 109 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
77

88
### Added
99

10+
- [[#413](https://github.com/plotly/plotly.rs/issues/413)] Add hover (`hovertext`/`hoverinfo`/`hovertemplate`/`customdata`/`hoverlabel`/`text`) and color-scale (`colorscale`/`colorbar`/`showscale`/`reversescale`/`autocolorscale`/`coloraxis`) support, plus `ids`/`meta`/`uirevision`/`below`, to `DensityMapbox`
1011
- [[#406](https://github.com/plotly/plotly.rs/issues/406)] Expose `plotly.js` 3.1–3.6 attributes
1112

1213
### Changed

plotly/src/traces/density_mapbox.rs

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
use plotly_derive::FieldSetter;
44
use serde::Serialize;
55

6-
use crate::common::{LegendGroupTitle, Line, PlotType, Visible};
6+
use crate::common::{
7+
ColorBar, ColorScale, Dim, HoverInfo, Label, LegendGroupTitle, Line, PlotType, Visible,
8+
};
9+
use crate::private::{NumOrString, NumOrStringCollection};
710
use crate::Trace;
811

912
#[serde_with::skip_serializing_none]
@@ -49,13 +52,59 @@ where
4952
/// Line display properties.
5053
line: Option<Line>,
5154

55+
/// Assigns id labels to each datum. These ids are for object constancy of
56+
/// data points during animation.
57+
ids: Option<Vec<String>>,
58+
5259
lat: Option<Vec<Lat>>,
5360
lon: Option<Vec<Lon>>,
5461
z: Option<Vec<Z>>,
5562

5663
/// Sets the opacity of the trace.
5764
opacity: Option<f64>,
5865

66+
/// Sets text elements associated with each (lat,lon,z) triplet. If a single
67+
/// string, the same string appears over all the data points. If an array of
68+
/// strings, the items are mapped in order to the data points. To be seen,
69+
/// the trace `hover_info` must contain a "text" flag.
70+
text: Option<Dim<String>>,
71+
72+
/// Sets hover text elements associated with each (lat,lon,z) triplet. If a
73+
/// single string, the same string appears over all the data points. If an
74+
/// array of strings, the items are mapped in order to the data points. To
75+
/// be seen, the trace `hover_info` must contain a "text" flag.
76+
#[serde(rename = "hovertext")]
77+
hover_text: Option<Dim<String>>,
78+
79+
/// Determines which trace information appears on hover.
80+
#[serde(rename = "hoverinfo")]
81+
hover_info: Option<HoverInfo>,
82+
83+
/// Template string used for rendering the information that appear on hover
84+
/// box. Note that this will override `hover_info`. Variables are inserted
85+
/// using %{variable}, for example "y: %{y}".
86+
#[serde(rename = "hovertemplate")]
87+
hover_template: Option<Dim<String>>,
88+
89+
/// Fallback string used for rendering the information that appear on hover
90+
/// box when the `hover_template` cannot be evaluated.
91+
#[serde(rename = "hovertemplatefallback")]
92+
hover_template_fallback: Option<Dim<String>>,
93+
94+
/// Properties of label displayed on mouse hover.
95+
#[serde(rename = "hoverlabel")]
96+
hover_label: Option<Label>,
97+
98+
/// Assigns extra meta information associated with this trace that can be
99+
/// used in various text attributes.
100+
meta: Option<NumOrString>,
101+
102+
/// Assigns extra data each datum. This may be useful when listening to
103+
/// hover, click and selection events. Note that, "scatter" traces also
104+
/// appends customdata items in the markers DOM elements.
105+
#[serde(rename = "customdata")]
106+
custom_data: Option<NumOrStringCollection>,
107+
59108
/// Sets a reference between this trace's data coordinates and a mapbox
60109
/// subplot. If "mapbox" (the default value), the data refer to
61110
/// `layout.mapbox`. If "mapbox2", the data refer to `layout.mapbox2`, and
@@ -79,8 +128,46 @@ where
79128
zoom: Option<u8>,
80129

81130
radius: Option<u8>,
82-
//color_continuous_scale: Option<HashMap<Z, NamedColor>>,
83-
//color_continuous_midpoint: Option<ContinuousColorScale>,
131+
132+
/// Sets the colorscale. The colorscale must be an array containing arrays
133+
/// mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named
134+
/// color string.
135+
#[serde(rename = "colorscale")]
136+
color_scale: Option<ColorScale>,
137+
138+
/// Determines whether the colorscale is a default palette
139+
/// (`auto_color_scale: true`) or the palette determined by `color_scale`.
140+
#[serde(rename = "autocolorscale")]
141+
auto_color_scale: Option<bool>,
142+
143+
/// Reverses the color mapping if true. If true, `zmin` will correspond to
144+
/// the last color in the array and `zmax` will correspond to the first
145+
/// color.
146+
#[serde(rename = "reversescale")]
147+
reverse_scale: Option<bool>,
148+
149+
/// Determines whether or not a colorbar is displayed for this trace.
150+
#[serde(rename = "showscale")]
151+
show_scale: Option<bool>,
152+
153+
/// Sets the colorbar properties.
154+
#[serde(rename = "colorbar")]
155+
color_bar: Option<ColorBar>,
156+
157+
/// Sets a reference to a shared color axis (e.g. "coloraxis",
158+
/// "coloraxis2").
159+
#[serde(rename = "coloraxis")]
160+
color_axis: Option<String>,
161+
162+
/// Determines if this trace's layer is displayed below the layer with the
163+
/// given id. By default, density traces are placed below the first layer of
164+
/// type symbol. Set `below` to "" to place it above every other layer.
165+
below: Option<String>,
166+
167+
/// Controls persistence of some user-driven changes to the trace: `visible`
168+
/// only. Defaults to `layout.uirevision`.
169+
#[serde(rename = "uirevision")]
170+
ui_revision: Option<NumOrString>,
84171
}
85172

86173
impl<Lat, Lon, Z> DensityMapbox<Lat, Lon, Z>
@@ -115,6 +202,7 @@ mod tests {
115202
use serde_json::{json, to_value};
116203

117204
use super::*;
205+
use crate::common::ColorScalePalette;
118206

119207
#[test]
120208
fn serialize_density_mapbox() {
@@ -126,7 +214,15 @@ mod tests {
126214
.legend_group("legend group")
127215
.zoom(5)
128216
.radius(20)
129-
.opacity(0.5);
217+
.opacity(0.5)
218+
.hover_text_array(vec!["Montreal"])
219+
.hover_info(HoverInfo::Text)
220+
.hover_template("%{lat}, %{lon}")
221+
.custom_data(vec!["Montreal"])
222+
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis))
223+
.show_scale(true)
224+
.reverse_scale(true)
225+
.below("");
130226
let expected = json!({
131227
"type": "densitymapbox",
132228
"lat": [45.5017],
@@ -140,6 +236,14 @@ mod tests {
140236
"opacity": 0.5,
141237
"zoom": 5,
142238
"radius": 20,
239+
"hovertext": ["Montreal"],
240+
"hoverinfo": "text",
241+
"hovertemplate": "%{lat}, %{lon}",
242+
"customdata": ["Montreal"],
243+
"colorscale": "Viridis",
244+
"showscale": true,
245+
"reversescale": true,
246+
"below": "",
143247
});
144248
assert_eq!(to_value(density_mapbox.clone()).unwrap(), expected);
145249
}

0 commit comments

Comments
 (0)