diff --git a/src/lib.rs b/src/lib.rs index d93cebb..0299a22 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,8 +19,8 @@ pub use crate::colors::Rgb; pub use crate::legend::Legend; pub use crate::shapes::Shape; pub use crate::texts::Text; -pub use crate::traces::barplot::{BarPlot, HorizontalBarPlot, VerticalBarPlot}; -pub use crate::traces::boxplot::{BoxPlot, HorizontalBoxPlot, VerticalBoxPlot}; +pub use crate::traces::barplot::BarPlot; +pub use crate::traces::boxplot::BoxPlot; pub use crate::traces::histogram::Histogram; pub use crate::traces::lineplot::LinePlot; pub use crate::traces::scatterplot::ScatterPlot; diff --git a/src/traces/barplot.rs b/src/traces/barplot.rs index cdca7d8..462cd0d 100644 --- a/src/traces/barplot.rs +++ b/src/traces/barplot.rs @@ -3,12 +3,10 @@ //! The `BarPlot` struct allow for the creation and customization of bar plots //! with various options for data, layout, and aesthetics. -#![allow(deprecated)] - use bon::bon; use plotly::{ - common::{ErrorData, ErrorType, Line as LinePlotly, Marker, Orientation as OrientationPlotly}, + common::{ErrorData, ErrorType, Line as LinePlotly, Marker}, layout::BarMode, Bar, Layout, Trace as TracePlotly, }; @@ -279,407 +277,3 @@ impl Plot for BarPlot { &self.traces } } -#[deprecated( - since = "0.5.0", - note = "`VerticalBarPlot` will be removed in v0.6.0. Please use `BarPlot` instead." -)] -/// A structure representing a vertical bar plot. -pub struct VerticalBarPlot { - traces: Vec>, - layout: Layout, -} - -#[bon] -impl VerticalBarPlot { - /// Creates a new `VerticalBarPlot`. - /// - /// # Arguments - /// - /// * `data` - A reference to the `DataFrame` containing the data to be plotted. - /// * `x` - A string specifying the column name to be used for the x-axis. - /// * `y` - A string specifying the column name to be used for the y-axis. - /// * `group` - An optional string specifying the column name to be used for grouping data points. - /// * `error` - An optional string specifying the column name containing error values for the y-axis data. - /// * `color` - An optional `Rgb` value specifying the color of the markers to be used for the plot. - /// * `colors` - An optional vector of `Rgb` values specifying the colors to be used for the plot. - /// * `plot_title` - An optional `Text` struct specifying the title of the plot. - /// * `x_title` - An optional `Text` struct specifying the title of the x-axis. - /// * `y_title` - An optional `Text` struct specifying the title of the y-axis. - /// * `legend_title` - An optional `Text` struct specifying the title of the legend. - /// * `x_axis` - An optional reference to an `Axis` struct for customizing the x-axis. - /// * `y_axis` - An optional reference to an `Axis` struct for customizing the y-axis. - /// * `legend` - An optional reference to a `Legend` struct for customizing the legend of the plot (e.g., positioning, font, etc.). - /// - /// # Returns - /// - /// Returns an instance of `VerticalBarPlot`. - /// - /// **Example** - /// - /// ``` - /// VerticalBarPlot::builder() - /// .data(&dataset) - /// .x("animals") - /// .y("values") - /// .group("gender") - /// .error("errors") - /// .color(Rgb(255, 0, 0)) - /// .plot_title( - /// Text::from("Vertical Bar Plot") - /// .font("Arial") - /// .size(18) - /// ) - /// .x_title( - /// Text::from("animal") - /// .font("Arial") - /// .size(15) - /// ) - /// .y_title( - /// Text::from("value") - /// .font("Arial") - /// .size(15) - /// ) - /// .legend_title( - /// Text::from("gender") - /// .font("Arial") - /// .size(15) - /// ) - /// .build() - /// .plot(); - /// ``` - /// - /// ![Vertical Bar Plot](https://imgur.com/Fd6DpB0.png) - #[builder(on(String, into), on(Text, into))] - pub fn new( - data: &DataFrame, - x: String, - y: String, - group: Option, - error: Option, - // Marker - color: Option, - colors: Option>, - // Layout - plot_title: Option, - x_title: Option, - y_title: Option, - legend_title: Option, - x_axis: Option<&Axis>, - y_axis: Option<&Axis>, - legend: Option<&Legend>, - ) -> Self { - let x_col = x.as_str(); - let y_col = y.as_str(); - - // Layout - let bar_mode = Some(BarMode::Group); - - let layout = Self::create_layout( - bar_mode, - plot_title, - x_title, - x_axis, - y_title, - y_axis, - legend_title, - legend, - ); - - // Trace - let box_points = None; - let point_offset = None; - let jitter = None; - let additional_series = None; - let orientation = None; - - let opacity = None; - let size = None; - let with_shape = None; - let shape = None; - let shapes = None; - let line_types = None; - let line_width = None; - - let traces = Self::create_traces( - data, - x_col, - y_col, - orientation, - group, - error, - box_points, - point_offset, - jitter, - additional_series, - opacity, - size, - color, - colors, - with_shape, - shape, - shapes, - line_types, - line_width, - ); - - Self { traces, layout } - } -} - -impl LayoutPlotly for VerticalBarPlot {} -impl Polar for VerticalBarPlot {} -impl Mark for VerticalBarPlot {} -impl Line for VerticalBarPlot {} - -impl Trace for VerticalBarPlot { - fn create_trace( - data: &DataFrame, - x_col: &str, - y_col: &str, - #[allow(unused_variables)] orientation: Option, - group_name: Option<&str>, - error: Option, - #[allow(unused_variables)] box_points: Option, - #[allow(unused_variables)] point_offset: Option, - #[allow(unused_variables)] jitter: Option, - #[allow(unused_variables)] with_shape: Option, - marker: Marker, - #[allow(unused_variables)] line: LinePlotly, - ) -> Box { - let x_data = Self::get_string_column(data, x_col); - let y_data = Self::get_numeric_column(data, y_col); - - let mut trace = Bar::default().x(x_data).y(y_data); - - if let Some(error) = error { - let error = Self::get_numeric_column(data, error.as_str()) - .iter() - .map(|x| x.unwrap() as f64) - .collect::>(); - - trace = trace.error_y(ErrorData::new(ErrorType::Data).array(error)) - } - - trace = trace.marker(marker); - - if let Some(name) = group_name { - trace = trace.name(name); - } - - trace - } -} - -impl Plot for VerticalBarPlot { - fn get_layout(&self) -> &Layout { - &self.layout - } - - fn get_traces(&self) -> &Vec> { - &self.traces - } -} - -#[deprecated( - since = "0.5.0", - note = "`HorizontalBarPlot` will be removed in v0.6.0. Please use `BarPlot` instead." -)] -/// A structure representing a horizontal bar plot. -pub struct HorizontalBarPlot { - traces: Vec>, - layout: Layout, -} - -#[bon] -impl HorizontalBarPlot { - /// Creates a new `HorizontalBarPlot`. - /// - /// # Arguments - /// - /// * `data` - A reference to the `DataFrame` containing the data to be plotted. - /// * `x` - A string specifying the column name to be used for the x-axis. - /// * `y` - A string specifying the column name to be used for the y-axis. - /// * `group` - An optional string specifying the column name to be used for grouping data points. - /// * `error` - An optional string specifying the column name containing error values for the x-axis data. - /// * `color` - An optional `Rgb` value specifying the color of the markers to be used for the plot. - /// * `colors` - An optional vector of `Rgb` values specifying the colors to be used for the plot. - /// * `plot_title` - An optional `Text` struct specifying the title of the plot. - /// * `x_title` - An optional `Text` struct specifying the title of the x-axis. - /// * `y_title` - An optional `Text` struct specifying the title of the y-axis. - /// * `legend_title` - An optional `Text` struct specifying the title of the legend. - /// * `x_axis` - An optional reference to an `Axis` struct for customizing the x-axis. - /// * `y_axis` - An optional reference to an `Axis` struct for customizing the y-axis. - /// * `legend` - An optional reference to a `Legend` struct for customizing the legend of the plot (e.g., positioning, font, etc.). - /// - /// # Returns - /// - /// Returns an instance of `HorizontalBarPlot`. - /// - /// **Example** - /// - /// ``` - /// HorizontalBarPlot::builder() - /// .data(&dataset) - /// .x("values") - /// .y("animals") - /// .group("gender") - /// .error("errors") - /// .color(Rgb(255, 0, 0)) - /// .plot_title( - /// Text::from("Horizontal Bar Plot") - /// .font("Arial") - /// .size(18) - /// ) - /// .x_title( - /// Text::from("value") - /// .font("Arial") - /// .size(15) - /// ) - /// .y_title( - /// Text::from("animal") - /// .font("Arial") - /// .size(15) - /// ) - /// .legend_title( - /// Text::from("gender") - /// .font("Arial") - /// .size(15) - /// ) - /// .build() - /// .plot(); - /// ``` - /// - /// ![Horizontal Bar Plot](https://imgur.com/saoTcNg.png) - #[builder(on(String, into), on(Text, into))] - pub fn new( - data: &DataFrame, - x: String, - y: String, - group: Option, - error: Option, - // Marker - color: Option, - colors: Option>, - // Layout - plot_title: Option, - x_title: Option, - y_title: Option, - legend_title: Option, - x_axis: Option<&Axis>, - y_axis: Option<&Axis>, - legend: Option<&Legend>, - ) -> Self { - let x_col = x.as_str(); - let y_col = y.as_str(); - - // Layout - let bar_mode = Some(BarMode::Group); - - let layout = Self::create_layout( - bar_mode, - plot_title, - x_title, - x_axis, - y_title, - y_axis, - legend_title, - legend, - ); - - // Trace - let box_points = None; - let point_offset = None; - let jitter = None; - let additional_series = None; - let orientation = None; - - let opacity = None; - let size = None; - let with_shape = None; - let shape = None; - let shapes = None; - let line_type = None; - let line_width = None; - - let traces = Self::create_traces( - data, - x_col, - y_col, - orientation, - group, - error, - box_points, - point_offset, - jitter, - additional_series, - opacity, - size, - color, - colors, - with_shape, - shape, - shapes, - line_type, - line_width, - ); - - Self { traces, layout } - } -} - -impl LayoutPlotly for HorizontalBarPlot {} -impl Polar for HorizontalBarPlot {} -impl Mark for HorizontalBarPlot {} -impl Line for HorizontalBarPlot {} - -impl Trace for HorizontalBarPlot { - fn create_trace( - data: &DataFrame, - x_col: &str, - y_col: &str, - #[allow(unused_variables)] orientation: Option, - group_name: Option<&str>, - error: Option, - #[allow(unused_variables)] box_points: Option, - #[allow(unused_variables)] point_offset: Option, - #[allow(unused_variables)] jitter: Option, - #[allow(unused_variables)] with_shape: Option, - marker: Marker, - #[allow(unused_variables)] line: LinePlotly, - ) -> Box { - let x_data = Self::get_numeric_column(data, x_col); - let y_data = Self::get_string_column(data, y_col); - - let mut trace = Bar::default() - .x(x_data) - .y(y_data) - .orientation(OrientationPlotly::Horizontal); - - if let Some(error) = error { - let error = Self::get_numeric_column(data, error.as_str()) - .iter() - .map(|x| x.unwrap() as f64) - .collect::>(); - - trace = trace.error_x(ErrorData::new(ErrorType::Data).array(error)) - } - - trace = trace.marker(marker); - - if let Some(name) = group_name { - trace = trace.name(name); - } - - trace - } -} - -impl Plot for HorizontalBarPlot { - fn get_layout(&self) -> &Layout { - &self.layout - } - - fn get_traces(&self) -> &Vec> { - &self.traces - } -} diff --git a/src/traces/boxplot.rs b/src/traces/boxplot.rs index 1d6205c..e202095 100644 --- a/src/traces/boxplot.rs +++ b/src/traces/boxplot.rs @@ -3,13 +3,11 @@ //! The `BoxPlot` structs allow for the creation and customization of box plots //! with various options for data, layout, and aesthetics. -#![allow(deprecated)] - use bon::bon; use plotly::{ box_plot::BoxPoints, - common::{Line as LinePlotly, Marker, Orientation as OrientationPlotly}, + common::{Line as LinePlotly, Marker}, BoxPlot as BoxPlotly, Layout, Trace as TracePlotly, }; @@ -313,442 +311,3 @@ impl Plot for BoxPlot { &self.traces } } - -#[deprecated( - since = "0.5.0", - note = "`VerticalBoxPlot` will be removed in v0.6.0. Please use `BoxPlot` instead." -)] -/// A structure representing a vertical box plot. -pub struct VerticalBoxPlot { - traces: Vec>, - layout: Layout, -} - -#[bon] -impl VerticalBoxPlot { - /// Creates a new `VerticalBoxPlot`. - /// - /// # Arguments - /// - /// * `data` - A reference to the `DataFrame` containing the data to be plotted. - /// * `x` - A string specifying the column name to be used for the x-axis. - /// * `y` - A string specifying the column name to be used for the y-axis. - /// * `group` - An optional string specifying the column name to be used for grouping data points. - /// * `box_points` - An optional boolean indicating whether individual data points should be plotted along with the box plot. - /// * `point_offset` - An optional f64 value specifying the horizontal offset for individual data points when `box_points` is enabled. - /// * `jitter` - An optional f64 value indicating the amount of jitter (random noise) to apply to individual data points for visibility. - /// * `opacity` - An optional f64 value specifying the opacity of the plot markers (range: 0.0 to 1.0). - /// * `color` - An optional `Rgb` value specifying the color of the markers to be used for the plot. - /// * `colors` - An optional vector of `Rgb` values specifying the colors to be used for the plot. - /// * `plot_title` - An optional `Text` struct specifying the title of the plot. - /// * `x_title` - An optional `Text` struct specifying the title of the x-axis. - /// * `y_title` - An optional `Text` struct specifying the title of the y-axis. - /// * `legend_title` - An optional `Text` struct specifying the title of the legend. - /// * `x_axis` - An optional reference to an `Axis` struct for customizing the x-axis. - /// * `y_axis` - An optional reference to an `Axis` struct for customizing the y-axis. - /// * `legend` - An optional reference to a `Legend` struct for customizing the legend of the plot (e.g., positioning, font, etc.). - /// - /// # Returns - /// - /// Returns an instance of `VerticalBoxPlot`. - /// - /// **Example** - /// - /// ``` - /// VerticalBoxPlot::builder() - /// .data(&dataset) - /// .x("species") - /// .y("body_mass_g") - /// .group("gender") - /// .box_points(true) - /// .point_offset(-1.5) - /// .jitter(0.01) - /// .opacity(0.1) - /// .colors(vec![ - /// Rgb(255, 0, 0), - /// Rgb(0, 255, 0), - /// Rgb(0, 0, 255), - /// ]) - /// .plot_title( - /// Text::from("Vertical Box Plot") - /// .font("Arial") - /// .size(18) - /// ) - /// .x_title( - /// Text::from("species") - /// .font("Arial") - /// .size(15) - /// ) - /// .y_title( - /// Text::from("body mass (g)") - /// .font("Arial") - /// .size(15) - /// ) - /// .legend_title( - /// Text::from("gender") - /// .font("Arial") - /// .size(15) - /// ) - /// .build() - /// .plot(); - /// ``` - /// - /// ![Vertical Box Plot](https://imgur.com/0Zn0mFu.png) - #[builder(on(String, into), on(Text, into))] - pub fn new( - data: &DataFrame, - x: String, - y: String, - group: Option, - box_points: Option, - point_offset: Option, - jitter: Option, - // Marker - opacity: Option, - color: Option, - colors: Option>, - // Layout - plot_title: Option, - x_title: Option, - y_title: Option, - legend_title: Option, - x_axis: Option<&Axis>, - y_axis: Option<&Axis>, - legend: Option<&Legend>, - ) -> Self { - let x_col = x.as_str(); - let y_col = y.as_str(); - - // Layout - let bar_mode = None; - - let layout = Self::create_layout( - bar_mode, - plot_title, - x_title, - x_axis, - y_title, - y_axis, - legend_title, - legend, - ); - - // Trace - let orientation = None; - let error = None; - let additional_series = None; - - let size = None; - let with_shape = None; - let shape = None; - let shapes = None; - let line_types = None; - let line_width = None; - - let traces = Self::create_traces( - data, - x_col, - y_col, - orientation, - group, - error, - box_points, - point_offset, - jitter, - additional_series, - opacity, - size, - color, - colors, - with_shape, - shape, - shapes, - line_types, - line_width, - ); - - Self { traces, layout } - } -} - -impl LayoutPlotly for VerticalBoxPlot {} -impl Polar for VerticalBoxPlot {} -impl Mark for VerticalBoxPlot {} -impl Line for VerticalBoxPlot {} - -impl Trace for VerticalBoxPlot { - fn create_trace( - data: &DataFrame, - x_col: &str, - y_col: &str, - #[allow(unused_variables)] orientation: Option, - group_name: Option<&str>, - #[allow(unused_variables)] error: Option, - box_points: Option, - point_offset: Option, - jitter: Option, - #[allow(unused_variables)] with_shape: Option, - marker: Marker, - #[allow(unused_variables)] line: LinePlotly, - ) -> Box { - let x_data = Self::get_string_column(data, x_col); - let y_data = Self::get_numeric_column(data, y_col); - - let mut trace = BoxPlotly::default().x(x_data).y(y_data); - - if let Some(all) = box_points { - if all { - trace = trace.box_points(BoxPoints::All); - } else { - trace = trace.box_points(BoxPoints::False); - } - } - - if let Some(point_position) = point_offset { - trace = trace.point_pos(point_position); - } - - if let Some(jitter) = jitter { - trace = trace.jitter(jitter); - } - - trace = trace.marker(marker); - - if let Some(name) = group_name { - trace = trace.name(name); - } - - trace - } -} - -impl Plot for VerticalBoxPlot { - fn get_layout(&self) -> &Layout { - &self.layout - } - - fn get_traces(&self) -> &Vec> { - &self.traces - } -} - -#[deprecated( - since = "0.5.0", - note = "`HorizontalBoxPlot` will be removed in v0.6.0. Please use `BoxPlot` instead." -)] -/// A structure representing a horizontal box plot. -pub struct HorizontalBoxPlot { - traces: Vec>, - layout: Layout, -} - -#[bon] -impl HorizontalBoxPlot { - /// Creates a new `HorizontalBoxPlot`. - /// - /// # Arguments - /// - /// * `data` - A reference to the `DataFrame` containing the data to be plotted. - /// * `x` - A string specifying the column name to be used for the x-axis. - /// * `y` - A string specifying the column name to be used for the y-axis. - /// * `group` - An optional string specifying the column name to be used for grouping data points. - /// * `box_points` - An optional boolean indicating whether individual data points should be plotted along with the box plot. - /// * `point_offset` - An optional f64 value specifying the horizontal offset for individual data points when `box_points` is enabled. - /// * `jitter` - An optional f64 value indicating the amount of jitter (random noise) to apply to individual data points for visibility. - /// * `opacity` - An optional f64 value specifying the opacity of the plot markers (range: 0.0 to 1.0). - /// * `color` - An optional `Rgb` value specifying the color of the markers to be used for the plot. - /// * `colors` - An optional vector of `Rgb` values specifying the colors to be used for the plot. - /// * `plot_title` - An optional `Text` struct specifying the title of the plot. - /// * `x_title` - An optional `Text` struct specifying the title of the x-axis. - /// * `y_title` - An optional `Text` struct specifying the title of the y-axis. - /// * `legend_title` - An optional `Text` struct specifying the title of the legend. - /// * `x_axis` - An optional reference to an `Axis` struct for customizing the x-axis. - /// * `y_axis` - An optional reference to an `Axis` struct for customizing the y-axis. - /// * `legend` - An optional reference to a `Legend` struct for customizing the legend of the plot (e.g., positioning, font, etc.). - /// - /// # Returns - /// - /// Returns an instance of `HorizontalBoxPlot`. - /// - /// **Example** - /// - /// ``` - /// HorizontalBoxPlot::builder() - /// .data(&dataset) - /// .x("body_mass_g") - /// .y("species") - /// .group("gender") - /// .box_points(true) - /// .point_offset(-1.5) - /// .jitter(0.01) - /// .opacity(0.1) - /// .colors(vec![ - /// Rgb(255, 0, 0), - /// Rgb(0, 255, 0), - /// Rgb(0, 0, 255), - /// ]) - /// .plot_title( - /// Text::from("Horizontal Box Plot") - /// .font("Arial") - /// .size(18) - /// ) - /// .x_title( - /// Text::from("body mass (g)") - /// .font("Arial") - /// .size(15) - /// ) - /// .y_title( - /// Text::from("species") - /// .font("Arial") - /// .size(15) - /// ) - /// .legend_title( - /// Text::from("gender") - /// .font("Arial") - /// .size(15) - /// ) - /// .build() - /// .plot(); - /// ``` - /// - /// ![Horizontal Box Plot](https://imgur.com/Lu92liU.png) - #[builder(on(String, into), on(Text, into))] - pub fn new( - data: &DataFrame, - x: String, - y: String, - group: Option, - box_points: Option, - point_offset: Option, - jitter: Option, - // Marker - opacity: Option, - color: Option, - colors: Option>, - // Layout - plot_title: Option, - x_title: Option, - y_title: Option, - legend_title: Option, - x_axis: Option<&Axis>, - y_axis: Option<&Axis>, - legend: Option<&Legend>, - ) -> Self { - let x_col = x.as_str(); - let y_col = y.as_str(); - - // Layout - let bar_mode = None; - - let layout = Self::create_layout( - bar_mode, - plot_title, - x_title, - x_axis, - y_title, - y_axis, - legend_title, - legend, - ); - - // Trace - let orientation = None; - let error = None; - let additional_series = None; - - let size = None; - let with_shape = None; - let shape = None; - let shapes = None; - let line_type = None; - let line_width = None; - - let traces = Self::create_traces( - data, - x_col, - y_col, - orientation, - group, - error, - box_points, - point_offset, - jitter, - additional_series, - opacity, - size, - color, - colors, - with_shape, - shape, - shapes, - line_type, - line_width, - ); - - Self { traces, layout } - } -} - -impl LayoutPlotly for HorizontalBoxPlot {} -impl Polar for HorizontalBoxPlot {} -impl Mark for HorizontalBoxPlot {} -impl Line for HorizontalBoxPlot {} - -impl Trace for HorizontalBoxPlot { - fn create_trace( - data: &DataFrame, - x_col: &str, - y_col: &str, - #[allow(unused_variables)] orientation: Option, - group_name: Option<&str>, - #[allow(unused_variables)] error: Option, - box_points: Option, - point_offset: Option, - jitter: Option, - #[allow(unused_variables)] with_shape: Option, - marker: Marker, - #[allow(unused_variables)] line: LinePlotly, - ) -> Box { - let x_data = Self::get_numeric_column(data, x_col); - let y_data = Self::get_string_column(data, y_col); - - let mut trace = BoxPlotly::default() - .x(x_data) - .y(y_data) - .orientation(OrientationPlotly::Horizontal); - - if let Some(all) = box_points { - if all { - trace = trace.box_points(BoxPoints::All); - } else { - trace = trace.box_points(BoxPoints::False); - } - } - - if let Some(point_position) = point_offset { - trace = trace.point_pos(point_position); - } - - if let Some(jitter) = jitter { - trace = trace.jitter(jitter); - } - - trace = trace.marker(marker); - - if let Some(name) = group_name { - trace = trace.name(name); - } - - trace - } -} - -impl Plot for HorizontalBoxPlot { - fn get_layout(&self) -> &Layout { - &self.layout - } - - fn get_traces(&self) -> &Vec> { - &self.traces - } -}