Skip to content

Commit a61dce7

Browse files
committed
Add useInitialHash prop and add types to map variables
1 parent 91351a9 commit a61dce7

File tree

3 files changed

+82
-24
lines changed

3 files changed

+82
-24
lines changed

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

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script lang="ts">
2-
//@ts-nocheck
3-
42
import {
53
MapLibre,
64
GeoJSON,
@@ -11,10 +9,11 @@
119
ControlButton,
1210
ControlGroup,
1311
} from "svelte-maplibre";
14-
import { contrastingColor } from "./colors";
12+
import { contrastingColor } from "./colors.js";
1513
import { colorbrewer } from "./colorbrewer.js";
1614
import { hoverStateFilter } from "svelte-maplibre/filters.js";
17-
import type { maplibregl, ExpressionSpecification } from "maplibre-gl";
15+
import type { LngLatLike } from "maplibre-gl";
16+
import type { FeatureCollection } from "geojson";
1817
import fullTopo from "./fullTopo.json";
1918
import * as topojson from "topojson-client";
2019
import Tooltip from "./Tooltip.svelte";
@@ -26,13 +25,12 @@
2625
} from "./mapUtils.js";
2726
import NonStandardControls from "./NonStandardControls.svelte";
2827
import { replaceState } from "$app/navigation";
29-
30-
import { joinData } from "./dataJoin";
3128
import { page } from "$app/state";
29+
import { joinData } from "./dataJoin.js";
3230
3331
let {
3432
data,
35-
cooperativeGestures,
33+
cooperativeGestures = true,
3634
standardControls = true,
3735
navigationControl,
3836
navigationControlPosition = "top-left",
@@ -52,7 +50,7 @@
5250
geoType,
5351
year,
5452
metric,
55-
breaksType,
53+
breaksType = "quantile",
5654
numberOfBreaks = 5,
5755
fillOpacity = 0.5,
5856
changeOpacityOnHover = true,
@@ -63,9 +61,43 @@
6361
updateHash = (u) => {
6462
replaceState(u, page.state);
6563
},
64+
useInitialHash = true,
6665
mapHeight = 200,
66+
}: {
67+
data: object[];
68+
cooperativeGestures?: boolean;
69+
standardControls?: boolean;
70+
navigationControl?: boolean;
71+
navigationControlPosition?: string;
72+
geolocateControl?: boolean;
73+
geolocateControlPosition?: string;
74+
fullscreenControl?: boolean;
75+
fullscreenControlPosition?: string;
76+
scaleControl?: boolean;
77+
scaleControlPosition?: string;
78+
scaleControlUnit?: string;
79+
styleSheet?: string | URL | object;
80+
colorPalette?: string;
81+
showBorder?: boolean;
82+
maxBorderWidth?: number;
83+
tooltip?: boolean;
84+
clickToZoom?: boolean;
85+
geoType?: string;
86+
year?: string | number;
87+
metric?: string;
88+
breaksType?: string;
89+
numberOfBreaks?: number;
90+
fillOpacity?: number;
91+
changeOpacityOnHover: boolean;
92+
hoverOpacity?: number;
93+
center?: LngLatLike | undefined;
94+
zoom?: number;
95+
hash?: boolean;
96+
updateHash?: (URL) => void;
97+
useInitialHash?: boolean;
98+
mapHeight?: number;
6799
} = $props();
68-
100+
$inspect(data);
69101
let styleLookup = {
70102
"Carto-light":
71103
"https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
@@ -74,7 +106,7 @@
74106
};
75107
let style = $derived(styleLookup[styleSheet] ?? styleSheet);
76108
77-
let mapData = $derived(data?.filter((d) => d.year == year)[0]?.data);
109+
let mapData = $derived(data?.filter((d) => d["year"] == year)[0]?.data);
78110
79111
let filteredMapData = $derived(
80112
mapData.map((el) => ({
@@ -84,13 +116,15 @@
84116
})),
85117
);
86118
87-
const geojsonData = $derived(
119+
const geojsonData: FeatureCollection = $derived(
88120
topojson.feature(fullTopo, fullTopo.objects[geoType]),
89121
);
90122
91123
let filteredGeoJsonData = $derived(filterGeo(geojsonData, year));
92124
93-
let fillColor = $derived(colorbrewer[colorPalette][numberOfBreaks]);
125+
let fillColors: string[] = $derived(
126+
colorbrewer[colorPalette][numberOfBreaks],
127+
);
94128
95129
let borderColor = "#003300";
96130
@@ -105,7 +139,7 @@
105139
: [],
106140
);
107141
108-
let colors = $derived(fillColor.map((d) => contrastingColor(d)));
142+
let colors = $derived(fillColors.map((d) => contrastingColor(d)));
109143
$effect(() => {
110144
//Things can get out of sync when changing source
111145
//this section makes sure that the geojson layers end up below the text layers
@@ -122,7 +156,7 @@
122156
)?.id;
123157
if (geoJsonLayerIds && labelLayerId) {
124158
for (let layer of geoJsonLayerIds) {
125-
map.moveLayer(layer, labelLayerId);
159+
map?.moveLayer(layer, labelLayerId);
126160
}
127161
}
128162
@@ -157,7 +191,7 @@
157191
filteredMapData.map((d) => {
158192
return {
159193
...d,
160-
color: getColor(d.metric, breaks, fillColor),
194+
color: getColor(d.metric, breaks, fillColors),
161195
};
162196
}),
163197
);
@@ -194,9 +228,21 @@
194228
]);
195229
}
196230
}
197-
//Even if hash is false, if the page is loaded with a location hash use that as the initial settings, rather than than the values passed to the component
231+
//When useInitialHash is true, even if hash is false, if the page is loaded with a location hash use that as the initial settings, rather than the values passed to the component
198232
const initialLocationHash = page.url.hash.replace("#", "").split("/");
199233
const useLocationHash = initialLocationHash.length >= 3 ? true : false;
234+
235+
center = useInitialHash
236+
? useLocationHash
237+
? [+initialLocationHash[2], +initialLocationHash[1]]
238+
: center
239+
: center;
240+
241+
zoom = useInitialHash
242+
? useLocationHash
243+
? +initialLocationHash[0]
244+
: zoom
245+
: zoom;
200246
</script>
201247

202248
<div style="height: {mapHeight}px;">
@@ -205,10 +251,8 @@
205251
bind:loaded
206252
{style}
207253
{standardControls}
208-
center={useLocationHash
209-
? [initialLocationHash[2], initialLocationHash[1]]
210-
: center}
211-
zoom={useLocationHash ? initialLocationHash[0] : zoom}
254+
{center}
255+
{zoom}
212256
{hash}
213257
{updateHash}
214258
class="map"

src/lib/components/data-vis/map/mapUtils.js renamed to src/lib/components/data-vis/map/mapUtils.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export function getColor(value, breaks, colors) {
1+
import type { FeatureCollection } from "geojson";
2+
3+
export function getColor(
4+
value: number,
5+
breaks: number[],
6+
colors: string[],
7+
): string {
28
let color;
39
let found = false;
410
let i = 1;
@@ -13,7 +19,7 @@ export function getColor(value, breaks, colors) {
1319
return color ? color : "lightgrey";
1420
}
1521

16-
export function filterGeo(geo, year) {
22+
export function filterGeo(geo: FeatureCollection, year: number | string) {
1723
let filtered = JSON.parse(JSON.stringify(geo));
1824
filtered.features = filtered.features
1925
.filter((f) => {
@@ -93,13 +99,13 @@ export function jenksBreaks(data, numBreaks) {
9399
return breaks;
94100
}
95101

96-
export function quantileBreaks(data, numBreaks) {
102+
export function quantileBreaks(data: number[], numBreaks: number): number[] {
97103
// Sort the data array
98104
data = data.sort((a, b) => a - b);
99105

100106
let len = data.length;
101107

102-
let breaks = [
108+
let breaks: number[] = [
103109
// data[0]
104110
// data[Math.floor(len * 0.2)],
105111
// data[Math.floor(len * 0.4)],

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@
497497
category: "View",
498498
value: false,
499499
},
500+
{
501+
name: "useInitialHash",
502+
isProp: true,
503+
description:
504+
"If the URL has a valid hash that will override initial viewport settings, even if hash is false. Recommended.",
505+
value: true,
506+
category: "View",
507+
},
500508
]),
501509
);
502510

0 commit comments

Comments
 (0)