Skip to content

Commit 989aa11

Browse files
committed
Added interactive function
1 parent dd2fc0e commit 989aa11

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
data,
3737
customPallet,
3838
setCustomPallet,
39+
interactive,
3940
cooperativeGestures = true,
4041
standardControls = true,
4142
navigationControl,
@@ -114,7 +115,7 @@
114115
mapHeight?: number;
115116
setCustomPallet?: boolean;
116117
customBreaks?: number[];
117-
interative: boolean;
118+
interactive: boolean;
118119
} = $props();
119120
120121
let styleLookup = {
@@ -125,12 +126,9 @@
125126
};
126127
let style = $derived(styleLookup[styleSheet] ?? styleSheet);
127128
128-
$inspect(customBreaks);
129-
130129
let breakCount = $derived(
131130
breaksType == "custom" ? customBreaks.length : numberOfBreaks,
132131
);
133-
$inspect(breakCount);
134132
135133
let mapData = $derived(data?.filter((d) => d["year"] == year)[0]?.data);
136134
@@ -230,8 +228,6 @@
230228
231229
let merged = $derived(joinData(filteredGeoJsonData, dataWithColor));
232230
233-
$inspect(merged);
234-
235231
let hoveredArea = $state();
236232
let hoveredAreaData = $state();
237233
let currentMousePosition = $state();
@@ -289,9 +285,6 @@
289285
let bounds = $derived(
290286
setMaxBounds ? convertToLngLatBounds(maxBoundsCoords) : undefined,
291287
);
292-
293-
// let displayBounds = $derived(bounds.map((b) => b.toFixed(4)).join(", "));
294-
// $inspect(displayBounds);
295288
</script>
296289

297290
<div style="height: {mapHeight}px;">
@@ -303,12 +296,13 @@
303296
{zoom}
304297
{maxZoom}
305298
{minZoom}
306-
{standardControls}
299+
standardControls={interactive && standardControls}
307300
{hash}
308301
{updateHash}
302+
{interactive}
309303
class="map"
310304
>
311-
{#if !standardControls}
305+
{#if interactive && !standardControls}
312306
<NonStandardControls
313307
{navigationControl}
314308
{navigationControlPosition}
@@ -329,45 +323,52 @@
329323
center: center,
330324
zoom: zoom,
331325
});
332-
}}>Reset view</button
333-
></ControlGroup
334-
>
326+
}}
327+
>
328+
Reset view
329+
</button>
330+
</ControlGroup>
335331
</Control>
336332
{/if}
337333

338334
<GeoJSON id="areas" data={merged} promoteId="areanm">
339335
<FillLayer
340336
paint={{
341-
//Get the color property of the area, or lightgrey if that's undefined
342337
"fill-color": ["coalesce", ["get", "color"], "lightgrey"],
343338
"fill-opacity": changeOpacityOnHover
344-
? hoverStateFilter(fillOpacity, hoverOpacity) //setting the fill-opacity based on whether the area is hovered
339+
? hoverStateFilter(fillOpacity, hoverOpacity)
345340
: fillOpacity,
346341
}}
347342
beforeLayerType="symbol"
348-
manageHoverState
349-
onclick={(e) => zoomToArea(e)}
350-
onmousemove={(e) => {
351-
hoveredArea = e.features[0].id;
352-
hoveredAreaData = e.features[0].properties.metric;
353-
currentMousePosition = e.event.point;
354-
}}
355-
onmouseleave={(e) => {
356-
(hoveredArea = null), (hoveredAreaData = null);
357-
}}
343+
manageHoverState={interactive}
344+
onclick={interactive ? (e) => zoomToArea(e) : null}
345+
onmousemove={interactive
346+
? (e) => {
347+
hoveredArea = e.features[0].id;
348+
hoveredAreaData = e.features[0].properties.metric;
349+
currentMousePosition = e.event.point;
350+
}
351+
: null}
352+
onmouseleave={interactive
353+
? () => {
354+
hoveredArea = null;
355+
hoveredAreaData = null;
356+
}
357+
: null}
358358
/>
359359
{#if showBorder}
360360
<LineLayer
361361
layout={{ "line-cap": "round", "line-join": "round" }}
362362
paint={{
363-
"line-color": hoverStateFilter(borderColor, "orange"), //setting the colour based on whether the area is hovered
364-
"line-width": zoomTransition(3, 0, 12, maxBorderWidth), //setting the line-width based on the zoom level
363+
"line-color": hoverStateFilter(borderColor, "orange"),
364+
"line-width": zoomTransition(3, 0, 12, maxBorderWidth),
365365
}}
366366
beforeLayerType="symbol"
367367
/>
368368
{/if}
369369
</GeoJSON>
370-
{#if tooltip}
370+
371+
{#if interactive && tooltip}
371372
<Tooltip
372373
{currentMousePosition}
373374
{hoveredArea}

0 commit comments

Comments
 (0)