Published
Edited
Nov 2, 2020
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, h))
.classed("crashes-heatmap", true)
const defs = svg.append("defs");
const linearGradient = defs.append("linearGradient")
.attr("id", "linear-gradient");
linearGradient.selectAll("stop")
.data(color.ticks().map((t, i, n) => ({ offset: `${100*i/n.length}%`, color: color(t) })))
.enter().append("stop")
.attr("offset", d => d.offset)
.attr("stop-color", d => d.color);
const g = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`)
g.selectAll("rect")
.data(data)
.enter().append("rect")
.attr("x", d => d.Month * gridSize)
.attr("y", d => d.Timezone * gridSize)
.attr("width", gridSize)
.attr("height", gridSize)
.attr("fill", d => color(d.Value))
.attr("stroke", "#e2e2e2")
g.selectAll("text")
.data(data)
.enter()
.append("text")
.attr("x", d => d.Month * gridSize + gridSize/2)
.attr("y", d => d.Timezone * gridSize + gridSize/2)
.style("text-anchor", "middle")
.attr("dy", ".35em")
.text(d => formatValue(d.Value))
g.selectAll(".timezone")
.data(Timezone)
.enter().append("text")
.text(d => d)
.classed("timezone", true)
.attr("x", 0)
.attr("y", (d, i) => i * gridSize)
.style("text-anchor", "end")
.attr("transform", "translate(-6," + gridSize / 1.8 + ")")
g.selectAll(".month")
.data(months)
.enter().append("text")
.text(d => d)
.classed("month", true)
.attr("x", (d, i) => i * gridSize)
.attr("y", 0)
.style("text-anchor", "middle")
.attr("transform", "translate(" + gridSize / 2 + ", -6)");
g.append("g")
.call(legend)
return svg.node();
}
Insert cell
margin = ({ top: 40, right: 50, bottom: 70, left: 100 })
Insert cell
w = width - margin.left - margin.right
Insert cell
gridSize = Math.floor(w / months.length )
Insert cell
h = gridSize * Timezone.length + margin.top + margin.bottom
Insert cell
Timezone = ["Asia AM", "Asia PM", "NYC AM", "NYC PM"]
Insert cell
months = d3.range(6,10)
Insert cell
color = d3
.scaleLinear()
.domain([d3.min(data, d => d.Value), 0, d3.max(data, d => d.Value)])
.range(["#FCC117", "#FFFFFF","#608AD8"])
//.interpolator(t => d3.interpolateViridis(1 - t)) // reverses the color ramp
Insert cell
legend = g => {
g.attr("transform", `translate(0, ${h - margin.bottom - legendBarHeight })`)
.append("rect")
.attr("width", w)
.attr("height", legendBarHeight)
.style("fill", "url(#linear-gradient)")
g.call(axisBottom)
}
Insert cell
axisScale = d3.scaleLinear()
.domain(color.domain())
.range([0, w])
Insert cell
axisBottom = g => {
g.classed("axis axis--bottom", true)
.attr("transform", `translate(0, ${h - margin.bottom - legendBarHeight})`)
.call(d3.axisBottom(axisScale)
.ticks(width / 80)
.tickSize(legendBarHeight)
.tickFormat(d3.format(".0%")))
.select(".domain")
.remove()
}
Insert cell
formatValue = d3.format(".2%")
Insert cell
legendBarHeight = 10
Insert cell
data = csv.map(({ Timezone, Month, Value }) => ({
Timezone: parseInt(Timezone),
Month: parseInt(Month),
Value: parseFloat(Value)
}))
Insert cell
csv = FileAttachment("Heatmap@2.csv").csv()
Insert cell
css = html`<style>
svg.crashes-heatmap text {
font-family: sans-serif;
font-size: 20px;
fill: #333;
}

svg.crashes-heatmap .axis.axis--bottom line {
stroke: #fff;
}
</style>`
Insert cell
Insert cell
d3 = require("d3@6")
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