Skip to content

Commit

Permalink
Merge pull request #17 from alceal:feature/axis_position
Browse files Browse the repository at this point in the history
feat: New `axis_position` method and the old one has been renamed to `axis_side` and the corresponding enum values have been updated
  • Loading branch information
alceal authored Sep 16, 2024
2 parents 97e14ff + f8ec85d commit 2465c42
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
41 changes: 28 additions & 13 deletions src/axis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use plotly::{
common::{AxisSide, ExponentFormat},
common::{AxisSide as AxisSidePlotly, ExponentFormat},
layout::{AxisType as AxisTypePlotly, TicksDirection},
};

Expand Down Expand Up @@ -69,7 +69,8 @@ use crate::Rgb;
#[derive(Default, Clone)]
pub struct Axis {
pub(crate) show_axis: Option<bool>,
pub(crate) axis_position: Option<AxisPosition>,
pub(crate) axis_side: Option<AxisSide>,
pub(crate) axis_position: Option<f64>,
pub(crate) axis_type: Option<AxisType>,
pub(crate) value_color: Option<Rgb>,
pub(crate) value_range: Option<Vec<f64>>,
Expand Down Expand Up @@ -118,16 +119,30 @@ impl Axis {
self
}

/// Sets the side of the axis.
///
/// # Arguments
///
/// * `side` - An `AxisSide` enum value representing the side of the axis.
///
/// # Returns
///
/// Returns the `Axis` instance with the updated side.
pub fn axis_side(mut self, side: AxisSide) -> Self {
self.axis_side = Some(side);
self
}

/// Sets the position of the axis.
///
/// # Arguments
///
/// * `position` - An `AxisPosition` enum value representing the position of the axis.
/// * `position` - A `f64` value representing the position of the axis.
///
/// # Returns
///
/// Returns the `Axis` instance with the updated position.
pub fn axis_position(mut self, position: AxisPosition) -> Self {
pub fn axis_position(mut self, position: f64) -> Self {
self.axis_position = Some(position);
self
}
Expand Down Expand Up @@ -451,7 +466,7 @@ impl From<&Axis> for Axis {
#[derive(Clone)]
pub enum TickDirection {
OutSide,
Inside,
InSide,
}

impl TickDirection {
Expand All @@ -463,32 +478,32 @@ impl TickDirection {
pub fn get_direction(&self) -> TicksDirection {
match self {
TickDirection::OutSide => TicksDirection::Outside,
TickDirection::Inside => TicksDirection::Inside,
TickDirection::InSide => TicksDirection::Inside,
}
}
}

/// Enumeration representing the position of the axis.
#[derive(Clone)]
pub enum AxisPosition {
pub enum AxisSide {
Top,
Bottom,
Left,
Right,
}

impl AxisPosition {
impl AxisSide {
/// Converts `AxisPosition` to the corresponding `AxisSide` from the `plotly` crate.
///
/// # Returns
///
/// Returns the corresponding `AxisSide`.
pub fn get_position(&self) -> AxisSide {
pub fn get_side(&self) -> AxisSidePlotly {
match self {
AxisPosition::Top => AxisSide::Top,
AxisPosition::Bottom => AxisSide::Bottom,
AxisPosition::Left => AxisSide::Left,
AxisPosition::Right => AxisSide::Right,
AxisSide::Top => AxisSidePlotly::Top,
AxisSide::Bottom => AxisSidePlotly::Bottom,
AxisSide::Left => AxisSidePlotly::Left,
AxisSide::Right => AxisSidePlotly::Right,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod traces;
mod traits;

pub use crate::aesthetics::{line::LineType, orientation::Orientation};
pub use crate::axis::{Axis, AxisPosition, AxisType, TickDirection, ValueExponent};
pub use crate::axis::{Axis, AxisSide, AxisType, TickDirection, ValueExponent};
pub use crate::colors::Rgb;
pub use crate::legend::Legend;
pub use crate::shapes::Shape;
Expand Down
8 changes: 6 additions & 2 deletions src/traits/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ pub(crate) trait LayoutPlotly {
x_axis_format = x_axis_format.visible(visible);
}

if let Some(axis_position) = axis_details.axis_position {
x_axis_format = x_axis_format.side(axis_position.get_position());
if let Some(axis_position) = axis_details.axis_side {
x_axis_format = x_axis_format.side(axis_position.get_side());
}

if let Some(axis_type) = axis_details.axis_type {
Expand Down Expand Up @@ -202,6 +202,10 @@ pub(crate) trait LayoutPlotly {
x_axis_format = x_axis_format.zero_line_width(zero_line_width);
}

if let Some(axis_position) = axis_details.axis_position {
x_axis_format = x_axis_format.position(axis_position);
}

x_axis_format
}

Expand Down

0 comments on commit 2465c42

Please sign in to comment.