Demonstration of binding Vincent graphs to individual GeoJSON data.
Last active
May 1, 2020 02:31
-
-
Save wrobstory/5762547 to your computer and use it in GitHub Desktop.
Choropleth with Vincent
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
[{"WA": 7.8, "DE": 7.1, "WI": 6.8, "WV": 7.5, "HI": 5.4, "FL": 8.2, "WY": 5.1, "NH": 5.7, "NJ": 9.6, "NM": 6.8, "TX": 6.4, "LA": 5.9, "NC": 9.4, "ND": 3.2, "NE": 3.9, "TN": 7.8, "NY": 8.4, "PA": 8.0, "CA": 10.1, "NV": 10.3, "VA": 5.8, "CO": 7.7, "AK": 6.8, "AL": 7.1, "AR": 7.2, "VT": 5.0, "IL": 8.8, "GA": 8.8, "IN": 8.4, "IA": 5.1, "OK": 5.2, "AZ": 8.1, "ID": 6.6, "CT": 8.4, "ME": 7.2, "MD": 6.8, "MA": 6.7, "OH": 6.9, "UT": 5.5, "MO": 6.7, "MN": 5.6, "MI": 9.1, "RI": 10.1, "KS": 5.6, "MT": 5.8, "MS": 9.1, "SC": 8.8, "KY": 8.1, "OR": 8.5, "SD": 4.4}] |
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> | |
<head> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.css" /> | |
<script src="http://cdn.leafletjs.com/leaflet-0.5/leaflet.js"></script> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script src="http://d3js.org/queue.v1.min.js"></script> | |
<script src="http://trifacta.github.com/vega/vega.js"></script> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> | |
<style> | |
.legend { | |
padding: 0px 0px; | |
font: 10px sans-serif; | |
background: white; | |
background: rgba(255,255,255,0.8); | |
box-shadow: 0 0 15px rgba(0,0,0,0.2); | |
border-radius: 5px; | |
} | |
.key path { | |
display: none; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map" style="width: 960px; height: 500px"></div> | |
<script> | |
queue() | |
.defer(d3.json, 'data.json') | |
.defer(d3.json, 'us-states.json') | |
.await(makeMap) | |
function makeMap(error, data_1,gjson_1) { | |
function matchKey(datapoint, key_variable){ | |
return(parseFloat(key_variable[0][datapoint])); | |
}; | |
function parse(spec, div) { | |
vg.parse.spec(spec, function(chart) { chart({el:div}).update(); }); | |
}; | |
var color = d3.scale.threshold() | |
.domain([5, 6, 7, 8, 9, 10]) | |
.range(['#EDF8FB', '#BFD3E6', '#9EBCDA', '#8C96C6', '#8C6BB1', '#88419D', '#6E016B']); | |
var map = L.map('map').setView([48, -102], 3); | |
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { | |
maxZoom: 18, | |
attribution: 'Map data (c) <a href="http://openstreetmap.org">OpenStreetMap</a> contributors' | |
}).addTo(map); | |
function style_1(feature) { | |
return { | |
fillColor: color(matchKey(feature.id, data_1)), | |
weight: 1, | |
opacity: 0.5, | |
color: 'black', | |
fillOpacity: 0.7 | |
}; | |
} | |
var test_layer; | |
function popup_bind(feature, layer) { | |
layer.on('click', function() { | |
var div = $('<div id="vis" style="width: 475px; height: 250px;"></div>')[0]; | |
layer.bindPopup(div); | |
layer._popup.options.maxWidth = 960; | |
layer.openPopup(); | |
parse('vis.json', '#vis') | |
}); | |
}; | |
gJson_layer_1 = L.geoJson(gjson_1, {style: style_1, onEachFeature: popup_bind}).addTo(map) | |
var legend = L.control({position: 'topright'}); | |
legend.onAdd = function (map) {var div = L.DomUtil.create('div', 'legend'); return div}; | |
legend.addTo(map); | |
var x = d3.scale.linear() | |
.domain([0, 11]) | |
.range([0, 400]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("top") | |
.tickSize(1) | |
.tickValues(color.domain()) | |
var svg = d3.select(".legend.leaflet-control").append("svg") | |
.attr("id", 'legend') | |
.attr("width", 450) | |
.attr("height", 40); | |
var g = svg.append("g") | |
.attr("class", "key") | |
.attr("transform", "translate(25,16)"); | |
g.selectAll("rect") | |
.data(color.range().map(function(d, i) { | |
return { | |
x0: i ? x(color.domain()[i - 1]) : x.range()[0], | |
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1], | |
z: d | |
}; | |
})) | |
.enter().append("rect") | |
.attr("height", 10) | |
.attr("x", function(d) { return d.x0; }) | |
.attr("width", function(d) { return d.x1 - d.x0; }) | |
.style("fill", function(d) { return d.z; }); | |
g.call(xAxis).append("text") | |
.attr("class", "caption") | |
.attr("y", 21) | |
.text('Unemployment Rate (%)'); | |
}; | |
</script> | |
</body> |
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
State | Unemployment | |
---|---|---|
AL | 7.1 | |
AK | 6.8 | |
AZ | 8.1 | |
AR | 7.2 | |
CA | 10.1 | |
CO | 7.7 | |
CT | 8.4 | |
DE | 7.1 | |
FL | 8.2 | |
GA | 8.8 | |
HI | 5.4 | |
ID | 6.6 | |
IL | 8.8 | |
IN | 8.4 | |
IA | 5.1 | |
KS | 5.6 | |
KY | 8.1 | |
LA | 5.9 | |
ME | 7.2 | |
MD | 6.8 | |
MA | 6.7 | |
MI | 9.1 | |
MN | 5.6 | |
MS | 9.1 | |
MO | 6.7 | |
MT | 5.8 | |
NE | 3.9 | |
NV | 10.3 | |
NH | 5.7 | |
NJ | 9.6 | |
NM | 6.8 | |
NY | 8.4 | |
NC | 9.4 | |
ND | 3.2 | |
OH | 6.9 | |
OK | 5.2 | |
OR | 8.5 | |
PA | 8 | |
RI | 10.1 | |
SC | 8.8 | |
SD | 4.4 | |
TN | 7.8 | |
TX | 6.4 | |
UT | 5.5 | |
VT | 5 | |
VA | 5.8 | |
WA | 7.8 | |
WV | 7.5 | |
WI | 6.8 | |
WY | 5.1 |
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
{ | |
"axes": [ | |
{ | |
"scale": "x", | |
"type": "x" | |
}, | |
{ | |
"scale": "y", | |
"type": "y" | |
} | |
], | |
"data": [ | |
{ | |
"name": "table", | |
"values": [ | |
{ | |
"x": 0, | |
"y": 10 | |
}, | |
{ | |
"x": 1, | |
"y": 20 | |
}, | |
{ | |
"x": 2, | |
"y": 30 | |
}, | |
{ | |
"x": 3, | |
"y": 40 | |
}, | |
{ | |
"x": 4, | |
"y": 30 | |
}, | |
{ | |
"x": 5, | |
"y": 20 | |
}, | |
{ | |
"x": 6, | |
"y": 50 | |
}, | |
{ | |
"x": 7, | |
"y": 60 | |
} | |
] | |
}, | |
{ | |
"name": "x_label", | |
"values": [ | |
{ | |
"label": "Fake X Data" | |
} | |
] | |
}, | |
{ | |
"name": "y_label", | |
"values": [ | |
{ | |
"label": "Fake Y Data" | |
} | |
] | |
} | |
], | |
"height": 200, | |
"marks": [ | |
{ | |
"from": { | |
"data": "table" | |
}, | |
"properties": { | |
"enter": { | |
"width": { | |
"band": true, | |
"offset": -1, | |
"scale": "x" | |
}, | |
"x": { | |
"field": "data.x", | |
"scale": "x" | |
}, | |
"y": { | |
"field": "data.y", | |
"scale": "y" | |
}, | |
"y2": { | |
"scale": "y", | |
"value": 0 | |
} | |
}, | |
"hover": { | |
"fill": { | |
"value": "#a63737" | |
} | |
}, | |
"update": { | |
"fill": { | |
"value": "#2a3140" | |
} | |
} | |
}, | |
"type": "rect" | |
}, | |
{ | |
"from": { | |
"data": "x_label" | |
}, | |
"name": "x_label", | |
"properties": { | |
"enter": { | |
"align": { | |
"value": "center" | |
}, | |
"baseline": { | |
"value": "middle" | |
}, | |
"dy": { | |
"value": 35 | |
}, | |
"fill": { | |
"value": "#000" | |
}, | |
"font": { | |
"value": "Helvetica Neue" | |
}, | |
"fontSize": { | |
"value": 14 | |
}, | |
"text": { | |
"field": "data.label" | |
}, | |
"x": { | |
"value": 200.0 | |
}, | |
"y": { | |
"value": 200 | |
} | |
} | |
}, | |
"type": "text" | |
}, | |
{ | |
"from": { | |
"data": "y_label" | |
}, | |
"name": "y_label", | |
"properties": { | |
"enter": { | |
"align": { | |
"value": "center" | |
}, | |
"angle": { | |
"value": -90 | |
}, | |
"baseline": { | |
"value": "middle" | |
}, | |
"dy": { | |
"value": -45 | |
}, | |
"fill": { | |
"value": "#000" | |
}, | |
"font": { | |
"value": "Helvetica Neue" | |
}, | |
"fontSize": { | |
"value": 14 | |
}, | |
"text": { | |
"field": "data.label" | |
}, | |
"x": { | |
"value": 0 | |
}, | |
"y": { | |
"value": 100.0 | |
} | |
} | |
}, | |
"type": "text" | |
} | |
], | |
"padding": { | |
"bottom": 50, | |
"left": 60, | |
"right": 20, | |
"top": 10 | |
}, | |
"scales": [ | |
{ | |
"domain": { | |
"data": "table", | |
"field": "data.x" | |
}, | |
"name": "x", | |
"range": "width", | |
"type": "ordinal" | |
}, | |
{ | |
"domain": { | |
"data": "table", | |
"field": "data.y" | |
}, | |
"name": "y", | |
"nice": true, | |
"range": "height" | |
} | |
], | |
"viewport": null, | |
"width": 400 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment