Skip to content

Instantly share code, notes, and snippets.

@jashcny
Created April 4, 2016 22:00
Show Gist options
  • Save jashcny/7a9bc0801a6b810cbebf6a3f8c5a8ca5 to your computer and use it in GitHub Desktop.
Save jashcny/7a9bc0801a6b810cbebf6a3f8c5a8ca5 to your computer and use it in GitHub Desktop.
Week11: Map
State Rate Percents
Alabama 0.5 0.005
Alaska 0.5 0.005
Arizona 0.6 0.006
Arkansas 0.5 0.005
California 0.8 0.008
Colorado 0.4 0.004
Connecticut 0.9 0.009
Delaware 0.5 0.005
District of Columbia 0.6 0.006
Florida 0.6 0.006
Georgia 0.6 0.006
Hawaii 0.5 0.005
Idaho 0.6 0.006
Illinois 0.6 0.006
Indiana 0.9 0.009
Iowa 0.1 0.001
Kansas 0.4 0.004
Kentucky 0.5 0.005
Louisiana 0.4 0.004
Maine 1.1 0.011
Maryland 0.8 0.008
Massachusetts 0.9 0.009
Michigan 0.8 0.008
Minnesota 1.4 0.014
Mississippi 0.4 0.004
Missouri 0.7 0.007
Montana 0.4 0.004
Nebraska 0.6 0.006
Nevada 0.7 0.007
New Hampshire 0.7 0.007
New Jersey 0.8 0.008
New Mexico 0.4 0.004
New York 0.7 0.007
North Carolina 0.7 0.007
North Dakota 0.5 0.005
Ohio 0.7 0.007
Oklahoma 0.4 0.004
Oregon 1.2 0.012
Pennsylvania 0.9 0.009
Rhode Island 1 0.01
South Carolina 0.4 0.004
South Dakota 0.4 0.004
Tennessee 0.5 0.005
Texas 0.6 0.006
Utah 0.6 0.006
Vermont 0.8 0.008
Virginia 0.8 0.008
Washington 0.7 0.007
West Virginia 0.5 0.005
Wisconsin 0.8 0.008
Wyoming 0.7 0.007
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Dosis);
@import url(https://fonts.googleapis.com/css?family=Raleway:400);
@import url(https://fonts.googleapis.com/css?family=Raleway:400italic|Poiret+One|Ubuntu|Oxygen);
@import url(https://fonts.googleapis.com/css?family=Raleway:400italic|Poiret+One|Ubuntu);
/* On mouse hover, lighten state color */
path {
stroke: White;
stroke-width: 1;
}
h2 {
font-size: 37px;
padding: 0px 0 5px 0;
text-align: center;
line-height: 1.2em;
font-family: 'Dosis', sans-serif;
color: #00A2EA;
}
#intro {
margin-left: 100px;
margin-right: 100px;
margin-bottom: 50px;
margin-top: 24px;
font-family: 'Ubuntu', sans-serif;
font-size: 18px;
}
/* Style for Custom Tooltip */
div.tooltip {
position: absolute;
min-width: 120px;
height: 60px;
padding: 2px;
font: 12px sans-serif;
background-color: #D8FFFF;
border: rgba(230,230,230,1) 1px solid;
border-radius: 5px;
pointer-events: none;
font-family: "Open Sans", sans-serif;
font-size:14px;
line-height: 1.4;
}
.tooltip p {
margin-left: 10px;
}
/* Legend Font Style */
body {
font: 11px sans-serif;
}
/* Legend Position Style */
.legend {
position:absolute;
left:800px;
top:350px;
}
svg {
margin-left: 150px;
}
</style>
</head>
<body>
<h2>Autism Rates by State</h2>
<div id="intro">
<p>According to a research from Centers for Disease Control and Prevention, one in sixty-eight children in United States live with autism specturm disorder. This map shows the autism rates of children ages 6 to 17.</p>
<p> Source: Los Angeles Times Research: Sandra Poindexter, Doug Smith and Alan Zarembo</p>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/1.9.0/d3-legend.js"></script>
<script type="text/javascript">
//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
// Define linear scale for output
var stateColor = d3.scale.linear().range(["#D8FFFF","#00A2EA"]).interpolate(d3.interpolateLab);
//Create SVG element and append map to the SVG
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
// Append Div for tooltip to SVG
var div = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("display", "none");
var tooltip = d3.select("body").append("div").attr("class", "tooltip");
queue()
.defer(d3.json, "us-states.json")
.defer(d3.csv, "autism_rate.csv")
.await(ready);
function ready(error, json, states) {
stateColor.domain(d3.extent(states, function(s) {return +s.Percents;}));
// Loop through each state data value in the .csv file
states.forEach(function(state) {
// Grab State Name
var dataState = state.State; // name
// Grab data value
var dataValue = +state.Percents; // number
// Find the corresponding state inside the GeoJSON
json.features.forEach(function(j) {
var jsonState = j.properties.name;
if (dataState == jsonState) { // assumes the names will match...
// Copy the data value into the JSON
j.properties.Rate = dataValue;
// Stop looking through the JSON
}
});
}); // ends data merge
// 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("fill", function(d) {
// Get data value for visited
var value = d.properties.Rate;
return stateColor(value);
})
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseout", mouseout);
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
var formatPercents = d3.format(".2%");
function mouseover(d) {
d3.select(this)
.transition()
.style("stroke", "white")
.style("stroke-width", "2.5");
d3.select(this).moveToFront();
tooltip
.style("display", null) // this removes the display none setting from it
.html("<p><strong>State: </strong>"+d.properties.name+"</br>"+"<strong>Rate: </strong>"+ formatPercents(d.properties.Rate)+"</p>");
}
function mousemove(d) {
tooltip
.style("top", (d3.event.pageY - 10) + "px" )
.style("left", (d3.event.pageX + 10) + "px");
}
function mouseout(d) {
d3.select(this)
.transition()
.style("stroke", null)
.style("stroke-width", null);
tooltip.style("display", "none"); // this sets it to invisible!
}
var linear = stateColor;
svg.append("g")
.attr("class", "legendLinear")
.attr("font-size","12px")
.attr("font-family","Ubuntu")
.attr("transform", "translate(560, 5)");
var legendLinear = d3.legend.color()
.shapeWidth(40)
.orient("horizontal")
.labelFormat(d3.format(".2%"))
.scale(linear);
svg.select(".legendLinear")
.call(legendLinear);
}
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment