-
Notifications
You must be signed in to change notification settings - Fork 444
/
Clustered Pie Chart HTML Markers.html
266 lines (227 loc) · 10.9 KB
/
Clustered Pie Chart HTML Markers.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!DOCTYPE html>
<html lang="en">
<head>
<title>Clustered Pie Chart HTML Markers - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample combines the HtmlMarkerLayer class with the PieChartMarker class to create pie charts for clustered markers on the map." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, markers, pins, pushpins, symbols, layer, bubbles, clustering, superclusterer, html marker layer, SVG template, chart, pie chart" />
<meta name="author" content="Microsoft Azure Maps" /><meta name="version" content="1.0" />
<meta name="screenshot" content="screenshot.jpg" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
<!-- Add a reference to the HTML Marker layer module. -->
<script src="/lib/azure-maps/azure-maps-html-marker-layer.min.js"></script>
<script>
var map, datasource, markerLayer, popup;
//GeoJSON feed that contains the data we want to map.
var geojsonFeed = '/data/geojson/SamplePoiDataSet.json';
//Colors for each EntityType property in point data: [Gas Station, Grocery Store, Restaurant, School]
var entityTypes = ['Gas Station', 'Grocery Store', 'Restaurant', 'School'];
var entityTypesColors = ['#3366CC', '#DC3912', '#FF9900', '#109618'];
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-97, 39],
zoom: 3,
style: 'night',
view: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
//Use Microsoft Entra ID authentication.
authType: 'anonymous',
clientId: 'e6b6ab59-eb5d-4d25-aa57-581135b927f0', //Your Azure Maps client id for accessing your Azure Maps account.
getToken: function (resolve, reject, map) {
//URL to your authentication service that retrieves an Microsoft Entra ID Token.
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsToken';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
}
//Alternatively, use an Azure Maps key. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '[YOUR_AZURE_MAPS_KEY]'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a reusable popup.
popup = new atlas.Popup({
pixelOffset: [0, -20]
});
//Add a style control to the map.
map.controls.add(new atlas.control.StyleControl({
mapStyles: 'all'
}), {
position: 'top-left'
});
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource(null, {
cluster: true,
clusterRadius: 150,
clusterProperties: { //Calculate counts for each entity type in a cluster
'Gas Station': ['+', ['case', ['==', ['get', 'EntityType'], 'Gas Station'], 1, 0]],
'Grocery Store': ['+', ['case', ['==', ['get', 'EntityType'], 'Grocery Store'], 1, 0]],
'Restaurant': ['+', ['case', ['==', ['get', 'EntityType'], 'Restaurant'], 1, 0]],
'School': ['+', ['case', ['==', ['get', 'EntityType'], 'School'], 1, 0]]
}
});
map.sources.add(datasource);
//Create a HTML marker layer for rendering data points.
markerLayer = new atlas.layer.HtmlMarkerLayer(datasource, null, {
markerCallback: function (id, position, properties) {
//Check to see if marker represents a cluster.
if (properties.cluster) {
var radius = 20;
if (properties.point_count > 1000) {
radius = 50;
} else if (properties.point_count > 100) {
radius = 40;
} else if (properties.point_count > 10) {
radius = 30;
}
//Get the counts for each entity type in the cluster.
var values = [0, 0, 0, 0];
for (var i = 0; i < entityTypes.length; i++) {
if (properties[entityTypes[i]]) {
values[i] = properties[entityTypes[i]];
}
}
return new atlas.PieChartMarker({
position: position,
values: values,
colors: entityTypesColors,
radius: radius,
fillColor: 'white',
strokeWidth: 1,
strokeColor: 'white',
innerRadius: radius * 0.5,
text: properties.point_count_abbreviated,
tooltipCallback: tooltipCallback
});
}
//Individual points are rendered dodger blue.
return new atlas.HtmlMarker({
position: position,
color: 'DodgerBlue'
});
}
});
//Add a click event to the marker layer.
map.events.add('click', markerLayer, markerClicked);
//Add marker layer to the map.
map.layers.add(markerLayer);
//Import the GeoJSON data into the data source.
datasource.importDataFromUrl(geojsonFeed);
});
}
function markerClicked(e) {
var marker = e.target;
if (marker.properties.cluster) {
//Get the cluster expansion zoom level. This is the zoom level at which the cluster starts to break apart.
datasource.getClusterExpansionZoom(marker.properties.cluster_id).then(function (zoom) {
//Update the map camera to be centered over the cluster.
map.setCamera({
center: marker.getOptions().position,
zoom: zoom,
type: 'ease',
duration: 200
});
});
} else {
var prop = marker.properties;
var desc = `<div class="popup"><div class="popup-title">${prop.Name}</div><div>${prop.EntityType}<br/>
${prop.Address}, ${prop.City}, ${prop.State}, ${prop.ZipCode}</div></div>`;
popup.setOptions({
content: desc,
position: marker.getOptions().position
});
popup.open(map);
}
}
function tooltipCallback(marker, sliceIdx) {
return `${entityTypes[sliceIdx]}: ${marker.getSliceValue(sliceIdx)} (${marker.getSlicePercentage(sliceIdx)}'%)`;
}
function updateData() {
//Create mock data as an update.
var data = [];
for (var i = 0; i < 16000; i++) {
data.push(new atlas.data.Feature(new atlas.data.Point([
Math.random() * 50 - 120,
Math.random() * 30 + 25
]), {
EntityType: entityTypes[i % 4],
Name: 'A place',
Address: '1234 main st',
City: 'My City',
State: 'My State',
ZipCode: '90210',
Country: 'USA'
}));
}
datasource.setShapes(data);
}
</script>
<style>
.popup-title {
font-weight: bold;
font-size: 14px;
}
.popup {
padding: 10px;
}
.legend {
position: absolute;
top: 10px;
right: 10px;
border-radius: 10px;
background-color: white;
padding: 10px;
}
.legend .color-box {
width: 10px;
height: 10px;
}
</style>
</head>
<body onload="getMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div class="legend">
<table>
<tr>
<td>
<div class="color-box" style="background-color:#3366CC"></div>
</td>
<td>Gas Station</td>
</tr>
<tr>
<td>
<div class="color-box" style="background-color:#DC3912"></div>
</td>
<td>Grocery Store</td>
</tr>
<tr>
<td>
<div class="color-box" style="background-color:#FF9900"></div>
</td>
<td>Restaurant</td>
</tr>
<tr>
<td>
<div class="color-box" style="background-color:#109618"></div>
</td>
<td>School</td>
</tr>
</table>
</div>
<input type="button" value="Update Data Source" onclick="updateData()" />
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>
<h1 style="font-size:16px">Clustered Pie Chart HTML Markers</h1>
</legend>
This sample combines the HtmlMarkerLayer class with the PieChartMarker class to create pie charts for clustered markers on the map.
In this case the values of the pie chart is the number of each slice category (EntityType value from data set) in a cluster.
This samples uses the open source <a href="https://github.com/Azure-Samples/azure-maps-html-marker-layer" target="_blank">Azure Maps HTML Marker Layer module</a>.
</fieldset>
</body>
</html>