Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions plotly/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,16 @@ pub enum Reference {
Paper,
}

/// Axis id for a 2D cartesian x axis.
///
/// Use `"x"` for the primary axis, `"x2"` for the second axis, and so on.
pub type XAxisId = String;

/// Axis id for a 2D cartesian y axis.
///
/// Use `"y"` for the primary axis, `"y2"` for the second axis, and so on.
pub type YAxisId = String;

#[derive(Serialize, Clone, Debug)]
pub struct Pad {
t: usize,
Expand Down Expand Up @@ -1799,6 +1809,14 @@ mod tests {
assert_eq!(to_value(Reference::Paper).unwrap(), json!("paper"));
}

#[test]
fn serialize_axis_id() {
assert_eq!(to_value(XAxisId::from("x")).unwrap(), json!("x"));
assert_eq!(to_value(XAxisId::from("x3")).unwrap(), json!("x3"));
assert_eq!(to_value(YAxisId::from("y")).unwrap(), json!("y"));
assert_eq!(to_value(YAxisId::from("y8")).unwrap(), json!("y8"));
}

#[test]
#[rustfmt::skip]
fn serialize_legend_group_title() {
Expand Down
6 changes: 3 additions & 3 deletions plotly/src/traces/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Serialize;
use crate::{
common::{
Calendar, ConstrainText, Dim, ErrorData, Font, HoverInfo, Label, LegendGroupTitle, Marker,
Orientation, PlotType, TextAnchor, TextPosition, Visible,
Orientation, PlotType, TextAnchor, TextPosition, Visible, XAxisId, YAxisId,
},
Trace,
};
Expand Down Expand Up @@ -70,9 +70,9 @@ where
#[serde(rename = "hovertemplate")]
hover_template: Option<Dim<String>>,
#[serde(rename = "xaxis")]
x_axis: Option<String>,
x_axis: Option<XAxisId>,
#[serde(rename = "yaxis")]
y_axis: Option<String>,
y_axis: Option<YAxisId>,
orientation: Option<Orientation>,
#[serde(rename = "alignmentgroup")]
alignment_group: Option<String>,
Expand Down
6 changes: 3 additions & 3 deletions plotly/src/traces/box_plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
color::Color,
common::{
Calendar, Dim, HoverInfo, Label, LegendGroupTitle, Line, Marker, Orientation, PlotType,
Visible,
Visible, XAxisId, YAxisId,
},
Trace,
};
Expand Down Expand Up @@ -124,9 +124,9 @@ where
#[serde(rename = "hovertemplate")]
hover_template: Option<Dim<String>>,
#[serde(rename = "xaxis")]
x_axis: Option<String>,
x_axis: Option<XAxisId>,
#[serde(rename = "yaxis")]
y_axis: Option<String>,
y_axis: Option<YAxisId>,
orientation: Option<Orientation>,
#[serde(rename = "alignmentgroup")]
alignment_group: Option<String>,
Expand Down
5 changes: 3 additions & 2 deletions plotly/src/traces/candlestick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
color::NamedColor,
common::{
Calendar, Dim, Direction, HoverInfo, Label, LegendGroupTitle, Line, PlotType, Visible,
XAxisId, YAxisId,
},
Trace,
};
Expand Down Expand Up @@ -72,9 +73,9 @@ where
#[serde(rename = "hoverinfo")]
hover_info: Option<HoverInfo>,
#[serde(rename = "xaxis")]
x_axis: Option<String>,
x_axis: Option<XAxisId>,
#[serde(rename = "yaxis")]
y_axis: Option<String>,
y_axis: Option<YAxisId>,
line: Option<Line>,
#[serde(rename = "whiskerwidth")]
whisker_width: Option<f64>,
Expand Down
37 changes: 30 additions & 7 deletions plotly/src/traces/contour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
color::Color,
common::{
Calendar, ColorBar, ColorScale, Dim, Font, HoverInfo, Label, LegendGroupTitle, Line,
PlotType, Visible,
PlotType, Visible, XAxisId, YAxisId,
},
private, Trace,
};
Expand Down Expand Up @@ -137,9 +137,9 @@ where
#[serde(rename = "hovertemplate")]
hover_template: Option<Dim<String>>,
#[serde(rename = "xaxis")]
x_axis: Option<String>,
x_axis: Option<XAxisId>,
#[serde(rename = "yaxis")]
y_axis: Option<String>,
y_axis: Option<YAxisId>,
line: Option<Line>,
#[serde(rename = "colorbar")]
color_bar: Option<ColorBar>,
Expand Down Expand Up @@ -403,8 +403,8 @@ where
Box::new(self)
}

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

Expand All @@ -418,8 +418,8 @@ where
Box::new(self)
}

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

Expand Down Expand Up @@ -657,4 +657,27 @@ mod tests {

assert_eq!(to_value(trace).unwrap(), expected);
}

