Skip to content

Instantly share code, notes, and snippets.

@iaindillingham
Last active December 21, 2017 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iaindillingham/0ad58e2cebf148c10c0ad0dfffe0526e to your computer and use it in GitHub Desktop.
Save iaindillingham/0ad58e2cebf148c10c0ad0dfffe0526e to your computer and use it in GitHub Desktop.
Animated Dotplots
license: mit
height: 500
scrolling: no
border: yes
{
"env": {
"browser": true
},
"extends": "eslint:recommended",
"globals": {
"d3": false,
"ss": false,
"topojson": false
},
"parserOptions": {
"ecmaFeatures": {
"impliedStrict": true
}
},
"rules": {
"indent": ["warn", 4],
"no-tabs": "error"
}
}

How might we represent change over time? One option is to animate between snapshots.

Here, each snapshot shows data for five countries for one year. As the year changes, the data change and the circles animate from their old position to their new position. Because the data are drawn from a uniform random distribution, the circles often traverse the horizontal axis. If the data were autocorrelated -- that is, if the data for one year were similar to the data for the previous year -- then the circles wouldn't move so much.

/* Remember: Use four-space indents.
* However, use two-space indents for operations that change the selection.
* For more information, see:
* https://bost.ocks.org/mike/d3/workshop/#35
*/
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 80},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
delay = 2000;
var years = d3.range(2000, 2010),
countries = ['Afghanistan', 'Bahamas', 'Cabo Verde', 'Denmark', 'Ecuador'],
scores = countries.map(function () { return years.map(Math.random); }),
data = d3.zip(countries, scores);
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scalePoint()
.domain(countries)
.range([0, height])
.padding(0.5);
var g = svg.append("g")
.attr("transform", translate(margin.left, margin.top));
// The domain
g.append("g").selectAll("line")
.data(data)
.enter().append("line")
.attr("x1", x.range()[0])
.attr("x2", x.range()[1])
.attr("y1", function (d) { return y(d[0]); })
.attr("y2", function (d) { return y(d[0]); })
.attr("stroke", "black");
g.append("g")
.attr("class", "year");
g.append("g")
.attr("class", "circles");
g.append("g")
.attr("transform", translate(0, height))
.attr("class", "x axis")
.call(d3.axisBottom(x))
.call(customAxis);
g.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(y))
.call(customAxis);
function update(elapsed) {
var t = Math.floor(elapsed / delay) % years.length;
var year = g.select(".year").selectAll("text")
.data([years[t]]);
year.enter().append("text")
.attr("x", width - 10)
.attr("y", 10)
.attr("text-anchor", "end")
.attr("font-size", 20)
.attr("font-family", "sans-serif")
.merge(year)
.text(function (d) { return d; });
var circles = g.select(".circles").selectAll("circle")
.data(data);
circles.enter().append("circle")
.attr("cx", function (d) { return x(d[1][t]); })
.attr("cy", function (d) { return y(d[0]); })
.attr("r", 6)
.attr("fill", "white")
.attr("stroke", "black")
.attr("stroke-width", 2)
.merge(circles).transition()
.duration(delay / 2)
.attr("cx", function (d) { return x(d[1][t]); });
}
update(0);
d3.interval(update, delay);
function customAxis(g) {
g.selectAll(".domain").remove();
g.selectAll(".tick line").remove();
}
function translate(x, y) {
return "translate(" + x + "," + y + ")";
}
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="500"></svg>
<script src="https://unpkg.com/d3@4/build/d3.min.js"></script>
<script src="block.js"></script>
MIT License
Copyright (c) 2017 Iain Dillingham
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment