Skip to content

Instantly share code, notes, and snippets.

@jpcofr
Created August 2, 2017 14:36
Show Gist options
  • Save jpcofr/15f58f7007d7dd23c0bb1d1dd9da2f0b to your computer and use it in GitHub Desktop.
Save jpcofr/15f58f7007d7dd23c0bb1d1dd9da2f0b to your computer and use it in GitHub Desktop.
Renders an svg circle at server side
var fs = require('fs');
var d3 = require('d3')
require('jsdom-global')()
var body = document.querySelector('body')
d3.select(body)
.append('svg:svg')
.attr('xmlns', 'http://www.w3.org/2000/svg')
.attr('xmlns:xlink', 'http://www.w3.org/1999/xlink')
.attr('width', 600)
.attr('height', 300)
.append('circle')
.attr('cx', 200)
.attr('cy', 100)
.attr('r', 55)
.attr('fill', '#0004FF')
fs.writeFile('./out.svg', document.body.innerHTML, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
{
"name": "poc_svg_generation",
"version": "1.0.0",
"description": "",
"main": "test_d3.js",
"dependencies": {
"d3": "^4.10.0",
"d3-node": "^1.0.3",
"jsdom": "^11.1.0",
"jsdom-global": "^3.0.2",
"xmldom": "^0.1.27"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment