Skip to content

Instantly share code, notes, and snippets.

@jfreels
Last active December 24, 2015 16:29
Show Gist options
  • Save jfreels/6828720 to your computer and use it in GitHub Desktop.
Save jfreels/6828720 to your computer and use it in GitHub Desktop.
d3js: Update paragraph with Select input.

d3js: Update paragraph with Select 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 data = { "blue" : [1,2,3], "red" : [4,5,6], "green" : [7,8,9] }
var body = d3.select('body')
body.append('select')
.on('change',getData)
.selectAll('option')
.data(d3.keys(data))
.enter()
.append('option')
.attr('value',function (d) { return d; })
.text(function (d) { return d; })
body.selectAll('p')
.data(data.blue)
.enter()
.append('p')
.text(function (d) { return d; })
function getData() {
var selectValue = d3.select('select').property('value')
var data2 = d3.values(data[selectValue])
body.selectAll('p')
.data(data2)
.text(function (d) { return d; })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment