Published
Edited
Apr 8, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// your code here
chart = {
const svg = d3.select(DOM.svg(width, height));

svg.append("g")
.selectAll("g")
.data(series)
.join("g")
.attr("fill", d => color(d.key))
.selectAll("rect")
.data(d => d)
.join("rect")
.attr("x", (d, i) => x(d.data.YEARS))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.attr("width", x.bandwidth());

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

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

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

return svg.node();
}
Insert cell
series = d3.stack().keys(migration.columns.slice(1))(migration)
Insert cell
[margin.left, width - margin.right]
Insert cell
x = d3.scaleBand()
.domain(migration.map(d => d.YEARS))
.range([margin.left, width - margin.right-100])
.padding(0.1)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(series, d => d3.max(d, d => d[1]))])
.rangeRound([height - margin.bottom, margin.top])
Insert cell
Insert cell
Insert cell
Insert cell
legend = svg => {
const g = svg
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.attr("transform", `translate(${width - margin.right},${margin.top})`)
.selectAll("g")
.data(series.slice().reverse())
.join("g")
.attr("transform", (d, i) => `translate(0,${i * 20})`);

g.append("rect")
.attr("x", -19)
.attr("width", 19)
.attr("height", 19)
.attr("fill", d => color(d.key));

g.append("text")
.attr("x", -24)
.attr("y", 12)
.attr("dy", "0.35em")
.text(d => d.key);
}
Insert cell
Insert cell
margin = ({top: 10, right: 10, bottom: 20, left: 40})
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