Skip to content

Instantly share code, notes, and snippets.

@jfreels
Last active December 24, 2015 14:29
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 jfreels/6812882 to your computer and use it in GitHub Desktop.
Save jfreels/6812882 to your computer and use it in GitHub Desktop.
d3js: Color and size SVG circle based on input.

d3js: Color and size SVG circle based on input.

<!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>
var h = 100
var w = 100
var colors = ['red','blue','green','yellow','orange','purple','black']
var body = d3.select('body')
// Create input choices
body.append('p').text('Select fill color:')
body.append('select')
.attr('class','inputColor')
.on('change',function () {
var colorsChoice = d3.select('.inputColor').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})
body.append('br')
body.append('p').text('Select radius size:')
body.append('input')
.attr('class','inputRadius')
.attr('type','range')
.attr('min',0)
.attr('max',45)
.attr('step',1)
.attr('value',40)
.on('change',function() {
var radiusChoice = d3.select('.inputRadius').property('value')
d3.select('circle')
.transition()
.duration(1000)
.attr('r',radiusChoice)
})
body.append('br')
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