Published
Edited
Feb 25, 2021
1 fork
9 stars
Insert cell
md`# Spiral Heatmap
Build with Tom Shanley's [d3-spiral-heatmap](https://github.com/tomshanley/d3-spiral-heatmap) plug-in.
`
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", width);
const g = svg
.append("g")
.attr(
"transform",
"translate(" + (chartRadius) + "," + (chartRadius) + ")"
);

g.datum(chartData).call(heatmap);

g.selectAll(".arc")
.selectAll("path")
// .style("stroke", "#dddddd")
.style("fill", d => {
return d.commits>0 ? colorScale(d.commits): '#dddddd'
})
g.selectAll('text').attr("font-size", "10px");

return svg.node();
}
Insert cell
colorScale = d3.scaleSequential(d3.interpolateOrRd).domain([0,20])
Insert cell
heatmap = spiralHeatmap()
.radius(chartRadius)
.holeRadiusProportion(0.2)
.arcsPerCoil(365)
.coilPadding(0.1)
.coilLabel("year");
Insert cell
chartRadius = width/2
Insert cell
formatDay = d3.timeFormat("%d")
Insert cell
formatMonth = d3.timeFormat("%m")
Insert cell
formatYear= d3.timeFormat("%Y")
Insert cell
formatDate = d3.timeFormat("%Y-%m-%d")
Insert cell
startAngle = (data[0].dayOfYear / 365) * 360 - 1
Insert cell
md`## Data
source: [@codingwithfire/d3-git-history-data](https://observablehq.com/@codingwithfire/d3-git-history-data)
`
Insert cell
groupedData = d3.rollup(data, v => v.length, d => d.date)
Insert cell
data = combined.map(d => {
const timestamp = timeParser(d.date).getTime()
const date = timeParser(d.date)
return {
...d,
timestamp,
year: +formatYear(d3.utcYear(timestamp)),
date: d3.timeDay(date),
month: +formatMonth(d3.utcMonth(date)),
day: +formatDay(d3.utcDay(date)),
dayOfYear: d3.timeDay.count(d3.timeYear(timestamp), timestamp)
}
}).sort((a, b) => a.date - b.date)
Insert cell
chartData = d3.timeDays(domain[0], domain[1]).map(date => {
return {
date,
commits: groupedData.get(date) || 0
}
})
Insert cell
domain = d3.extent(data.map(d => d.date))
Insert cell
import {spiralHeatmap} from '@tomshanley/spiral-heatmap'
Insert cell
import { combined, timeParser } from '@codingwithfire/d3-git-history-data'
Insert cell
luxon = require("luxon@1/build/amd/luxon.js")
Insert cell
d3 = require('d3@6')
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