|
66 | 66 | zoom = 5,
|
67 | 67 | minZoom,
|
68 | 68 | maxZoom,
|
69 |
| - maxBounds, |
| 69 | + maxBoundsCoords, |
70 | 70 | hash = false,
|
71 |
| - interactive, |
72 | 71 | updateHash = (u) => {
|
73 | 72 | replaceState(u, page.state);
|
74 | 73 | },
|
|
107 | 106 | zoom?: number;
|
108 | 107 | minZoom?: number | undefined;
|
109 | 108 | maxZoom?: number | undefined;
|
110 |
| - maxBounds?: [number, number][]; |
| 109 | + maxBoundsCoords?: [number, number][]; |
111 | 110 | setMaxBounds?: boolean;
|
112 | 111 | hash?: boolean;
|
113 | 112 | updateHash?: (URL) => void;
|
114 | 113 | useInitialHash?: boolean;
|
115 | 114 | mapHeight?: number;
|
116 | 115 | setCustomPallet?: boolean;
|
117 | 116 | customBreaks?: number[];
|
118 |
| - interative?: boolean; |
| 117 | + interative: boolean; |
119 | 118 | } = $props();
|
| 119 | + $inspect(setMaxBounds); |
120 | 120 | let styleLookup = {
|
121 | 121 | "Carto-light":
|
122 | 122 | "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
|
|
125 | 125 | };
|
126 | 126 | let style = $derived(styleLookup[styleSheet] ?? styleSheet);
|
127 | 127 |
|
| 128 | + $inspect(customBreaks); |
| 129 | +
|
| 130 | + let breakCount = $derived( |
| 131 | + breaksType == "custom" ? customBreaks.length : numberOfBreaks, |
| 132 | + ); |
| 133 | + $inspect(breakCount); |
| 134 | +
|
128 | 135 | let mapData = $derived(data?.filter((d) => d["year"] == year)[0]?.data);
|
129 | 136 |
|
130 | 137 | let filteredMapData = $derived(
|
|
144 | 151 | let fillColors: string[] = $derived(
|
145 | 152 | setCustomPallet == true
|
146 | 153 | ? customPallet
|
147 |
| - : colorbrewer[colorPalette][numberOfBreaks], |
| 154 | + : colorbrewer[colorPalette][breakCount], |
148 | 155 | );
|
149 | 156 |
|
150 | 157 | let borderColor = "#003300";
|
|
204 | 211 |
|
205 | 212 | let breaks = $derived(
|
206 | 213 | breaksType == "jenks"
|
207 |
| - ? jenksBreaks(vals, numberOfBreaks) |
| 214 | + ? jenksBreaks(vals, breakCount) |
208 | 215 | : breaksType == "quantile"
|
209 |
| - ? quantileBreaks(vals, numberOfBreaks) |
| 216 | + ? quantileBreaks(vals, breakCount) |
210 | 217 | : customBreaks,
|
211 | 218 | );
|
212 | 219 |
|
|
237 | 244 | return bounds;
|
238 | 245 | }
|
239 | 246 |
|
240 |
| - if (setMaxBounds) { |
241 |
| - let boundary = convertToLngLatBounds(maxBounds); |
242 |
| - } |
243 |
| -
|
244 | 247 | function zoomToArea(e) {
|
245 | 248 | if (clickToZoom) {
|
246 | 249 | let coordArray =
|
|
280 | 283 | ? +initialLocationHash[0]
|
281 | 284 | : zoom
|
282 | 285 | : 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); |
283 | 293 | </script>
|
284 | 294 |
|
285 | 295 | <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 | + > |
302 | 310 | {#if !standardControls}
|
303 | 311 | <NonStandardControls
|
304 | 312 | {navigationControl}
|
|
325 | 333 | >
|
326 | 334 | </Control>
|
327 | 335 | {/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 |
354 | 339 | 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, |
357 | 345 | }}
|
358 | 346 | 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} |
359 | 376 | />
|
360 | 377 | {/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} |
372 | 380 | </div>
|
373 | 381 |
|
374 | 382 | <style>
|
|
0 commit comments