Published
Edited
Mar 25, 2020
Fork of Calendar
1 fork
1 star
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(0,${height * i + cellSize * 1.5})`);
const day = year.append("g")
.selectAll("rect")
.data(d => d3.utcDays(d3.utcMonth(d.values[0].date), d3.utcMonth.ceil(d.values[d.values.length - 1].date)))
.join("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x", d => dayPositionX(d))
.attr("y", d => d3.utcMonday.count(d3.utcMonth(d), d) * cellSize + 0.5)
.attr("fill", d => color2(d));
/*
year.append("g")
.selectAll("rect")
.data(d => d.values)
.join("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x", d => countDay(d.date) * cellSize + countMonth(d.date) * cellSize * 8)
.attr("y", d => d3.utcMonday.count(d3.utcMonth(d.date), d.date) * cellSize + 0.5)
.attr("fill", d => color(d.value))
.append("title")
.text(d => `${formatDate(d.date)}: ${format(d.value)}`);
*/
const dayNumber = year.append("g")
.selectAll("g")
.data(d => d3.utcDays(d3.utcMonth(d.values[0].date), d3.utcMonth.ceil(d.values[d.values.length - 1].date)))
.join("g");
dayNumber.append('text')
.attr("class", "subdomain-text")
.attr("x", d => dayPositionX(d) + cellSize / 2)
.attr("y", d => d3.utcMonday.count(d3.utcMonth(d), d) * cellSize + 0.5 + cellSize / 2)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "central")
.text(d => formatDay(d));

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.append("text")
.attr("x", d => countMonth(d) * cellSize * 7 + countMonth(d) * monthPadding)
.attr("y", -5)
.text(formatMonth);

return svg.node();
}
Insert cell
cellSize = 21
Insert cell
width = 954
Insert cell
height = cellSize * 9
Insert cell
monthPadding = 10
Insert cell
countMonth = d => d.getMonth()
Insert cell
countDay = d => (d.getUTCDay() + 6 ) % 7
Insert cell
format = d3.format("+.2%")
Insert cell
formatDate = d3.utcFormat("%x")
Insert cell
formatDay = d3.utcFormat("%d")
Insert cell
formatMonth = d3.utcFormat("%b %Y")
Insert cell
dayPositionX = {
return (d) => countDay(d) * cellSize + countMonth(d) * cellSize * 7 + countMonth(d) * monthPadding
}
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
color2 = {
return (d) => {
return data2[d] ? 'rgba(120, 255, 120, 0.5)' : '#eee';
}
}
Insert cell
data = {
const data = await d3.csvParse(await FileAttachment("dji@6.csv").text(), d3.autoType);
return d3.pairs(data, ({close: previous}, {date, close}) => {
return {date, value: (close - previous) / previous};
});
}
Insert cell
data2 = {
const data = {
"2010-01-28": ['DBA1'],
"2010-03-28": ['DBA1'],
"2010-06-02": ['DBA1']
}
return _.mapKeys(data, (value, key) => new Date(key))
}
Insert cell
_.keys(data2)
Insert cell
d3 = require("d3@5")
Insert cell
_ = require("lodash")
Insert cell
html`<style>
.subdomain-text {
font-size: 10px;
fill: #999;
pointer-events: none;
}
</style>`
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