Skip to content

Instantly share code, notes, and snippets.

@sbh4th
Last active December 24, 2016 19:37
Show Gist options
  • Save sbh4th/87c84c520388bc628ac9234e63274031 to your computer and use it in GitHub Desktop.
Save sbh4th/87c84c520388bc628ac9234e63274031 to your computer and use it in GitHub Desktop.
US seat belt law changes
license: mit
years place lat lon
2 New York City 40.71455 -74.007124
6 San Francisco 37.7771187 -122.4196396
8 Santa Cruz 36.9740181 -122.0309525
3 Santa Barbara 34.4193802 -119.6990509
10 Tucson 32.22155 -110.9697571
1 Washington DC 38.8903694 -77.0319595
<!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: .7;
}
/* Style for Custom Tooltip */
div.tooltip {
position: absolute;
text-align: center;
width: 60px;
height: 28px;
padding: 2px;
font: 12px sans-serif;
background: white;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
/* Legend Font Style */
body {
font: 11px sans-serif;
}
/* Legend Position Style */
.legend {
position:absolute;
left:800px;
top:350px;
}
</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
// Define linear scale for output
var color = d3.scale.linear()
.range(["rgb(213,222,217)","rgb(69,173,168)","rgb(84,36,55)","rgb(217,91,67)"]);
var legendText = ["Upgrade to Primary", "Remained Primary", "Remained Secondary", "No Law"];
//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("opacity", 0);
// Load in my states data!
d3.csv("stateslived.csv", function(data) {
color.domain([0,1,2,3]); // setting the range of the input data
// path data
/*d3.json("us-states.json", function(unitedState) {
var data = topojson.feature(unitedState, unitedState.objects.states).features;
// our names
d3.tsv("us-state-names.tsv", function(tsv){
// extract just the names and Ids
var names = {};
tsv.forEach(function(d,i){
names[d.id] = d.name;
});*/
// 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++) {
// Grab State Name
var dataState = data[i].state;
// Grab State Abbreviation
var dataStabb = data[i].id;
// Grab data value
var dataValue = data[i].law3;
// Find the corresponding state inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonState = json.features[j].properties.name;
if (dataState == jsonState) {
// Copy the data value into the JSON
json.features[j].properties.visited = dataValue;
// 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", "#fff")
.style("stroke-width", "1")
.style("fill", function(d) {
// Get data value
var value = d.properties.visited;
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "rgb(213,222,217)";
}
});
// Modified Legend Code from Mike Bostock: http://bl.ocks.org/mbostock/3888852
var legend = d3.select("body").append("svg")
.attr("class", "legend")
.attr("width", 140)
.attr("height", 200)
.selectAll("g")
.data(color.domain().slice().reverse())
.enter()
.append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.data(legendText)
.attr("x", 24)
.attr("y", 9)
.attr("dy", ".35em")
.text(function(d) { return d; });
});
});
</script>
</body>
</html>
state visited law3
Alabama 2 2
Alaska 2 3
Arizona 1 1
Arkansas 2 3
California 2 2
Colorado 1 1
Connecticut 2 2
Delaware 2 3
District of Columbia 2 2
Florida 2 3
Georgia 2 2
Hawaii 2 2
Idaho 1 1
Illinois 2 3
Indiana 2 2
Iowa 2 2
Kansas 2 3
Kentucky 2 3
Louisiana 2 2
Maine 2 3
Maryland 2 2
Massachusetts 1 1
Michigan 2 3
Minnesota 2 3
Mississippi 2 3
Missouri 1 1
Montana 1 1
Nebraska 1 1
Nevada 1 1
New Hampshire 0 0
New Jersey 2 3
New Mexico 2 2
New York 2 2
North Carolina 2 2
North Dakota 1 1
Ohio 1 1
Oklahoma 2 2
Oregon 2 2
Pennsylvania 1 1
Rhode Island 2 3
South Carolina 2 3
South Dakota 1 1
Tennessee 2 3
Texas 2 2
Utah 1 1
Vermont 1 1
Virginia 1 1
Washington 2 3
West Virginia 2 3
Wisconsin 2 3
Wyoming 1 1
id code name
1 AL Alabama
2 AK Alaska
4 AZ Arizona
5 AR Arkansas
6 CA California
8 CO Colorado
9 CT Connecticut
10 DE Delaware
11 DC District of Columbia
12 FL Florida
13 GA Georgia
15 HI Hawaii
16 ID Idaho
17 IL Illinois
18 IN Indiana
19 IA Iowa
20 KS Kansas
21 KY Kentucky
22 LA Louisiana
23 ME Maine
24 MD Maryland
25 MA Massachusetts
26 MI Michigan
27 MN Minnesota
28 MS Mississippi
29 MO Missouri
30 MT Montana
31 NE Nebraska
32 NV Nevada
33 NH New Hampshire
34 NJ New Jersey
35 NM New Mexico
36 NY New York
37 NC North Carolina
38 ND North Dakota
39 OH Ohio
40 OK Oklahoma
41 OR Oregon
42 PA Pennsylvania
44 RI Rhode Island
45 SC South Carolina
46 SD South Dakota
47 TN Tennessee
48 TX Texas
49 UT Utah
50 VT Vermont
51 VA Virginia
53 WA Washington
54 WV West Virginia
55 WI Wisconsin
56 WY Wyoming
60 AS America Samoa
64 FM Federated States of Micronesia
66 GU Guam
68 MH Marshall Islands
69 MP Northern Mariana Islands
70 PW Palau
72 PR Puerto Rico
74 UM U.S. Minor Outlying Islands
78 VI Virgin Islands of the United States
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment