d3 toggle button code example

Example: d3 toggle on click

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);
          });

Tags:

Misc Example