Den Dribbles

Build Your Own Heat Map with D3

July 31, 2020

Heat maps are a great way to display correlations between two sets of data or quickly communicating progress on a project (think

I recently had to draw up some heat maps at work to track progress on some projects I am working on with the power of D3. We are going to implement a short look at generating one with D3.

Note: we are not going to use any frameworks today.


We are going to use Vercel’s serve package to serve our static files.

Follow the link to read more about it.

# Install serve globally
npm i -g serve

Getting Started

mkdir d3-heatmap
cd d3-heatmap
touch index.html main.css main.js

The styles file

Let’s add some CSS to our main.css file.

text {
  font-size: 10px;
  font-family: "Roboto Mono", monospace;
  font-weight: 700;

path {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;

As this is a trivial example, we are going to target the HTML. Normally, applying a class is a better idea.

This sets the font to be Roboto Mono (which we will bring in from the Google Fonts CDN) and sets some CSS property values for the line and path SVG elements.

The JavaScript

The JavaScript is the main place where the magic happens.

Let’s add the following to main.js. I will add comments in the code about what is happening.

// Assign a 2d array of correlating values.
// This each subarray will render as a row
const data = [[1, 1, 1, 1], [1, 0.8, 1, 0.5], [0, 1, 1, 1], [1, 1, 1, 0]]

// Add our labels as an array of strings
const rowLabelsData = ["First Row", "Second Row", "Third Row", "Fourth Row"]
const columnLabelsData = [
  "First Column",
  "Second Column",
  "Third Column",
  "Fourth Column",

function Matrix(options) {
  // Set some base properties.
  // Some come from an options object
  // pass when `Matrix` is called.
  const margin = { top: 50, right: 50, bottom: 180, left: 180 },
    width = 350,
    height = 350,
    container = options.container,
    startColor = options.start_color,
    endColor = options.end_color

  // Find our max and min values
  const maxValue = d3.max(data, layer => {
    return d3.max(layer, d => {
      return d
  const minValue = d3.min(data, layer => {
    return d3.min(layer, d => {
      return d

  const numrows = data.length
  // assume all subarrays have same length
  const numcols = data[0].length

  // Create the SVG container
  const svg = d3
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + + margin.bottom)
    .attr("transform", "translate(" + margin.left + "," + + ")")

  // Add a background to the SVG
  const background = svg
    .style("stroke", "black")
    .attr("width", width)
    .attr("height", height)

  // Build some scales for us to use
  const x = d3.scale
    .rangeBands([0, width])

  const y = d3.scale
    .rangeBands([0, height])

  // This scale in particular will
  // scale our colors from the start
  // color to the end color.
  const colorMap = d3.scale
    .domain([minValue, maxValue])
    .range([startColor, endColor])

  // Generate rows and columns and add
  // color fills.
  const row = svg
    .attr("class", "row")
    .attr("transform", (d, i) => {
      return "translate(0," + y(i) + ")"

  const cell = row
    .data(d => {
      return d
    .attr("class", "cell")
    .attr("transform", (d, i) => {
      return "translate(" + x(i) + ", 0)"

    .attr("width", x.rangeBand() - 0.3)
    .attr("height", y.rangeBand() - 0.3)

    .data((d, i) => {
      return data[i]
    .style("fill", colorMap)

  const labels = svg.append("g").attr("class", "labels")

  const columnLabels = labels
    .attr("class", "column-label")
    .attr("transform", (d, i) => {
      return "translate(" + x(i) + "," + height + ")"

    .style("stroke", "black")
    .style("stroke-width", "1px")
    .attr("x1", x.rangeBand() / 2)
    .attr("x2", x.rangeBand() / 2)
    .attr("y1", 0)
    .attr("y2", 5)

    .attr("x", 0)
    .attr("y", y.rangeBand() / 2 + 20)
    .attr("dy", ".82em")
    .attr("text-anchor", "end")
    .attr("transform", "rotate(-60)")
    .text((d, i) => {
      return d

  const rowLabels = labels
    .attr("class", "row-label")
    .attr("transform", (d, i) => {
      return "translate(" + 0 + "," + y(i) + ")"

    .style("stroke", "black")
    .style("stroke-width", "1px")
    .attr("x1", 0)
    .attr("x2", -5)
    .attr("y1", y.rangeBand() / 2)
    .attr("y2", y.rangeBand() / 2)

    .attr("x", -8)
    .attr("y", y.rangeBand() / 2)
    .attr("dy", ".32em")
    .attr("text-anchor", "end")
    .text((d, i) => {
      return d

The HTML file

Inside of index.html, add the following.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Heatmap Example</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
    <div style="display:inline-block; float:left" id="container"></div>
    <script src="main.js"></script>
        container: "#container",
        start_color: "#FC7C89",
        end_color: "#21A38B",

In this file, we are bringing in D3 + a Roboto Mono theme in from CDNs, plus loading out main.css and main.js files.

Finally, we call Matrix with the options object that we wrote in the JS file.


Within our work directory, run serve . - this will serve the files on port 5000.

Alternatively, you could just run open index.html to open the file in the default browser. I only added serve to this tutorial as I use it all the time to serve more complex builds.

If we open up http://localhost:5000 we will see our heat map.

Final heatmap

Resources and Further reading

  1. Google Fonts
  2. Serve
  3. Node Green
  4. D3 Heatmap

Image credit: Anqi Lu

Related Articles

August 07, 2020

Extend the Error prototype with ES6 classes to capture errors and send them to Sentry

July 28, 2020

Explore how redirection works in C

July 26, 2020

Use the React Context API with Tailwind's theme generator to access the values anywhere throughout the application

July 25, 2020

Get up and running with Tailwind faster than your bread takes to toast

July 23, 2020

Discover how the Skypack CDN can help use your favourite modules with Deno

July 19, 2020

Upgrade a CRA template to start making use of Snowpack

A personal blog on all things of interest. Written by Dennis O'Keeffe, Follow me on Twitter