Skip to content

Instantly share code, notes, and snippets.

@7man10
Last active July 25, 2017 15:39
Show Gist options
  • Save 7man10/b29e5d13a27cfe2cef5ed0c345da0f4a to your computer and use it in GitHub Desktop.
Save 7man10/b29e5d13a27cfe2cef5ed0c345da0f4a to your computer and use it in GitHub Desktop.
Zika Virus 2016
license: mit
<!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
States Symptomatic disease cases Presumptive viremic blood donors Total cases
Alabama 37 0 37
Arizona 54 1 55
Arkansas 13 0 13
California 421 4 425
Colorado 55 0 55
Connecticut 58 0 58
Delaware 17 0 17
District of Columbia 39 0 39
Florida 1115 23 1138
Georgia 107 0 107
Hawaii 11 0 11
Idaho 4 0 4
Illinois 103 0 103
Indiana 49 0 49
Iowa 26 1 27
Kansas 20 0 20
Kentucky 32 0 32
Louisiana 38 0 38
Maine 12 0 12
Maryland 130 0 130
Massachusetts 118 1 119
Michigan 67 0 67
Minnesota 67 0 67
Mississippi 23 0 23
Missouri 35 0 35
Montana 9 0 9
Nebraska 13 0 13
Nevada 22 1 23
New Hampshire 11 0 11
New Jersey 180 0 180
New Mexico 10 0 10
New York 1001 2 1003
North Carolina 97 0 97
North Dakota 3 0 3
Ohio 83 0 83
Oklahoma 29 0 29
Oregon 47 0 47
Pennsylvania 175 0 175
Rhode Island 54 0 54
South Carolina 59 0 59
South Dakota 3 0 3
Tennessee 61 0 61
Texas 312 4 316
Utah 21 0 21
Vermont 11 0 11
Virginia 108 0 108
Washington 69 0 69
West Virginia 11 0 11
Wisconsin 60 0 60
Wyoming 2 0 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment