-
Notifications
You must be signed in to change notification settings - Fork 1
/
wind_rose.html
196 lines (181 loc) · 7.58 KB
/
wind_rose.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<html>
<head>
<title>2021 age distribution estimate by DHB</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css"
integrity="sha256-iYUgmrapfDGvBrePJPrMWQZDcObdAcStKBpjP3Az+3s=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"
integrity="sha256-CNm+7c26DTTCGRQkM9vp7aP85kHFMqs9MhPEuytF+fQ=" crossorigin="anonymous"></script>
<script src="https://unpkg.com/leaflet-providers@1.3.0/leaflet-providers.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.2/papaparse.min.js"
integrity="sha512-SGWgwwRA8xZgEoKiex3UubkSkV1zSE1BS6O4pXcaxcNtUlQsOmOmhVnDwIvqGRfEmuz83tIGL13cXMZn6upPyg=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/leaflet.markercluster@1.4.1/dist/leaflet.markercluster.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/2.4.2/chroma.min.js" integrity="sha512-zInFF17qBFVvvvFpIfeBzo7Tj7+rQxLeTJDmbxjBz5/zIr89YVbTNelNhdTT+/DCrxoVzBeUPVFJsczKbB7sew==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<link rel="stylesheet" href="style.css" />
<style>
.leaflet-marker-icon {
background-color: transparent;
border-radius: 14px;
}
</style>
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.Default.css" />
</head>
<body>
<h1 id="title">2021 age distribution estimate by DHB</h1>
<div id="map"></div>
<script>
var map = L.map('map', {
zoom: 6,
minZoom: 6,
maxZoom: 14,
center: [-41.235726, 172.5118422]
});
var bounds = map.getBounds();
bounds._northEast.lat += 10;
bounds._northEast.lng += 10;
bounds._southWest.lat -= 10;
bounds._southWest.lng -= 10;
map.setMaxBounds(bounds);
var baseMaps = {
"OSM": L.tileLayer.provider("OpenStreetMap.Mapnik"),
"OSM Grayscale": L.tileLayer.provider("OpenStreetMap.BlackAndWhite"),
"CartoDB Positron": L.tileLayer.provider('CartoDB.PositronNoLabels').addTo(map),
"CartoDB Dark Matter": L.tileLayer.provider("CartoDB.DarkMatter"),
"ESRI WorldImagery": L.tileLayer.provider("Esri.WorldImagery"),
"Google Hybrid": L.tileLayer('http://{s}.google.com/vt/lyrs=s,h&x={x}&y={y}&z={z}', {
maxZoom: 20,
subdomains: ['mt0', 'mt1', 'mt2', 'mt3']
}),
"Wikimedia": L.tileLayer.provider("Wikimedia")
}
map.createPane('labels');
map.getPane('labels').style.zIndex = 625;
map.getPane('labels').style.pointerEvents = 'none';
var labels = L.tileLayer.provider("CartoDB.PositronOnlyLabels", {
pane: "labels",
interactive: false,
opacity: .8,
});
map.createPane('whitelabels');
map.getPane('whitelabels').style.zIndex = 625;
map.getPane('whitelabels').style.pointerEvents = 'none';
map.getPane('whitelabels').style.filter = 'invert(100%)';
var whitelabels = L.tileLayer.provider("CartoDB.PositronOnlyLabels", {
pane: "whitelabels",
interactive: false,
opacity: .8,
});
var overlays = {
"City labels": labels,
"City labels (inverted)": whitelabels,
}
L.control.layers(baseMaps, overlays).addTo(map);
var clusterLayer = L.markerClusterGroup().addTo(map);
// On zoom in or out, when the markers cluster or uncluster, update the plots
clusterLayer.on("animationend", function () {
console.log("animationend")
clusterLayer.eachLayer(function (layer) {
// Only add the plot if the div exists
if ($("#" + layer.plot_data.plot_div_id).length) {
Plotly.newPlot(layer.plot_data.plot_div_id, layer.plot_data.data, layer.plot_data.layout)
}
})
})
// Set min and max values for the color scale to 0 and 50000
var scale = chroma.scale("BuPu").domain([0, 50000])
console.log(scale)
$.getJSON("NZ_DHB2012.geojson", function (NZ_DHB2012) {
Papa.parse("DHB age distribution 2021.csv", {
download: true,
header: true,
dynamicTyping: true,
skipEmptyLines: true,
complete: function (age_data) {
console.log(NZ_DHB2012);
console.log(age_data)
new L.GeoJSON(NZ_DHB2012, {
onEachFeature: function (feature, layer) {
var name = feature.properties.NAME;
var plot_div_id = "DHB_plot_" + feature.properties.DHB12;
if (name == "Hutt") name = "Hutt Valley"
if (name == "Midcentral") name = "MidCentral"
layer.bindTooltip(name + " DHB", {sticky: true})
age_data_lookup = {}
for (var d of age_data.data) {
age_data_lookup[d.DHB.trim()] = d
}
var age_data_for_DHB = age_data_lookup[name]
if (!age_data_for_DHB) {
console.warn(`No population data for ${name} DHB`)
}
var keys = Object.keys(age_data_for_DHB).filter(k => k.includes("Years"))
var keysDisplay = keys.map(k => k.replace(" Years", "").trim())
console.log()
var marker = L.marker(layer.getBounds().getCenter()).addTo(clusterLayer);
const icon = L.divIcon({
html: `<div id="${plot_div_id}"></div>`,
className: 'icon',
iconSize: [200, 200],
iconAnchor: [100, 100],
});
marker.setIcon(icon);
var data = [{
r: keys.map(k => age_data_for_DHB[k]),
theta: keysDisplay,
name: "Age distribution",
marker: {
color: keys.map(k => scale(age_data_for_DHB[k]).css()),
//colorscale: colorscale,
//reversescale: true
},
type: "barpolar",
hovertemplate: '%{theta} age group: %{r} people<extra></extra>'
}]
var layout = {
//title: `Population distribution in ${name} DHB`,
width: 300,
height: 200,
margin: {
l: 30,
r: 30,
b: 30,
t: 30,
},
paper_bgcolor: 'rgba(0,0,0,0)',
plot_bgcolor: 'rgba(0,0,0,0)',
font: { size: 10 },
polar: {
barmode: "overlay",
//bargap: 0,
//radialaxis: { ticksuffix: "%", angle: 45, dtick: 20 },
angularaxis: { direction: "clockwise" }
}
}
// Store all of the information required to make the plot in the marker itself, for later use
marker.plot_data = {
plot_div_id: plot_div_id,
data: data,
layout: layout
}
// Only add the plot if the div exists
if ($("#" + plot_div_id).length) {
Plotly.newPlot(plot_div_id, data, layout)
}
},
style: function (feature) {
return {
color: "black",
weight: 1
}
}
}).addTo(map);
}
})
});
</script>
</body>
</html>