New Year's Resolution for 2017: Make a D3 Block a day to teach myself D3. This is Number 6. This example is my first map example and shamelessly copies a neat example by Michelle Chandra. This example tweaks some of the tooltip stylings but keeps everything else pretty much the same.
Last active
January 10, 2017 19:25
-
-
Save dbetebenner/9b537c237b6ac96b107f259c98e62b81 to your computer and use it in GitHub Desktop.
D3 Block-a-Day: Day 6, January 6th, 2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license:gpl-3.0 | |
height:600 | |
border:no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
years | place | lat | lon | |
---|---|---|---|---|
7 | Boise | 43.6187 | -116.2146 | |
11 | Cheyenne | 41.1400 | -104.8202 | |
0.8 | Salt Lake City | 40.7608 | -111.8910 | |
11 | Laramie | 41.3114 | -105.5911 | |
1 | Urbana | 40.1106 | -88.2073 | |
0.2 | Providence | 41.8240 | -71.4128 | |
9 | Boulder | 40.0150 | -105.2705 | |
0.2 | Iowa City | 41.6611 | -91.5302 | |
12 | Boston | 42.3601 | -71.0589 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<head> | |
<title>D3 Block-a-Day: Day 6, January 6, 2017</title> | |
<script src="http://d3js.org/d3.v4.min.js"></script> | |
<style type="text/css"> | |
/* On mouse hover, lighten state color */ | |
path:hover { | |
fill-opacity: .8; | |
} | |
/* Style for Custom Tooltip */ | |
div.tooltip { | |
position: absolute; | |
text-align: center; | |
width: 80px; | |
padding: 7px; | |
font: 12px Helvetica Neue; | |
background: #fff8dc; | |
border: 2px solid; | |
border-color: black; | |
border-radius: 3px; | |
pointer-events: none; | |
box-shadow: rgba(0, 0, 0, 0.3) 0 2px 10px; | |
} | |
/* Legend Font Style */ | |
body { | |
font: 11px Helvetica Neue; | |
} | |
/* 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: | |
Michelle Chandra’s Block 0b2ce4923dc9b5809922 | |
https://gist.github.com/michellechandra/0b2ce4923dc9b5809922 */ | |
//Width and height of map | |
var width = 960; | |
var height = 500; | |
// D3 Projection | |
var projection = d3.geoAlbersUsa() | |
.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.geoPath() // 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.scaleLinear() | |
.range(["rgb(213,222,217)","rgb(69,173,168)","rgb(84,36,55)","rgb(217,91,67)"]); | |
var legendText = ["Cities Lived", "States Lived", "States Visited", "Nada"]; | |
//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("states-lived.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 "rgb(213,222,217)"; | |
} | |
}); | |
// Map the cities I have lived in! | |
d3.csv("cities-lived.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) * 3; | |
}) | |
.style("fill", "rgb(217,91,67)") | |
.style("opacity", 0.85) | |
.style("stroke", "#fff") | |
.style("stroke-width", "1") | |
// 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 + 10) + "px") | |
.style("top", (d3.event.pageY - 30) + "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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
�PNG | |