Skip to content

Instantly share code, notes, and snippets.

@7man10
Last active July 25, 2017 15:47
Show Gist options
  • Save 7man10/1b0a74c6c9e57522d2a88040df5d7800 to your computer and use it in GitHub Desktop.
Save 7man10/1b0a74c6c9e57522d2a88040df5d7800 to your computer and use it in GitHub Desktop.
Zika Virus 2015
license: mit
State Symptomatic disease cases Presumptive viremic blood donors Total cases
Alabama 0 0 0
Arizona 0 0 0
Arkansas 2 0 2
California 13 0 13
Colorado 0 0 0
Connecticut 0 0 0
Delaware 0 0 0
District of Columbia 4 0 4
Florida 9 0 9
Georgia 1 0 1
Hawaii 4 0 4
Idaho 0 0 0
Illinois 0 0 0
Indiana 1 0 1
Iowa 0 0 0
Kansas 0 0 0
Kentucky 1 0 1
Louisiana 0 0 0
Maine 0 0 0
Maryland 2 0 2
Massachusetts 2 0 2
Michigan 0 0 0
Minnesota 1 0 1
Mississippi 0 0 0
Missouri 1 0 1
Montana 0 0 0
Nebraska 0 0 0
Nevada 0 0 0
New Hampshire 1 0 1
New Jersey 1 0 1
New Mexico 0 0 0
New York 3 0 3
North Carolina 0 0 0
North Dakota 0 0 0
Ohio 0 0 0
Oklahoma 0 0 0
Oregon 0 0 0
Pennsylvania 1 0 1
Rhode Island 1 0 1
South Carolina 0 0 0
South Dakota 0 0 0
Tennessee 0 0 0
Texas 8 0 8
Utah 1 0 1
Vermont 0 0 0
Virginia 4 0 4
Washington 0 0 0
West Virginia 0 0 0
Wisconsin 0 0 0
Wyoming 0 0 0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<style type="text/css">
/* On mouse hover, lighten state color */
path:hover {
fill-opacity: .9;
}
body {
font: 11px sans-serif;
}
</style>
</head>
<body>
<script type="text/javascript">
/* This visualization was made possible by modifying code provided by:
Scott Murray, Choropleth example from "Interactive Data Visualization for the Web"
https://github.com/alignedleft/d3-book/blob/master/chapter_12/05_choropleth.html
Malcolm Maclean, tooltips example tutorial
http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
Mike Bostock, Pie Chart Legend
http://bl.ocks.org/mbostock/3888852 */
//Width and height of map
var width = 960;
var height = 500;
// D3 Projection
var projection = d3.geo.albersUsa()
.translate([width/2, height/2]) // translate to center of screen
.scale([1000]); // scale things down so see entire US
// Define path generator
var path = d3.geo.path() // path generator that will convert GeoJSON to SVG paths
.projection(projection); // tell path generator to use albersUsa projection
/* set color_by variable to column header you want to display */
var color_by = "Total cases";
var color = d3.scale.log()
.range(["White", "Black"]);
//Create SVG element and append map to the SVG
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
// Load in my states data
d3.csv("WestNile.csv", function(data) {
//console.log(data);
data.forEach(function(d){
d["Neuroinvasive Disease Cases"] = +d["Neuroinvasive Disease Cases"];
d["Non–neuroinvasive Disease Cases"] = +d["Non–neuroinvasive Disease Cases"];
d["Total cases"] = +d["Total cases"];
d["Deaths"] = +d["Deaths"];
d["Presumptive viremic blood donors"] = +d["Presumptive viremic blood donors"];
});
color.domain([d3.min(data, function(d) { return d[color_by] || Infinity;; }),d3.max(data, function(d) { return d[color_by]; })]); // setting the range of the input data
console.log([d3.min(data, function(d) { console.log("color",d[color_by]); return d[color_by] || Infinity;; }),d3.max(data, function(d) { return d[color_by]; })])
// Load GeoJSON data and merge with states data
d3.json("us-states.json", function(json) {
// Loop through each state data value in the .csv file
for (var i = 0; i < data.length; i++) {
//console.log("i", i, data[i][color_by]);
// Grab State Name
var dataState = data[i].State;
var dataValue = data[i][color_by];
// Find the corresponding state inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonState = json.features[j].properties.name;
//console.log(jsonState);
if (dataState == jsonState) {
// Copy the data value into the JSON
json.features[j].properties.cases = dataValue;
//console.log(jsonState,json.features[j].properties.cases);
// Stop looking through the JSON
break;
}
}
}
// Bind the data to the SVG and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("stroke", "#353535")
.style("stroke-width", "1")
.style("fill", function(d) {
// Get data value
var value = d.properties.cases;
//console.log("value", value, color(value));
if (value && color(value)) {
return color(value);
} else {
return "MistyRose";
}
});
});
});
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
State Neuroinvasive Disease Cases Non–neuroinvasive Disease Cases Total cases Deaths Presumptive viremic blood donors
Alabama 8 7 15 0 3
Arizona 57 21 78 5 11
Arkansas 8 1 9 1 1
California 315 109 424 19 47
Colorado 59 90 149 8 9
Connecticut 1 0 1 0 0
District of Columbia 1 0 1 0 0
Florida 5 1 6 0 2
Georgia 5 1 6 0 6
Idaho 3 6 9 0 1
Illinois 28 125 153 5 9
Indiana 15 3 18 2 5
Iowa 14 23 37 1 5
Kansas 17 19 36 4 7
Kentucky 5 3 8 1 1
Louisiana 20 20 40 1 6
Massachusetts 10 6 16 0 0
Maryland 6 0 6 0 0
Michigan 41 1 42 3 5
Minnesota 31 35 66 5 16
Mississippi 27 16 43 1 13
Missouri 8 2 10 0 0
Montana 3 3 6 1 2
Nebraska 34 60 94 0 30
Nevada 13 3 16 0 1
New Jersey 11 0 11 1 2
New Mexico 6 0 6 1 0
New York 6 13 19 1 0
North Carolina 2 0 2 0 0
North Dakota 14 54 68 2 0
Ohio 13 5 18 4 5
Oklahoma 17 12 29 0 7
Oregon 2 1 3 0 1
Pennsylvania 11 5 16 2 3
Rhode Island 2 0 2 0 0
South Carolina 6 3 9 0 4
South Dakota 35 116 151 6 16
Tennessee 3 3 6 1 1
Texas 240 113 353 16 44
Utah 7 6 13 1 0
Vermont 2 1 3 0 0
Virginia 6 2 8 0 2
Washington 8 1 9 1 2
West Virginia 0 1 1 0 0
Wisconsin 8 4 12 1 7
Wyoming 7 3 10 0 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment