Skip to content

Commit 51df28b

Browse files
authored
Merge pull request #471 from fujidana/patch-1
docs: Add 'full-json' docs, examples
2 parents d0af0f5 + f85d4d4 commit 51df28b

3 files changed

Lines changed: 65 additions & 41 deletions

File tree

‎_posts/plotly_js/2016-06-03-plotly_js_function_ref.html‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ <h4 id="plotly-purge"><a href="{{ BASE_URL }}/javascript/plotlyjs-function-refer
602602
<h4 id="plotly-toImage"><a href="{{ BASE_URL }}/javascript/plotlyjs-function-reference/#plotlytoimage">Plotly.toImage</a></h4>
603603

604604
<code>toImage</code> will generate a promise to an image of the plot in data URL format.
605+
606+
<code>format</code> takes one of <code>png</code>, <code>jpeg</code>, <code>webp</code>, <code>svg</code>, or <code>full-json</code>. The default is <code>png</code>.
607+
<code>full-json</code> returns the figure as JSON instead of a rendered image. The JSON holds the data, layout, frames, and config, with every default value filled in.
608+
Set <code>imageDataOnly</code> to <code>true</code> to get the raw JSON string rather than a <code>data:application/json</code> URL.
605609
<pre><code class="language-javascript hljs" data-lang="javascript">
606610
// Plotly.toImage will turn the plot in the given div into a data URL string
607611
// toImage takes the div as the first argument and an object specifying image properties as the other
@@ -616,6 +620,8 @@ <h4 id="plotly-toImage"><a href="{{ BASE_URL }}/javascript/plotlyjs-function-ref
616620
<h4 id="plotly-downloadImage"><a href="{{ BASE_URL }}/javascript/plotlyjs-function-reference/#plotlydownloadimage">Plotly.downloadImage</a></h4>
617621

618622
<code>downloadImage</code> will trigger a request to download the image of a Plotly plot.
623+
624+
<code>format</code> takes the same values as <code>Plotly.toImage</code>.
619625
<pre><code class="language-javascript hljs" data-lang="javascript">
620626
// downloadImage will accept the div as the first argument and an object specifying image properties as the other
621627
Plotly.downloadImage(graphDiv, {format: 'png', width: 800, height: 600, filename: 'newplot'});

‎_posts/plotly_js/fundamentals/config-options/2015-09-24-config-opt-toImageButtonOptions.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
var config = {
2828
toImageButtonOptions: {
29-
format: 'svg', // one of png, svg, jpeg, webp
29+
format: 'svg', // one of png, svg, jpeg, webp, full-json
3030
filename: 'custom_image',
3131
height: 500,
3232
width: 700,
Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: How to export graphs as static images in JavaScript. The Plotly JavaScript
3-
graphing library supports `.jpg`, `.png`, and `.svg` as formats for static image
4-
export.
3+
graphing library supports `.jpg`, `.png`, `.webp`, and `.svg` as formats for static
4+
image export.
55
display_as: file_settings
66
language: plotly_js
77
layout: base
@@ -15,57 +15,75 @@ thumbnail: thumbnail/png-export.png
1515

1616
You can save graphs created with `plotly.js` to static images and view them in your browser. Consider the following example:
1717

18-
var img_jpg= d3.select('#jpg-export');
19-
20-
// Plotting the Graph
21-
22-
var trace={x:[3,9,8,10,4,6,5],y:[5,7,6,7,8,9,8],type:"scatter"};
23-
var trace1={x:[3,4,1,6,8,9,5],y:[4,2,5,2,1,7,3],type:"scatter"};
24-
var data = [trace,trace1];
25-
var layout = {title : "Simple JavaScript Graph"};
26-
Plotly.newPlot(
27-
'plotly_div',
28-
data,
29-
layout)
30-
31-
// static image in jpg format
32-
33-
.then(
34-
function(gd)
35-
{
36-
Plotly.toImage(gd,{height:300,width:300})
37-
.then(
38-
function(url)
39-
{
40-
img_jpg.attr("src", url);
41-
}
42-
)
43-
});
18+
const imgJpg = document.getElementById('jpg-export');
19+
20+
const trace = {x: [3, 9, 8, 10, 4, 6, 5], y: [5, 7, 6, 7, 8, 9, 8], type: 'scatter'};
21+
const trace1 = {x: [3, 4, 1, 6, 8, 9, 5], y: [4, 2, 5, 2, 1, 7, 3], type: 'scatter'};
22+
const data = [trace, trace1];
23+
const layout = {title: {text: 'Simple JavaScript Graph'}};
24+
25+
Plotly.newPlot('plotly_div', data, layout)
26+
.then((gd) => Plotly.toImage(gd, {format: 'jpeg', height: 300, width: 300}))
27+
.then((url) => {
28+
imgJpg.src = url;
29+
});
30+
4431
To view this image in your page include following HTML tag:
4532

46-
<img id="jpg-export"></img>
33+
<img id="jpg-export">
4734

4835
Height and width of the image can be adjusted by specifying the same in `toImage` call:
4936

50-
Plotly.toImage(
51-
gd,{
52-
format:'jpeg',
53-
height:desired_height,
54-
width:desired_width,
37+
Plotly.toImage(gd, {
38+
format: 'jpeg',
39+
height: desiredHeight,
40+
width: desiredWidth
5541
});
5642

5743
You can also save the image using different formats.
5844

5945
# Formats Supported
6046

61-
The common image formats: 'PNG', 'JPG/JPEG' are supported. In addition, formats like 'EPS', 'SVG' and 'PDF' are also available for user with a Personal or Professional subscription. You can get more details on our [pricing page] (https://plotly.com/products/cloud/)
47+
`Plotly.toImage` and `Plotly.downloadImage` take one of five values for `format`:
6248

63-
**Note:** It is important to note that any figures containing WebGL traces (i.e. of type scattergl, scatter3d, surface, mesh3d, scatterpolargl, cone, streamtube, splom, or parcoords) that are exported in a vector format like SVG or PDF will include encapsulated rasters instead of vectors for some parts of the image.
49+
- `png` - raster image, and the default format
50+
- `jpeg` - raster image with no transparency
51+
- `webp` - raster image
52+
- `svg` - vector image
53+
- `full-json` - the figure specification as JSON, not a rendered image
54+
55+
**Note:** A figure that contains a WebGL trace (of type scattergl, scatter3d, surface, mesh3d, scatterpolargl, cone, streamtube, splom, or parcoords) holds encapsulated rasters instead of vectors for some parts of an SVG export.
6456

6557
## Saving as PNG ##
66-
img_png.attr("src", url);
67-
Plotly.toImage(gd,{format:'png',height:400,width:400});
58+
59+
const imgPng = document.getElementById('png-export');
60+
61+
Plotly.toImage(gd, {format: 'png', height: 400, width: 400}).then((url) => {
62+
imgPng.src = url;
63+
});
6864

6965
## Saving as SVG ##
70-
img_svg.attr("src", url);
71-
Plotly.toImage(gd,{format:'svg',height:800,width:800});
66+
67+
const imgSvg = document.getElementById('svg-export');
68+
69+
Plotly.toImage(gd, {format: 'svg', height: 800, width: 800}).then((url) => {
70+
imgSvg.src = url;
71+
});
72+
73+
## Saving as JSON ##
74+
75+
The `full-json` format returns the figure with every default value filled in, including data, layout, frames, config, and the plotly.js `version`.
76+
77+
// toImage returns a data:application/json URL
78+
Plotly.toImage(gd, {format: 'full-json'}).then((dataUrl) => {
79+
console.log(dataUrl);
80+
});
81+
82+
// Set imageDataOnly to get the raw JSON string instead
83+
Plotly.toImage(gd, {format: 'full-json', imageDataOnly: true}).then((json) => {
84+
console.log(JSON.parse(json));
85+
});
86+
87+
`Plotly.downloadImage` downloads the figure JSON instead, and saves it as `newplot.full.json`:
88+
89+
Plotly.downloadImage(gd, {format: 'full-json', filename: 'newplot'});

0 commit comments

Comments
 (0)