chart = {
const arcs = pie(data);
const svg = d3
.create("svg")
.attr("viewBox", [
-width / 2,
-margin.top,
width,
height / 2 + margin.bottom + margin.top
]);
svg
.selectAll("path")
.data(arcs)
.join("path")
.attr("fill", d => color(d.data.Stance))
.attr("d", arc)
.append("title")
.text(d => `${d.data.Stance}: ${d.data[r]}`);
svg
.append("g")
.attr("font-family", "Acumin Pro")
.attr("font-size", 20)
.attr("text-anchor", "middle")
.selectAll("text")
.data(arcs)
.join("text")
.attr("transform", d => `translate(${arc.centroid(d)})`)
.call(text =>
text
.filter(d => d.endAngle - d.startAngle > 0.1)
.append("tspan")
.attr("y", "-0.4em")
.attr("font-weight", "bold")
.text(d => d.data.Stance)
)
.call(text =>
text
.filter(d => d.endAngle - d.startAngle > 0.1)
.append("tspan")
.attr("x", 0)
.attr("y", "0.7em")
.attr("fill-opacity", 0.7)
.text(d => d.data[r])
);
const legend = svg
.append('g')
.attr(
'transform',
`translate(${-width / 2 + margin.left},${-height / 2.1})`
);
legend
.selectAll(null)
.data(data)
.enter()
.append('rect')
.attr('x', width * 0.75)
.attr('y', (d, i) => labelHeight * i * 1.8 + height * 0.8)
.attr('width', labelHeight)
.attr('height', labelHeight)
.attr('fill', d => color(d.Stance))
.attr('stroke', 'grey')
.style('stroke-width', '1px');
legend
.selectAll(null)
.data(data)
.enter()
.append('text')
.text(d => `${d.Stance}: ${d[r]}`)
.attr('x', labelHeight * 1.2 + width * 0.75)
.attr(
'y',
(d, i) => labelHeight * i * 1.8 + labelHeight / 1.1 + height * 0.8
)
.style('font-size', `${labelHeight}px`);
return svg.node();
}