Skip to content

Instantly share code, notes, and snippets.

@ppkn
Last active August 29, 2015 14:19
Show Gist options
  • Save ppkn/12c2b2955fd45a8ca5bf to your computer and use it in GitHub Desktop.
Save ppkn/12c2b2955fd45a8ca5bf to your computer and use it in GitHub Desktop.
Beginning D3 demo
<!DOCTYPE html>
<meta charset="utf-8">
<style>
</style>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 1400,
height = 500;
d3.json('https://opendata.utah.gov/resource/3nnk-8ku2.json', function(
data) {
data.sort(function(a, b) {
return +b.adequately_immunized - +a.adequately_immunized;
});
var h = d3.scale.linear()
.range([height, 0])
.domain([0, d3.max(data, function(d) {
return +d.adequately_immunized;
})]);
var mean = d3.mean(data, function(d) {
return +d.adequately_immunized;
});
window.data = data;
d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height)
.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('x', function(d, i) {
return i * 2;
})
.attr('y', function(d) {
return h(+d.adequately_immunized);
})
.attr('height', 0)
.attr('width', 1)
.style('fill', function(d) {
return ((+d.adequately_immunized > mean) ? 'steelblue' :
'red');
})
.transition()
.delay(function(d, i) {
return i * 10;
})
.attr('height', function(d) {
return height - h(+d.adequately_immunized);
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment