Skip to content

Instantly share code, notes, and snippets.

@Orbifold
Last active July 5, 2018 05:50
Show Gist options
  • Save Orbifold/d6dbc1ef67e963caad86d25d449689cb to your computer and use it in GitHub Desktop.
Save Orbifold/d6dbc1ef67e963caad86d25d449689cb to your computer and use it in GitHub Desktop.
Creating a pie-chart with NodeJS and d3.
var fs = require('fs');
var path = require('path');
var d3 = require('d3');
const jsdom = require("jsdom");
const JSDOM = jsdom.JSDOM;
var chartWidth = 500, chartHeight = 500;
var arc = d3.svg.arc()
.outerRadius(chartWidth / 2 - 10)
.innerRadius(0);
var colours = ['#F00', '#000', '#000', '#000', '#000', '#000', '#000', '#000', '#000'];
function go(pieData, outputLocation) {
if(!pieData) pieData = [12, 31];
if(!outputLocation) outputLocation = path.join(__dirname, './test.svg');
const dom = new JSDOM("");
dom.window.d3 = d3.select(dom.window.document); //get d3 into the dom
//do yr normal d3 stuff
var svg = dom.window.d3.select('body')
.append('div').attr('class', 'container') //make a container div to ease the saving process
.append('svg')
.attr({
xmlns: 'http://www.w3.org/2000/svg',
width: chartWidth,
height: chartHeight
})
.append('g')
.attr('transform', 'translate(' + chartWidth / 2 + ',' + chartWidth / 2 + ')');
svg.selectAll('.arc')
.data(d3.layout.pie()(pieData))
.enter()
.append('path')
.attr({
'class': 'arc',
'd': arc,
'fill': function(d, i) {
return colours[i];
},
'stroke': '#fff'
});
fs.writeFileSync(outputLocation, dom.window.d3.select('.container').html()) //using sync to keep the code simple
}
go()
{
"name": "node-d3",
"version": "1.0.0",
"description": "Serverside SVG via D3 & jsdom",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "[email protected]",
"license": "AGPLv3",
"dependencies": {
"d3": "3.5.5",
"jsdom": "*"
}
}
@flameddd
Copy link

flameddd commented Jul 5, 2018

for d3js v5

const fs = require('fs');
const path = require('path');
const d3 = require('d3');
const jsdom = require("jsdom");
const JSDOM = jsdom.JSDOM;

const chartWidth = 500;
const chartHeight = 500;

const arc = d3.arc()
  .outerRadius(chartWidth / 2 - 10)
  .innerRadius(0);

const colours = ["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"];
function go(
  pieData = [12, 31],
  outputLocation = path.join(__dirname, './test.svg')
) {
  const dom = new JSDOM("");

  dom.window.d3 = d3.select(dom.window.document); //get d3 into the dom

  //do yr normal d3 stuff
  const svg = dom.window.d3.select('body')
    .append('div').attr('class', 'container') //make a container div to ease the saving process
    .append('svg')
    .attr("xmlns", 'http://www.w3.org/2000/svg')
    .attr("width", chartWidth)
    .attr("height", chartHeight)
    .append('g')
    .attr('transform', 'translate(' + chartWidth / 2 + ',' + chartWidth / 2 + ')');

  svg.selectAll('.arc')
    .data(d3.pie()(pieData))
    .enter()
    .append('path')
    .attr("class", 'arc')
    .attr("d", arc)
    .attr("fill", (d, i) => colours[i])
    .attr("stroke", '#fff')

  //using sync to keep the code simple
  fs.writeFileSync(outputLocation, dom.window.d3.select('.container').html())
}

go()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment