Skip to content

Commit 86111f3

Browse files
refactor: use string axis ids
Signed-off-by: Mridankan Mandal <xerontitan90@gmail.com>
1 parent 9ce2f57 commit 86111f3

10 files changed

Lines changed: 98 additions & 27 deletions

File tree

plotly/src/common/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,16 @@ pub enum Reference {
893893
Paper,
894894
}
895895

896+
/// Axis id for a 2D cartesian x axis.
897+
///
898+
/// Use `"x"` for the primary axis, `"x2"` for the second axis, and so on.
899+
pub type XAxisId = String;
900+
901+
/// Axis id for a 2D cartesian y axis.
902+
///
903+
/// Use `"y"` for the primary axis, `"y2"` for the second axis, and so on.
904+
pub type YAxisId = String;
905+
896906
#[derive(Serialize, Clone, Debug)]
897907
pub struct Pad {
898908
t: usize,
@@ -1799,6 +1809,14 @@ mod tests {
17991809
assert_eq!(to_value(Reference::Paper).unwrap(), json!("paper"));
18001810
}
18011811

1812+
#[test]
1813+
fn serialize_axis_id() {
1814+
assert_eq!(to_value(XAxisId::from("x")).unwrap(), json!("x"));
1815+
assert_eq!(to_value(XAxisId::from("x3")).unwrap(), json!("x3"));
1816+
assert_eq!(to_value(YAxisId::from("y")).unwrap(), json!("y"));
1817+
assert_eq!(to_value(YAxisId::from("y8")).unwrap(), json!("y8"));
1818+
}
1819+
18021820
#[test]
18031821
#[rustfmt::skip]
18041822
fn serialize_legend_group_title() {

plotly/src/traces/bar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Serialize;
66
use crate::{
77
common::{
88
Calendar, ConstrainText, Dim, ErrorData, Font, HoverInfo, Label, LegendGroupTitle, Marker,
9-
Orientation, PlotType, TextAnchor, TextPosition, Visible,
9+
Orientation, PlotType, TextAnchor, TextPosition, Visible, XAxisId, YAxisId,
1010
},
1111
Trace,
1212
};
@@ -70,9 +70,9 @@ where
7070
#[serde(rename = "hovertemplate")]
7171
hover_template: Option<Dim<String>>,
7272
#[serde(rename = "xaxis")]
73-
x_axis: Option<String>,
73+
x_axis: Option<XAxisId>,
7474
#[serde(rename = "yaxis")]
75-
y_axis: Option<String>,
75+
y_axis: Option<YAxisId>,
7676
orientation: Option<Orientation>,
7777
#[serde(rename = "alignmentgroup")]
7878
alignment_group: Option<String>,

plotly/src/traces/box_plot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
color::Color,
88
common::{
99
Calendar, Dim, HoverInfo, Label, LegendGroupTitle, Line, Marker, Orientation, PlotType,
10-
Visible,
10+
Visible, XAxisId, YAxisId,
1111
},
1212
Trace,
1313
};
@@ -124,9 +124,9 @@ where
124124
#[serde(rename = "hovertemplate")]
125125
hover_template: Option<Dim<String>>,
126126
#[serde(rename = "xaxis")]
127-
x_axis: Option<String>,
127+
x_axis: Option<XAxisId>,
128128
#[serde(rename = "yaxis")]
129-
y_axis: Option<String>,
129+
y_axis: Option<YAxisId>,
130130
orientation: Option<Orientation>,
131131
#[serde(rename = "alignmentgroup")]
132132
alignment_group: Option<String>,

plotly/src/traces/candlestick.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
color::NamedColor,
88
common::{
99
Calendar, Dim, Direction, HoverInfo, Label, LegendGroupTitle, Line, PlotType, Visible,
10+
XAxisId, YAxisId,
1011
},
1112
Trace,
1213
};
@@ -72,9 +73,9 @@ where
7273
#[serde(rename = "hoverinfo")]
7374
hover_info: Option<HoverInfo>,
7475
#[serde(rename = "xaxis")]
75-
x_axis: Option<String>,
76+
x_axis: Option<XAxisId>,
7677
#[serde(rename = "yaxis")]
77-
y_axis: Option<String>,
78+
y_axis: Option<YAxisId>,
7879
line: Option<Line>,
7980
#[serde(rename = "whiskerwidth")]
8081
whisker_width: Option<f64>,

plotly/src/traces/contour.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
color::Color,
88
common::{
99
Calendar, ColorBar, ColorScale, Dim, Font, HoverInfo, Label, LegendGroupTitle, Line,
10-
PlotType, Visible,
10+
PlotType, Visible, XAxisId, YAxisId,
1111
},
1212
private, Trace,
1313
};
@@ -137,9 +137,9 @@ where
137137
#[serde(rename = "hovertemplate")]
138138
hover_template: Option<Dim<String>>,
139139
#[serde(rename = "xaxis")]
140-
x_axis: Option<String>,
140+
x_axis: Option<XAxisId>,
141141
#[serde(rename = "yaxis")]
142-
y_axis: Option<String>,
142+
y_axis: Option<YAxisId>,
143143
line: Option<Line>,
144144
#[serde(rename = "colorbar")]
145145
color_bar: Option<ColorBar>,
@@ -403,8 +403,8 @@ where
403403
Box::new(self)
404404
}
405405

406-
pub fn x_axis(mut self, axis: &str) -> Box<Self> {
407-
self.x_axis = Some(axis.to_string());
406+
pub fn x_axis(mut self, axis: impl Into<XAxisId>) -> Box<Self> {
407+
self.x_axis = Some(axis.into());
408408
Box::new(self)
409409
}
410410

@@ -418,8 +418,8 @@ where
418418
Box::new(self)
419419
}
420420

421-
pub fn y_axis(mut self, axis: &str) -> Box<Self> {
422-
self.y_axis = Some(axis.to_string());
421+
pub fn y_axis(mut self, axis: impl Into<YAxisId>) -> Box<Self> {
422+
self.y_axis = Some(axis.into());
423423
Box::new(self)
424424
}
425425

@@ -657,4 +657,27 @@ mod tests {
657657

658658
assert_eq!(to_value(trace).unwrap(), expected);
659659
}
660+
661+
#[test]
662+
fn serialize_contour_axis_ids() {
663+
use crate::common::{XAxisId, YAxisId};
664+
665+
let x_axis: XAxisId = "x2".into();
666+
let y_axis: YAxisId = "y12".into();
667+
668+
let trace = Contour::new(vec![0., 1.], vec![2., 3.], vec![4., 5.])
669+
.x_axis(x_axis)
670+
.y_axis(y_axis);
671+
672+
let expected = json!({
673+
"type": "contour",
674+
"x": [0.0, 1.0],
675+
"y": [2.0, 3.0],
676+
"z": [4.0, 5.0],
677+
"xaxis": "x2",
678+
"yaxis": "y12",
679+
});
680+
681+
assert_eq!(to_value(trace).unwrap(), expected);
682+
}
660683
}

