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

How to save the JSON object #35

Closed
diegozea opened this issue Apr 20, 2016 · 3 comments
Closed

How to save the JSON object #35

diegozea opened this issue Apr 20, 2016 · 3 comments

Comments

@diegozea
Copy link
Contributor

Hi! I want to export/save the JSON plot data, in order to load it in a HTML and modify it using JS to interact with the plot in different ways. Is it possible to do that? Thanks!

@cc7768
Copy link
Collaborator

cc7768 commented Apr 20, 2016

Hi. It looks like you can do what you want. I'm not quite sure what exactly you wanted to extract, but here is an example of what you can do.

I first create a plot object

julia> using PlotlyJS
julia> using JSON

julia> t = scatter(;y=randn(10))
scatter with field y


julia> p = plot(t);

I'm not 100% sure what you wanted from the json. First, I get the entire corresponding json which describes both data and layout. I also show you can get each of them separately.

julia> println(json(p.plot))
{"data":[{"type":"scatter","y":[-0.36056101860419904,0.010378695002435814,0.6862578856068621,1.1708753904672111,0.02193530205277864,-1.9889936066496465,0.5101794208421222,-1.6809582341634173,0.13301085439172255,1.6620886473634708]}],"layout":{"margin":{"r":50,"l":50,"b":50,"t":60}}}

julia> println(json(p.plot.data))
[{"type":"scatter","y":[-0.36056101860419904,0.010378695002435814,0.6862578856068621,1.1708753904672111,0.02193530205277864,-1.9889936066496465,0.5101794208421222,-1.6809582341634173,0.13301085439172255,1.6620886473634708]}]

julia> println(json(p.plot.layout))
{"margin":{"r":50,"l":50,"b":50,"t":60}}

Additionally, if you wanted to just get the HTML on its own (without having to pass it anywhere) then you could get the html by using

julia> println(PlotlyJS.html_body(p.plot))
<div id="b0e29592-832f-41a5-9421-804cc36044d2" class="plotly-graph-div"></div>

<script>
    window.PLOTLYENV=window.PLOTLYENV || {};
    window.PLOTLYENV.BASE_URL="https://plot.ly";
   Plotly.newPlot('b0e29592-832f-41a5-9421-804cc36044d2', [{"type":"scatter","y":[-0.36056101860419904,0.010378695002435814,0.6862578856068621,1.1708753904672111,0.02193530205277864,-1.9889936066496465,0.5101794208421222,-1.6809582341634173,0.13301085439172255,1.6620886473634708]}],  {"margin":{"r":50,"l":50,"b":50,"t":60}}, {showLink: false});

 </script>

Kind of a lot to read. Does that help?

@diegozea
Copy link
Contributor Author

Thanks @cc7768 ! This was really useful :)

@sglyon
Copy link
Member

sglyon commented Apr 20, 2016

Quick note: you can also do json(p.plot, 2) to have it nicely formatted with indentation length 2.

And you can do JSON.parsefile(PlotlyJS.Plot, filename) to re-construct a plot assuming at some point you did open(filename, "w") do f; print(f, json(p)); end

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

3 participants