Skip to content

Commit

Permalink
Merge pull request #28 from alceal:feature/export_to-json
Browse files Browse the repository at this point in the history
feat: Convert plots into JSON with the `to_json`method
  • Loading branch information
alceal authored Nov 9, 2024
2 parents f49c1e4 + 92fcdf7 commit 2eb782a
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ polars = { version = "0.44.2", features = [
"strings",
] }
serde = "1.0.214"
serde_json = "1.0.132"
9 changes: 8 additions & 1 deletion src/common/plot.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::env;

use plotly::{Layout, Plot as Plotly, Trace};
use serde::Serialize;

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

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

fn to_json(&self) -> Result<String, serde_json::Error>;

// fn write_image(
// &self,
// path: impl Into<String>,
Expand All @@ -33,7 +36,7 @@ pub(crate) trait PlotHelper {
// Implement the public trait `Plot` for any type that implements `PlotHelper`.
impl<T> Plot for T
where
T: PlotHelper,
T: PlotHelper + Serialize + Clone,
{
fn plot(&self) {
let mut plot = Plotly::new();
Expand All @@ -53,6 +56,10 @@ where
plot.write_html(path.into());
}

fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}

// fn write_image(
// &self,
// path: impl Into<String>,
Expand Down
2 changes: 2 additions & 0 deletions src/plots/barplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use plotly::{
};

use polars::frame::DataFrame;
use serde::Serialize;

use crate::{
common::{Layout, Marker, PlotHelper, Polar},
Expand Down Expand Up @@ -105,6 +106,7 @@ use crate::{
/// ```
///
/// ![Example](https://imgur.com/2alZlO5.png)
#[derive(Clone, Serialize)]
pub struct BarPlot {
traces: Vec<Box<dyn Trace + 'static>>,
layout: LayoutPlotly,
Expand Down
2 changes: 2 additions & 0 deletions src/plots/boxplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use plotly::{
};

use polars::frame::DataFrame;
use serde::Serialize;

use crate::{
common::{Layout, Marker, PlotHelper, Polar},
Expand Down Expand Up @@ -106,6 +107,7 @@ use crate::{
/// ```
///
/// ![Example](https://imgur.com/uj1LY90.png)
#[derive(Clone, Serialize)]
pub struct BoxPlot {
traces: Vec<Box<dyn Trace + 'static>>,
layout: LayoutPlotly,
Expand Down
1 change: 1 addition & 0 deletions src/plots/heatmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ use crate::{
/// ```
///
/// ![Example](https://imgur.com/5uFih4M.png)
#[derive(Clone, Serialize)]
pub struct HeatMap {
pub traces: Vec<Box<dyn Trace + 'static>>,
pub layout: LayoutPlotly,
Expand Down
2 changes: 2 additions & 0 deletions src/plots/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use plotly::{
};

use polars::frame::DataFrame;
use serde::Serialize;

use crate::{
common::{Layout, Marker, PlotHelper, Polar},
Expand Down Expand Up @@ -98,6 +99,7 @@ use crate::{
/// ```
///
/// ![Example](https://imgur.com/w2oiuIo.png)
#[derive(Clone, Serialize)]
pub struct Histogram {
traces: Vec<Box<dyn Trace + 'static>>,
layout: LayoutPlotly,
Expand Down
2 changes: 2 additions & 0 deletions src/plots/lineplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use polars::{
frame::DataFrame,
prelude::{col, IntoLazy},
};
use serde::Serialize;

use crate::{
common::{Layout, Line, Marker, PlotHelper, Polar},
Expand Down Expand Up @@ -109,6 +110,7 @@ use crate::{
/// ```
///
/// ![Example](https://imgur.com/PaXG300.png)
#[derive(Clone, Serialize)]
pub struct LinePlot {
traces: Vec<Box<dyn Trace + 'static>>,
layout: LayoutPlotly,
Expand Down
2 changes: 2 additions & 0 deletions src/plots/scatter3dplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use plotly::{
};

use polars::frame::DataFrame;
use serde::Serialize;

use crate::{
common::{Layout, Marker, PlotHelper, Polar},
Expand Down Expand Up @@ -89,6 +90,7 @@ use crate::{
/// ```
///
/// ![Example](https://imgur.com/BXlxKfg.png)
#[derive(Clone, Serialize)]
pub struct Scatter3dPlot {
traces: Vec<Box<dyn Trace + 'static>>,
layout: LayoutPlotly,
Expand Down
2 changes: 2 additions & 0 deletions src/plots/scatterplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use plotly::{
};

use polars::frame::DataFrame;
use serde::Serialize;

use crate::{
common::{Layout, Marker, PlotHelper, Polar},
Expand Down Expand Up @@ -105,6 +106,7 @@ use crate::{
/// ```
///
/// ![Example](https://imgur.com/9jfO8RU.png)
#[derive(Clone, Serialize)]
pub struct ScatterPlot {
traces: Vec<Box<dyn Trace + 'static>>,
layout: LayoutPlotly,
Expand Down
2 changes: 2 additions & 0 deletions src/plots/timeseriesplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use polars::{
frame::DataFrame,
prelude::{col, IntoLazy},
};
use serde::Serialize;

use crate::{
common::{Layout, Line, Marker, PlotHelper, Polar},
Expand Down Expand Up @@ -89,6 +90,7 @@ use crate::{
/// ```
///
/// ![Example](https://imgur.com/1GaGFbk.png)
#[derive(Clone, Serialize)]
pub struct TimeSeriesPlot {
traces: Vec<Box<dyn Trace + 'static>>,
layout: LayoutPlotly,
Expand Down

0 comments on commit 2eb782a

Please sign in to comment.