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 Request] Add Plot -> JSON output #27

Closed
matthewalltop opened this issue Nov 9, 2024 · 1 comment
Closed

[Feature Request] Add Plot -> JSON output #27

matthewalltop opened this issue Nov 9, 2024 · 1 comment

Comments

@matthewalltop
Copy link

Thank you for making this crate - I've greatly enjoyed using it.

I have been using the .to_json() methods exposed from the Plot trait to build a JSON API payload for a web application using the Plot layout + traces - I noticed this is now inaccessible in the latest version of the crate - looks like a change from #26 .

Could you add a .to_json() method that can be called off of .build() for each plot & output a serialized JSON payload of the plot?

I have been doing it similarly to this:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct PlotlyPlot {
    pub(crate) layout: String,
    pub(crate) traces: Vec<String>
}

// Example Plot Type
let plots = BarPlot::builder()/**/.build();
    
// Layout
let layout = plots.get_layout().to_json();
    
// Traces
let traces: Vec<String> = plots.get_traces().into_iter().map(|x| x.as_ref().to_json()).collect();

// Serialize
let response = PlotlyGraph { layout, traces };
let json = serde_json::to_string(&response);

i.e.

let response = BarPlot::builder()
             /** */
             .build()
             .to_json();

That can be plugged into the PlotlyJS component in custom web applications:

<plotly-plot [layout]="plot.layout" [data]="plot.traces"></plotly-plot>
@alceal
Copy link
Owner

alceal commented Nov 9, 2024

@matthewalltop Thank you for using this crate and for contribute with an idea to improve it. I've published the version 0.7.1. The plots have the to_json method.

@alceal alceal closed this as completed Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants