Published
Edited
Jul 8, 2021
Fork of Calendar
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height * years.length])
.attr("font-family", "sans-serif")
.attr("font-size", 10);

const year = svg.selectAll("g")
.data(years)
.join("g")
.attr("transform", (d, i) => `translate(40.5,${height * i + cellSize * 1.5})`);

year.append("text")
.attr("x", -5)
.attr("y", -5)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(([key]) => key);

year.append("g")
.attr("text-anchor", "end")
.selectAll("text")
.data(d3.range(7))
.join("text")
.attr("x", -5)
.attr("y", i => (countDay(i) + 0.5) * cellSize)
.attr("dy", "0.31em")
.text(formatDay);

year.append("g")
.selectAll("rect")
.data(([, values]) => values)
.join("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x", d => timeWeek.count(d3.utcYear(d.date), d.date) * cellSize + 0.5)
.attr("y", d => countDay(d.date.getUTCDay()) * cellSize + 0.5)
.attr("fill", d => color(d.value))
.append("title")
.text(d => `${formatDate(d.date)}
${formatValue(d.value)}${d.close === undefined ? "" : `
${formatClose(d.close)}`}`);

const month = year.append("g")
.selectAll("g")
.data(([, values]) => d3.utcMonths(d3.utcMonth(values[0].date), values[values.length - 1].date))
.join("g");

month.filter((d, i) => i).append("path")
.attr("fill", "none")
.attr("stroke", "#fff")
.attr("stroke-width", 3)
.attr("d", pathMonth);

month.append("text")
.attr("x", d => timeWeek.count(d3.utcYear(d), timeWeek.ceil(d)) * cellSize + 2)
.attr("y", -5)
.text(formatMonth);

return svg.node();
}
Insert cell
data = {
const data = await d3.csv("https://code.natwelch.com/data/commit.csv");
return data.map(({ date, commits }) => {
return { date: d3.timeParse("%Y-%m-%d")(date), value: +commits };
});
}
Insert cell
years = d3.groups(data, (d) => {
return d.date.getUTCFullYear()
}).reverse()
Insert cell
cellSize = 17
Insert cell
width = 954
Insert cell
height = cellSize * (9)
Insert cell
timeWeek = d3.utcSunday
Insert cell
countDay = i => i
Insert cell
function pathMonth(t) {
const n = 7;
const d = Math.max(0, Math.min(n, countDay(t.getUTCDay())));
const w = timeWeek.count(d3.utcYear(t), t);
return `${d === 0 ? `M${w * cellSize},0`
: d === n ? `M${(w + 1) * cellSize},0`
: `M${(w + 1) * cellSize},0V${d * cellSize}H${w * cellSize}`}V${n * cellSize}`;
}
Insert cell
formatValue = d3.format("+.2%")
Insert cell
formatClose = d3.format("$,.2f")
Insert cell
formatDate = d3.utcFormat("%x")
Insert cell
formatDay = i => "SMTWTFS"[i]
Insert cell
formatMonth = d3.utcFormat("%b")
Insert cell
color = {
const max = d3.quantile(data, 0.9975, d => Math.abs(d.value));
return d3.scaleSequential(d3.interpolatePiYG).domain([-max, +max]);
}
Insert cell
d3 = require("d3@6")
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