Skip to content

Instantly share code, notes, and snippets.

@sholtomaud
Created March 19, 2015 00:36
Show Gist options
  • Save sholtomaud/3f2456bc165e087abfab to your computer and use it in GitHub Desktop.
Save sholtomaud/3f2456bc165e087abfab to your computer and use it in GitHub Desktop.
d3.js + jsdom + svg export from cli
var jsdom = require('jsdom'),
d3 = require('d3'),
htmlStub = '<html><head></head><body><div id="dataviz-container"></div><div id="cy"></div><script src="js/d3.v3.min.js"></script></body></html>';
jsdom.env({
features : {QuerySelector:true},
html : htmlStub,
done : function (err, window) {
var el = window.document.querySelector('#dataviz-container'),
body = window.document.querySelector('body');
var width = 1000;
var height = 1000;
var svg = d3.select(el)
.append("svg")
.attr('width', width)
.attr('height', height);
svg.append("rect")
.attr("x", 10)
.attr("y", 10)
.attr("width", 80)
.attr("height", 80)
.style("fill", "orange");
svg.append("circle")
.attr("cx", 25)
.attr("cy", 25)
.attr("r", 25)
.style("fill", "purple")
svg.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 25)
.style("fill", "purple")
svg.append("circle")
.attr("cx", 190)
.attr("cy", 190)
.attr("r", 25)
svg.append("line")
.attr("x1", 100)
.attr("y1", 100)
.attr("x2", 190)
.attr("y2", 190)
.style("width",2)
// PRINT OUT:
console.log(d3.select(el).html());
// fs.writeFileSync('out.svg', window.d3.select("body").html()); // or this
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment