Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3js: how to toggle css class after clicking on anelement

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 ?

like image 437
onkami Avatar asked Mar 22 '14 21:03

onkami


1 Answers

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);
          });
like image 73
Lars Kotthoff Avatar answered Sep 28 '22 06:09

Lars Kotthoff