makeChart1 = (dataset) => {
const width = 600,
height = 500,
margin = 50;
const radius = Math.min(width, height) / 2 - margin
const svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", `translate(${width/2},${height/2})`);
const data = {Manhattan: 60001, Staten_Island: 1, Bronx:19421, Queens:36722, Brooklyn:90797}
const color = d3.scaleOrdinal()
.domain(["Manhattan", "Staten_Island", "Bronx", "Queens", "Brooklyn"])
.range(d3.schemeDark2);
const pie = d3.pie()
.sort(null)
.value(d => d[1])
const data_ready = pie(Object.entries(data))
const arc = d3.arc()
.innerRadius(radius * 0.4)
.outerRadius(radius * 0.6)
const innerArc = d3.arc()
.innerRadius(radius * 0.6)
.outerRadius(radius * 0.6)
const outerArc = d3.arc()
.innerRadius(radius * 0.6+10)
.outerRadius(radius * 0.6+10)
svg
.selectAll('allSlices')
.data(data_ready)
.join('path')
.attr('d', arc)
.attr('fill', d => color(d.data[1]))
.attr("stroke", "white")
.style("stroke-width", "2px")
.style("opacity", 0.7)
svg
.selectAll('allPolylines')
.data(data_ready)
.join('polyline')
.attr("stroke", d => color(d.data[1]))
.style("fill", "none")
.attr("stroke-width", 1)
.attr('points', function(d) {
const posA = innerArc.centroid(d)
const posB = outerArc.centroid(d)
const posC = outerArc.centroid(d);
const midangle = d.startAngle + (d.endAngle - d.startAngle) / 2
posC[0] = radius * 0.95 * (midangle < Math.PI ? 1 : -1);
return [posA, posB, posC]
})
svg
.selectAll('allLabels')
.data(data_ready)
.join('text')
.text(d => d.data[0])
.attr('transform', function(d) {
const pos = outerArc.centroid(d);
const midangle = d.startAngle + (d.endAngle - d.startAngle) / 2
pos[0] = (radius * 0.95 + 4) * (midangle < Math.PI ? 1 : -1);
return `translate(${pos})`;
})
.style('text-anchor', function(d) {
const midangle = d.startAngle + (d.endAngle - d.startAngle) / 2
return (midangle < Math.PI ? 'start' : 'end')
})
.style("alignment-baseline", "middle")
.style("fill", d => color(d.data[1]));
}