Published
Edited
Dec 10, 2019
Fork of Calendar
1 star
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const years = d3.nest()
.key(d => d.date.getUTCFullYear())
.entries(data)
.reverse();

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,${height * i + cellSize * 1.5})`);

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

year.append("g")
.attr("text-anchor", "end")
.selectAll("text")
.data((weekday === "weekday" ? d3.range(2, 7) : d3.range(7)).map(i => new Date(1995, 0, i)))
.join("text")
.attr("x", -5)
.attr("y", d => (countDay(d) + 0.5) * cellSize)
.attr("dy", "0.31em")
.text(formatDay);

year.append("g")
.selectAll("rect")
.data(d => d.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) * cellSize + 0.5)
.attr("fill", d => color(d.value))
.append("title")
.text(d => `${formatDate(d.date)}: ${format(d.value)}`);

const month = year.append("g")
.selectAll("g")
.data(d => d3.utcMonths(d3.utcMonth(d.values[0].date), d.values[d.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
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" ? d => d.getUTCDay() : d => (d.getUTCDay() + 6) % 7
Insert cell
function pathMonth(t) {
const n = weekday === "weekday" ? 5 : 7;
const d = Math.max(0, Math.min(n, countDay(t)));
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
format = d3.format("+.2%")
Insert cell
formatDate = d3.utcFormat("%x")
Insert cell
formatDay = d => "SMTWTFS"[d.getUTCDay()]
Insert cell
formatMonth = d3.utcFormat("%b")
Insert cell
color = {
const max = d3.quantile(data.map(d => Math.abs(d.value)).sort(d3.ascending), 0.995);
return d3.scaleSequential(d3.interpolatePiYG).domain([-max, +max]);
}
Insert cell
data = {
const data = await d3.csvParse(await FileAttachment("d3GoldVerTwo.csv").text(), d3.autoType);
return d3.pairs(data, ({close: previous}, {date, close}) => {
return {date, value: (close - previous) / previous};
});
}
Insert cell
d3 = require("d3@5")
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