/
legends-leaflet.html
185 lines (168 loc) · 6.17 KB
/
legends-leaflet.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
<!DOCTYPE html>
<html>
<head>
<title>Legends | CARTO</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Open+Sans:300,400,600" rel="stylesheet">
<!-- Include Leaflet -->
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" rel="stylesheet">
<!-- Include CARTO.js -->
<script src="../../../dist/public/carto.js"></script>
<link href="../style.css" rel="stylesheet">
</head>
<body>
<div id="map"></div>
<aside class="toolbox">
<div class="box">
<header>
<h1>Legends</h1>
<button class="github-logo js-source-link"></button>
</header>
<section>
<p class="description open-sans">Create dynamic legends.</p>
<div class="separator"></div>
<div id="controls">
<ul>
<li onclick="setAllCities()">
<input type="radio" name="data" checked id="all">
<label for="all">ALL cities</label>
</li>
<li onclick="setEuropeanCities()">
<input type="radio" name="data" id="europe">
<label for="europe">European cities</label>
</li>
<li onclick="setSpanishCities()">
<input type="radio" name="data" id="spain">
<label for="spain">Spanish cities</label>
</li>
</ul>
<div class="separator"></div>
<ul>
<li onchange="invertColors()">
<input type="checkbox" name="data" id="invertColors">
<label for="invertColors">Invert colors</label>
</li>
</ul>
</div>
</section>
<footer class="js-footer"></footer>
</div>
<div class="box legend">
<h2 class="h2">World cities</h2>
<ul class="category open-sans">
<li id="capital"></li>
<li id="normal"></li>
</ul>
<h2 class="h2">Inhabitants</h2>
<ul id="legend-population">
<li class="size open-sans">
<div id="min"></div>
<div id="max"></div>
</li>
<li class="avg open-sans"></li>
</ul>
</div>
</aside>
<script>
const map = L.map('map').setView([30, 0], 3);
map.scrollWheelZoom.disable();
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
const client = new carto.Client({
apiKey: 'default_public',
username: 'cartojs-test'
});
const populatedPlacesSource = new carto.source.SQL('SELECT * FROM ne_10m_populated_places_simple');
const populatedPlacesStyle = new carto.style.CartoCSS(`
#layer {
marker-width: ramp([pop_max], range(2, 12), quantiles(5));
marker-fill: ramp([adm0cap], (#EE4D5A, #68B69E), (0, 1), "=", category);
marker-fill-opacity: 0.6;
marker-allow-overlap: true;
marker-line-width: 1;
marker-line-color: #000;
marker-line-opacity: 0.2;
}
`);
const populatedPlacesLayer = new carto.layer.Layer(populatedPlacesSource, populatedPlacesStyle);
client.addLayer(populatedPlacesLayer);
client.getLeafletLayer().addTo(map);
populatedPlacesLayer.on('metadataChanged', function(event) {
event.styles.forEach(function (styleMetadata) {
switch(styleMetadata.getProperty()) {
case 'marker-width':
renderLegendSize(styleMetadata);
break;
case 'marker-fill':
renderLegendCapital(styleMetadata);
break;
}
});
});
function renderLegendSize(metadata) {
document.getElementById('min').innerHTML = `
<div class="circle circle-outline" style="width:8px; height:8px;"></div> <p>${metadata.getMin()}</p>
`;
document.getElementById('max').innerHTML = `
<div class="circle circle-outline" style="width:24px; height:24px;"></div> <p>${metadata.getMax()}</p>
`;
document.querySelector('.legend .avg').innerHTML = `
<p><b>Average: </b> ${metadata.getAverage().toFixed(2)}</p>
`;
}
function renderLegendCapital(metadata) {
const categories = metadata.getCategories();
for (category of categories) {
switch (category.name) {
case 0:
document.getElementById('normal').innerHTML = `
<div class="circle" style="background:${category.value}"></div> Normal
`;
break;
case 1:
document.getElementById('capital').innerHTML = `
<div class="circle" style="background:${category.value}"></div> Capital
`;
break;
}
}
}
function setAllCities() {
populatedPlacesSource.setQuery('SELECT * FROM ne_10m_populated_places_simple');
}
function setEuropeanCities() {
populatedPlacesSource.setQuery(`
SELECT *
FROM ne_10m_populated_places_simple
WHERE adm0name IN (SELECT admin FROM ne_adm0_europe)
`);
}
function setSpanishCities() {
populatedPlacesSource.setQuery(`
SELECT *
FROM ne_10m_populated_places_simple
WHERE adm0name = \'Spain\'
`);
}
function invertColors() {
const invert = document.getElementById('invertColors').checked;
const colors = invert ? '(#68B69E, #EE4D5A)' : '(#EE4D5A, #68B69E)';
const content = `
#layer {
marker-width: ramp([pop_max], range(2, 12), quantiles(5));
marker-fill: ramp([adm0cap], ${colors}, (0, 1), "=", category);
marker-fill-opacity: 0.6;
marker-allow-overlap: true;
marker-line-width: 1;
marker-line-color: #000;
marker-line-opacity: 0.2;
}
`;
populatedPlacesStyle.setContent(content);
}
</script>
</body>
</html>