Published
Edited
Jan 29, 2019
3 forks
35 stars
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

svg.append("g")
.attr("transform", `translate(${width - margin.right},0)`)
.call(d3.axisLeft(y)
.ticks(d3.timeFriday)
.tickFormat(d => d.toLocaleString(undefined, {day: "numeric"})))
.call(g => g.select(".domain").remove());

svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisRight(y)
.ticks(d3.timeMonth)
.tickSize(width - margin.left - margin.right)
.tickFormat(d => d.toLocaleString(undefined, {month: "long"})))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").attr("stroke", "#ccc"))
.call(g => g.selectAll(".tick text").attr("x", null).attr("dy", "-0.6em"));

svg.append("g")
.selectAll("path")
.data(series)
.join("path")
.attr("fill", d => z(d.sum))
.attr("d", area)
.append("title")
.text(d => d.key);

yield svg.node();

svg.append("g")
.attr("font-size", 10)
.attr("font-family", "sans-serif")
.attr("text-anchor", "middle")
.selectAll("text")
.data(series
.map(d => {
const i = d3.scan(d, (b, a) => (a[1] - a[0]) - (b[1] - b[0]));
return {
key: d.key,
date: data[i].date,
value: d[i][1] - d[i][0],
center: (d[i][0] + d[i][1]) / 2
};
})
.filter(d => d.value > 1e6)
.sort((a, b) => b.value - a.value)
)
.join("text")
.attr("x", d => x(d.center))
.attr("y", d => y(d.date))
.attr("dy", "0.35em")
.text(d => d.key)
.each(labelRemover());
}
Insert cell
function labelRemover() {
const spanIndex = new Map;
return function() {
const box = this.getBBox();
const spans = spanIndex.get(box.y) || [];
const x0 = box.x - 8, x1 = box.x + box.width + 8;
for (const [s0, s1] of spans) {
if (x1 >= s0 && x0 <= s1) {
this.parentNode.removeChild(this);
return;
}
}
spans.push([x0, x1, this.textContent]);
spanIndex.set(box.y, spans);
};
}
Insert cell
x = d3.scaleLinear()
.domain([
d3.min(series, d => d3.min(d, d => d[0])),
d3.max(series, d => d3.max(d, d => d[1]))
])
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleTime()
.domain(d3.extent(data, d => d.date))
.rangeRound([height - margin.top, margin.bottom])
Insert cell
z = d3.scaleSequentialSqrt(t => d3.interpolateGreens((t + 0.5) / 2))
.domain(d3.extent(series, d => d.sum))
Insert cell
area = d3.area()
.curve(d3.curveBasis)
.y((d, i) => y(data[i].date))
.x0(d => x(d[0]))
.x1(d => x(d[1]))
Insert cell
height = width * 3.4
Insert cell
margin = ({top: 10, right: 0, bottom: 10, left: 0})
Insert cell
data = {
const response = await fetch("https://gist.githubusercontent.com/mbostock/79cd2fac8512a3c8c149e7ca97ec8151/raw/788a18daee56c463c07518764cbd3951cab1b4ae/box-office-2018.json");
const object = await response.json();
const titles = new Set;
const parseDate = d3.timeParse("%Y-%m-%d");
const data = Object.entries(object).map(([key, value], i) => {
const movies = Object.create(null);
for (const {title, gross} of value) {
titles.add(title);
movies[title] = gross;
}
return {date: parseDate(key), movies};
});
return Object.assign(data, {titles: Array.from(titles)});
}
Insert cell
stack = d3.stack()
.keys(data.titles)
.offset(d3.stackOffsetWiggle)
.order(d3.stackOrderInsideOut)
.value((d, key) => d.movies[key] || 0)
Insert cell
series = {
const series = stack(data);
for (const s of series) {
s.sum = d3.sum(s, d => d[1] - d[0]);
}
return series;
}
Insert cell
d3 = require("d3@^5.8")
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