Skip to content

Instantly share code, notes, and snippets.

@puripant
Forked from michellechandra/README.md
Last active December 14, 2021 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save puripant/7cc6dcb82db482f61ee65c0a2e180a91 to your computer and use it in GitHub Desktop.
Save puripant/7cc6dcb82db482f61ee65c0a2e180a91 to your computer and use it in GitHub Desktop.
States I have visited

I lived in New York (in dark green) and have visited the green states. One rule: passing by or layover doesn't count; I have spent at least one night in these states. Major cities that I have visited are also shown as small bubbles.

Forked from https://gist.github.com/michellechandra/0b2ce4923dc9b5809922

years place lat lon
1 New York City 40.71455 -74.007124
1 San Francisco 37.7771187 -122.4196396
1 Washington D.C. 38.8903694 -77.0319595
1 Los Angeles 34.0194 -118.4108
1 Chicago 41.8376 -87.6818
1 Houston 29.7805 -95.3863
1 Philadelphia 40.0094 -75.1333
1 Austin 30.3072 -97.7560
1 San Francisco 37.7751 -122.4193
1 Seatle 47.6205 -122.3509
1 Denver 39.7618 -104.8806
1 Boston 42.3320 -71.0202
1 Portland (Oregon) 45.5370 -122.6500
1 Las Vegas 36.2277 -115.2640
1 Atlanta 33.7629 -84.4227
1 Miami 25.7752 -80.2086
1 Phoenix 33.5722 -112.0880
1 New Orleans 30.0686 -89.9390
1 El Paso 31.8484 -106.4270
1 Orlando 28.4159 -81.2988
1 Charlotte 35.2087 -80.8307
1 Stony Brook 40.9256 -73.1429
1 New Haven 41.3108 -72.9250
1 Providence 41.8231 -71.4188
1 Portland (Maine) 43.6569 -70.2518
<!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(["gainsboro","mediumaquamarine","lightseagreen","darkcyan"]);
var legendText = ["Cities visited", "States lived", "States visited", "Middle of nowhere"];
//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
// 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 data value
var dataValue = data[i].visited;
// 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 "gainsboro";
}
});
// Map the cities I have visited
d3.csv("cities-visited.csv", function(data) {
svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("cy", function(d) {
return projection([d.lon, d.lat])[1];
})
.attr("r", function(d) {
return Math.sqrt(d.years) * 4;
})
.style("fill", "darkcyan")
.style("opacity", 0.85)
// Modification of custom tooltip code provided by Malcolm Maclean, "D3 Tips and Tricks"
// http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.text(d.place)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
// fade out tooltip on mouse out
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
});
// 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
Alabama 0
Alaska 0
Arkansas 0
Arizona 1
California 1
Colorado 1
Connecticut 1
Delaware 0
Florida 1
Georgia 1
Hawaii 0
Iowa 0
Idaho 0
Illinois 1
Indiana 0
Kansas 0
Kentucky 0
Louisiana 1
Maine 1
Maryland 1
Massachusetts 1
Michigan 0
Minnesota 0
Missouri 0
Mississippi 0
Montana 0
North Carolina 1
North Dakota 0
Nebraska 0
New Hampshire 0
New Jersey 1
New Mexico 1
Nevada 1
New York 2
Ohio 0
Oklahoma 0
Oregon 1
Pennsylvania 1
Rhode Island 1
South Carolina 0
South Dakota 0
Tennessee 0
Texas 1
Utah 0
Virginia 1
Vermont 0
Washington 1
Wisconsin 0
West Virginia 0
Wyoming 0
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