#[test]
fn serialize_contour_axis_ids() {
use crate::common::{XAxisId, YAxisId};

let x_axis: XAxisId = "x2".into();
let y_axis: YAxisId = "y12".into();

let trace = Contour::new(vec![0., 1.], vec![2., 3.], vec![4., 5.])
.x_axis(x_axis)
.y_axis(y_axis);

let expected = json!({
"type": "contour",
"x": [0.0, 1.0],
"y": [2.0, 3.0],
"z": [4.0, 5.0],
"xaxis": "x2",
"yaxis": "y12",
});

assert_eq!(to_value(trace).unwrap(), expected);
}
}
5 changes: 3 additions & 2 deletions plotly/src/traces/heat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::Serialize;
use crate::{
common::{
Calendar, ColorBar, ColorScale, Dim, HoverInfo, Label, LegendGroupTitle, PlotType, Visible,
XAxisId, YAxisId,
},
private::{NumOrString, NumOrStringCollection},
Trace,
Expand Down Expand Up @@ -106,14 +107,14 @@ where
visible: Option<Visible>,
x: Option<Vec<X>>,
#[serde(rename = "xaxis")]
x_axis: Option<String>,
x_axis: Option<XAxisId>,
#[serde(rename = "xcalendar")]
x_calendar: Option<Calendar>,
#[serde(rename = "xgap")]
x_gap: Option<NumOrString>,
y: Option<Vec<Y>>,
#[serde(rename = "yaxis")]
y_axis: Option<String>,
y_axis: Option<YAxisId>,
#[serde(rename = "ycalendar")]
y_calendar: Option<Calendar>,
#[serde(rename = "ygap")]
Expand Down
6 changes: 3 additions & 3 deletions plotly/src/traces/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::ndarray::ArrayTraces;
use crate::{
common::{
Calendar, Dim, ErrorData, HoverInfo, Label, LegendGroupTitle, Marker, Orientation,
PlotType, Visible,
PlotType, Visible, XAxisId, YAxisId,
},
Trace,
};
Expand Down Expand Up @@ -155,14 +155,14 @@ where
visible: Option<Visible>,
x: Option<Vec<H>>,
#[serde(rename = "xaxis")]
x_axis: Option<String>,
x_axis: Option<XAxisId>,
#[serde(rename = "xbins")]
x_bins: Option<Bins>,
#[serde(rename = "xcalendar")]
x_calendar: Option<Calendar>,
y: Option<Vec<H>>,
#[serde(rename = "yaxis")]
y_axis: Option<String>,
y_axis: Option<YAxisId>,
#[serde(rename = "ybins")]
y_bins: Option<Bins>,
#[serde(rename = "ycalendar")]
Expand Down
6 changes: 3 additions & 3 deletions plotly/src/traces/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use plotly_derive::FieldSetter;
use serde::Serialize;

use crate::color::{Rgb, Rgba};
use crate::common::{Dim, HoverInfo, Label, LegendGroupTitle, PlotType, Visible};
use crate::common::{Dim, HoverInfo, Label, LegendGroupTitle, PlotType, Visible, XAxisId, YAxisId};
use crate::private::{NumOrString, NumOrStringCollection};
use crate::Trace;

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

/// Color model used to map the numerical color components described in `z`
/// into colors. If `source` is specified, this attribute will be set to
Expand Down
28 changes: 25 additions & 3 deletions plotly/src/traces/scatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
color::Color,
common::{
Calendar, Dim, ErrorData, Fill, Font, HoverInfo, HoverOn, Label, LegendGroupTitle, Line,
Marker, Mode, Orientation, PlotType, Position, Visible,
Marker, Mode, Orientation, PlotType, Position, Visible, XAxisId, YAxisId,
},
private::{NumOrString, NumOrStringCollection},
Trace,
Expand Down Expand Up @@ -180,13 +180,13 @@ where
/// `Layout::x_axis`. If "x2", the x coordinates
/// refer to `Layout::x_axis2`, and so on.
#[serde(rename = "xaxis")]
x_axis: Option<String>,
x_axis: Option<XAxisId>,
/// Sets a reference between this trace's y coordinates and a 2D cartesian y
/// axis. If "y" (the default value), the y coordinates refer to
/// `Layout::y_axis`. If "y2", the y coordinates
/// refer to `Layout::y_axis2`, and so on.
#[serde(rename = "yaxis")]
y_axis: Option<String>,
y_axis: Option<YAxisId>,
/// Only relevant when `stackgroup` is used, and only the first
/// `orientation` found in the `stackgroup` will be used - including if
/// `visible` is "legendonly" but not if it is `false`.
Expand Down Expand Up @@ -528,4 +528,26 @@ mod tests {

assert_eq!(to_value(trace).unwrap(), expected);
}

#[test]
fn serialize_scatter_axis_ids() {
use crate::common::{XAxisId, YAxisId};

let x_axis: XAxisId = "x2".into();
let y_axis: YAxisId = "y12".into();

let trace = Scatter::new(vec![0, 1], vec![2, 3])
.x_axis(x_axis)
.y_axis(y_axis);

let expected = json!({
"type": "scatter",
"x": [0, 1],
"y": [2, 3],
"xaxis": "x2",
"yaxis": "y12",
});

assert_eq!(to_value(trace).unwrap(), expected);
}
}
8 changes: 7 additions & 1 deletion plotly_derive/src/field_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,13 @@ impl FieldReceiver {
quote![value.as_ref().to_owned()],
quote![],
),
FieldType::OptionOther(inner_ty) => (quote![#inner_ty], quote![value], quote![]),
FieldType::OptionOther(inner_ty) => {
if matches!(field_ident.to_string().as_str(), "x_axis" | "y_axis") {
(quote![impl Into<#inner_ty>], quote![value.into()], quote![])
} else {
(quote![#inner_ty], quote![value], quote![])
}
}
FieldType::OptionVecString => (
quote![Vec<impl AsRef<str>>],
quote![value.into_iter().map(|v| v.as_ref().to_owned()).collect()],
Expand Down