Inspired by Trulia Trends - but with code and using SVG.
Example data shows concurrent user sessions over time, taken from a development environment.
MIT Licensed, see LICENSE.MD.
forked from tjdecke's block: REAL_student_ Heatmap
xxxxxxxxxx
<meta charset="utf-8">
<html>
<head>
<style>
rect.bordered {
stroke: #E6E6E6;
stroke-width:2px;
}
text.mono {
font-size: 9pt;
font-family: Consolas, courier;
fill: #aaa;
}
text.axis-workweek {
fill: #000;
}
text.axis-worktime {
fill: #000;
}
</style>
<script src="https://d3js.org/d3.v3.js"></script>
</head>
<body>
<div id="chart"></div>
<div id="dataset-picker">
</div>
<script type="text/javascript">
var margin = { top: 50, right: 630, bottom: 100, left: 131 },
width = 960 - margin.left - margin.right,
height = 430 - margin.top - margin.bottom,
schools = 5,
gridSize = Math.floor(width / schools),
legendElementWidth = gridSize*2,
buckets = 5,
colors = ["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"], // alternatively colorbrewer.YlGnBu[9]
days = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
times = ["1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a", "12a", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p", "12p"],
columns = ["k_rank_cond_parq5","k_rank_cond_parq4","k_rank_cond_parq3","k_rank_cond_parq2","k_rank_cond_parq1"];
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var heatmapChart = function(csvFile) {
d3.csv(csvFile,
function(d) {
return {
school:d.school,
category:d.category,
value:+d.value,
};
},
function(error, data) {
var yLables = svg.selectAll(".yLabel")
.data(columns)
.enter().append("text")
.text(function (d) { return d; })
.attr("x", 0)
.attr("y", function (d, i) { return i * gridSize; })
.style("text-anchor", "end")
.attr("transform", "translate(-6," + gridSize / 2 + ")")
var nest = d3.nest()
.key(function(d) { return d.school; })
.entries(data);
var xLabels = svg.selectAll(".xLabel")
.data(nest)
.enter().append("text")
.text(function(d) { return d.key; })
.attr("x", function(d, i) { return i*gridSize; })
// .attr("y", 0)
.attr("y", 0)
.style("text-anchor", "middle")
.attr("transform", "translate(" + gridSize / 2 + ", -6)")
// .attr("transform", "rotate(-90)")
.style("font", "3px avenir")
var xScale = d3.scale.ordinal().rangeBands([0, width]);
var yScale = d3.scale.ordinal().rangeBands([0, height]);
var colorScale = d3.scale.quantile()
.domain([0, buckets - 1, 1])
.range(colors);
var cards = svg.selectAll(".school")
.data(data, function(d) {return d.school+':'+d.value;});
cards.append("title");
cards.enter().append("rect")
.attr("x", function(d, i) {return gridSize* (i % schools) ; })
.attr("y", function(d, i) { return gridSize * Math.floor(i / 5) ; })
.attr("rx", 2)
.attr("ry", 2)
.attr("class", "hour bordered")
.attr("width", gridSize)
.attr("height", gridSize)
.style("fill", colors[0]);
var names = [];
cards.transition().duration(1000)
.style("fill", function(d) { return colorScale(d.value); });
cards.select("title").text(function(d) { return d.value; });
cards.exit().remove();
var legend = svg.selectAll(".legend")
.data([0].concat(colorScale.quantiles()), function(d) { return d; });
// legend.enter().append("g")
// .attr("class", "legend");
// legend.append("rect")
// .attr("x", function(d, i) { return legendElementWidth * i; })
// .attr("y", height)
// .attr("width", legendElementWidth)
// .attr("height", gridSize / 2)
// .style("fill", function(d, i) { return colors[i]; });
// legend.append("text")
// .attr("class", "mono")
// .text(function(d) { return "≥ " + Math.round(d); })
// .attr("x", function(d, i) { return legendElementWidth * i; })
// .attr("y", height + gridSize);
// legend.exit().remove();
});
};
heatmapChart("heat_test.csv");
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js