Skip to content
Merged
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
50 changes: 14 additions & 36 deletions plotly/src/layout/rangebreaks.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
use serde::{Deserialize, Serialize};
use plotly_derive::FieldSetter;
use serde::Serialize;

use crate::private::NumOrString;

/// Struct representing a rangebreak for Plotly axes.
/// See: https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-rangebreaks
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Serialize, PartialEq, FieldSetter)]
pub struct RangeBreak {
/// Sets the lower and upper bounds for this range break, e.g. ["sat",
/// "mon"]
#[serde(skip_serializing_if = "Option::is_none")]
pub bounds: Option<[String; 2]>,
#[field_setter(skip)]
pub bounds: Option<[NumOrString; 2]>,

/// Sets the pattern by which this range break is generated, e.g. "day of
/// week"
#[serde(skip_serializing_if = "Option::is_none")]
pub pattern: Option<String>,
pub pattern: Option<NumOrString>,

/// Sets the values at which this range break occurs.
/// See Plotly.js docs for details.
#[serde(skip_serializing_if = "Option::is_none")]
pub values: Option<Vec<String>>,
#[field_setter(skip)]
pub values: Option<Vec<NumOrString>>,

/// Sets the size of each range break in milliseconds (for time axes).
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -28,45 +33,18 @@ pub struct RangeBreak {
pub enabled: Option<bool>,
}

impl Default for RangeBreak {
fn default() -> Self {
Self::new()
}
}

impl RangeBreak {
pub fn new() -> Self {
Self {
bounds: None,
pattern: None,
values: None,
dvalue: None,
enabled: None,
}
Default::default()
}

pub fn bounds(mut self, lower: impl Into<String>, upper: impl Into<String>) -> Self {
pub fn bounds<T: Into<NumOrString>>(mut self, lower: T, upper: T) -> Self {
self.bounds = Some([lower.into(), upper.into()]);
self
}

pub fn pattern(mut self, pattern: impl Into<String>) -> Self {
self.pattern = Some(pattern.into());
self
}

pub fn values(mut self, values: Vec<impl Into<String>>) -> Self {
self.values = Some(values.into_iter().map(|v| v.into()).collect());
self
}

pub fn dvalue(mut self, dvalue: u64) -> Self {
self.dvalue = Some(dvalue);
self
}

pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = Some(enabled);
pub fn values<T: Into<NumOrString>>(mut self, values: Vec<T>) -> Self {
self.values = Some(values.into_iter().map(Into::into).collect());
self
}
}
Loading