Public
Edited
Feb 17
Insert cell
Insert cell
chart = {
const width = 975; // width of the chart
const cellSize = 17; // height of a day
const height = cellSize * 9; // height of a week (7 days + padding)

// Define formatting functions for the axes and tooltips.
const formatValue = i => ["Holiday", "Workday"][i]
const formatClose = d3.format("$,.2f");
const formatDate = d => `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`
const formatDay = i => "SMTWTFS"[i];
const formatMonth = d3.timeFormat("%b");

// Helpers to compute a day’s position in the week.
const timeWeek = d3.timeMonday;
const countDay = i => (i + 6) % 7;

// Compute the values used to color the cells: percent change is the difference between the day’s
// closing value and the previous day’s, as a fraction of the latter.
const data = wrs.map(({Date, Workday}) => ({
date: parseDate(Date),
value: Workday
}));

// Compute the extent of the value, ignore the outliers
// and define a diverging and symmetric color scale.
// const max = d3.quantile(data, 0.9975, d => Math.abs(d.value));
// const color = d3.scaleSequential(d3.interpolatePiYG).domain([-max, +max]);
const color = v => v === 1 ? '#d7eeb7' : '#ffb6c6'

// Group data by year, in reverse input order. (Since the dataset is chronological,
// this will show years in reverse chronological order.)
const years = d3.groups(data, d => d.date.getFullYear()).reverse();

// A function that draws a thin white line to the left of each month.
function pathMonth(t) {
const d = Math.max(0, Math.min(7, countDay(t.getDay())));
const w = timeWeek.count(d3.timeYear(t), t);
return `${d === 0 ? `M${w * cellSize},0`
: d === 7 ? `M${(w + 1) * cellSize},0`
: `M${(w + 1) * cellSize},0V${d * cellSize}H${w * cellSize}`}V${7 * cellSize}`;
}

const svg = d3.create("svg")
.attr("width", width)
.attr("height", height * years.length)
.attr("viewBox", [0, 0, width, height * years.length])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

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()
.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()
// .data(([, values]) => values.filter(d => ![0, 6].includes(d.date.getDay())))
.data(([, values]) => values)
.join("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x", d => timeWeek.count(d3.timeYear(d.date), d.date) * cellSize + 0.5)
.attr("y", d => countDay(d.date.getDay()) * cellSize + 0.5)
.attr("fill", d => color(d.value))
.append("title")
.text(d => `${formatDate(d.date)}`);

const month = year.append("g")
.selectAll()
.data(([, values]) => d3.timeMonths(d3.timeMonth(values[0].date), values.at(-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.timeYear(d), timeWeek.ceil(d)) * cellSize + 2)
.attr("y", -5)
.text(formatMonth);

const element = svg.node();
// Add download button
// Add download buttons
const downloadPNG = () => {
const svgData = new XMLSerializer().serializeToString(element);
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const img = new Image();
img.onload = () => {
canvas.width = width;
canvas.height = height * years.length;
context.drawImage(img, 0, 0);
const pngUrl = canvas.toDataURL("image/png");
const downloadLink = document.createElement("a");
downloadLink.href = pngUrl;
downloadLink.download = "calendar.png";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
};
img.src = 'data:image/svg+xml;base64,' + btoa(svgData);
};

// Create download buttons
const buttonPNG = document.createElement("button");
buttonPNG.textContent = "Download PNG";
buttonPNG.onclick = downloadPNG;
document.body.appendChild(buttonPNG);

return Object.assign(element, {scales: {color}});
}
Insert cell
wrs = FileAttachment("workday.json").json()
Insert cell
parseDate = d3.timeParse('%Y-%m-%d')
Insert cell
DOM.download(() => serialize(chart), undefined, "Save as SVG")
Insert cell
import {serialize, rasterize} from "@mbostock/saving-svg"
Insert cell
DOM.download(() => rasterize(chart), undefined, "Save as PNG")
Insert cell
import {Swatches} from "@d3/color-legend"
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