chart = {
const width = 975;
const cellSize = 17;
const height = cellSize * 9;
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");
const timeWeek = d3.timeMonday;
const countDay = i => (i + 6) % 7;
const data = wrs.map(({Date, Workday}) => ({
date: parseDate(Date),
value: Workday
}));
const color = v => v === 1 ? '#d7eeb7' : '#ffb6c6'
const years = d3.groups(data, d => d.date.getFullYear()).reverse();
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}});
}