d3js: Color SVG circle based on select box value.
Last active
December 24, 2015 14:19
-
-
Save jfreels/6811751 to your computer and use it in GitHub Desktop.
d3js: Color SVG circle based on select box value.
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 h = 100 | |
var w = 100 | |
var colors = ['red','blue','green','yellow','orange','purple','black'] | |
var body = d3.select('body') | |
body.append('select') | |
.on('change',function () { | |
var colorsChoice = d3.select('select').property('value') | |
d3.select('circle') | |
.transition() | |
.duration(1000) | |
.attr('fill',colorsChoice) | |
}) | |
.selectAll('option') | |
.data(colors) | |
.enter() | |
.append('option') | |
.attr('value',function (d) { return d }) | |
.text(function (d) { return d}) | |
var svg = d3.select('body').append('svg') | |
.attr('width',w) | |
.attr('height',h) | |
var circle = svg.append('circle') | |
.attr('r',40) | |
.attr('cx',w/2) | |
.attr('cy',h/2) | |
.attr('fill','red') | |
.attr('stroke','black') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment