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 −
The complete class diagram of the dc.heatMap is as follows −
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 −
This method is used to get or set the handler, when an individual cell is clicked in the heatmap.
This method is used get or set the keys to create the columns of the heatmap.
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.
This method is used to get or set the values used to create the rows of the heatmap.
This method is used to get or set the handler, when a column tick is clicked in the x-axis.
This method is used to set the X border radius. If the value is set to 0, then you will get full rectangles.
Let us draw a heatmap in DC. To do this, we need to follow the steps given below −
Let us define a variable as shown below −
var chart = dc.heatMap('#heatmap');
Here, the heatMap function is mapped with the id heatmap.
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 ...................... ......................
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.
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();
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,
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.