Skip to content

Commit

Permalink
Merge pull request #14 from alceal:feature/color
Browse files Browse the repository at this point in the history
feat: Add `color` argument
  • Loading branch information
alceal authored Sep 13, 2024
2 parents dfcea11 + 7f416c5 commit f8d3b49
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 39 deletions.
26 changes: 23 additions & 3 deletions src/aesthetics/mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,36 @@ pub(crate) trait Mark {
marker!(opacity, size)
}

fn set_color(marker: &Marker, colors: &Option<Vec<Rgb>>, index: usize) -> Marker {
fn set_color(
marker: &Marker,
color: &Option<Rgb>,
colors: &Option<Vec<Rgb>>,
index: usize,
) -> Marker {
let mut updated_marker = marker.clone();

if let Some(colors) = colors {
if let Some(rgb) = colors.get(index) {
match color {
Some(rgb) => {
let group_color = plotly::color::Rgb::new(rgb.0, rgb.1, rgb.2);
updated_marker = updated_marker.color(group_color);
}
None => {
if let Some(colors) = colors {
if let Some(rgb) = colors.get(index) {
let group_color = plotly::color::Rgb::new(rgb.0, rgb.1, rgb.2);
updated_marker = updated_marker.color(group_color);
}
}
}
}

// if let Some(colors) = colors {
// if let Some(rgb) = colors.get(index) {
// let group_color = plotly::color::Rgb::new(rgb.0, rgb.1, rgb.2);
// updated_marker = updated_marker.color(group_color);
// }
// }

updated_marker
}
}
2 changes: 1 addition & 1 deletion src/axis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::Rgb;

/// A structure representing an axis with customizable properties such as position, type, color, ticks, and grid lines.
///
/// Examples:
/// **Examples**
///
/// ```
/// let axis_format = Axis::new()
Expand Down
2 changes: 1 addition & 1 deletion src/legend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Orientation, Rgb};

/// A structure representing a customizable plot legend with properties such as background color, border, font, orientation, and position.
///
/// Examples:
/// **Example**
///
/// ```
/// let legend_format = Legend::new()
Expand Down
28 changes: 15 additions & 13 deletions src/traces/barplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl BarPlot {
/// * `orientation` - An optional `Orientation` enum specifying whether the plot should be horizontal or vertical.
/// * `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.
Expand All @@ -53,7 +54,7 @@ impl BarPlot {
///
/// Returns an instance of `BarPlot`.
///
/// # Example
/// **Examples**
///
/// ```
/// BarPlot::builder()
Expand All @@ -63,9 +64,7 @@ impl BarPlot {
/// .orientation(Orientation::Vertical)
/// .group("gender")
/// .error("errors")
/// .colors(vec![
/// Rgb(255, 0, 0),
/// ])
/// .color(Rgb(255, 0, 0))
/// .plot_title(
/// Text::from("Vertical Bar Plot")
/// .font("Arial")
Expand Down Expand Up @@ -135,6 +134,7 @@ impl BarPlot {
group: Option<String>,
error: Option<String>,
// Marker
color: Option<Rgb>,
colors: Option<Vec<Rgb>>,
// Layout
plot_title: Option<Text>,
Expand Down Expand Up @@ -185,6 +185,7 @@ impl BarPlot {
additional_series,
opacity,
size,
color,
colors,
line_types,
);
Expand Down Expand Up @@ -318,6 +319,7 @@ impl VerticalBarPlot {
/// * `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.
Expand All @@ -330,7 +332,7 @@ impl VerticalBarPlot {
///
/// Returns an instance of `VerticalBarPlot`.
///
/// # Example
/// **Example**
///
/// ```
/// VerticalBarPlot::builder()
Expand All @@ -339,9 +341,7 @@ impl VerticalBarPlot {
/// .y("values")
/// .group("gender")
/// .error("errors")
/// .colors(vec![
/// Rgb(255, 0, 0),
/// ])
/// .color(Rgb(255, 0, 0))
/// .plot_title(
/// Text::from("Vertical Bar Plot")
/// .font("Arial")
Expand Down Expand Up @@ -375,6 +375,7 @@ impl VerticalBarPlot {
group: Option<String>,
error: Option<String>,
// Marker
color: Option<Rgb>,
colors: Option<Vec<Rgb>>,
// Layout
plot_title: Option<Text>,
Expand Down Expand Up @@ -426,6 +427,7 @@ impl VerticalBarPlot {
additional_series,
opacity,
size,
color,
colors,
line_types,
);
Expand Down Expand Up @@ -508,6 +510,7 @@ impl HorizontalBarPlot {
/// * `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.
Expand All @@ -520,7 +523,7 @@ impl HorizontalBarPlot {
///
/// Returns an instance of `HorizontalBarPlot`.
///
/// # Example
/// **Example**
///
/// ```
/// HorizontalBarPlot::builder()
Expand All @@ -529,9 +532,7 @@ impl HorizontalBarPlot {
/// .y("animals")
/// .group("gender")
/// .error("errors")
/// .colors(vec![
/// Rgb(255, 0, 0),
/// ])
/// .color(Rgb(255, 0, 0))
/// .plot_title(
/// Text::from("Horizontal Bar Plot")
/// .font("Arial")
Expand All @@ -557,7 +558,6 @@ impl HorizontalBarPlot {
/// ```
///
/// ![Horizontal Bar Plot](https://imgur.com/saoTcNg.png)
///
#[builder(on(String, into), on(Text, into))]
pub fn new(
data: &DataFrame,
Expand All @@ -566,6 +566,7 @@ impl HorizontalBarPlot {
group: Option<String>,
error: Option<String>,
// Marker
color: Option<Rgb>,
colors: Option<Vec<Rgb>>,
// Layout
plot_title: Option<Text>,
Expand Down Expand Up @@ -617,6 +618,7 @@ impl HorizontalBarPlot {
additional_series,
opacity,
size,
color,
colors,
line_type,
);
Expand Down
17 changes: 12 additions & 5 deletions src/traces/boxplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl BoxPlot {
/// * `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.
Expand All @@ -56,7 +57,7 @@ impl BoxPlot {
///
/// Returns an instance of `BoxPlot`.
///
/// # Example
/// **Examples**
///
/// ```
/// BoxPlot::builder()
Expand Down Expand Up @@ -153,6 +154,7 @@ impl BoxPlot {
jitter: Option<f64>,
// Marker
opacity: Option<f64>,
color: Option<Rgb>,
colors: Option<Vec<Rgb>>,
// Layout
plot_title: Option<Text>,
Expand Down Expand Up @@ -200,6 +202,7 @@ impl BoxPlot {
additional_series,
opacity,
size,
color,
colors,
line_types,
);
Expand Down Expand Up @@ -358,6 +361,7 @@ impl VerticalBoxPlot {
/// * `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.
Expand All @@ -370,7 +374,7 @@ impl VerticalBoxPlot {
///
/// Returns an instance of `VerticalBoxPlot`.
///
/// # Example
/// **Example**
///
/// ```
/// VerticalBoxPlot::builder()
Expand Down Expand Up @@ -412,7 +416,6 @@ impl VerticalBoxPlot {
/// ```
///
/// ![Vertical Box Plot](https://imgur.com/0Zn0mFu.png)
///
#[builder(on(String, into), on(Text, into))]
pub fn new(
data: &DataFrame,
Expand All @@ -424,6 +427,7 @@ impl VerticalBoxPlot {
jitter: Option<f64>,
// Marker
opacity: Option<f64>,
color: Option<Rgb>,
colors: Option<Vec<Rgb>>,
// Layout
plot_title: Option<Text>,
Expand Down Expand Up @@ -472,6 +476,7 @@ impl VerticalBoxPlot {
additional_series,
opacity,
size,
color,
colors,
line_types,
);
Expand Down Expand Up @@ -564,6 +569,7 @@ impl HorizontalBoxPlot {
/// * `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.
Expand All @@ -576,7 +582,7 @@ impl HorizontalBoxPlot {
///
/// Returns an instance of `HorizontalBoxPlot`.
///
/// # Example
/// **Example**
///
/// ```
/// HorizontalBoxPlot::builder()
Expand Down Expand Up @@ -618,7 +624,6 @@ impl HorizontalBoxPlot {
/// ```
///
/// ![Horizontal Box Plot](https://imgur.com/Lu92liU.png)
///
#[builder(on(String, into), on(Text, into))]
pub fn new(
data: &DataFrame,
Expand All @@ -630,6 +635,7 @@ impl HorizontalBoxPlot {
jitter: Option<f64>,
// Marker
opacity: Option<f64>,
color: Option<Rgb>,
colors: Option<Vec<Rgb>>,
// Layout
plot_title: Option<Text>,
Expand Down Expand Up @@ -678,6 +684,7 @@ impl HorizontalBoxPlot {
additional_series,
opacity,
size,
color,
colors,
line_type,
);
Expand Down
6 changes: 4 additions & 2 deletions src/traces/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Histogram {
/// * `x` - A string specifying the column name to be used for the x-axis.
/// * `group` - An optional string specifying the column name to be used for grouping data points.
/// * `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.
Expand All @@ -45,7 +46,7 @@ impl Histogram {
///
/// Returns an instance of `Histogram`.
///
/// # Example
/// **Example**
///
/// ```
/// Histogram::builder()
Expand Down Expand Up @@ -83,14 +84,14 @@ impl Histogram {
/// ```
///
/// ![Histogram](https://imgur.com/ZNomy9V.png)
///
#[builder(on(String, into), on(Text, into))]
pub fn new(
data: &DataFrame,
x: String,
group: Option<String>,
// Marker
opacity: Option<f64>,
color: Option<Rgb>,
colors: Option<Vec<Rgb>>,
// Layout
plot_title: Option<Text>,
Expand Down Expand Up @@ -142,6 +143,7 @@ impl Histogram {
additional_series,
opacity,
size,
color,
colors,
line_types,
);
Expand Down
Loading

0 comments on commit f8d3b49

Please sign in to comment.