Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/scatter-3d #26

Merged
merged 3 commits into from
Nov 6, 2024
Merged
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
2,425 changes: 2,257 additions & 168 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ categories = ["visualization"]

[dependencies]
bon = "2.3.0"
plotly = "0.10.0"
polars = { version = "0.43.1", features = [
plotly = { version = "0.10.0", features = ["kaleido"]}
polars = { version = "0.44.2", features = [
"lazy",
"strings",
] }
serde = "1.0.210"
serde = "1.0.214"
3 changes: 3 additions & 0 deletions src/common/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ pub(crate) trait Layout {
plot_title: Option<Text>,
x_title: Option<Text>,
y_title: Option<Text>,
z_title: Option<Text>,
legend_title: Option<Text>,
x_axis: Option<&Axis>,
y_axis: Option<&Axis>,
z_axis: Option<&Axis>,
legend: Option<&Legend>,
) -> LayoutPlotly {
let mut layout = LayoutPlotly::new();
Expand All @@ -21,6 +23,7 @@ pub(crate) trait Layout {

layout = layout.x_axis(Axis::set_axis(x_title, x_axis));
layout = layout.y_axis(Axis::set_axis(y_title, y_axis));
layout = layout.z_axis(Axis::set_axis(z_title, z_axis));
layout = layout.legend(Legend::set_legend(legend_title, legend));
layout
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub(crate) mod polar;
pub(crate) use layout::Layout;
pub(crate) use line::Line;
pub(crate) use mark::Marker;
pub(crate) use plot::Plot;
pub(crate) use plot::PlotHelper;
pub(crate) use polar::Polar;
67 changes: 51 additions & 16 deletions src/common/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,69 @@ use plotly::{Layout, Plot as Plotly, Trace};

/// A trait representing a generic plot that can be displayed or rendered.
pub trait Plot {
fn get_layout(&self) -> &Layout;
fn plot(&self);

fn write_html(&self, path: impl Into<String>);

// fn write_image(
// &self,
// path: impl Into<String>,
// width: usize,
// height: usize,
// scale: f64,
// );
}

// Private helper trait containing methods not exposed publicly.
pub(crate) trait PlotHelper {
fn get_layout(&self) -> &Layout;
fn get_traces(&self) -> &Vec<Box<dyn Trace + 'static>>;

fn plot(self)
where
Self: Sized,
{
let mut plot = Plotly::new();
// fn get_image_format(&self, extension: &str) -> ImageFormat {
// match extension {
// "png" => ImageFormat::PNG,
// _ => panic!("no image extension provided")
// }
// }
}

plot.set_layout(self.get_layout().clone());
plot.add_traces(self.get_traces().clone());
// Implement the public trait `Plot` for any type that implements `PlotHelper`.
impl<T> Plot for T
where
T: PlotHelper,
{
fn plot(&self) {
let mut plot = Plotly::new();
plot.set_layout(self.get_layout().to_owned());
plot.add_traces(self.get_traces().to_owned());

match env::var("EVCXR_IS_RUNTIME") {
Ok(_) => plot.notebook_display(),
_ => plot.show(),
}
}

fn write_html(self, path: impl Into<String>)
where
Self: Sized,
{
fn write_html(&self, path: impl Into<String>) {
let mut plot = Plotly::new();

plot.set_layout(self.get_layout().clone());
plot.add_traces(self.get_traces().clone());

plot.set_layout(self.get_layout().to_owned());
plot.add_traces(self.get_traces().to_owned());
plot.write_html(path.into());
}

// fn write_image(
// &self,
// path: impl Into<String>,
// width: usize,
// height: usize,
// scale: f64,
// ) {
// let mut plot = Plotly::new();
// plot.set_layout(self.get_layout().to_owned());
// plot.add_traces(self.get_traces().to_owned());

// if let Some((filename, extension)) = path.into().rsplit_once('.') {
// let format = self.get_image_format(extension);
// plot.write_image(filename, format, width, height, scale);
// }
// }
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ pub use crate::plots::boxplot::BoxPlot;
pub use crate::plots::heatmap::HeatMap;
pub use crate::plots::histogram::Histogram;
pub use crate::plots::lineplot::LinePlot;
pub use crate::plots::scatter3dplot::Scatter3dPlot;
pub use crate::plots::scatterplot::ScatterPlot;
pub use crate::plots::timeseriesplot::TimeSeriesPlot;
9 changes: 7 additions & 2 deletions src/plots/barplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use plotly::{
use polars::frame::DataFrame;

use crate::{
common::{Layout, Marker, Plot, Polar},
common::{Layout, Marker, PlotHelper, Polar},
components::{Axis, Legend, Orientation, Rgb, Text},
};

Expand Down Expand Up @@ -130,13 +130,18 @@ impl BarPlot {
y_axis: Option<&Axis>,
legend: Option<&Legend>,
) -> Self {
let z_title = None;
let z_axis = None;

let mut layout = Self::create_layout(
plot_title,
x_title,
y_title,
z_title,
legend_title,
x_axis,
y_axis,
z_axis,
legend,
);

Expand Down Expand Up @@ -298,7 +303,7 @@ impl Layout for BarPlot {}
impl Marker for BarPlot {}
impl Polar for BarPlot {}

impl Plot for BarPlot {
impl PlotHelper for BarPlot {
fn get_layout(&self) -> &LayoutPlotly {
&self.layout
}
Expand Down
9 changes: 7 additions & 2 deletions src/plots/boxplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use plotly::{
use polars::frame::DataFrame;

use crate::{
common::{Layout, Marker, Plot, Polar},
common::{Layout, Marker, PlotHelper, Polar},
components::{Axis, Legend, Orientation, Rgb, Text},
};

Expand Down Expand Up @@ -134,13 +134,18 @@ impl BoxPlot {
y_axis: Option<&Axis>,
legend: Option<&Legend>,
) -> Self {
let z_title = None;
let z_axis = None;

let mut layout = Self::create_layout(
plot_title,
x_title,
y_title,
z_title,
legend_title,
x_axis,
y_axis,
z_axis,
legend,
);

Expand Down Expand Up @@ -335,7 +340,7 @@ impl Layout for BoxPlot {}
impl Marker for BoxPlot {}
impl Polar for BoxPlot {}

impl Plot for BoxPlot {
impl PlotHelper for BoxPlot {
fn get_layout(&self) -> &LayoutPlotly {
&self.layout
}
Expand Down
12 changes: 8 additions & 4 deletions src/plots/heatmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use polars::frame::DataFrame;
use serde::Serialize;

use crate::{
common::{Layout, Marker, Plot, Polar},
common::{Layout, Marker, PlotHelper, Polar},
components::{Axis, Text},
ColorBar, Palette,
};
Expand Down Expand Up @@ -71,8 +71,8 @@ use crate::{
///
/// ![Example](https://imgur.com/5uFih4M.png)
pub struct HeatMap {
traces: Vec<Box<dyn Trace + 'static>>,
layout: LayoutPlotly,
pub traces: Vec<Box<dyn Trace + 'static>>,
pub layout: LayoutPlotly,
}

#[bon]
Expand All @@ -96,14 +96,18 @@ impl HeatMap {
) -> Self {
let legend = None;
let legend_title = None;
let z_title = None;
let z_axis = None;

let layout = Self::create_layout(
plot_title,
x_title,
y_title,
z_title,
legend_title,
x_axis,
y_axis,
z_axis,
legend,
);

Expand Down Expand Up @@ -259,7 +263,7 @@ impl Layout for HeatMap {}
impl Marker for HeatMap {}
impl Polar for HeatMap {}

impl Plot for HeatMap {
impl PlotHelper for HeatMap {
fn get_layout(&self) -> &LayoutPlotly {
&self.layout
}
Expand Down
9 changes: 7 additions & 2 deletions src/plots/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use plotly::{
use polars::frame::DataFrame;

use crate::{
common::{Layout, Marker, Plot, Polar},
common::{Layout, Marker, PlotHelper, Polar},
components::{Axis, Legend, Rgb, Text},
};

Expand Down Expand Up @@ -121,13 +121,18 @@ impl Histogram {
y_axis: Option<&Axis>,
legend: Option<&Legend>,
) -> Self {
let z_title = None;
let z_axis = None;

let mut layout = Self::create_layout(
plot_title,
x_title,
y_title,
z_title,
legend_title,
x_axis,
y_axis,
z_axis,
legend,
);

Expand Down Expand Up @@ -222,7 +227,7 @@ impl Layout for Histogram {}
impl Marker for Histogram {}
impl Polar for Histogram {}

impl Plot for Histogram {
impl PlotHelper for Histogram {
fn get_layout(&self) -> &LayoutPlotly {
&self.layout
}
Expand Down
9 changes: 7 additions & 2 deletions src/plots/lineplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use polars::{
};

use crate::{
common::{Layout, Line, Marker, Plot, Polar},
common::{Layout, Line, Marker, PlotHelper, Polar},
components::{Axis, Legend, Line as LineStyle, Rgb, Shape, Text},
};

Expand Down Expand Up @@ -139,13 +139,18 @@ impl LinePlot {
y_axis: Option<&Axis>,
legend: Option<&Legend>,
) -> Self {
let z_title = None;
let z_axis = None;

let layout = Self::create_layout(
plot_title,
x_title,
y_title,
z_title,
legend_title,
x_axis,
y_axis,
z_axis,
legend,
);

Expand Down Expand Up @@ -279,7 +284,7 @@ impl Line for LinePlot {}
impl Marker for LinePlot {}
impl Polar for LinePlot {}

impl Plot for LinePlot {
impl PlotHelper for LinePlot {
fn get_layout(&self) -> &LayoutPlotly {
&self.layout
}
Expand Down
1 change: 1 addition & 0 deletions src/plots/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub(crate) mod boxplot;
pub(crate) mod heatmap;
pub(crate) mod histogram;
pub(crate) mod lineplot;
pub(crate) mod scatter3dplot;
pub(crate) mod scatterplot;
pub(crate) mod timeseriesplot;
Loading