Skip to content
Draft
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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
with:
chrome-version: 'latest'
install-chromedriver: true
id: setup-chrome
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

https://github.com/plotly/plotly.rs/pull/350

## [Unreleased]

### Fixed

- [#5583](https://github.com/plotly/plotly.rs/issues/5583) Add typed axis-id enums for trace `x_axis` and `y_axis` setters

## [0.14.1] - 2026-02-15

### Fixed
Expand Down
124 changes: 124 additions & 0 deletions plotly/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,114 @@ pub enum Reference {
Paper,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum XAxisId {
X1,
X2,
X3,
X4,
Custom(String),
}

impl XAxisId {
fn as_str(&self) -> &str {
match self {
Self::X1 => "x",
Self::X2 => "x2",
Self::X3 => "x3",
Self::X4 => "x4",
Self::Custom(value) => value.as_str(),
}
}
}

impl Serialize for XAxisId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.as_str())
}
}

impl From<&str> for XAxisId {
fn from(value: &str) -> Self {
match value {
"x" | "x1" => Self::X1,
"x2" => Self::X2,
"x3" => Self::X3,
"x4" => Self::X4,
_ => Self::Custom(value.to_string()),
}
}
}

impl From<String> for XAxisId {
fn from(value: String) -> Self {
Self::from(value.as_str())
}
}

impl From<XAxisId> for String {
fn from(value: XAxisId) -> Self {
value.as_str().to_string()
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum YAxisId {
Y1,
Y2,
Y3,
Y4,
Custom(String),
}

impl YAxisId {
fn as_str(&self) -> &str {
match self {
Self::Y1 => "y",
Self::Y2 => "y2",
Self::Y3 => "y3",
Self::Y4 => "y4",
Self::Custom(value) => value.as_str(),
}
}
}

impl Serialize for YAxisId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.as_str())
}
}

impl From<&str> for YAxisId {
fn from(value: &str) -> Self {
match value {
"y" | "y1" => Self::Y1,
"y2" => Self::Y2,
"y3" => Self::Y3,
"y4" => Self::Y4,
_ => Self::Custom(value.to_string()),
}
}
}

impl From<String> for YAxisId {
fn from(value: String) -> Self {
Self::from(value.as_str())
}
}

impl From<YAxisId> for String {
fn from(value: YAxisId) -> Self {
value.as_str().to_string()
}
}

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

#[test]
fn serialize_axis_id() {
assert_eq!(to_value(XAxisId::X1).unwrap(), json!("x"));
assert_eq!(to_value(XAxisId::X3).unwrap(), json!("x3"));
assert_eq!(
to_value(XAxisId::Custom("x7".to_string())).unwrap(),
json!("x7")
);
assert_eq!(to_value(YAxisId::Y1).unwrap(), json!("y"));
assert_eq!(to_value(YAxisId::Y4).unwrap(), json!("y4"));
assert_eq!(
to_value(YAxisId::Custom("y8".to_string())).unwrap(),
json!("y8")
);
}

#[test]
#[rustfmt::skip]
fn serialize_legend_group_title() {
Expand Down
10 changes: 5 additions & 5 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 Expand Up @@ -180,9 +180,9 @@ mod tests {
.text_template_array(vec!["text_template"])
.visible(Visible::LegendOnly)
.width(999.0)
.x_axis("xaxis")
.x_axis(XAxisId::from("xaxis"))
.x_calendar(Calendar::Nanakshahi)
.y_axis("yaxis")
.y_axis(YAxisId::from("yaxis"))
.y_calendar(Calendar::Ummalqura);

let expected = json!({
Expand Down
10 changes: 5 additions & 5 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 Expand Up @@ -307,9 +307,9 @@ mod tests {
.visible(Visible::LegendOnly)
.whisker_width(0.2)
.width(50)
.x_axis("xaxis")
.x_axis(XAxisId::from("xaxis"))
.x_calendar(Calendar::Chinese)
.y_axis("yaxis")
.y_axis(YAxisId::from("yaxis"))
.y_calendar(Calendar::Coptic);

let expected = json!({
Expand Down
9 changes: 5 additions & 4 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 Expand Up @@ -151,8 +152,8 @@ mod tests {
.hover_text_array(vec!["hover", "text"])
.hover_text("hover text")
.hover_info(HoverInfo::Skip)
.x_axis("x1")
.y_axis("y1")
.x_axis(XAxisId::from("x1"))
.y_axis(YAxisId::from("y1"))
.line(Line::new())
.whisker_width(0.4)
.increasing(Direction::Increasing { line: Line::new() })
Expand Down
18 changes: 9 additions & 9 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 @@ -598,11 +598,11 @@ mod tests {
.transpose(true)
.visible(Visible::True)
.x(vec![0.0, 1.0])
.x_axis("x0")
.x_axis(XAxisId::from("x0"))
.x_calendar(Calendar::Ethiopian)
.x0(0.)
.y(vec![2.0, 3.0])
.y_axis("y0")
.y_axis(YAxisId::from("y0"))
.y_calendar(Calendar::Gregorian)
.y0(0.)
.zauto(false)
Expand Down
9 changes: 5 additions & 4 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 Expand Up @@ -227,10 +228,10 @@ mod tests {
.text_array(vec!["te", "xt"])
.transpose(true)
.visible(Visible::LegendOnly)
.x_axis("x")
.x_axis(XAxisId::from("x"))
.x_calendar(Calendar::Hebrew)
.x_gap(1.0)
.y_axis("y")
.y_axis(YAxisId::from("y"))
.y_calendar(Calendar::Islamic)
.y_gap("10")
.zauto(true)
Expand Down
10 changes: 5 additions & 5 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 Expand Up @@ -419,10 +419,10 @@ mod tests {
.text("text")
.text_array(vec!["text_1", "text_2"])
.visible(Visible::True)
.x_axis("xaxis")
.x_axis(XAxisId::from("xaxis"))
.x_bins(Bins::new(1.0, 2.0, 1.0))
.x_calendar(Calendar::Julian)
.y_axis("yaxis")
.y_axis(YAxisId::from("yaxis"))
.y_bins(Bins::new(2.0, 3.0, 4.0))
.y_calendar(Calendar::Mayan);

Expand Down
Loading