Last active
August 29, 2015 14:13
-
-
Save SimonJThompson/340deea4699f54b30548 to your computer and use it in GitHub Desktop.
D3 - Updating Data
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
{ | |
"nodes" : [ | |
{"name":"test","1990":20, "1991":22, "1992":19}, | |
{"name":"test2","1990":10, "1991":12, "1992":16}, | |
{"name":"test3","1990":6, "1991":22, "1992":30} | |
] | |
} |
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> | |
<head> | |
<title>D3 - Updating Data</title></title> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<style type="text/css"> | |
body { | |
padding: 32px; | |
} | |
/* vis */ | |
svg circle { | |
fill: #5394b7; | |
} | |
</style> | |
</head> | |
<body> | |
<input id="rangeSelector" type="range" min="1990" max="1992" value="1990" onChange="update();"> | |
<p>Current Year : <span id="year">1990</span></p> | |
<svg width="900" height="700" id="visualisation"></svg> | |
<script> | |
var nodes = false, | |
pack = false, | |
year = 1990; | |
d3.json('data.json', function(error, root) { | |
nodes = d3 | |
.select('#visualisation') | |
.selectAll('circle') | |
.data(root.nodes) | |
.enter() | |
.append('circle') | |
.attr('cy', 60) | |
.attr('cx', function(d, i) {return (i+1) * 80}) | |
.attr('r', function(d) {return d[year]; }); | |
}); | |
function update() { | |
year = document.getElementById('rangeSelector').value; | |
document.getElementById('year').innerHTML = year; | |
nodes | |
.transition() | |
.attr('r', function(d) {return d[year];}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment