Skip to content

Instantly share code, notes, and snippets.

@danioyuan
danioyuan / pre_render_d3_graph.js
Last active May 9, 2019 03:11
Render d3 force-directed graph on server side. Require Node.js, d3.js, and jsdom.
// Pre-render d3 force-directed graph at server side
// Call node pre_render_d3_graph.js to generate d3_graph.html
// Original idea and framework borrowed from https://gist.github.com/mef/7044786
var d3 = require('d3')
, jsdom = require('jsdom')
, fs = require('fs')
, htmlStub = '<html><head> \
<style>.node { stroke: #fff; fill: #ccc; stroke-width: 1.5px; } \
.link { stroke: #333; stroke-opacity: .5; stroke-width: 1.5px; }</style> \