I have a heatmap plotted exactly as in this d3js example http://bl.ocks.org/tjdecke/5558084.
How do I modify the code such, that if I click on a square, it toggles a particular CSS class for that square on/off ?
Quite simple -- change the code thusly:
var heatMap = svg.selectAll(".hour")
.data(data)
.enter().append("rect")
.attr("x", function(d) { return (d.hour - 1) * gridSize; })
.attr("y", function(d) { return (d.day - 1) * gridSize; })
.attr("rx", 4)
.attr("ry", 4)
.attr("class", "hour bordered")
.attr("width", gridSize)
.attr("height", gridSize)
.style("fill", colors[0])
.on("click", function() {
d3.select(this).classed("myCssClass", d3.select(this).classed("myCssClass") ? false : true);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With