Utah 2014 vaccination rates by school.
Created
April 22, 2015 20:00
-
-
Save tysonanderson/a443532e0d04f3aec55b to your computer and use it in GitHub Desktop.
Utah 2014 vaccination rates by school
This file contains 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"> | |
<style> | |
body{ | |
font-family: Helvetica; | |
font-size: 10px; | |
} | |
</style> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
//margins | |
var margin = {top: 10, right: 10, bottom: 10, left: 10}; | |
//dimensions | |
var width = 1400 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
//scales | |
var h = d3.scale.linear().range([height,0]); | |
var color = d3.scale.category20(); | |
//load json file from Socrata SODA API | |
d3.json('https://opendata.utah.gov/resource/3nnk-8ku2.json', function (data){ | |
//create svg element | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
//set width domain to max of immunization rate | |
h.domain([0,d3.max(data, function (d){ return +d.adequately_immunized })]); | |
//sort data in descending order | |
data.sort( function (a,b){ return (+b.adequately_immunized) - (+a.adequately_immunized) }); | |
//get mean immunization rate | |
var mean = d3.mean(data, function (d){ return +d.adequately_immunized }); | |
//create svg rect elements | |
svg.selectAll('rect') | |
.data(data) | |
.enter() | |
.append('rect') | |
.attr('y', function (d){ return h(+d.adequately_immunized) }) | |
.attr('x', function (d,i){ return i * 2 }) | |
.attr('height', function (d){ return height - h(+d.adequately_immunized) }) | |
.attr('width', 1) | |
.style('fill', function (d){ | |
if(+d.adequately_immunized < mean){ | |
return 'red'; | |
} | |
else{ | |
return 'steelblue'; | |
} | |
}) | |
.on('mouseover', function (e){ | |
svg.selectAll('.mmr').style('visibility', 'hidden'); | |
svg.selectAll('.dtap').style('visibility', 'hidden'); | |
svg.selectAll('.mmr').each(function (d){ if(e === d){ d3.select(this).style('visibility', 'visible') } }); | |
svg.selectAll('.dtap').each(function (d){ if(e === d){ d3.select(this).style('visibility', 'visible') } }); | |
}) | |
.on('mouseout', function (e){ | |
svg.selectAll('.mmr').style('visibility', 'visible'); | |
svg.selectAll('.dtap').style('visibility', 'visible'); | |
}) | |
.append('title') | |
.text(function (d){ return d.school + ": " + d.adequately_immunized + "%" }); | |
//create mmr dots | |
svg.selectAll('.mmr') | |
.data(data) | |
.enter() | |
.append('circle') | |
.attr('class', 'mmr') | |
.attr('cx', function (d,i){ return i * 2 }) | |
.attr('cy', function (d){ return h(+d.receiving_mmr) }) | |
.attr('r', 1) | |
.style('fill', '#000'); | |
//create dtap dots | |
svg.selectAll('.dtap') | |
.data(data) | |
.enter() | |
.append('circle') | |
.attr('class', 'dtap') | |
.attr('cx', function (d,i){ return i * 2 }) | |
.attr('cy', function (d){ return h(+d.receiving_dtap) }) | |
.attr('r', 1) | |
.style('fill', '#999'); | |
//create axis | |
var yAxis = d3.svg.axis() | |
.scale(h) | |
.orient('right'); | |
svg.append('g') | |
.attr('class', 'axis') | |
.attr('transform', 'translate(' + (width -30) + ',0)') | |
.call(yAxis); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment