|
79 | 79 | mapHeight = 200,
|
80 | 80 | setMaxBounds = false,
|
81 | 81 | onload,
|
| 82 | + onerror, |
| 83 | + onclick, |
| 84 | + ondblclick, |
| 85 | + onmousemove, |
| 86 | + oncontextmenu, |
| 87 | + onmovestart, |
| 88 | + onmoveend, |
| 89 | + onzoomstart, |
| 90 | + onzoom, |
| 91 | + onzoomend, |
| 92 | + onpitch, |
| 93 | + onrotate, |
| 94 | + onwheel, |
| 95 | + ondata, |
| 96 | + onstyleload, |
| 97 | + onstyledata, |
82 | 98 | onidle,
|
83 | 99 | }: {
|
84 | 100 | data: object[];
|
|
122 | 138 | customBreaks?: number[];
|
123 | 139 | interactive?: boolean;
|
124 | 140 | onload?: (map: maplibregl.Map) => void;
|
| 141 | + onerror?: (error: Partial<ErrorEvent>) => void; |
| 142 | + onclick?: (e: maplibregl.MapMouseEvent) => void; |
| 143 | + ondblclick?: (e: maplibregl.MapMouseEvent) => void; |
| 144 | + onmousemove?: (e: maplibregl.MapMouseEvent) => void; |
| 145 | + oncontextmenu?: (e: maplibregl.MapMouseEvent) => void; |
| 146 | + onmovestart?: (e: MapMoveEvent) => void; |
| 147 | + onmoveend?: (e: MapMoveEvent) => void; |
| 148 | + onzoomstart?: (e: maplibregl.MapLibreZoomEvent) => void; |
| 149 | + onzoom?: (e: maplibregl.MapLibreZoomEvent) => void; |
| 150 | + onzoomend?: (e: maplibregl.MapLibreZoomEvent) => void; |
| 151 | + onpitch?: ( |
| 152 | + e: maplibregl.MapLibreEvent<MouseEvent | TouchEvent | undefined>, |
| 153 | + ) => void; |
| 154 | + onrotate?: ( |
| 155 | + e: maplibregl.MapLibreEvent<MouseEvent | TouchEvent | undefined>, |
| 156 | + ) => void; |
| 157 | + onwheel?: (e: maplibregl.MapWheelEvent) => void; |
| 158 | + ondata?: (e: maplibregl.MapDataEvent) => void; |
| 159 | + onstyleload?: (e: StyleLoadEvent) => void; |
| 160 | + onstyledata?: (e: maplibregl.MapStyleDataEvent) => void; |
125 | 161 | onidle?: (e: maplibregl.MapLibreEvent) => void;
|
126 | 162 | } = $props();
|
127 | 163 |
|
|
257 | 293 | }),
|
258 | 294 | );
|
259 | 295 |
|
| 296 | + let legendItems = $derived([ |
| 297 | + ...breaks |
| 298 | + .map((b, i) => { |
| 299 | + const from = b; |
| 300 | + const to = breaks[i + 1]; |
| 301 | + return { |
| 302 | + color: fillColors[i], |
| 303 | + label: to ? `${from} – ${to}` : `${from}+`, |
| 304 | + }; |
| 305 | + }) |
| 306 | + .slice(0, fillColors.length), |
| 307 | + { |
| 308 | + color: "lightgrey", |
| 309 | + label: "No data", |
| 310 | + }, |
| 311 | + ]); |
| 312 | +
|
260 | 313 | let merged = $derived(joinData(filteredGeoJsonData, dataWithColor));
|
261 | 314 |
|
262 | 315 | let hoveredArea = $state();
|
|
322 | 375 | );
|
323 | 376 | </script>
|
324 | 377 |
|
325 |
| -<div style="height: {mapHeight}px;"> |
| 378 | +<div style="position: relative; height: {mapHeight}px;"> |
326 | 379 | <MapLibre
|
327 | 380 | bind:map
|
328 | 381 | bind:loaded
|
|
337 | 390 | class="map"
|
338 | 391 | {onload}
|
339 | 392 | {onidle}
|
| 393 | + {onerror} |
| 394 | + {onclick} |
| 395 | + {ondblclick} |
| 396 | + {onmousemove} |
| 397 | + {oncontextmenu} |
| 398 | + {onmovestart} |
| 399 | + {onmoveend} |
| 400 | + {onzoomstart} |
| 401 | + {onzoom} |
| 402 | + {onzoomend} |
| 403 | + {onpitch} |
| 404 | + {onrotate} |
| 405 | + {onwheel} |
| 406 | + {ondata} |
| 407 | + {onstyleload} |
| 408 | + {onstyledata} |
340 | 409 | >
|
341 | 410 | {#if interactive && !standardControls}
|
342 | 411 | <NonStandardControls
|
|
418 | 487 | </MapLibre>
|
419 | 488 | </div>
|
420 | 489 |
|
| 490 | +<div class="legend"> |
| 491 | + {#each legendItems as item} |
| 492 | + <div class="legend-item"> |
| 493 | + <div class="legend-color" style="background-color: {item.color};"></div> |
| 494 | + <span>{item.label}</span> |
| 495 | + </div> |
| 496 | + {/each} |
| 497 | +</div> |
| 498 | + |
421 | 499 | <style>
|
422 | 500 | :global(.maplibregl-ctrl-group button.reset-button) {
|
423 | 501 | /* margin: 10px; */
|
|
426 | 504 | font-size: 16px;
|
427 | 505 | height: 100%;
|
428 | 506 | }
|
| 507 | + .legend { |
| 508 | + position: absolute; |
| 509 | + bottom: 20px; |
| 510 | + left: 10px; |
| 511 | + background: white; |
| 512 | + padding: 10px; |
| 513 | + border-radius: 4px; |
| 514 | + font-size: 12px; |
| 515 | + box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); |
| 516 | + } |
| 517 | +
|
| 518 | + .legend-item { |
| 519 | + display: flex; |
| 520 | + align-items: center; |
| 521 | + margin-bottom: 4px; |
| 522 | + } |
| 523 | +
|
| 524 | + .legend-color { |
| 525 | + width: 20px; |
| 526 | + height: 20px; |
| 527 | + margin-right: 8px; |
| 528 | + border: 1px solid #ccc; |
| 529 | + } |
429 | 530 | </style>
|
0 commit comments