Skip to content

Instantly share code, notes, and snippets.

@maggie-lee
Created September 11, 2014 16:47
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 maggie-lee/549ff3af3060ef622b66 to your computer and use it in GitHub Desktop.
Save maggie-lee/549ff3af3060ef622b66 to your computer and use it in GitHub Desktop.
State vaccination rate by demographic
<!DOCTYPE html>
<html class="ocks-org do-not-copy">
<meta charset="utf-8">
<title>Rate by demographic</title>
<style>
@import url(http://fonts.googleapis.com/css?family=PT+Serif|PT+Serif:b|PT+Serif:i|PT+Sans|PT+Sans:b);
html {
min-width: 1040px;
}
svg {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar rect {
fill: blue;
}
.bar:hover rect {
fill: red;
}
.value {
fill: white;
}
.axis {
shape-rendering: crispEdges;
}
.axis path {
stroke: none;
}
.x.axis line {
stroke: #fff;
stroke-opacity: .8;
}
.y.axis path {
stroke: black;
}
</style>
<h1>Rate by demographic</h1>
<p id="chart">
<p id="menu"><b>State vaccination rate by demography:</b><br>Choose a demographic: <select></select>
<script src="http://d3js.org/d3.v2.min.js?2.9.1"></script>
<script>
var margin = {top: 20, right: 40, bottom: 10, left: 40},
width = 960,
height = 550 - margin.top - margin.bottom;
var format = d3.format(".1%"),
states,
age;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.ordinal()
.rangeRoundBands([0, height], .1);
var xAxis = d3.svg.axis()
.scale(x)
.orient("top")
.tickSize(-height - margin.bottom)
.tickFormat(format);
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("margin-left", -margin.left + "px")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis");
svg.append("g")
.attr("class", "y axis")
.append("line")
.attr("class", "domain")
.attr("y2", height);
var menu = d3.select("#menu select")
.on("change", change);
d3.csv("states-age.csv", function(data) {
data.forEach(function(d) // let's put the labels in English & make them into numbers
{
//d['b'] = +d['Adults 18-49'] // this fails to change the name of the property or turn it into a number
d['b'] = +d['b'], // this, however, does turn the NaN into a number
d['c'] = +d['c'] // but it stops working here
//and none of these work
d['c'] = +d['Adults 18-49 at high risk'],
d['d'] = +d['Adults 18-64 years'],
d['e'] = +d['Adults 18 and up'],
d['f'] = +d['Adults 18-64 at high risk'],
d['g'] = +d['Adults 50-64 years'],
d['h'] = +d['Adults 18 years and up'],
d['i'] = +d['Adults 65 years and up'],
d['j'] = +d['Children 13 to 17 years'],
d['k'] = +d['Children 5 to 12 years'],
d['l'] = +d['Children 6 months to 17 years'],
d['m'] = +d['Children 6 months to 4 years'],
d['n'] = +d['Persons 6 months and up'],
d['o'] = +d['Persons 6 months and up, hispanic'],
d['p'] = +d['Persons 6 months and up, black'],
d['q'] = +d['Persons 6 months and up, other or multiple race']
;
});
console.log("&&&&&&&&&& first data");
console.log(data);
console.log("%%%%%%%%% states");
states = data;
console.log(states)
var ages = d3.keys(states[0]).filter(function(key) {
return key != "state";
});
console.log("****** ages");
console.log(ages);
states.forEach(function(state) { // I don't understand what this does. It is a copy and paste job.
ages.forEach(function(age) {
state[age] = state[age];
});
});
console.log("****** states again");
console.log(states);
console.log("+++++++++ data again");
console.log(data);
menu.selectAll("option")
.data(ages)
.enter().append("option")
.text(function(d) { return d; });
menu.property("value", 'Adults 18-49');
redraw();
});
var altKey;
d3.select(window)
.on("keydown", function() { altKey = d3.event.altKey; })
.on("keyup", function() { altKey = false; });
function change() {
clearTimeout(timeout);
d3.transition()
.duration(altKey ? 7500 : 750)
.each(redraw);
}
function redraw() {
var age1 = menu.property("value"),
top = states.sort(function(a, b) { return b[age1] - a[age1]; });
y.domain(top.map(function(d) { return d.state; }));
var bar = svg.selectAll(".bar")
.data(top, function(d) { return d.state; });
var barEnter = bar.enter().insert("g", ".axis")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(0," + (y(d.State) + height) + ")"; })
.style("fill-opacity", 0);
barEnter.append("rect")
.attr("width", age && function(d) { return x(d[age]); })
.attr("height", y.rangeBand());
barEnter.append("rect")
.attr("width", age && function(d) { return x(d[age]); })
.attr("height", y.rangeBand());
barEnter.append("text")
.attr("class", "label")
.attr("x", -3)
.attr("y", y.rangeBand() / 2)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(function(d) { return d.state; });
barEnter.append("text")
.attr("class", "value")
.attr("x", age && function(d) { return x(d[age]) - 3; })
.attr("y", y.rangeBand() / 2)
.attr("dy", ".35em")
.attr("text-anchor", "end");
x.domain([0, top[0][age = age1]]);
var barUpdate = d3.transition(bar)
.attr("transform", function(d) { return "translate(0," + (d.y0 = y(d.state)) + ")"; })
.style("fill-opacity", 0.5);
barUpdate.select("rect")
.attr("width", function(d) { return x(d[age]); });
barUpdate.select(".value")
.attr("x", function(d) { return x(d[age]) - 3; })
.text(function(d) { return format(d[age]); });
var barExit = d3.transition(bar.exit())
.attr("transform", function(d) { return "translate(0," + (d.y0 + height) + ")"; })
.style("fill-opacity", 0)
.remove();
barExit.select("rect")
.attr("width", function(d) { return x(d[age]); });
barExit.select(".value")
.attr("x", function(d) { return x(d[age]) - 3; })
.text(function(d) { return format(d[age]); });
d3.transition(svg).select(".x.axis")
.call(xAxis);
}
var timeout = setTimeout(function() {
menu.property("value", "b").node().focus();
change();
}, 5000);
</script>
state b c d e f g h i j k l m n o p q
Alabama 32.7 38.6 38.1 48.7 48.6 43.8 66.6 39.4 52.1 52.1 69.3 45.7 54 39.5 44 47.8
Alaska 31.2 33.2 34.5 46.7 40.9 37.4 56 32.9 46.9 46.4 61.4 39.7 53.8 48.9 39.2 38
Arizona 22.9 38.8 27.2 40.5 37 34.9 65.5 30.1 53.7 48.9 64 38.3 35.9 42.4 39.4 39.5
Arkansas 31.2 34 37 46.1 48.6 42.3 62.6 52.8 68.7 62.2 59.6 47 58.1 37.7 36.6 48.4
California 29.8 38.8 34.7 47 45 40.2 66.5 41.5 58.3 56.9 69.8 44.2 39.7 42.4 48.3 46.6
Colorado 35.6 43.7 40.1 54 49.4 45.2 72.1 46 55.9 58.4 72.7 48.3 45.9 50 43.9 49.4
Connecticut 30 45.2 34.4 46.8 42.3 41.2 66.8 45.4 69 64.9 84.1 46.5 44 46.7 39.6 47.6
Delaware 34.8 43 40.5 50.4 51.8 46.8 71.4 54.8 67.1 67.4 81.3 51.3 59.7 49 42.3 51.6
District of Columbia 34.5 29.8 38.5 42.3 50.2 42.4 63.6 56.7 71.4 73.1 85.9 47.4 61.7 41.2 46.6 51
Florida 19.6 17.9 22.6 27.2 28.8 30.8 56.7 36 44.3 46.9 64 34.1 31.5 33.6 41.4 40.2
Georgia 29.4 34.4 32.7 40.1 39.8 37.4 62 35 56.3 52.3 64 41.1 43.2 35.6 44.8 43.4
Hawaii 42.3 46.4 44.2 52.5 47.9 50 73.5 61.6 77.9 69.7 67 54.3 55.7 56.5 55.9 49.8
Idaho 25.7 33.4 29.8 38 38.4 35.5 60.5 32.1 42.7 44 61.9 37.8 47.4 35.5 36.6
Illinois 30.2 39.3 34.6 46.5 44.1 40.2 65 36.9 54 52.5 69 43.1 47 40.9 43.2 42.4
Indiana 27 37.5 32.8 44.3 44.6 38.8 64.6 40.2 57.5 53.1 62.3 42.2 43.5 37.7 43.8 42.5
Iowa 38.2 42.5 43.3 54.7 52.6 49.7 73.9 41.1 53.5 52.6 68 50.4 47.7 38.8 55.3 50.7
Kansas 28.4 36.4 33.4 44.8 43.4 39 65.3 31.7 47.9 45.9 62.1 40.7 38.9 37.7 41 44.4
Kentucky 29.7 34 36.3 45 48.4 43 71.1 40.7 58.3 59 79.8 46.6 60.5 49.2 46.2 45.9
Louisiana 34.2 43.8 38.8 52.2 48.5 44.1 69 48.6 56 56.9 67.8 47.1 45.6 47 50.3 47
Maine 33.9 52.7 40.6 59.9 51 46.9 68.4 57.2 62.8 62.6 71.7 50 45.2 39.5 57.9 49.8
Maryland 41.7 47.8 43.9 51.4 48.7 48.9 71.9 49.7 73.8 67.5 77 53.1 49.7 47.3 46.6 57.6
Massachusetts 45 50.9 48.5 58.1 55.5 52.8 70.6 66.9 78 75.3 83.2 57.5 64.9 56.1 60.4 56
Michigan 26.2 37.2 32.2 44.9 43.2 37.9 61 38 53.9 50.5 61.6 40.8 47.2 28.7 38.7 42.5
Minnesota 42.4 58.8 45.5 59.9 51.6 50.3 71.2 49.8 61.4 59.8 70.5 52.5 51.8 57.5 54 52.2
Mississippi 27.9 37.8 32.6 43.4 42 39.2 67.3 35.5 48.1 45.9 54.1 40.8 36.9 35.6 44.3 44.3
Missouri 32.8 41 38.1 48 48.4 44.8 72.4 41.3 49.3 51.6 73.3 46.4 49.5 39.1 50.7 46.6
Montana 27.1 47.5 34.1 52.6 45.6 40.6 65.6 25.9 48.4 45.8 65.6 41.7 40 50.7 41.2
Nebraska 37.3 42.1 41.8 49.8 51.1 47.2 70.4 45.5 61.8 60 72.3 50.3 43.4 41.1 57 51.1
Nevada 28.5 37.2 32.3 43.3 40 36 53 35.2 52.9 51.1 63.7 39.6 43.2 32.8 32.5 40.5
New Hampshire 36 40 40.3 46 47.2 46.1 69.7 45.1 62 59.2 74.8 48.9 46.7 50.9 60 48.5
New Jersey 29.8 41.3 33.1 43.4 39.3 39.1 63.6 48 67.4 66.4 88 45.3 43.3 40 46.4 46.7
New Mexico 34.2 45.2 37.5 50.3 43.9 42.1 61.5 52.6 70.5 66.9 75.5 48.1 49.5 41.1 52.2 46.5
New York 31.6 35 36.5 45.6 46.8 42.7 68.3 48 64.7 60.9 70.2 46.6 46 42.6 39.7 49
North Carolina 36.3 56.7 42.1 59.9 54.2 47.8 72.8 49.7 58.7 57.6 66 50.1 53.5 47.4 44.7 50.8
North Dakota 35.2 46.3 40.1 55.4 49.9 45.3 65.4 49.6 62.7 62.2 74.8 48.9 64.3 53.7 48.8 48.4
Ohio 29.9 39.7 36.4 50.7 48.7 42 63.9 42.2 54.8 54.1 66.7 44.8 47.1 47 42.8 44.4
Oklahoma 34.1 54 38.7 54.5 48 44.9 71.6 36 50.8 50.1 66 46.1 41.7 33.6 47.1 47.9
Oregon 27 35.2 32.3 44.4 42.7 38 61.3 40.2 48.9 47.7 57.2 40.1 42 38.6 40.7
Pennsylvania 30.1 46.4 34.8 51.2 43.9 41.2 64.6 44.8 70.1 64.9 83.5 46.2 53.4 45.7 51.6 45.5
Region 1 38.8 48.3 43 54.2 50.7 48.3 69.4 58.8 72.8 70.1 81.9 52.9 57.6 50.9 54 52.4
Region 2 30.9 37.5 35.2 44.7 44.1 41.4 66.8 48.1 65.6 62.9 76.9 46.1 44.9 42.1 42.4 48.2
Region 3 35 44.2 38.9 50.3 46.8 44.6 67.8 45.8 69.1 64 77.7 48.8 50.2 45.6 49.2 49.3
Region 4 28.3 35.8 32.9 43.3 42.2 39.3 64.7 39.8 53.2 52.3 65.6 42.2 38.8 39.2 43.4 46
Region 5 29.8 39.8 35 47.4 45.2 40.4 63.3 40.4 55.7 53.5 66.4 43.5 47.7 39.7 43 43.7
Region 6 31.1 41.8 35.3 47.9 44.9 40.9 67.7 42.1 59.5 56.7 67.3 44.9 42.3 41.7 49.5 46.7
Region 7 33.9 40.7 39 49 48.8 45.3 71.3 39.4 51.6 51.6 69.5 46.8 44.4 38.9 50.2 48
Region 8 35 43.5 39.3 52.4 48.5 44.3 68.1 39.6 56 55.5 70 47 44.2 49.6 49 47.3
Region 9 29.3 39.2 33.9 46.3 43.8 39.6 65.9 40 58 55.8 68.6 43.5 39.7 41.4 47.7 45
Region 10 31 39.1 36 47 45.8 41.2 64.5 40.1 51.9 52.6 72.2 43.8 44.5 41.9 45.6 43.4
Rhode Island 40.1 45.4 44.9 52.8 55 50.2 71.5 76.7 81.1 81.6 92.7 56.7 63.5 43.4 55.8 56.6
South Carolina 30.7 32.1 36 44.1 46.6 42.7 68.8 37.2 58 52.1 57.2 44.8 47.8 39.5 38.9 47.1
South Dakota 44.4 46.2 48.3 58.1 55.3 53.4 73.8 57.3 78.4 73.2 79.3 56.7 58.8 32.8 60.5 56
Tennessee 35.9 48.3 42.6 58.8 55.6 49.2 78 43.4 57.5 56.4 70.8 50.8 46.1 42.8 49.7 52.8
Texas 29.7 39.8 33.6 45.6 43.1 39.2 67.9 40.4 59.3 56.2 67.3 43.7 41.2 39.7 51.2 45.9
United States 31.1 39.8 35.7 47 45.1 41.5 66.2 42.5 58.6 56.6 69.8 45 42.7 41.3 46.7 46.4
Utah 33.5 43.3 36.6 48 45.3 39.9 58.8 30.7 52.6 49.7 65.7 42.9 37.5 59.9 46.1 43.3
Vermont 33.9 46.4 41.1 54.2 52.4 46.7 68.7 49.6 64.3 61.1 73.9 49.6 54.1 43.5 49.8
Virginia 37 36.6 40.8 47.1 48.5 46 69.7 43.3 67.2 61.3 72.2 49.4 44.2 44.5 51.7 50.9
Washington 34.3 43.2 39.3 50.5 49.3 44.4 67.9 42.8 57.1 58.3 81 47.5 44.7 44.9 50.6 47.4
West Virginia 33.5 39.8 40 50.9 51.4 47.2 73 43 57.4 54.9 65.7 48.8 58.6 38.6 47.5 48.9
Wisconsin 28.6 33.6 31.9 40.2 38.5 36.5 55.5 40.7 56.4 54.3 68.8 40.6 57 34.9 39.7 39.9
Wyoming 26.7 27.7 31.5 38.7 39.3 37.2 62.9 31.5 48.6 46 54.4 39.2 34.6 41.2 39.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment