Skip to content

Commit 0640fa8

Browse files
author
Miles Hinchliffe
committed
Merge branch 'main-check-merge' of github.com:communitiesuk/mhclg_svelte_component_library into main-check-merge
2 parents 647bc2d + 7f2ce59 commit 0640fa8

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

src/lib/components/data-vis/map/Map.svelte

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@
7979
mapHeight = 200,
8080
setMaxBounds = false,
8181
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,
8298
onidle,
8399
}: {
84100
data: object[];
@@ -122,6 +138,26 @@
122138
customBreaks?: number[];
123139
interactive?: boolean;
124140
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;
125161
onidle?: (e: maplibregl.MapLibreEvent) => void;
126162
} = $props();
127163
@@ -257,6 +293,23 @@
257293
}),
258294
);
259295
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+
260313
let merged = $derived(joinData(filteredGeoJsonData, dataWithColor));
261314
262315
let hoveredArea = $state();
@@ -322,7 +375,7 @@
322375
);
323376
</script>
324377

325-
<div style="height: {mapHeight}px;">
378+
<div style="position: relative; height: {mapHeight}px;">
326379
<MapLibre
327380
bind:map
328381
bind:loaded
@@ -337,6 +390,22 @@
337390
class="map"
338391
{onload}
339392
{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}
340409
>
341410
{#if interactive && !standardControls}
342411
<NonStandardControls
@@ -418,6 +487,15 @@
418487
</MapLibre>
419488
</div>
420489

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+
421499
<style>
422500
:global(.maplibregl-ctrl-group button.reset-button) {
423501
/* margin: 10px; */
@@ -426,4 +504,27 @@
426504
font-size: 16px;
427505
height: 100%;
428506
}
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+
}
429530
</style>

0 commit comments

Comments
 (0)