Published
Edited
Sep 13, 2020
3 forks
2 stars
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])
);

return svg.node();
}
Insert cell
groupedData = d3.rollups(data, v => v.length, d => d[colAttr])
Insert cell
arcs = d3
.pie()
.padAngle(0.01)
.value(d => d[1])(groupedData)
Insert cell
arc
Insert cell
arc = d3
.arc()
.innerRadius(radius * 0.6)
.outerRadius(radius * 0.9)
Insert cell
html`<svg><path d="${arc(arcs[0])}" stroke="black" /></svg>`
Insert cell
radius = Math.min(width / 2, height / 2)
Insert cell
color = d3.scaleOrdinal(d3.schemePastel1).domain(data.map(d => d[colAttr]))
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