From 92fcdf789bb90d6cccf4ea989a6bebf0b5ee105b Mon Sep 17 00:00:00 2001 From: alceal Date: Sat, 9 Nov 2024 04:44:48 +0100 Subject: [PATCH] feat: Convert plots into JSON with the `to_json`method --- Cargo.lock | 5 +++-- Cargo.toml | 1 + src/common/plot.rs | 9 ++++++++- src/plots/barplot.rs | 2 ++ src/plots/boxplot.rs | 2 ++ src/plots/heatmap.rs | 1 + src/plots/histogram.rs | 2 ++ src/plots/lineplot.rs | 2 ++ src/plots/scatter3dplot.rs | 2 ++ src/plots/scatterplot.rs | 2 ++ src/plots/timeseriesplot.rs | 2 ++ 11 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ddcdeb5..b14dd23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1687,6 +1687,7 @@ dependencies = [ "plotly", "polars", "serde", + "serde_json", ] [[package]] @@ -2831,9 +2832,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.128" +version = "1.0.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" dependencies = [ "itoa", "memchr", diff --git a/Cargo.toml b/Cargo.toml index c18d473..9b139ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,4 @@ polars = { version = "0.44.2", features = [ "strings", ] } serde = "1.0.214" +serde_json = "1.0.132" diff --git a/src/common/plot.rs b/src/common/plot.rs index b9d7a9b..436a281 100644 --- a/src/common/plot.rs +++ b/src/common/plot.rs @@ -1,6 +1,7 @@ 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 { @@ -8,6 +9,8 @@ pub trait Plot { fn write_html(&self, path: impl Into); + fn to_json(&self) -> Result; + // fn write_image( // &self, // path: impl Into, @@ -33,7 +36,7 @@ pub(crate) trait PlotHelper { // Implement the public trait `Plot` for any type that implements `PlotHelper`. impl Plot for T where - T: PlotHelper, + T: PlotHelper + Serialize + Clone, { fn plot(&self) { let mut plot = Plotly::new(); @@ -53,6 +56,10 @@ where plot.write_html(path.into()); } + fn to_json(&self) -> Result { + serde_json::to_string(self) + } + // fn write_image( // &self, // path: impl Into, diff --git a/src/plots/barplot.rs b/src/plots/barplot.rs index 02a8439..9584224 100644 --- a/src/plots/barplot.rs +++ b/src/plots/barplot.rs @@ -7,6 +7,7 @@ use plotly::{ }; use polars::frame::DataFrame; +use serde::Serialize; use crate::{ common::{Layout, Marker, PlotHelper, Polar}, @@ -105,6 +106,7 @@ use crate::{ /// ``` /// /// ![Example](https://imgur.com/2alZlO5.png) +#[derive(Clone, Serialize)] pub struct BarPlot { traces: Vec>, layout: LayoutPlotly, diff --git a/src/plots/boxplot.rs b/src/plots/boxplot.rs index 21179d1..0afcfe8 100644 --- a/src/plots/boxplot.rs +++ b/src/plots/boxplot.rs @@ -6,6 +6,7 @@ use plotly::{ }; use polars::frame::DataFrame; +use serde::Serialize; use crate::{ common::{Layout, Marker, PlotHelper, Polar}, @@ -106,6 +107,7 @@ use crate::{ /// ``` /// /// ![Example](https://imgur.com/uj1LY90.png) +#[derive(Clone, Serialize)] pub struct BoxPlot { traces: Vec>, layout: LayoutPlotly, diff --git a/src/plots/heatmap.rs b/src/plots/heatmap.rs index fd00be3..714cf7a 100644 --- a/src/plots/heatmap.rs +++ b/src/plots/heatmap.rs @@ -70,6 +70,7 @@ use crate::{ /// ``` /// /// ![Example](https://imgur.com/5uFih4M.png) +#[derive(Clone, Serialize)] pub struct HeatMap { pub traces: Vec>, pub layout: LayoutPlotly, diff --git a/src/plots/histogram.rs b/src/plots/histogram.rs index 0bf4245..2b9916e 100644 --- a/src/plots/histogram.rs +++ b/src/plots/histogram.rs @@ -6,6 +6,7 @@ use plotly::{ }; use polars::frame::DataFrame; +use serde::Serialize; use crate::{ common::{Layout, Marker, PlotHelper, Polar}, @@ -98,6 +99,7 @@ use crate::{ /// ``` /// /// ![Example](https://imgur.com/w2oiuIo.png) +#[derive(Clone, Serialize)] pub struct Histogram { traces: Vec>, layout: LayoutPlotly, diff --git a/src/plots/lineplot.rs b/src/plots/lineplot.rs index f673909..22e4b26 100644 --- a/src/plots/lineplot.rs +++ b/src/plots/lineplot.rs @@ -9,6 +9,7 @@ use polars::{ frame::DataFrame, prelude::{col, IntoLazy}, }; +use serde::Serialize; use crate::{ common::{Layout, Line, Marker, PlotHelper, Polar}, @@ -109,6 +110,7 @@ use crate::{ /// ``` /// /// ![Example](https://imgur.com/PaXG300.png) +#[derive(Clone, Serialize)] pub struct LinePlot { traces: Vec>, layout: LayoutPlotly, diff --git a/src/plots/scatter3dplot.rs b/src/plots/scatter3dplot.rs index 355d6da..c96bc4e 100644 --- a/src/plots/scatter3dplot.rs +++ b/src/plots/scatter3dplot.rs @@ -6,6 +6,7 @@ use plotly::{ }; use polars::frame::DataFrame; +use serde::Serialize; use crate::{ common::{Layout, Marker, PlotHelper, Polar}, @@ -89,6 +90,7 @@ use crate::{ /// ``` /// /// ![Example](https://imgur.com/BXlxKfg.png) +#[derive(Clone, Serialize)] pub struct Scatter3dPlot { traces: Vec>, layout: LayoutPlotly, diff --git a/src/plots/scatterplot.rs b/src/plots/scatterplot.rs index 9cc0a7f..46f4504 100644 --- a/src/plots/scatterplot.rs +++ b/src/plots/scatterplot.rs @@ -6,6 +6,7 @@ use plotly::{ }; use polars::frame::DataFrame; +use serde::Serialize; use crate::{ common::{Layout, Marker, PlotHelper, Polar}, @@ -105,6 +106,7 @@ use crate::{ /// ``` /// /// ![Example](https://imgur.com/9jfO8RU.png) +#[derive(Clone, Serialize)] pub struct ScatterPlot { traces: Vec>, layout: LayoutPlotly, diff --git a/src/plots/timeseriesplot.rs b/src/plots/timeseriesplot.rs index f51e010..43cd141 100644 --- a/src/plots/timeseriesplot.rs +++ b/src/plots/timeseriesplot.rs @@ -9,6 +9,7 @@ use polars::{ frame::DataFrame, prelude::{col, IntoLazy}, }; +use serde::Serialize; use crate::{ common::{Layout, Line, Marker, PlotHelper, Polar}, @@ -89,6 +90,7 @@ use crate::{ /// ``` /// /// ![Example](https://imgur.com/1GaGFbk.png) +#[derive(Clone, Serialize)] pub struct TimeSeriesPlot { traces: Vec>, layout: LayoutPlotly,