Skip to content

Commit 2ddfe98

Browse files
committed
bug fix for custom breaks
1 parent 74cd705 commit 2ddfe98

File tree

2 files changed

+77
-69
lines changed

2 files changed

+77
-69
lines changed

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

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@
6666
zoom = 5,
6767
minZoom,
6868
maxZoom,
69-
maxBounds,
69+
maxBoundsCoords,
7070
hash = false,
71-
interactive,
7271
updateHash = (u) => {
7372
replaceState(u, page.state);
7473
},
@@ -107,16 +106,17 @@
107106
zoom?: number;
108107
minZoom?: number | undefined;
109108
maxZoom?: number | undefined;
110-
maxBounds?: [number, number][];
109+
maxBoundsCoords?: [number, number][];
111110
setMaxBounds?: boolean;
112111
hash?: boolean;
113112
updateHash?: (URL) => void;
114113
useInitialHash?: boolean;
115114
mapHeight?: number;
116115
setCustomPallet?: boolean;
117116
customBreaks?: number[];
118-
interative?: boolean;
117+
interative: boolean;
119118
} = $props();
119+
$inspect(setMaxBounds);
120120
let styleLookup = {
121121
"Carto-light":
122122
"https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
@@ -125,6 +125,13 @@
125125
};
126126
let style = $derived(styleLookup[styleSheet] ?? styleSheet);
127127
128+
$inspect(customBreaks);
129+
130+
let breakCount = $derived(
131+
breaksType == "custom" ? customBreaks.length : numberOfBreaks,
132+
);
133+
$inspect(breakCount);
134+
128135
let mapData = $derived(data?.filter((d) => d["year"] == year)[0]?.data);
129136
130137
let filteredMapData = $derived(
@@ -144,7 +151,7 @@
144151
let fillColors: string[] = $derived(
145152
setCustomPallet == true
146153
? customPallet
147-
: colorbrewer[colorPalette][numberOfBreaks],
154+
: colorbrewer[colorPalette][breakCount],
148155
);
149156
150157
let borderColor = "#003300";
@@ -204,9 +211,9 @@
204211
205212
let breaks = $derived(
206213
breaksType == "jenks"
207-
? jenksBreaks(vals, numberOfBreaks)
214+
? jenksBreaks(vals, breakCount)
208215
: breaksType == "quantile"
209-
? quantileBreaks(vals, numberOfBreaks)
216+
? quantileBreaks(vals, breakCount)
210217
: customBreaks,
211218
);
212219
@@ -237,10 +244,6 @@
237244
return bounds;
238245
}
239246
240-
if (setMaxBounds) {
241-
let boundary = convertToLngLatBounds(maxBounds);
242-
}
243-
244247
function zoomToArea(e) {
245248
if (clickToZoom) {
246249
let coordArray =
@@ -280,25 +283,30 @@
280283
? +initialLocationHash[0]
281284
: zoom
282285
: zoom;
286+
287+
let bounds = $derived(
288+
setMaxBounds ? convertToLngLatBounds(maxBoundsCoords) : {},
289+
);
290+
291+
// let displayBounds = $derived(bounds.map((b) => b.toFixed(4)).join(", "));
292+
// $inspect(displayBounds);
283293
</script>
284294

285295
<div style="height: {mapHeight}px;">
286-
<MapLibre
287-
bind:map
288-
bind:loaded
289-
{style}
290-
{center}
291-
{interactive}
292-
{zoom}
293-
{maxZoom}
294-
{minZoom}
295-
{...setMaxBounds ? { maxBounds: boundary } : {}}
296-
{standardControls}
297-
{hash}
298-
{updateHash}
299-
class="map"
300-
>
301-
{#if interactive}
296+
{#key bounds}
297+
<MapLibre
298+
bind:map
299+
bind:loaded
300+
{style}
301+
{center}
302+
{zoom}
303+
{maxZoom}
304+
{minZoom}
305+
{standardControls}
306+
{hash}
307+
{updateHash}
308+
class="map"
309+
>
302310
{#if !standardControls}
303311
<NonStandardControls
304312
{navigationControl}
@@ -325,50 +333,50 @@
325333
>
326334
</Control>
327335
{/if}
328-
{/if}
329-
330-
<GeoJSON id="areas" data={merged} promoteId="areanm">
331-
<FillLayer
332-
paint={{
333-
//Get the color property of the area, or lightgrey if that's undefined
334-
"fill-color": ["coalesce", ["get", "color"], "lightgrey"],
335-
"fill-opacity": changeOpacityOnHover
336-
? hoverStateFilter(fillOpacity, hoverOpacity) //setting the fill-opacity based on whether the area is hovered
337-
: fillOpacity,
338-
}}
339-
beforeLayerType="symbol"
340-
manageHoverState
341-
onclick={(e) => zoomToArea(e)}
342-
onmousemove={(e) => {
343-
hoveredArea = e.features[0].id;
344-
hoveredAreaData = e.features[0].properties.metric;
345-
currentMousePosition = e.event.point;
346-
}}
347-
onmouseleave={(e) => {
348-
(hoveredArea = null), (hoveredAreaData = null);
349-
}}
350-
/>
351-
{#if showBorder}
352-
<LineLayer
353-
layout={{ "line-cap": "round", "line-join": "round" }}
336+
337+
<GeoJSON id="areas" data={merged} promoteId="areanm">
338+
<FillLayer
354339
paint={{
355-
"line-color": hoverStateFilter(borderColor, "orange"), //setting the colour based on whether the area is hovered
356-
"line-width": zoomTransition(3, 0, 12, maxBorderWidth), //setting the line-width based on the zoom level
340+
//Get the color property of the area, or lightgrey if that's undefined
341+
"fill-color": ["coalesce", ["get", "color"], "lightgrey"],
342+
"fill-opacity": changeOpacityOnHover
343+
? hoverStateFilter(fillOpacity, hoverOpacity) //setting the fill-opacity based on whether the area is hovered
344+
: fillOpacity,
357345
}}
358346
beforeLayerType="symbol"
347+
manageHoverState
348+
onclick={(e) => zoomToArea(e)}
349+
onmousemove={(e) => {
350+
hoveredArea = e.features[0].id;
351+
hoveredAreaData = e.features[0].properties.metric;
352+
currentMousePosition = e.event.point;
353+
}}
354+
onmouseleave={(e) => {
355+
(hoveredArea = null), (hoveredAreaData = null);
356+
}}
357+
/>
358+
{#if showBorder}
359+
<LineLayer
360+
layout={{ "line-cap": "round", "line-join": "round" }}
361+
paint={{
362+
"line-color": hoverStateFilter(borderColor, "orange"), //setting the colour based on whether the area is hovered
363+
"line-width": zoomTransition(3, 0, 12, maxBorderWidth), //setting the line-width based on the zoom level
364+
}}
365+
beforeLayerType="symbol"
366+
/>
367+
{/if}
368+
</GeoJSON>
369+
{#if tooltip}
370+
<Tooltip
371+
{currentMousePosition}
372+
{hoveredArea}
373+
{hoveredAreaData}
374+
{year}
375+
{metric}
359376
/>
360377
{/if}
361-
</GeoJSON>
362-
{#if tooltip}
363-
<Tooltip
364-
{currentMousePosition}
365-
{hoveredArea}
366-
{hoveredAreaData}
367-
{year}
368-
{metric}
369-
/>
370-
{/if}
371-
</MapLibre>
378+
</MapLibre>
379+
{/key}
372380
</div>
373381

374382
<style>

src/wrappers/components/data-vis/map/MapWrapper.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
let parametersSourceArray = $derived(
157157
addIndexAndInitalValue([
158158
{
159-
name: "Interactive",
159+
name: "interactive",
160160
category: "UI",
161161
value: false,
162162
description: "Turn on/off the interactvity funtion for the maps",
@@ -369,7 +369,7 @@
369369
name: "customPallet",
370370
isProp: true,
371371
category: "Styling",
372-
value: [[]],
372+
value: ["#979e4f"],
373373
visable: { name: "setCustomPallet", value: true },
374374
description:
375375
"Pass in an array of colours you want to be used in a custom pallet. Write in a hex format",
@@ -544,7 +544,7 @@
544544
},
545545
546546
{
547-
name: "maxBounds",
547+
name: "maxBoundsCoords",
548548
isProp: true,
549549
category: "View",
550550
value: [

0 commit comments

Comments
 (0)