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

plotly-graph: add support for JSON exports #309

Merged
merged 6 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/component/plotly-graph/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ module.exports = {
svg: 'image/svg+xml',
pdf: 'application/pdf',
eps: 'application/postscript',
emf: 'image/emf'
emf: 'image/emf',
json: 'application/json'
},

statusMsg: {
400: 'invalid or malformed request syntax',
406: 'requested format is not acceptable',
525: 'plotly.js error',
526: 'plotly.js version 1.11.0 or up required',
527: 'plotly.js version 1.53.0 or up required for exporting to `json`',
530: 'image conversion error'
},

Expand Down
4 changes: 4 additions & 0 deletions src/component/plotly-graph/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ function convert (info, opts, reply) {
}

switch (format) {
case 'json':
body = imgData
bodyLength = body.length
return done()
case 'png':
case 'jpeg':
case 'webp':
Expand Down
21 changes: 20 additions & 1 deletion src/component/plotly-graph/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ function render (info, opts, sendToMain) {
const PRINT_TO_PDF = (format === 'pdf' || format === 'eps')
const PRINT_TO_EMF = (format === 'emf')

let imgOptsFormat
if (PRINT_TO_PDF || PRINT_TO_EMF) {
imgOptsFormat = 'svg'
} else if (format === 'json') {
imgOptsFormat = 'full-json'
} else {
imgOptsFormat = format
}

// stash `paper_bgcolor` here in order to set the pdf window bg color
let bgColor
const pdfBackground = (gd, _bgColor) => {
Expand All @@ -51,7 +60,7 @@ function render (info, opts, sendToMain) {
}

const imgOpts = {
format: (PRINT_TO_PDF || PRINT_TO_EMF) ? 'svg' : format,
format: imgOptsFormat,
width: info.width,
height: info.height,
// only works as of plotly.js v1.31.0
Expand All @@ -64,6 +73,16 @@ function render (info, opts, sendToMain) {
: ''
}

if (
// 'full-json' was introduced in plotly.js v1.53.0
// see: https://github.com/plotly/plotly.js/releases/tag/v1.53.0
imgOpts.format === 'full-json' && semver.lt(Plotly.version, '1.53.0')
) {
errorCode = 527
result.error = `plotly.js version: ${Plotly.version}`
return done()
}

let promise

if (semver.gte(Plotly.version, '1.30.0')) {
Expand Down
1 change: 1 addition & 0 deletions test/image/baselines/29.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/image/baselines/basic_heatmap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":[{"autocolorscale":false,"colorbar":{"bgcolor":"rgba(0,0,0,0)","bordercolor":"#444","borderwidth":0,"exponentformat":"B","len":1,"lenmode":"fraction","nticks":0,"outlinecolor":"#444","outlinewidth":1,"separatethousands":false,"showexponent":"all","showticklabels":true,"thickness":30,"thicknessmode":"pixels","tickangle":"auto","tickfont":{"color":"#444","family":"\"Open Sans\", verdana, arial, sans-serif","size":12},"tickformat":"","tickmode":"auto","tickprefix":"","ticks":"","ticksuffix":"","title":{"font":{"color":"#444","family":"\"Open Sans\", verdana, arial, sans-serif","size":12},"side":"top","text":"Click to enter Colorscale title"},"x":1.02,"xanchor":"left","xpad":10,"y":0.5,"yanchor":"middle","ypad":10},"colorscale":[[0,"rgb(5,10,172)"],[0.35,"rgb(106,137,247)"],[0.5,"rgb(190,190,190)"],[0.6,"rgb(220,170,132)"],[0.7,"rgb(230,145,90)"],[1,"rgb(178,10,28)"]],"connectgaps":false,"dx":1,"dy":1,"hoverinfo":"x+y+z+text","hoverlabel":{"align":"auto","font":{"family":"Arial, sans-serif","size":13},"namelength":15},"hoverongaps":true,"hovertemplate":"","index":0,"legendgroup":"","name":"trace 0","opacity":1,"reversescale":false,"showlegend":false,"showscale":true,"transpose":false,"type":"heatmap","uid":"data0","visible":true,"x0":0,"xaxis":"x","xcalendar":"gregorian","xgap":0,"y0":0,"yaxis":"y","ycalendar":"gregorian","ygap":0,"z":[[1,20,30],[20,1,60],[30,60,1]],"zauto":true,"zhoverformat":"","zmax":60,"zmin":1,"zsmooth":false}],"layout":{"annotations":[],"autosize":false,"calendar":"gregorian","clickmode":"event","colorscale":{"diverging":[[0,"rgb(5,10,172)"],[0.35,"rgb(106,137,247)"],[0.5,"rgb(190,190,190)"],[0.6,"rgb(220,170,132)"],[0.7,"rgb(230,145,90)"],[1,"rgb(178,10,28)"]],"sequential":[[0,"rgb(220,220,220)"],[0.2,"rgb(245,195,157)"],[0.4,"rgb(245,160,105)"],[1,"rgb(178,10,28)"]],"sequentialminus":[[0,"rgb(5,10,172)"],[0.35,"rgb(40,60,190)"],[0.5,"rgb(70,100,245)"],[0.6,"rgb(90,120,245)"],[0.7,"rgb(106,137,247)"],[1,"rgb(220,220,220)"]]},"colorway":["#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#7f7f7f","#bcbd22","#17becf"],"dragmode":"zoom","font":{"color":"#444","family":"\"Open Sans\", verdana, arial, sans-serif","size":12},"height":500,"hidesources":false,"hoverdistance":20,"hoverlabel":{"align":"auto","font":{"family":"Arial, sans-serif","size":13},"namelength":15},"hovermode":"x","images":[],"margin":{"autoexpand":true,"b":80,"l":80,"pad":0,"r":80,"t":100},"modebar":{"activecolor":"rgba(68, 68, 68, 0.7)","bgcolor":"rgba(255, 255, 255, 0.5)","color":"rgba(68, 68, 68, 0.3)","orientation":"h"},"paper_bgcolor":"#fff","plot_bgcolor":"#fff","separators":".,","shapes":[],"showlegend":false,"sliders":[],"spikedistance":20,"title":{"font":{"color":"#444","family":"\"Open Sans\", verdana, arial, sans-serif","size":17},"pad":{"b":0,"l":0,"r":0,"t":0},"text":"Click to enter Plot title","x":0.5,"xanchor":"auto","xref":"container","y":"auto","yanchor":"auto","yref":"container"},"uniformtext":{"mode":false},"updatemenus":[],"width":700,"xaxis":{"anchor":"y","automargin":false,"autorange":true,"color":"#444","constrain":"range","constraintoward":"center","domain":[0,1],"dtick":0.5,"exponentformat":"B","fixedrange":false,"gridcolor":"rgb(238, 238, 238)","gridwidth":1,"hoverformat":"","layer":"above traces","mirror":false,"nticks":0,"range":[-0.5,2.5],"rangemode":"normal","separatethousands":false,"showexponent":"all","showgrid":true,"showline":false,"showspikes":false,"showticklabels":true,"side":"bottom","tick0":0,"tickangle":"auto","tickcolor":"#444","tickfont":{"color":"#444","family":"\"Open Sans\", verdana, arial, sans-serif","size":12},"tickformat":"","ticklen":5,"tickmode":"auto","tickprefix":"","ticks":"outside","ticksuffix":"","tickwidth":1,"title":{"font":{"color":"#444","family":"\"Open Sans\", verdana, arial, sans-serif","size":14},"text":"Click to enter X axis title"},"type":"linear","visible":true,"zeroline":true,"zerolinecolor":"#444","zerolinewidth":1},"yaxis":{"anchor":"x","automargin":false,"autorange":true,"color":"#444","constrain":"range","constraintoward":"middle","domain":[0,1],"dtick":0.5,"exponentformat":"B","fixedrange":false,"gridcolor":"rgb(238, 238, 238)","gridwidth":1,"hoverformat":"","layer":"above traces","mirror":false,"nticks":0,"range":[-0.5,2.5],"rangemode":"normal","separatethousands":false,"showexponent":"all","showgrid":true,"showline":false,"showspikes":false,"showticklabels":true,"side":"left","tick0":0,"tickangle":"auto","tickcolor":"#444","tickfont":{"color":"#444","family":"\"Open Sans\", verdana, arial, sans-serif","size":12},"tickformat":"","ticklen":5,"tickmode":"auto","tickprefix":"","ticks":"outside","ticksuffix":"","tickwidth":1,"title":{"font":{"color":"#444","family":"\"Open Sans\", verdana, arial, sans-serif","size":14},"text":"Click to enter Y axis title"},"type":"linear","visible":true,"zeroline":true,"zerolinecolor":"#444","zerolinewidth":1}},"frames":[],"config":{"autosizable":false,"displayModeBar":false,"displaylogo":true,"doubleClick":false,"doubleClickDelay":300,"editable":false,"edits":{},"fillFrame":false,"frameMargins":0,"globalTransforms":[],"linkText":"Edit chart","locale":"en-US","locales":{},"logging":1,"mapboxAccessToken":null,"modeBarButtons":false,"modeBarButtonsToAdd":[],"modeBarButtonsToRemove":[],"notifyOnLogging":0,"plotGlPixelRatio":2.5,"plotlyServerURL":"","queueLength":0,"responsive":false,"scrollZoom":false,"sendData":true,"setBackground":"_function","showAxisDragHandles":true,"showAxisRangeEntryBoxes":true,"showEditInChartStudio":false,"showLink":false,"showSendToCloud":false,"showSources":false,"showTips":false,"staticPlot":true,"toImageButtonOptions":{},"topojsonURL":"https://cdn.plot.ly/","watermark":false},"version":"1.53.0"}
Binary file modified test/image/baselines/geo_choropleth-usa.emf
Binary file not shown.
Loading