Last active
October 9, 2015 21:11
-
-
Save majomo/8985577b1d11b55bf082 to your computer and use it in GitHub Desktop.
Map of BC
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
<html lang='en'> | |
<head> | |
<meta charset='utf-8'> | |
<title>Map of BC</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script> | |
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> | |
<style> | |
path { | |
stroke:#2B3C40 ; | |
stroke-width: 1.6px; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
//GET SVG CROWBAR | |
var width = 800, //5000 | |
height =700, | |
centered; //2500 | |
var projection = d3.geo.mercator() | |
//.rotate([5,-4,-3]) | |
//.parallels([-5, 4]) | |
//.rotate([-8,1,1]) | |
.center([-126,55]) //-124,57 | |
.scale([2900] /1.65) //2800 | |
.translate([width / 2, height / 2]) | |
; | |
var path = d3.geo.path() | |
.projection(projection); | |
var color = d3.scale.quantize() | |
.range(['rgb(222,235,247)','rgb(158,202,225)','rgb(49,130,189)']); | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
d3.csv(src="https://gist.githubusercontent.com/majomo/65e7a9367c1edef6f573/raw/e3f34d7016a51bec3c7d465f2b4b5386121b7819/regDistBc.csv", function(data){ | |
//Set input domain for color scale | |
color.domain([ | |
d3.min(data, function(d) { return d.value; }), | |
d3.max(data, function(d) { return d.value; }) | |
]); | |
d3.json(src="https://gist.githubusercontent.com/majomo/1beba4e212d12f3d6e29/raw/1bd280591bc4959449505395c90f7ffdd2e2ddbd/bcGeo.json", function(json) | |
{ | |
for(var i = 0; i < data.length; i++){ | |
var dataCDNAME = data[i].CDNAME; | |
var dataValue = parseFloat(data[i].value); | |
for(var j = 0; j <json.features.length; j++) | |
{ | |
var jsonCDNAME = json.features[j].properties.CDNAME; | |
if(dataCDNAME == jsonCDNAME){ | |
json.features[j].properties.value = dataValue; | |
break; | |
} | |
} | |
} | |
//Bind data 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 | |
var value = d.properties.value; | |
if (value) { | |
//If value exists… | |
return color(value); | |
} else { | |
//If value is undefined… | |
return "#ccc"; | |
} | |
}); | |
d3.csv(src="https://gist.githubusercontent.com/majomo/71bea3a673397ebdbabc/raw/5d1097dd66fcbc1ef6ff5eef8892ad4c8ae156ae/bccities.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(parseInt(d.population) * 0.002); | |
}) | |
.style("fill", "red") | |
.style("opacity", 0.55); | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment