-
Notifications
You must be signed in to change notification settings - Fork 3
/
Choropleth.svelte
146 lines (125 loc) · 4.29 KB
/
Choropleth.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<svelte:options immutable={true} />
<script lang="ts">
import turfBbox from '@turf/bbox';
import type { SourceSpecification } from 'maplibre-gl';
// eslint-disable-next-line import/no-unresolved
import type { BBox, FeatureCollection } from 'geojson';
import type { ColorsScale, DataBounds, Color } from '../types';
import MapRender from './MapRender.svelte';
import { BLANK } from './mapStyles';
import { colorShapes, LIGHT_GREY, DARK_GREY } from './utils';
import type {
ChoroplethDataValue,
ChoroplethLayer,
ChoroplethOptions,
MapRenderTooltipFunction,
ChoroplethShapeValue,
ChoroplethTooltipFormatter,
MapLegend,
} from './types';
export let data: { value: ChoroplethDataValue[] }; // values, and the key to match
export let options: ChoroplethOptions; // contains the shapes to display & match
let shapes: ChoroplethShapeValue;
let colorsScale: ColorsScale;
const defaultColorsScale: ColorsScale = {
type: 'gradient',
colors: {
start: LIGHT_GREY,
end: DARK_GREY,
},
};
const defaultEmptyValueColor = '#cccccc';
let aspectRatio: number;
let renderTooltip: MapRenderTooltipFunction;
let bbox: BBox;
let activeShapes: string[] | undefined;
let interactive: boolean;
let legend: MapLegend | undefined;
// Used to apply a chosen color for shapes without values (default: #cccccc)
let emptyValueColor: Color;
const defaultInteractive = true;
$: ({
shapes,
colorsScale = defaultColorsScale,
legend,
aspectRatio,
activeShapes,
interactive = defaultInteractive,
emptyValueColor = defaultEmptyValueColor,
} = options);
// Choropleth is always display over a blank map, for readability purposes
const style = BLANK;
let layer: ChoroplethLayer;
let source: SourceSpecification;
let dataBounds: DataBounds;
function computeSourceLayerAndBboxes(
values: ChoroplethDataValue[] = [],
newShapes: ChoroplethShapeValue,
newColorScale: ColorsScale
) {
if (newShapes.type === 'geojson' && !newShapes.geoJson) {
// We don't have everything we need yet
return;
}
if (newShapes.type === 'geojson') {
const computeColors = colorShapes(
newShapes.geoJson as FeatureCollection,
values,
newColorScale,
emptyValueColor
);
const coloredShapes = computeColors.geoJson;
dataBounds = computeColors.bounds;
source = {
type: 'geojson',
data: coloredShapes,
};
layer = {
type: 'fill',
layout: {},
paint: {
'fill-color': ['get', 'color'],
'fill-opacity': 0.8,
'fill-outline-color': '#fff',
},
};
bbox = turfBbox(newShapes.geoJson);
} else {
// eslint-disable-next-line no-console
console.error('Unknown shapes type', newShapes.type);
}
}
$: computeSourceLayerAndBboxes(data.value, shapes, colorsScale);
const defaultFormat: ChoroplethTooltipFormatter = ({ value, label }) =>
value ? `${label} — ${value}` : label;
$: renderTooltip = (hoveredFeature) => {
const values = data.value || [];
const matchedFeature = values.find(
(item) => String(item.x) === hoveredFeature.properties?.key
);
const tooltipRawValues: { value?: number; label: string; key: string } = {
value: matchedFeature?.y,
label: hoveredFeature.properties?.label || hoveredFeature.properties?.key,
key: hoveredFeature.properties?.key, // === matchedFeature.x
};
const format = options?.tooltip?.label;
return format ? format(tooltipRawValues) : defaultFormat(tooltipRawValues);
};
</script>
<div>
<MapRender
{style}
{source}
{layer}
{aspectRatio}
{dataBounds}
{colorsScale}
{legend}
{renderTooltip}
{bbox}
{activeShapes}
{interactive}
/>
</div>
<style>
</style>