Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SVG from file with D3.js
const fs = require('fs');
const d3 = require('d3');
const D3Node = require('d3-node');
var jsdom = require('jsdom');
const { JSDOM } = jsdom;
var contents = fs.readFileSync(__dirname + '/data/rectangle_100.svg', 'utf8');
// console.log(contents);
const dom = new JSDOM(contents);
// console.log(dom.window.document.querySelector('svg').outerHTML);
const options = { selector: '#bkit_svg', container: contents };
const d3n = new D3Node(options); // initializes D3 with container element
d3.select(d3n.document.querySelector('#bkit_svg')).append('rect');
// console.log(d3n.html()); // output: <html>...
console.log(d3n.chartHTML()); // output: <svg id="bkit_svg">...
JSDOM.fromFile(__dirname + '/data/rectangle_100.svg').then(function (jsdom) {
// console.log(jsdom.window.document.querySelector('svg').outerHTML);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.