Published
Edited
Apr 10, 2019
Insert cell
Insert cell
Insert cell
Insert cell
//import d3 library
d3 = require("d3@5")
Insert cell
//import migration data and parse values as numbers
migration = d3.csv("https://raw.githubusercontent.com/sw2279/data/b3ad3a358eabc4c40670d9ca5d83d762cd79e10b/migration_forbarchart.csv", ({YEARS, Acquisition, In_asylum, In_foreign, Out_foreign, Stock_foreign_labor,Stock_foreign_pop}) => ({YEARS: +YEARS, Acquisition: +Acquisition, In_asylum: +In_asylum, In_foreign: +In_foreign, Out_foreign: +Out_foreign, Stock_foreign_labor: +Stock_foreign_labor, Stock_foreign_pop: +Stock_foreign_pop}))
Insert cell
Insert cell
//example graph
Insert cell
Insert cell
//set margins for the graph display; took defaults from stacked bar graph link above
margin = ({top: 10, right: 10, bottom: 20, left: 40})
Insert cell
//set height for graph display
height = 600
Insert cell
// your code here
chart = {
//initiate SVG
const svg = d3.select(DOM.svg(width, height));
//set attributes for svg including the shapes (rectangles), axis, height, width
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());

//add on the axes labels and legend
svg.append("g")
.call(xAxis);

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

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

//return the graph
return svg.node();
}
Insert cell
//reshapes the df such that each object in the array now represents all the values for a variable (acquisition, in-flows, etc) rather than for a year
series = d3.stack().keys(migration.columns.slice(1))(migration)
Insert cell
//scale x-axis values (which reflect years)
x = d3.scaleBand()
.domain(migration.map(d => d.YEARS))
.range([margin.left, width - margin.right-150])
.padding(.2)
Insert cell
//scale y-axis values (migration numbers))
y = d3.scaleLinear()
.domain([0, d3.max(series, d => d3.max(d, d => d[1]))])
.rangeRound([height - margin.bottom, margin.top])
.nice()
Insert cell
// define colors
color = d3.scaleOrdinal()
.domain(series.map(d => d.key)) //maps
.range(d3.quantize(t => d3.interpolateSpectral(t * 0.9 + 0.1), series.length).reverse())
.unknown("#ccc")
Insert cell
// set x-axis ticks, labels
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove())
Insert cell
// set y-axis ticks, labels
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.selectAll(".domain").remove())
Insert cell
legend = svg => {
//sets format for legend
const g = svg
.attr("font-family", "sans-serif")
.attr("font-size", 12)
.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 * 25})`);
//shape and location of legend
g.append("rect")
.attr("x", -19)
.attr("width", 19)
.attr("height", 19)
.attr("fill", d => color(d.key));
//shape and location of legend text
g.append("text")
.attr("x", -24)
.attr("y", 9.5)
.attr("dy", "0.35em")
.text(d => d.key);
}
Insert cell
Insert cell
// see Stellar submissions for files
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more