Reputation: 6154
This is the code to compute the coordinates of the sunburst nodes:
var arc = d3.svg.arc()
.startAngle(function(d) { return d.x; })
.endAngle(function(d) { return d.x + d.dx; })
.innerRadius(function(d) { return Math.sqrt(d.y); })
.outerRadius(function(d) { return Math.sqrt(d.y + d.dy); });
Where:
x: the minimum x-coordinate of the node position
y: the minimum y-coordinate of the node position
dx: the x-extent of the node position
dy: the y-extent of the node position
However, in the recently realeased version v4, the space-filling layouts d3.treemap and d3.partition now output x0, x1, y0, y1 on each node instead of x0, dx, y0, dy
node.x0 - the left edge of the rectangle
node.y0 - the top edge of the rectangle
node.x1 - the right edge of the rectangle
node.y1 - the bottom edge of the rectangle
What would be the correspinding code for v4 as the following does not produce the correct layout?
var arc = d3.arc()
.startAngle(function(d) { return d.x0; })
.endAngle(function(d) { return d.x0 + (d.x1 - d.x0); })
.innerRadius(function(d) { return d.y0; })
.outerRadius(function(d) { return d.y0 + (d.y1 - d.y0); });
See codepen
Upvotes: 1
Views: 2778
Reputation: 6154
See codepen
var data = {...}
var width = 960;
var height = 700;
var radius = Math.min(width, height) / 2;
var color = d3.scaleOrdinal(d3.schemeCategory20c)
var g = d3.select('#container')
.append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + width/2 + ',' + height/2 + ')');
var partition = d3.partition()
.size([360, radius])
.padding(0)
//.round(true); //this will produce weird leaf node size if true
var root = d3.hierarchy(data, function(d) { return d.children })
.sum( function(d) {
if(d.children) {
return 0
} else {
return 1
}
})
.sort(null);
partition(root)
var xScale = d3.scaleLinear()
.domain([0, radius])
.range([0, Math.PI * 2])
.clamp(true);
var arc = d3.arc()
.startAngle(function(d) { return xScale(d.x0) })
.endAngle(function(d) { return xScale(d.x1) })
.innerRadius(function(d) { return d.y0 })
.outerRadius(function(d) { return d.y1 })
var path = g.selectAll('path')
.data(root.descendants())
.enter().append('path')
.attr("display", function(d) { return d.depth ? null : "none"; })
.attr("d", arc)
.attr("fill-rule", "evenodd")
.style('stroke', '#fff')
.style("fill", function(d) { return color((d.children ? d : d.parent).data.name); })
Upvotes: 6