Skip to content

Instantly share code, notes, and snippets.

@jacari15
Created July 26, 2017 00:50
Show Gist options
  • Save jacari15/d44e972a794d82cf19530c76c494122a to your computer and use it in GitHub Desktop.
Save jacari15/d44e972a794d82cf19530c76c494122a to your computer and use it in GitHub Desktop.
Virus chloropleth
license: mit

A D3 map of West Nile virus and Zika virus infections in the United States. The map is being used by GEAR UP Virginia (http://www.schev.edu/index/students-and-parents/resources/gear-up) at James Madison University as part of the "Bugs to Barcodes" project. This was built by modifying Michelle Chandra’s Basic US State Map - D3, which was built by modifying choropleth example code by Scott Murray, tooltip example code by Malcolm Maclean, and legend code example by Mike Bostock.

forked from michellechandra's block: Basic US State Map - D3

forked from scresawn's block: West Nile Virus

forked from scresawn's block: Virus chloropleth

forked from scresawn's block: Virus chloropleth

<!DOCTYPE html>
<html lang="en">
<head>
<link href="https://fonts.googleapis.com/css?family=Gruppo" rel="stylesheet">
<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;
}
h1 {
text-align: center;
font-size: xx-large;
font-family: 'Gruppo', cursive;
}
body {
font: 11px sans-serif;
}
svg {
display: block;
margin: auto;
}
div.tooltip {
color: orange;
background-color: #686868;
padding: .5em;
font-size: medium;
border-radius: 2px;
opacity: 0.9;
position: absolute;
}
</style>
</head>
<body>
<!-- The title for your page. Make sure your title matches the
data shown on your map. -->
<h1>Total West Nile Virus Cases in 2016</h1>
<script type="text/javascript">
/* This visualization was made possible by modifying code provided by:
Michelle Chandra’s Basic US State Map - D3
http://bl.ocks.org/michellechandra/0b2ce4923dc9b5809922
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(["aqua", "midnightblue"]);
//Create SVG element and append map to the SVG
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var tooltip = d3.select('body').append('div')
.attr('class', 'hidden tooltip');
// Load in my states data
d3.csv("west nile.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;
if (value && color(value)) {
return color(value);
} else {
return "white";
}
})
.on('mousemove', function(d) {
console.log(d);
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 15) +
'px; top:' + (mouse[1] + 50) + 'px')
.html(d.properties.name + ": " + d.properties.cases);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
});;
});
});
</script>
</body>
</html>
State,Neuroinvasive Disease Cases,Non-neuroinvasive Disease Cases,Total cases,Deaths,Presumptive Viremic Blood Donors
Alabama,5,4,9,0,1
Arizona,67,36,103,7,17
Arkansas,16,2,18,2,0
California,585,198,783,53,77
Colorado,57,44,101,3,8
Connecticut,8,2,10,0,0
Delaware,0,6,6,0,4
District of Colombia,3,2,5,0,0
Florida,12,1,13,2,2
Georgia,13,2,15,0,2
Idaho,5,8,13,0,6
Illinois,51,26,77,9,1
Indiana,16,5,21,3,11
Iowa,4,10,14,0,5
Kansas,12,22,34,2,17
Kentucky,1,1,2,1,3
Louisiana,41,10,51,5,21
Maine,1,0,1,0,0
Maryland,31,14,45,5,9
Massachusetts,7,3,10,2,2
Michigan,16,2,18,2,3
Minnesota,3,6,9,0,7
Mississippi,25,13,38,1,3
Missouri,23,6,29,4,16
Montana,3,0,3,0,1
Nebraska,19,49,68,2,25
Nevada,4,3,7,0,6
New Jersey,23,3,26,3,0
New Mexico,12,2,14,1,3
New York,42,15,57,5,2
North Carolina,4,0,4,1,2
North Dakota,10,13,23,1,2
Ohio,23,12,35,2,10
Oklahoma,49,40,89,9,18
Oregon,0,1,1,0,6
Pennsylvania,17,13,30,1,2
South Carolina,0,0,0,0,2
South Dakota,11,29,40,0,2
Tennessee,5,3,8,0,4
Texas,196,79,275,16,29
Utah,5,3,8,0,2
Virginia,13,8,21,2,3
Washington,8,16,24,1,10
Wisconsin,6,3,9,1,1
Wyoming,3,5,8,0,0
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
State Total cases
Alabama 0
Arizona 0
Arkansas 2
California 13
Colorado 0
Connecticut 0
Delaware 0
District of Columbia 4
Florida 9
Georgia 1
Hawaii 4
Idaho 0
Illinois 0
Indiana 1
Iowa 0
Kansas 0
Kentucky 1
Louisiana 0
Maine 0
Maryland 2
Massachusetts 2
Michigan 0
Minnesota 1
Mississippi 0
Missouri 1
Montana 0
Nebraska 0
Nevada 0
New Hampshire 1
New Jersey 1
New Mexico 0
New York 3
North Carolina 0
North Dakota 0
Ohio 0
Oklahoma 0
Oregon 0
Pennsylvania 1
Rhode Island 1
South Carolina 0
South Dakota 0
Tennessee 0
Texas 8
Utah 1
Vermont 0
Virginia 4
Washington 0
West Virginia 0
Wisconsin 0
Wyoming 0
American Samoa 0
Puerto Rico 9
U.S. Virgin Islands 0
State Total cases
Alabama 37
Arizona 54
Arkansas 13
California 421
Colorado 55
Connecticut 58
Delaware 17
District of Columbia 39
Florida 1115
Georgia 107
Hawaii 11
Idaho 4
Illinois 103
Indiana 49
Iowa 26
Kansas 20
Kentucky 32
Louisiana 38
Maine 12
Maryland 130
Massachusetts 118
Michigan 67
Minnesota 67
Mississippi 23
Missouri 35
Montana 9
Nebraska 13
Nevada 22
New Hampshire 11
New Jersey 180
New Mexico 10
New York 1001
North Carolina 97
North Dakota 3
Ohio 83
Oklahoma 29
Oregon 47
Pennsylvania 175
Rhode Island 54
South Carolina 59
South Dakota 3
Tennessee 61
Texas 312
Utah 21
Vermont 11
Virginia 108
Washington 69
West Virginia 11
Wisconsin 60
Wyoming 2
American Samoa 129
Puerto Rico 34963
U.S. Virgin Islands 987
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 Total cases
Alabama 15
Arizona 78
Arkansas 9
California 424
Colorado 149
Connecticut 1
District of Columbia 1
Florida 6
Georgia 6
Idaho 9
Illinois 153
Indiana 18
Iowa 37
Kansas 36
Kentucky 8
Louisiana 40
Massachusetts 16
Maryland 6
Michigan 42
Minnesota 66
Mississippi 43
Missouri 10
Montana 6
Nebraska 94
Nevada 16
New Jersey 11
New Mexico 6
New York 19
North Carolina 2
North Dakota 68
Ohio 18
Oklahoma 29
Oregon 3
Pennsylvania 16
Rhode Island 2
South Carolina 9
South Dakota 151
Tennessee 6
Texas 353
Utah 13
Vermont 3
Virginia 8
Washington 9
West Virginia 1
Wisconsin 12
Wyoming 10
Totals 2,038
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment