Generate plotly.js images using nw.js
- Install nw.js (see instructions)
- Clone this gist and
cd
into it
./path/to/nw .
or if nw.js
is linked to a nw
executable
npm start
and 🍻.`
Generate plotly.js images using nw.js |
node_modules | |
npm-debug.log |
Generate plotly.js images using nw.js
cd
into it./path/to/nw .
or if nw.js
is linked to a nw
executable
npm start
and 🍻.`
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>plotly.js in nw.js</title> | |
<script type="text/javascript" src="../../plotly/plotly.js/build/plotly.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript" src="main.js"></script> | |
</body> | |
</html> |
'use strict'; | |
/* global Plotly:true */ | |
var fs = require('fs'); | |
var gui = require('nw.gui'); | |
var MOCK = '../../plotly/plotly.js/test/image/mocks/gl2d_marker_line_width.json'; | |
var FORMATS = ['svg', 'jpeg', 'png']; | |
var BASE_FILENAME = 'out'; | |
FORMATS.forEach(generateOne); | |
function generateOne(format) { | |
var imageOptions = { format: format, imageDataOnly: true }; | |
var outPath = BASE_FILENAME + '.' + format; | |
var div = document.createElement('div'); | |
fs.readFile(MOCK, 'utf-8', function(err, raw) { | |
if(err) throw err; | |
var fig = JSON.parse(raw); | |
Plotly.plot(div, fig) | |
.then(toImage) | |
.then(decodeImage) | |
.then(saveToFile) | |
.then(quit); | |
}); | |
function toImage(gd) { | |
return Plotly.toImage(gd, imageOptions); | |
} | |
function decodeImage(img) { | |
return new Promise(function(resolve) { | |
switch(imageOptions.format) { | |
case 'png': | |
case 'jpeg': | |
img = new Buffer(img, 'base64'); | |
break; | |
case 'svg': | |
break; | |
} | |
resolve(img); | |
}); | |
} | |
function saveToFile(img) { | |
return new Promise(function(resolve, reject) { | |
fs.writeFile(outPath, img, function(err) { | |
if(err) reject(err); | |
resolve(outPath); | |
}); | |
}); | |
} | |
function quit() { | |
log('generated ' + outPath) | |
gui.App.quit(); | |
} | |
} | |
function log(msg) { | |
process.stdout.write(msg + '\n'); | |
} |
{ | |
"name": "nw.js-plotly.js", | |
"version": "2.0.0", | |
"description": "generate plotly.js images using nw.js", | |
"main": "index.html", | |
"scripts": { | |
"start": "nw ." | |
}, | |
"author": "Étienne Tétreault-Pinard", | |
"license": "MIT", | |
"dependencies": { | |
"plotly.js": "^1.16.2" | |
} | |
} |