Customizing heatmap in d3.js

This post follows the previous one on basic heatmaps. It shows how to custom the heatmap: a special care is given to axis, hover effect is added to cells and a better color scale is set up.

Heatmap section


  • The Html part of the code just creates a div that will be modified by d3 later on.

  • The first part of the javascript code set a svg area. It specify the chart size and its margin. Read more.

  • A dummy dataset has been created for this example, at the long format. (3 columns: row, col, value).

  • The first step is to build scales and axis. Thus each entity will have a position on the grid.

  • A scaleBand() is used, which allows to control the size of each square using the padding property. If padding is close from 1, square are very small. (0 for very big)

  • Finally, each square can be added using a rect element.
<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src=""></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

<!-- Load color palettes -->
<script src=""></script>


// set the dimensions and margins of the graph
var margin = {top: 80, right: 25, bottom: 30, left: 40},
  width = 450 - margin.left - margin.right,
  height = 450 - - margin.bottom;

// append the svg object to the body of the page
var svg ="#my_dataviz")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + + margin.bottom)
        "translate(" + margin.left + "," + + ")");

//Read the data
d3.csv("", function(data) {

  // Labels of row and columns -> unique identifier of the column called 'group' and 'variable'
  var myGroups =, function(d){return;}).keys()
  var myVars =, function(d){return d.variable;}).keys()

  // Build X scales and axis:
  var x = d3.scaleBand()
    .range([ 0, width ])
    .style("font-size", 15)
    .attr("transform", "translate(0," + height + ")")

  // Build Y scales and axis:
  var y = d3.scaleBand()
    .range([ height, 0 ])
    .style("font-size", 15)

  // Build color scale
  var myColor = d3.scaleSequential()

  // create a tooltip
  var tooltip ="#my_dataviz")
    .style("opacity", 0)
    .attr("class", "tooltip")
    .style("background-color", "white")
    .style("border", "solid")
    .style("border-width", "2px")
    .style("border-radius", "5px")
    .style("padding", "5px")

  // Three function that change the tooltip when user hover / move / leave a cell
  var mouseover = function(d) {
      .style("opacity", 1)
      .style("stroke", "black")
      .style("opacity", 1)
  var mousemove = function(d) {
      .html("The exact value of<br>this cell is: " + d.value)
      .style("left", (d3.mouse(this)[0]+70) + "px")
      .style("top", (d3.mouse(this)[1]) + "px")
  var mouseleave = function(d) {
      .style("opacity", 0)
      .style("stroke", "none")
      .style("opacity", 0.8)

  // add the squares
    .data(data, function(d) {return':'+d.variable;})
      .attr("x", function(d) { return x( })
      .attr("y", function(d) { return y(d.variable) })
      .attr("rx", 4)
      .attr("ry", 4)
      .attr("width", x.bandwidth() )
      .attr("height", y.bandwidth() )
      .style("fill", function(d) { return myColor(d.value)} )
      .style("stroke-width", 4)
      .style("stroke", "none")
      .style("opacity", 0.8)
    .on("mouseover", mouseover)
    .on("mousemove", mousemove)
    .on("mouseleave", mouseleave)

// Add title to graph
        .attr("x", 0)
        .attr("y", -50)
        .attr("text-anchor", "left")
        .style("font-size", "22px")
        .text("A d3.js heatmap");

// Add subtitle to graph
        .attr("x", 0)
        .attr("y", -20)
        .attr("text-anchor", "left")
        .style("font-size", "14px")
        .style("fill", "grey")
        .style("max-width", 400)
        .text("A short description of the take-away message of this chart.");


Related blocks →