d3js: Update SVG circles with Select input.
Last active
September 15, 2017 20:30
-
-
Save jfreels/6829213 to your computer and use it in GitHub Desktop.
d3js: Update SVG circles with Select input.
This file contains hidden or 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> | |
<meta charset='utf-8'> | |
<html> | |
<head> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<link rel='stylesheet' href='style.css'> | |
</head> | |
<body> | |
<script type='text/javascript' src='script.js'></script> | |
</body> | |
</html> |
This file contains hidden or 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
var data = { "red" : [ { "cx" : 50, "cy" : 50, "r" : 20, "fill" : "red" } ], | |
"blue" : [ { "cx" : 100, "cy" : 100, "r" : 30, "fill" : "blue" } ], | |
"green" : [ { "cx" : 150, "cy" : 150, "r" : 40, "fill" : "green" } ] | |
} | |
var w = 250 | |
var h = 250 | |
var body = d3.select('body') | |
body.append('select') | |
.on('change',updateCircles) | |
.selectAll('option') | |
.data(d3.keys(data)) | |
.enter() | |
.append('option') | |
.attr('value',function (d) { return d }) | |
.text(function (d) { return d }) | |
body.append('br') | |
var svg = body.append('svg') | |
.attr('width',w) | |
.attr('height',h) | |
var circles = svg.selectAll('circle') | |
.data(data.red) | |
.enter() | |
.append('circle') | |
.attr('cx', function (d) { return d.cx }) | |
.attr('cy', function (d) { return d.cy }) | |
.attr('r', function (d) { return d.r }) | |
.attr('fill', function (d) { return d.fill }) | |
function updateCircles() { | |
var selectValue = d3.select('select').property('value') | |
var data2 = data[selectValue] | |
svg.selectAll('circle') | |
.data(data2) | |
.transition() | |
.duration(1000) | |
.attr('cx', function (d) { return d.cx }) | |
.attr('cy', function (d) { return d.cy }) | |
.attr('r', function (d) { return d.r }) | |
.attr('fill', function (d) { return d.fill }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment