Published
Edited
Mar 13, 2019
4 stars
Insert cell
Insert cell
chart = {
const root = partition(data);

const svg = d3.select(DOM.svg(width, width))
.style("width", "100%")
.style("height", "auto")
.style("padding", "10px")
.style("font", "10px sans-serif")
.style("box-sizing", "border-box");
svg.append("g")
.selectAll("path")
.data(root.descendants().filter(d => d.depth))
.enter().append("path")
.attr("fill", d => color(d.data.data.polarite))
.attr("d", arc)
.append("title")
.text(d => `${d.ancestors().map(d => d.data.data.name).reverse().join("/")}•${format(d.data.data.value)}•${format(d.data.data.polarite)}`);

svg.append("g")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.selectAll("text")
.data(root.descendants().filter(d => d.depth && (d.y0 + d.y1) / 2 * (d.x1 - d.x0) > 10))
.enter().append("text")
.attr("transform", function(d) {
const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
const y = (d.y0 + d.y1) / 2;
return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
})
.attr("dy", "0.35em")
.text(d => d.data.data.name);

return autosize(svg.node());
}
Insert cell
Insert cell
Insert cell
Insert cell
partition = data => d3.partition()
.size([2 * Math.PI, radius])
(d3.hierarchy(data, d => d.children)
.sum(d => d.data.value)
.sort((a, b) => b.data.value - a.data.value))
Insert cell
color = d3.scaleDiverging(d3.interpolateBrBG).domain([d3.min(csv.map(d => d.polarite)), 0, d3.max(csv.map(d => d.polarite))])
Insert cell
format = d3.format(",d")
Insert cell
width = 932
Insert cell
radius = width / 2
Insert cell
arc = d3.arc()
.startAngle(d => d.x0)
.endAngle(d => d.x1)
.padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
.padRadius(radius / 2)
.innerRadius(d => d.y0)
.outerRadius(d => d.y1 - 1)
Insert cell
function autosize(svg) {
document.body.appendChild(svg);
const box = svg.getBBox();
document.body.removeChild(svg);
svg.setAttribute("viewBox", `${box.x} ${box.y} ${box.width} ${box.height}`);
return svg;
}
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more