Unlisted
Edited
May 2, 2023
Insert cell
Insert cell
import {Legend, Swatches} from "@d3/color-legend"
Insert cell
data = FileAttachment("d3_all_time_adj_output@1.csv").csv({typed: true})
Insert cell
viewof year = Inputs.range([1923, 2023], {step: 1})
Insert cell
viewof adjForInflation = Inputs.toggle({label: "Adjust for Inflation (2022 ticket prices)"})
Insert cell
scale = getScale();
Insert cell
Insert cell
swatches = Swatches(scale, {
columns: "200px",
swatchSize: 25
})
Insert cell
Insert cell
duration = 350
Insert cell
n = 25
Insert cell
names = new Set(data.map(d => d.title))
Insert cell
function chooseGross(d) {
if(adjForInflation === true) {
return d.lifetime_gross_domestic_adj
} else {
return d.lifetime_gross_domestic
}
}
Insert cell
datevalues = Array.from(d3.rollup(data, ([d]) => chooseGross(d), d => d.date, d => d.title))
.map(([date, data]) => [new Date(date), data])
.sort(([a], [b]) => d3.ascending(a, b))
Insert cell
function rank(value) {
const data = Array.from(names, name => ({name, value: value(name)}));
data.sort((a, b) => d3.descending(a.value, b.value));
for (let i = 0; i < data.length; ++i) data[i].rank = Math.min(n, i);
return data;
}
Insert cell
k = 1
Insert cell
keyframes = {
const keyframes = [];
let ka, a, kb, b;
for ([[ka, a], [kb, b]] of d3.pairs(datevalues)) {
for (let i = 0; i < k; ++i) {
const t = i / k;
keyframes.push([
new Date(ka * (1 - t) + kb * t),
rank(name => (a.get(name) || 0) * (1 - t) + (b.get(name) || 0) * t)
]);
}
}
keyframes.push([new Date(kb), rank(name => b.get(name) || 0)]);
return keyframes;
}
Insert cell
nameframes = d3.groups(keyframes.flatMap(([, data]) => data), d => d.name)
Insert cell
Insert cell
Insert cell
dataByName = new Map(d3.groups(data.flatMap((obj) => obj), obj => obj.title))
Insert cell
data.slice(0, n)
Insert cell
function bars(svg) {
let bar = svg.append("g")
.attr("fill-opacity", 0.6)
.selectAll("rect");

return ([date, data], transition) => bar = bar
.data(data.slice(0, n), d => d.title)
.join(
enter => enter.append("rect")
.attr("fill", color)
.attr("height", y.bandwidth())
.attr("x", x(0))
.attr("y", d => y((prev.get(d) || d).rank))
.attr("width", d => x((prev.get(d) || d).value) - x(0)),
update => update,
exit => exit.transition(transition).remove()
.attr("y", d => y((next.get(d) || d).rank))
.attr("width", d => x((next.get(d) || d).value) - x(0))
)
.call(bar => bar.transition(transition)
.attr("y", d => y(d.rank))
.attr("width", d => x(d.value) - x(0)))

// Needed for top bar for some reason
.attr("fill", color);
}
Insert cell
function labels(svg) {
let label = svg.append("g")
.style("font", "bold 12px var(--sans-serif)")
.style("font-variant-numeric", "tabular-nums")
.attr("text-anchor", "end")
.selectAll("text");

return ([date, data], transition) => label = label
.data(data.slice(0, n), d => d.name)
.join(
enter => enter.append("text")
.attr("transform", d => `translate(${x((prev.get(d) || d).value)},${y((prev.get(d) || d).rank)})`)
.attr("y", y.bandwidth() / 2)
.attr("x", -6)
.attr("dy", "-0.25em")
.text(d => d.name + " (" + dataByName.get(d.name)[0].year + ")")
.call(text => text.append("tspan")
.attr("fill-opacity", 0.7)
.attr("font-weight", "normal")
.attr("x", -6)
.attr("dy", "1.15em")),
update => update,
exit => exit.transition(transition).remove()
.attr("transform", d => `translate(${x((next.get(d) || d).value)},${y((next.get(d) || d).rank)})`)
.call(g => g.select("tspan").tween("text", d => textTween(d.value, (next.get(d) || d).value)))
)
.call(bar => bar.transition(transition)
.attr("transform", d => `translate(${x(d.value)},${y(d.rank)})`)
.call(g => g.select("tspan").tween("text", d => textTween((prev.get(d) || d).value, d.value))));
}
Insert cell
function textTween(a, b) {
const i = d3.interpolateNumber(a, b);
return function(t) {
this.textContent = formatNumber(i(t));
};
}
Insert cell
formatNumber = d3.format(",d")
Insert cell
tickFormat = undefined // override as desired
Insert cell
function axis(svg) {
const g = svg.append("g")
.attr("transform", `translate(0,${margin.top})`);

const axis = d3.axisTop(x)
.ticks(width / 160, tickFormat)
.tickSizeOuter(0)
.tickSizeInner(-barSize * (n + y.padding()));

return (_, transition) => {
g.transition(transition).call(axis);
g.select(".tick:first-of-type text").remove();
g.selectAll(".tick:not(:first-of-type) line").attr("stroke", "white");
g.select(".domain").remove();
};
}
Insert cell
function ticker(svg) {
const now = svg.append("text")
.style("font", `bold ${barSize}px var(--sans-serif)`)
.style("font-size", "100px")
.style("font-variant-numeric", "tabular-nums")
.attr("text-anchor", "end")
.attr("class", "ticker")
.style("position", "sticky")
.style("bottom", 0)
.attr("x", width - 6)
.attr("y", margin.top + barSize * (n / 2.5))
.attr("dy", "0.32em")
.text(formatDate(keyframes[0][0]));

return ([date], transition) => {
transition.end().then(() => now.text(formatDate(date)));
};
}
Insert cell
formatDate = d3.utcFormat("%Y")
Insert cell
genreByName = new Map(data.map(d => [d.title, d.genre]))
Insert cell
decadeByName = new Map(data.map(d => [d.title, d.decade]))
Insert cell
function getScale() {
var categoryMap = decadeByName;
const scale = d3.scaleOrdinal(d3.schemePaired);
scale.domain(Array.from(new Set(categoryMap.values())).sort());
return scale;
}
Insert cell
color = {
let scale = getScale();
return d => scale(decadeByName.get(d.name));
}
Insert cell
x = d3.scaleLinear([0, 1], [margin.left, width - margin.right])
Insert cell
y = d3.scaleBand()
.domain(d3.range(n + 1))
.rangeRound([margin.top, margin.top + barSize * (n + 1 + 0.1)])
.padding(0.1)
Insert cell
height = margin.top + barSize * n + margin.bottom
Insert cell
barSize = 48
Insert cell
margin = ({top: 16, right: 6, bottom: 6, left: 0})
Insert cell
d3 = require("d3@6")
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