- something is up with selector containing
not
e.g.'path:not([d])'
- must remove
getComputedTextLength
method in Plots.addLinks - must shim
XMLSerializer
(wait forjsdom
to natively support it) - some problem with
canvas
maybe??toImage
with non-svg format result in Segmentation faults
Last active
April 14, 2021 07:44
-
-
Save etpinard/bee7d62b43b6bb286950 to your computer and use it in GitHub Desktop.
plotly.js in node (WIP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules | |
npm-debug.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var jsdom = require('jsdom'); | |
var PATH_TO_PLOTLYJS = require.resolve('plotly.js/dist/plotly.js'); | |
var plotlySrc = fs.readFileSync(PATH_TO_PLOTLYJS, 'utf-8'); | |
var html = ` | |
<body> | |
<div id="graph"></div> | |
</body>`; | |
var data = [{ | |
x: [1,2,3], | |
y: [2,1,2], | |
mode: 'markers', | |
marker: { | |
size: 20, | |
color: 'green' | |
} | |
}]; | |
var layout = { | |
}; | |
var config = { | |
showLinks: false | |
} | |
var imageOptions = { | |
format: 'svg' | |
}; | |
var OUTFILENAME = path.join(__dirname, 'out.' + imageOptions.format); | |
var virtualConsole = jsdom.createVirtualConsole(); | |
virtualConsole.on('log', (message) => { | |
console.log('console.log called ->', message); | |
}); | |
virtualConsole.on('jsdomError', (message) => { | |
console.error('Error ->', message); | |
}); | |
jsdom.env({ | |
html: html, | |
src: [plotlySrc], | |
// Taken from: https://gist.github.com/hugolpz/1c34e14d8b7dd457f802 (you | |
// need query selector for D3 to work) | |
features: { QuerySelector: true }, | |
virtualConsole: virtualConsole, | |
done: main | |
}); | |
function main(err, window) { | |
if(err) throw err; | |
var document = window.document; | |
var Plotly = window.Plotly; | |
var d3 = Plotly.d3; | |
var gd = document.getElementById('graph'); | |
// mock XML Serializer (for now) | |
window.XMLSerializer = function() { | |
return { | |
serializeToString: (node) => { | |
return String(node.outerHTML) | |
} | |
}; | |
}; | |
Plotly.plot(gd, data, layout, config).then((gd) => { | |
return Plotly.toImage(gd, imageOptions); | |
}) | |
.then((img) => { | |
var svg = decodeURIComponent(img.replace(/^data:image\/svg\+xml,/, '')) | |
fs.writeFile(OUTFILENAME, svg, (err) => { | |
if(err) throw err; | |
console.log('done'); | |
}) | |
}) | |
.catch((err) => { | |
console.log(err) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "plotly-in-node", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"author": "Étienne Tétreault-Pinard", | |
"license": "MIT", | |
"dependencies": { | |
"canvas": "^1.6.5", | |
"jsdom": "^9.12.0", | |
"plotly.js": "^1.25.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work anymore with JSDOM v10, API has changed.