Last active
December 19, 2015 01:19
-
-
Save JohnDelacour/5874737 to your computer and use it in GitHub Desktop.
Show/hide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Click show hide</title> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<style type="text/css"> | |
circle {fill: #000; stroke: #000;} | |
</style> | |
</head> | |
<body> | |
<script> | |
var svg = d3.select("body").append("svg") | |
.attr({width:600, height:600}) ; | |
svg.append("rect") // "Hide" button | |
.attr({x:0, width:60, height:40, fill:"red" }) | |
.on("click", function() { | |
// Hide all circles of class "dot" | |
d3.selectAll(".dot").attr("visibility", "hidden") | |
}) ; | |
svg.append("rect") // "Show" button | |
.attr({x:70, width:60, height:40, fill:"green" }) | |
.on("click", function() { | |
// Show all circles of class "dot" | |
d3.selectAll(".dot").attr("visibility", "") | |
}) ; | |
var g0 = svg.append("g") // Group for dot grid | |
.attr("transform", "translate(4 50)") ; | |
var data = []; // Create coordinates for dots | |
for(i = 0; i < 60; i++) { | |
for (j = 0; j < 50; j++) { | |
data.push( {cx:i*10, cy:j*10} ) ; | |
} | |
} | |
g0.selectAll("circle") // Draw the dots | |
.data(data) .enter().append("circle") | |
.attr("class", "dot") .attr("r", 2) | |
.attr("cx", function(d) {return d.cx}) | |
.attr("cy", function(d) {return d.cy}) ; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment