Published
Edited
Oct 6, 2020
4 forks
5 stars
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height))
.attr("viewBox", `${-width / 2} ${-height / 2} ${width} ${height}`)
.style("width", "100%")
.style("height", "auto")
.style("font", "10px sans-serif");

svg.append("g")
.selectAll("g")
.data(d3.stack().keys(data.columns.slice(1))(data))
.join("g")
.attr("fill", d => z(d.key))
.selectAll("path")
.data(d => d)
.join("path")
.attr("d", arc);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

svg.append("g")
.call(legend);

return svg.node();
}
Insert cell
data = d3.csvParse(`Month,Other,Wounds,Disease
Apr1854,5,0,1
May1854,9,0,12
Jun1854,6,0,11
Jul1854,23,0,359
Aug1854,30,1,828
Sep1854,70,81,788
Oct1854,128,132,503
Nov1854,106,287,844
Dec1854,131,114,1725
Jan1855,324,83,2761
Feb1855,361,42,2120
Mar1855,172,32,1205
Apr1855,57,48,477
May1855,37,49,508
Jun1855,31,209,802
Jul1855,33,134,382
Aug1855,25,164,483
Sep1855,20,276,189
Oct1855,18,53,128
Nov1855,32,33,178
Dec1855,28,18,91
Jan1856,48,2,42
Feb1856,19,0,24
Mar1856,35,0,15
`, (d, _, columns) => {
let total = 0;
for (let i = 1; i < columns.length; ++i) total += d[columns[i]] = +d[columns[i]];
d.total = total;
return d;
})
Insert cell
arc = d3.arc()
.innerRadius(d => y(d[0]))
.outerRadius(d => y(d[1]))
.startAngle(d => x(d.data.Month))
.endAngle(d => x(d.data.Month) + x.bandwidth())
.padRadius(innerRadius)
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.Month))
.range([-0.5*Math.PI, 0.5*Math.PI])
.align(0)
Insert cell
// This scale maintains area proportionality of radial bars
y = d3.scaleRadial()
.domain([0, d3.max(data, d => d.total)])
.range([innerRadius, outerRadius])
Insert cell
z = d3.scaleOrdinal()
.domain(data.columns.slice(1))//.range(["#DB004C", "#8CCBFF", "#FF3700"])
.range(["#FF3700", "#DB004C", "#8CCBFF"])
Insert cell
xAxis = g => g
.attr("text-anchor", "middle")
.call(g => g.selectAll("g")
.data(data)
.join("g")
.attr("transform", d => `
rotate(${((x(d.Month) + x.bandwidth() / 2) * 180 / Math.PI - 90)})
translate(${innerRadius},0)
`)
.call(g => g.append("line")
.attr("x2", -5)
.attr("stroke", "#000"))
.call(g => g.append("text")
.attr("transform", d => (x(d.Month) + x.bandwidth() / 2 + Math.PI / 2) % (Math.PI) < 0.5*Math.PI
? "rotate(180)translate(30,3)"
: "rotate(0)translate(-30,3)")
.text(d => d.Month)))
Insert cell
yAxis = g => g
.attr("text-anchor", "middle")
.call(g => g.selectAll("g")
.data(y.ticks(5).slice(1))
.join("g")
.attr("fill", "none")
.call(g => g.append("path")
.attr("stroke", "#000")
.attr("stroke-opacity", 0.05)
.attr("d", d3.arc()
.innerRadius(y)
.outerRadius(y)
.startAngle(-0.5*Math.PI)
.endAngle(0.5*Math.PI) ))

.call(g => g.append("text")
.attr("x", d => -y(d))
.attr("dx", "0.35em")
.attr("dy", "0.35em")
.attr("stroke", "#fff")
.attr("stroke-width", 5)
.text(y.tickFormat())
.clone(true)
.attr("fill", "#000")
.attr("stroke", "none"))
.call(g => g.append("text")
.attr("x", d => y(d))
.attr("dx", "0.35em")
.attr("dy", "-0.35em")
.attr("stroke", "#fff")
.attr("stroke-width", 5)
.text(y.tickFormat())
.clone(true)
.attr("fill", "#000")
.attr("stroke", "none"))
)
Insert cell
legend = g => g.append("g")
.selectAll("g")
.data(data.columns.slice(1).reverse())
.join("g")
.attr("transform", (d, i) => `translate(-20,${(i - (data.columns.length - 1) / 2) * 20-40})`)
.call(g => g.append("rect")
.attr("width", 18)
.attr("height", 18)
.attr("fill", z))
.call(g => g.append("text")
.attr("x", 24)
.attr("y", 9)
.attr("dy", "0.35em")
.text(d => d))
Insert cell
width = 975
Insert cell
height = width
Insert cell
innerRadius = 200
Insert cell
outerRadius = Math.min(width, height) / 2
Insert cell
d3 = require("d3@6")
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