DC.js Tutorial on DC.js Heat Map

a heat map is a graphical representation of data in the form of a map, in which data values are represented as colors. this chapter explains about a heat map in detail.

before moving on to draw a heat map, we should understand the dc.heatmap class and its methods. the dc.heatmap uses mixins to get the basic functionality of drawing a chart, which are listed below −

  • dc.colormixin
  • dc.marginmixin
  • dc.basemixin

the complete class diagram of the dc.heatmap is as follows −

heat map

the dc.heatmap gets all the methods of the above-specified mixins. it has its own methods to draw the heat map, which are explained below −

boxonclick( [handler])

this method is used to get or set the handler, when an individual cell is clicked in the heatmap.

cols( [cols])

this method is used get or set the keys to create the columns of the heatmap.

colslabel( [label])

this method is used to get or set the column label, which is represented as the column name. similarly, we can perform a row label as well.

rows( [rows])

this method is used to get or set the values used to create the rows of the heatmap.

xaxisonclick( [handler])

this method is used to get or set the handler, when a column tick is clicked in the x-axis.

xborderradius( [border])

this method is used to set the x border radius. if the value is set to 0, then you will get full rectangles.

draw a heatmap

let us draw a heatmap in dc. to do this, we need to follow the steps given below −

step 1: define a variable

let us define a variable as shown below −

var chart = dc.heatmap('#heatmap');

here, the heatmap function is mapped with the id heatmap.

step 2: read the data

read the data from the howell1.csv file as shown below −

d3.csv("data/howell1.csv", function(errors, people) {
   var mycrossfilter = crossfilter(people);
}

here, we have used the same howell1.csv file and it looks as shown below −

"height","weight","age","male"
151.765,47.8256065,63,1
139.7,36.4858065,63,0
136.525,31.864838,65,0
156.845,53.0419145,41,1
145.415,41.276872,51,0
163.83,62.992589,35,1
149.225,38.2434755,32,0
168.91,55.4799715,27,1
147.955,34.869885,19,0
165.1,54.487739,54,1
154.305,49.89512,47,0

......................
......................

step 3: fetch the records

let us fetch the records using the coding given below −

people.foreach(function(x) {
   x.age = math.floor(x.age) + 1;
   x.heightrange = math.floor(x.height / 10) + 1;
   x.weightrange = math.floor(x.weight / 10) + 1;
   if(x.male == 1) {
      x.gender = 1;
   } else {
      x.gender = 2;
   }
});

here, we have checked the gender and have set the height and width range of the x-axis by using the above formula.

step 4: set the dimension

you can set the dimension using the coding given below −

var agedimension = mycrossfilter.dimension(function(data) { 
   return [+data.gender, +data.heightrange];
});

after the dimension has been assigned, group the gender using the coding given below −

var gendergroup = genderdimension.group().reducecount();

step 5: generate a chart

now, generate a heatmap using the coding given below −

chart
   .width(20 * 45 + 80)
   .height(2 * 45 + 40)
   .dimension(agedimension)
   .group(agegroup)
   .keyaccessor(function(d) { return +d.key[1]; })
   .valueaccessor(function(d) { return +d.key[0]; })
   .coloraccessor(function(d) { return +d.value; })
   .title(function(d) {
      return "height range:   " + ((d.key[1] - 1) * 10) + " - " + (d.key[1] * 10) + "cm\n" +
      "gender:  " + (d.key[0] == 1 ? "male" : "female") + "\n" +
      "count: " + (d.value) + " count";
   })
   .calculatecolordomain()

chart.render();
});

here,

  • we have assigned the chart width as 20 × 45 + 80 and height as 2 × 45 + 40.
  • then we have assigned the gender dimension and group.
  • key and value accessor returns the key and value from the heatmaps.
  • we have to use the coloraccessor() function to return the color.
  • finally, set the title and render the chart.

step 6: working example

the complete coding is as follows. create a web page heatmap.html and add the following changes to it.

<html>
   <head>
      <title>dc heat map sample</title>
      <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css">
      <link rel = "stylesheet" type = "text/css" href = "css/dc.css"/>

      <script src = "js/d3.js"></script>
      <script src = "js/crossfilter.js"></script>
      <script src = "js/dc.js"></script>
   </head>
   
   <body>
      <div>
         <div id = "heatmap"></div>
      </div>

      <script language = "javascript">
         var chart = dc.heatmap('#heatmap');

         d3.csv("data/howell1.csv", function(errors, people) {
            var mycrossfilter = crossfilter(people);

            people.foreach(function(x) {
               x.age = math.floor(x.age) + 1;
               x.heightrange = math.floor(x.height / 10) + 1;
               x.weightrange = math.floor(x.weight / 10) + 1;
               if(x.male == 1) {
                  x.gender = 1;
               } else {
                  x.gender = 2;
               }
            });

            var agedimension = mycrossfilter.dimension(function(data) { 
               return [+data.gender, +data.heightrange];
            });

            var agegroup = agedimension.group().reducecount();
            chart
               .width(20 * 45 + 80)
               .height(2 * 45 + 40)
               .dimension(agedimension)
               .group(agegroup)
               .keyaccessor(function(d) { return +d.key[1]; })
               .valueaccessor(function(d) { return +d.key[0]; })
               .coloraccessor(function(d) { return +d.value; })
               .title(function(d) {
                  return "height range:   " + ((d.key[1] - 1) * 10) + " - " +
                  (d.key[1] * 10) + "cm\n" +
                  "gender:  " + (d.key[0] == 1 ? "male" : "female") + "\n" +
                  "count: " + (d.value) + " count";})
               .calculatecolordomain()

            chart.render();
         });
      </script>
   </body>
</html>

now, request the browser and we will see the following response.