Published
Edited
Oct 27, 2020
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// this 'update' block uses the value of the 'focus' viewof. The input event (.dispatch("input")) is used to tell Observable that the view's value has changed. https://observablehq.com/@observablehq/introduction-to-views
// (From: https://observablehq.com/@d3/focus-context?collection=@d3/d3-brush)
update = {
const [minIndex, maxIndex] = d3.extent(focus);
const maxY = d3.max(data, (d, i) => (minIndex <= i) && (i <= maxIndex) ? d.population : NaN);
const focusData = data.slice(minIndex, maxIndex+1) // relies on array sort order
const focusX = x.copy().domain(focus)
const focusY = y.copy().domain([0, maxY])
chart.update(focusX, focusY, focusData);
}
Insert cell
x = d3.scaleBand()
.domain(d3.range(data.length))
.range([margin.left, width - margin.right])
.padding(0.2)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.population)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = (g, x, height) => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call( d3.axisBottom(x)
.tickFormat(i => x.domain().length < 90 ? data[i].municipality : null) // hide labels when x domain becomes too wide
.tickSizeOuter(0)
)
.call(g => {
return g
.selectAll("text")
.attr("text-anchor", "end")
.attr("font-size", "0.8em")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-45)")}
)
Insert cell
yAxis = (g, y, title) => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".title").data(["Pop"]).join("text")
.attr("class", "title")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
Insert cell
bars = (g, x, y, data) => g
.attr("fill", "steelblue")
.selectAll("rect")
.data(data, d => d.municipality)
.join("rect")
.attr("x", (d) => x(d.rank))
.attr("y", (d) => y(d.population))
.attr("height", d => y(0) - y(d.population))
.attr("width", x.bandwidth())
.append("title")
.text(d => `${d.municipality}: Population ${format(d.population)}`)
Insert cell
height = 500
Insert cell
focusHeight = 100
Insert cell
margin = ({top: 30, right: 0, bottom: 80, left: 50})
Insert cell
marginFocus = ({ bottom: 10 })
Insert cell
data = Object.assign(
d3.csvParse(
await FileAttachment("bevolking.csv").text(), ({regios, aantal}) => ({ municipality: regios, population: +aantal })).sort((a, b) => d3.descending(a.population, b.population)).map((d,i) => {d.rank = i; return d}), {format: ",", y: "↑ Population"})
Insert cell
format = d3.format(data.format)
Insert cell
totalPopulation = d3.sum(data, d => d.population)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = import("d3@v6")
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