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: 14 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ jobs:
components: clippy
targets: wasm32-unknown-unknown
# lint plotly_static for all features
- run: cargo clippy -p plotly_static --features geckodriver,webdriver_download -- -D warnings -A deprecated
- run: cargo clippy -p plotly_static --features chromedriver,webdriver_download -- -D warnings -A deprecated
- run: |
export WEBDRIVER_PATH="${{ steps.setup-chrome.outputs.chromedriver-path }}"
export BROWSER_PATH="${{ steps.setup-chrome.outputs.chrome-path }}"
cargo clippy -p plotly_static --features geckodriver,webdriver_download -- -D warnings -A deprecated
cargo clippy -p plotly_static --features chromedriver,webdriver_download -- -D warnings -A deprecated
# lint the main library workspace for non-wasm target
- run: cargo clippy --features all -- -D warnings -A deprecated
# lint the non-wasm examples
Expand Down Expand Up @@ -132,7 +135,10 @@ jobs:
# Run tests on Ubuntu with Chrome
- name: Run tests (${{ matrix.os }} - Chrome)
if: matrix.os == 'ubuntu-latest' && matrix.browser == 'chrome'
run: cargo test --workspace --features ${{ matrix.features }} --exclude plotly_kaleido
run: |
export WEBDRIVER_PATH="${{ steps.setup-chrome.outputs.chromedriver-path }}"
export BROWSER_PATH="${{ steps.setup-chrome.outputs.chrome-path }}"
cargo test --workspace --features ${{ matrix.features }} --exclude plotly_kaleido

# Install xvfb for Firefox WebGL support
- name: Install xvfb
Expand All @@ -146,6 +152,7 @@ jobs:
if: matrix.os == 'ubuntu-latest' && matrix.browser == 'firefox'
run: |
# Set environment variables for Firefox WebDriver
export WEBDRIVER_PATH="/usr/local/share/gecko_driver"
export BROWSER_PATH="${{ steps.setup-firefox.outputs.firefox-path }}"
export RUST_LOG="debug"
export RUST_BACKTRACE="1"
Expand All @@ -159,7 +166,10 @@ jobs:
# Run tests on macOS with Chrome
- name: Run tests (${{ matrix.os }} - Chrome)
if: matrix.os == 'macos-latest' && matrix.browser == 'chrome'
run: cargo test --workspace --features ${{ matrix.features }} --exclude plotly_kaleido
run: |
export WEBDRIVER_PATH="${{ steps.setup-chrome.outputs.chromedriver-path }}"
export BROWSER_PATH="${{ steps.setup-chrome.outputs.chrome-path }}"
cargo test --workspace --features ${{ matrix.features }} --exclude plotly_kaleido

# Run tests on Windows with Chrome WebDriver
- name: Run tests (${{ matrix.os }} - Chrome)
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 @@ -69,9 +69,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 @@ -179,9 +179,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
Loading