Skip to content

Instantly share code, notes, and snippets.

@jasondavies
Created December 31, 2011 12:22
Show Gist options
  • Save jasondavies/1543851 to your computer and use it in GitHub Desktop.
Save jasondavies/1543851 to your computer and use it in GitHub Desktop.
Fast highlighting in D3
<!DOCTYPE html>
<head>
<title>Fast highlighting in D3</title>
<style>
circle { fill: #fff; stroke: #000; }
.highlight { fill: #fc0; }
</style>
<script src="http://d3js.org/d3.v2.min.js"></script>
</head>
<body>
<script>
var w = 500,
active = null;
d3.select("body").append("svg")
.attr("width", w)
.attr("height", w)
.selectAll("circle")
.data(d3.range(1000))
.enter().append("circle")
.attr("r", 10)
.attr("cx", x)
.attr("cy", x)
.on("click", function() {
if (active) active.classed("highlight", false);
active = d3.select(this.parentNode.appendChild(this))
.classed("highlight", true);
});
function x() {
return Math.random() * w;
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment