Public
Edited
Feb 1, 2023
Insert cell
Insert cell
Insert cell
svg = {
const svg = d3
.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);

// Your marks here
svg
.selectAll(".arc")
.data(arcs)
.join("path")
.attr("class", "arc")
.attr("fill", d => color(d.data[0]))
.attr("d", a => arc(a));

// Your marks here
svg
.selectAll(".label")
.data(arcs)
.join("text")
.attr("class", "label")
.attr("fill", "#333")
.attr("transform", a => `translate(${arc.centroid(a)})`)
.style("font-size", "10pt")
.style("text-anchor", "middle")
.style("font-family", "sans-serif")
.call(text =>
text
.append("tspan")
.style("font-weight", "bold")
.attr("x", 0)
.text(a => a.data[0])
)
.call(text =>
text
.append("tspan")
.attr("x", 0)
.attr("y", "1em")
.text(a => a.data[1])
);

svg
.selectAll(".axis")
.data(d3.range(...r.domain(), radius / 4))
.join("circle")
.attr("class", "axis")
.attr("r", d => r(d))
.attr("stroke", "#aaaa")
.attr("stroke-dasharray", "2 5")
.attr("fill", "none");

svg
.selectAll(".axisText")
.data(d3.range(...r.domain(), 50))
.join("text")
.attr("class", "axisText")
.attr("y", d => r(d) - 2)
.attr("fill", "#aaaa")
.style("font-size", "8pt")
.text(d => d); // I didn't need to invert because the data was already in the domain

return svg.node();
}
Insert cell
d3.range(...r.domain(), radius / 4)
Insert cell
groupedData = d3.rollups(data, v => v.length, d => d[colAttr])
Insert cell
arcs = d3
.pie()
.padAngle(0.01)
.value(d => 1)(groupedData)
Insert cell
arc
Insert cell
arc = d3
.arc()
.innerRadius(0)
.outerRadius(d => r(d.data[1]))
Insert cell
radius = Math.min(width / 2, height / 2)
Insert cell
color = d3.scaleOrdinal(d3.schemePastel1).domain(data.map(d => d[colAttr]))
Insert cell
r = d3
.scaleSqrt()
.domain([0, d3.max(arcs, d => d.data[1])])
.range([0, radius])
.nice()
Insert cell
colAttr = "Species"
Insert cell
height = 400
Insert cell
margin = ({ left: 50, top: 30, right: 20, bottom: 20 })
Insert cell
md`## Data`
Insert cell
data = vegaDatasets["penguins.json"]()
Insert cell
vegaDatasets = require("vega-datasets@2")
Insert cell
d3 = require("d3@6")
Insert cell
import { groupToStack } from "@john-guerra/d3-stack-with-d3-group"
Insert cell
import { swatches } from "@d3/color-legend"
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