Skip to content

Instantly share code, notes, and snippets.

@shancarter
Last active June 7, 2019 11:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shancarter/e988a8a9576e2205af6e3f0e06c26e1a to your computer and use it in GitHub Desktop.
Save shancarter/e988a8a9576e2205af6e3f0e06c26e1a to your computer and use it in GitHub Desktop.
Using d3 to generate SVGs in node.js

In order to generate some svg with d3 in node.js, you need a dom. This is a bare bones example of using jsdom for said dom.

#!/usr/bin/env node
var d3 = require("d3"),
jsdom = require("jsdom").jsdom;
var body = d3.select(jsdom().documentElement).select("body");
var width = 1000,
height = 500;
var svg = body.append("svg");
var svg = body.append("svg")
.attr("version", "1.1")
.attr("xmlns", d3.namespaces.svg)
.attr("xmlns:xlink", d3.namespaces.xlink)
.attr("width", width)
.attr("height", height)
.attr("viewBox", "0 0 " + width + " " + height);
process.stdout.write(body.node().innerHTML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment