Skip to content

Instantly share code, notes, and snippets.

@zanarmstrong
Last active September 8, 2015 03:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zanarmstrong/4f635a8c78e6172d3edf to your computer and use it in GitHub Desktop.
Example tests enter/update/exit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="testingExitEnter.css">
<link href='http://fonts.googleapis.com/css?family=Raleway:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<h1>Mouse around the screen and see what you find... </h1>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="testingExitEnter.js"></script>
</body>
"use strict";
var svg = d3.select("body").append("svg")
.attr("width", 1000)
.attr("height", 400)
var data = d3.range(1, 30)
var colors = d3.scale.linear().domain([1, 15, 30]).range(["grey", "blue", "red"])
svg.on("mousemove", function(d) {
return update(d3.mouse(this)[0])
})
function update(x) {
var d = Math.ceil(x / 25);
var k = svg.selectAll("rect").data(data.slice(d - 1, d + 5), function(m) {
return m
})
k.attr("opacity", .5);
k.enter().append("rect")
.attr({
width: 25,
height: 100,
y: 62
})
.attr("x", function(d) {
return d * 25
})
.attr("fill", function(d) {
return colors(d)
})
.attr("stroke", "white");
k.exit().remove();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment