var q = d3.queue() .defer(d3.csv, 'tree-nz-govt.csv') .await(visualize); var sf = 3.8, c = d3.conventions({ parentSel: d3.select('#chart'), width: 1500, height: 3200 }); var tree = d3.tree() .size([c.height, c.width - 120]); function visualize(errors, data) { var g = c.svg.append("g").attr("transform", "translate(40,0)"); var root = d3.stratify() .id(function(d) { return d.id; }) .parentId(function(d) { return d.parent_id; }) (data); root .sum(function(d) { return d.value; }) .sort(function(a, b) { return b.height - a.height || a.id.localeCompare(b.id); }); tree(root); var link = g.selectAll(".link") .data(root.descendants().slice(1)) .enter().append("path") .attr("class", "link") .attr("fill", "none") .attr("stroke", "#e1e1e1") .attr("stroke-width", 1.5) .attr("d", function(d) { return "M" + d.y + "," + d.x + "C" + (d.y + d.parent.y) / 2 + "," + d.x + " " + (d.y + d.parent.y) / 2 + "," + d.parent.x + " " + d.parent.y + "," + d.parent.x; }); var node = g.selectAll(".node") .data(root.descendants()) .enter().append("g") .attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); }) .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; }) node.append("circle") .attr("fill", function(d) { if (d.data.type == 'sector') { return '#666'} else { return '#ccc'} }) .attr("r", 1.8); node.append("text") .attr("font-size", 10) .attr("dy", 3) .attr("x", function(d) { return d.children ? -8 : 8; }) .attr("class", function(d) { return d.data.type; }) .style("text-anchor", function(d) { return d.children ? "end" : "start"; }) .text(function(d) { if (d.data.type == 'organisation') { return d.data.name;} else {return d.data.name;} }); };