Published
Edited
Apr 19, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csvParse(`age,population
<5,2704659
5-13,4499890
14-17,2159981
18-24,3853788
25-44,14106543
45-64,8819342
≥65,612463`)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
var width = 960,
height = 500,
radius = Math.min(width, height) / 2;

var color = d3.scaleOrdinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 70);

var pie = d3.pie()
.sort(null)
.value(function(d) { return d.population; });
var container = html`<svg></svg>`;

var svg = d3.select(container)
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");

g.append("path")
.attr("d", arc)
.attr("fill", function(d) { return color(d.data.age); });

g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.text(function(d) { return d.data.age; });
return container;
}
Insert cell
Insert cell
Insert cell
html`
<style>

.arc text {
font: 10px sans-serif;
text-anchor: middle;
}

.arc path {
stroke: #fff;
}

</style>
`
Insert cell
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