-
Notifications
You must be signed in to change notification settings - Fork 25
/
script.js
166 lines (147 loc) · 5.14 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Edit the initial year and number of tabs to match your GeoJSON data and tabs in index.html
var year = "1910";
var tabs = 11;
// Edit the center point and zoom level
var map = L.map('map', {
center: [41.79, -72.6],
zoom: 10,
scrollWheelZoom: false
});
// Edit links to your GitHub repo and data source credit
map.attributionControl
.setPrefix('View <a href="http://github.com/jackdougherty/leaflet-map-polygon-tabs">data and code on GitHub</a>, created with <a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>; design by <a href="http://ctmirror.org">CT Mirror</a>');
// Basemap layer
new L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
// Edit to upload GeoJSON data file from your local directory
$.getJSON("town-home-value-index.geojson", function (data) {
geoJsonLayer = L.geoJson(data, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
});
// Edit range cutoffs and colors to match your data; see http://colorbrewer.org
// Any values not listed in the ranges below displays as the last color
function getColor(d) {
return d > 2.0 ? '#006d2c' :
d > 1.5 ? '#31a354' :
d > 1.0 ? '#74c476' :
d > 0.5 ? '#bae4b3' :
d > 0.1 ? '#edf8e9' :
'white' ;
}
// Edit the getColor property to match data properties in your GeoJSON file
// In this example, columns follow this pattern: index1910, index1920...
function style(feature) {
return {
fillColor: getColor(feature.properties["index" + year]),
weight: 1,
opacity: 1,
color: 'black',
fillOpacity: 0.9
};
}
// This highlights the polygon on hover, also for mobile
function highlightFeature(e) {
resetHighlight(e);
var layer = e.target;
layer.setStyle({
weight: 4,
color: 'black',
fillOpacity: 0.7
});
info.update(layer.feature.properties);
}
// This resets the highlight after hover moves away
function resetHighlight(e) {
geoJsonLayer.setStyle(style);
info.update();
}
// This instructs highlight and reset functions on hover movement
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: highlightFeature
});
}
// Creates an info box on the map
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
// Edit info box labels (such as props.town) to match properties of the GeoJSON data
info.update = function (props) {
var winName =
this._div.innerHTML = (props ?
'<div class="areaName">' + props.town + '</div>' : '<div class="areaName faded"><small>Hover over areas<br>Click tabs or arrow keys</small></div>') + '<div class="areaLabel"><div class="areaValue">Home Value Index</div>' +(props ? '' + (checkNull(props["index" + year])) : '--') + '</div>';
};
info.addTo(map);
// When a new tab is selected, this changes the year displayed
$(".tabItem").click(function() {
$(".tabItem").removeClass("selected");
$(this).addClass("selected");
year = $(this).html();
// year = $(this).html().split("-")[1]; /* use for school years, eg 2010-11 */
geoJsonLayer.setStyle(style);
});
// Edit grades in legend to match the range cutoffs inserted above
// In this example, the last grade will appear as "2+"
var legend = L.control({position: 'bottomright'});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0.1, 0.5, 1.0, 1.5, 2],
labels = [],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
// manually inserted from + 0.1 to start one step above default 0 = white color
labels.push(
'<i style="background:' + getColor(from + 0.1) + '"></i> ' +
from + (to ? '–' + to : '+'));
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(map);
// In info.update, this checks if GeoJSON data contains a null value, and if so displays "--"
function checkNull(val) {
if (val != null || val == "NaN") {
return comma(val);
} else {
return "--";
}
}
// Use in info.update if GeoJSON data needs to be displayed as a percentage
function checkThePct(a,b) {
if (a != null && b != null) {
return Math.round(a/b*1000)/10 + "%";
} else {
return "--";
}
}
// Use in info.update if GeoJSON data needs to be displayed with commas (such as 123,456)
function comma(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return val;
}
// This watches for arrow keys to advance the tabs
$("body").keydown(function(e) {
var selectedTab = parseInt($(".selected").attr('id').replace('tab', ''));
var nextTab;
// previous tab with left arrow
if (e.keyCode == 37) {
nextTab = (selectedTab == 1) ? tabs : selectedTab - 1;
}
// next tab with right arrow
else if (e.keyCode == 39) {
nextTab = (selectedTab == tabs) ? 1 : selectedTab + 1;
}
$('#tab' + nextTab).click();
});