Skip to content

Instantly share code, notes, and snippets.

@JulienAssouline
Created November 20, 2017 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JulienAssouline/1ae3480c5277e2eecd34b71515783d6f to your computer and use it in GitHub Desktop.
Save JulienAssouline/1ae3480c5277e2eecd34b71515783d6f to your computer and use it in GitHub Desktop.
A simple choropleth map
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Loading GeoJSON data and generating SVG paths</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style type="text/css">
/* No style rules here yet */
body,html{
margin: 0;
padding: 0;
font-family: "Arial", sans-serif;
font-size: 11px;
text-align: center;
}
/*
#chart{
background-color: #F5F2EB;
border: 1px solid #CCC;
} */
</style>
</head>
<body>
<script type="text/javascript">
//Width and height
var w = 500;
var h = 300;
var margin = {
top: 60,
bottom: 40,
left: 70,
right: 40
};
var width = w - margin.left - margin.right;
var height = h - margin.top - margin.bottom;
// define map projection
var projection = d3.geoAlbersUsa()
.translate([w/2, h/2])
.scale([500]);
//Define default path generator
var path = d3.geoPath()
.projection(projection);
var svg = d3.select("body")
.append("svg")
.attr("id", "chart")
.attr("width", w)
.attr("height", h)
.append("g")
.attr("tranform", "translate(0" + margin.left + "," + margin.top + ")");
var color = d3.scaleQuantile()
.range(["rgb(237, 248, 233)", "rgb(186, 228, 179)", "rgb(116,196,118)", "rgb(49,163,84)", "rgb(0,109,44)"]);
d3.csv("us-cities-agriculture.csv", function(data){
color.domain([ d3.min(data, function(d){ return d.value; }),
d3.max(data, function(d){ return d.value; })
]);
d3.json("us-states.json", function(json){
//Merge the agriculture and GeoJSON data
//Loop through once for each agriculture data value
for(var i = 0; i < data.length; i++){
// grab state name
var dataState = data[i].state;
//grab data value, and convert from string to float
var dataValue = parseFloat(data[i].value);
//find the corresponding state inside the GeoJSON
for(var n = 0; n < json.features.length; n++){
// properties name gets the states name
var jsonState = json.features[n].properties.name;
// if statment to merge by name of state
if(dataState == jsonState){
//Copy the data value into the JSON
// basically creating a new value column in JSON data
json.features[n].properties.value = dataValue;
//stop looking through the JSON
break;
}
}
}
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(d){
//get the data value
var value = d.properties.value;
if(value){
//If value exists
return color(value);
} else {
// If value is undefined
//we do this because alaska and hawaii are not in dataset we are using but still in projections
return "#ccc"
}
});
});
})
</script>
</body>
</html>
state value
Alabama 1.1791
Arkansas 1.3705
Arizona 1.3847
California 1.7979
Colorado 1.0325
Connecticut 1.3209
Delaware 1.4345
Florida 1.6304
Georgia 1.3891
Iowa 1.5297
Idaho 1.4285
Illinois 1.5297
Indiana 1.4220
Kansas 1.0124
Kentucky 0.9403
Louisiana 0.9904
Maine 1.3877
Maryland 1.2457
Massachusetts 1.1458
Michigan 1.1058
Minnesota 1.2359
Missouri 1.0212
Mississippi 1.1306
Montana 0.8145
North Carolina 1.3554
North Dakota 1.0278
Nebraska 1.1619
New Hampshire 1.0204
New Jersey 1.2831
New Mexico 0.8925
Nevada 0.9640
New York 1.1327
Ohio 1.2075
Oklahoma 0.7693
Oregon 1.3154
Pennsylvania 1.0601
Rhode Island 1.4192
South Carolina 1.1247
South Dakota 1.0760
Tennessee 0.7648
Texas 0.8873
Utah 0.9638
Virginia 0.9660
Vermont 1.0762
Washington 1.1457
Wisconsin 1.1130
West Virginia 0.5777
Wyoming 0.5712
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