plotly/src/traces/heat_map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use serde::Serialize;
66
use crate::{
77
common::{
88
Calendar, ColorBar, ColorScale, Dim, HoverInfo, Label, LegendGroupTitle, PlotType, Visible,
9+
XAxisId, YAxisId,
910
},
1011
private::{NumOrString, NumOrStringCollection},
1112
Trace,
@@ -106,14 +107,14 @@ where
106107
visible: Option<Visible>,
107108
x: Option<Vec<X>>,
108109
#[serde(rename = "xaxis")]
109-
x_axis: Option<String>,
110+
x_axis: Option<XAxisId>,
110111
#[serde(rename = "xcalendar")]
111112
x_calendar: Option<Calendar>,
112113
#[serde(rename = "xgap")]
113114
x_gap: Option<NumOrString>,
114115
y: Option<Vec<Y>>,
115116
#[serde(rename = "yaxis")]
116-
y_axis: Option<String>,
117+
y_axis: Option<YAxisId>,
117118
#[serde(rename = "ycalendar")]
118119
y_calendar: Option<Calendar>,
119120
#[serde(rename = "ygap")]

plotly/src/traces/histogram.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::ndarray::ArrayTraces;
1010
use crate::{
1111
common::{
1212
Calendar, Dim, ErrorData, HoverInfo, Label, LegendGroupTitle, Marker, Orientation,
13-
PlotType, Visible,
13+
PlotType, Visible, XAxisId, YAxisId,
1414
},
1515
Trace,
1616
};
@@ -155,14 +155,14 @@ where
155155
visible: Option<Visible>,
156156
x: Option<Vec<H>>,
157157
#[serde(rename = "xaxis")]
158-
x_axis: Option<String>,
158+
x_axis: Option<XAxisId>,
159159
#[serde(rename = "xbins")]
160160
x_bins: Option<Bins>,
161161
#[serde(rename = "xcalendar")]
162162
x_calendar: Option<Calendar>,
163163
y: Option<Vec<H>>,
164164
#[serde(rename = "yaxis")]
165-
y_axis: Option<String>,
165+
y_axis: Option<YAxisId>,
166166
#[serde(rename = "ybins")]
167167
y_bins: Option<Bins>,
168168
#[serde(rename = "ycalendar")]

plotly/src/traces/image.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use plotly_derive::FieldSetter;
88
use serde::Serialize;
99

1010
use crate::color::{Rgb, Rgba};
11-
use crate::common::{Dim, HoverInfo, Label, LegendGroupTitle, PlotType, Visible};
11+
use crate::common::{Dim, HoverInfo, Label, LegendGroupTitle, PlotType, Visible, XAxisId, YAxisId};
1212
use crate::private::{NumOrString, NumOrStringCollection};
1313
use crate::Trace;
1414

@@ -280,13 +280,13 @@ pub struct Image {
280280
/// `Layout::x_axis`. If "x2", the x coordinates
281281
/// refer to `Layout::x_axis2`, and so on.
282282
#[serde(rename = "xaxis")]
283-
x_axis: Option<String>,
283+
x_axis: Option<XAxisId>,
284284
/// Sets a reference between this trace's y coordinates and a 2D cartesian y
285285
/// axis. If "y" (the default value), the y coordinates refer to
286286
/// `Layout::y_axis`. If "y2", the y coordinates
287287
/// refer to `Layout::y_axis2`, and so on.
288288
#[serde(rename = "yaxis")]
289-
y_axis: Option<String>,
289+
y_axis: Option<YAxisId>,
290290

291291
/// Color model used to map the numerical color components described in `z`
292292
/// into colors. If `source` is specified, this attribute will be set to

plotly/src/traces/scatter.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
color::Color,
1212
common::{
1313
Calendar, Dim, ErrorData, Fill, Font, HoverInfo, HoverOn, Label, LegendGroupTitle, Line,
14-
Marker, Mode, Orientation, PlotType, Position, Visible,
14+
Marker, Mode, Orientation, PlotType, Position, Visible, XAxisId, YAxisId,
1515
},
1616
private::{NumOrString, NumOrStringCollection},
1717
Trace,
@@ -180,13 +180,13 @@ where
180180
/// `Layout::x_axis`. If "x2", the x coordinates
181181
/// refer to `Layout::x_axis2`, and so on.
182182
#[serde(rename = "xaxis")]
183-
x_axis: Option<String>,
183+
x_axis: Option<XAxisId>,
184184
/// Sets a reference between this trace's y coordinates and a 2D cartesian y
185185
/// axis. If "y" (the default value), the y coordinates refer to
186186
/// `Layout::y_axis`. If "y2", the y coordinates
187187
/// refer to `Layout::y_axis2`, and so on.
188188
#[serde(rename = "yaxis")]
189-
y_axis: Option<String>,
189+
y_axis: Option<YAxisId>,
190190
/// Only relevant when `stackgroup` is used, and only the first
191191
/// `orientation` found in the `stackgroup` will be used - including if
192192
/// `visible` is "legendonly" but not if it is `false`.
@@ -528,4 +528,26 @@ mod tests {
528528

529529
assert_eq!(to_value(trace).unwrap(), expected);
530530
}
531+
532+
#[test]
533+
fn serialize_scatter_axis_ids() {
534+
use crate::common::{XAxisId, YAxisId};
535+
536+
let x_axis: XAxisId = "x2".into();
537+
let y_axis: YAxisId = "y12".into();
538+
539+
let trace = Scatter::new(vec![0, 1], vec![2, 3])
540+
.x_axis(x_axis)
541+
.y_axis(y_axis);
542+
543+
let expected = json!({
544+
"type": "scatter",
545+
"x": [0, 1],
546+
"y": [2, 3],
547+
"xaxis": "x2",
548+
"yaxis": "y12",
549+
});
550+
551+
assert_eq!(to_value(trace).unwrap(), expected);
552+
}
531553
}

plotly_derive/src/field_setter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,13 @@ impl FieldReceiver {
341341
quote![value.as_ref().to_owned()],
342342
quote![],
343343
),
344-
FieldType::OptionOther(inner_ty) => (quote![#inner_ty], quote![value], quote![]),
344+
FieldType::OptionOther(inner_ty) => {
345+
if matches!(field_ident.to_string().as_str(), "x_axis" | "y_axis") {
346+
(quote![impl Into<#inner_ty>], quote![value.into()], quote![])
347+
} else {
348+
(quote![#inner_ty], quote![value], quote![])
349+
}
350+
}
345351
FieldType::OptionVecString => (
346352
quote![Vec<impl AsRef<str>>],
347353
quote![value.into_iter().map(|v| v.as_ref().to_owned()).collect()],

0 commit comments

Comments
 (0)