Published
Edited
Aug 23, 2021
Fork of Calendar
Insert cell
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(weekday === "weekday" ? d3.range(1, 6) : 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(weekday === "weekday"
? ([, values]) => values.filter(d => ![0, 6].includes(d.date.getUTCDay()))
: ([, 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 => d ? 'blue' : 'white');

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
d3.csvParse
Insert cell
data2 = d3.csvParse(`date
2019-09-13
2019-09-20
2019-09-30
2019-10-07
2019-10-14
2019-10-21
2019-10-28
2019-11-04
2019-11-11
2019-11-18
2019-11-25
2019-12-03
2019-12-10
2019-12-16
2019-12-23
2019-12-30
2020-01-14
2020-01-21
2020-01-29
2020-02-05
2020-02-11
2020-02-19
2020-02-26
2020-03-04
2020-03-11
2020-03-18
2020-03-25
2020-04-01
2020-04-08
2020-04-16
2020-04-22
2020-04-29
2020-05-07
2020-05-14
2020-05-21
2020-05-29
2020-06-04
2020-06-12
2020-06-19
2020-06-26
2020-07-01
2020-07-09
2020-07-17
2020-07-23
2020-07-30
2020-08-07
2020-08-13
2020-08-21
2020-09-03
2020-09-11
2020-09-18
2020-09-26
2020-10-02
2020-10-10
2020-10-16
2020-10-24
2020-11-01
2020-11-07
2020-11-14
2020-11-22
2020-11-28
2020-12-06
2020-12-13
2020-12-19
2020-12-27
2021-01-03
2021-01-10
2021-01-17
2021-01-24
2021-01-31
2021-02-07
2021-02-14
2021-02-21
2021-02-28
2021-03-07
2021-03-14
2021-03-21
2021-03-28
2021-04-04
2021-04-12
2021-04-18
2021-04-26
2021-05-03
2021-05-10
2021-05-18
2021-05-25
2021-06-03
2021-06-12
2021-06-19
2021-06-28
2021-07-06
2021-07-13
2021-07-21
2021-07-29
2021-08-06
2021-08-14
2021-08-22`, d3.autoType)
Insert cell
years = d3.groups(data2, d => d.date.getUTCFullYear())
Insert cell
cellSize = 17
Insert cell
width = 954
Insert cell
height = cellSize * (weekday === "weekday" ? 7 : 9)
Insert cell
timeWeek = weekday === "sunday" ? d3.utcSunday : d3.utcMonday
Insert cell
countDay = weekday === "sunday" ? i => i : i => (i + 6) % 7
Insert cell
function pathMonth(t) {
const n = weekday === "weekday" ? 5 : 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
d3 = require("d3@6")
Insert cell
import {legend} from "@d3/color-legend"
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