-
Notifications
You must be signed in to change notification settings - Fork 3
/
Choropleth.svelte
178 lines (154 loc) · 5.48 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<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, mapKeyToColor } from './utils';
import { DefaultColors } from './constants';
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: DefaultColors.LightGrey,
end: DefaultColors.DarkGrey,
},
};
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 = DefaultColors.EmptyShape,
} = 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)
|| (newShapes.type === 'vtiles' && !newShapes.url)
|| !values
) {
// 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': DefaultColors.ShapeOutline,
},
};
bbox = turfBbox(newShapes.geoJson);
} else if (newShapes.type === 'vtiles') {
dataBounds = { min: 1, max: 27 }; // FIXME: don't hardcode this
source = {
type: 'vector',
tiles: [newShapes.url],
};
const keyToColor = mapKeyToColor(values, newColorScale);
const matchExpression = [
'match',
['get', 'reg_code'], // FIXME: Don't hardcode the key
];
Object.entries(keyToColor).forEach(e => matchExpression.push(...e));
matchExpression.push(DefaultColors.EmptyShape); // Default fallback color
layer = {
'type': 'fill',
'source-layer': 'fr_40_region_2021', // FIXME: Don't hardcode the source layer
'layout': {},
'paint': {
'fill-color': matchExpression,
'fill-outline-color': DefaultColors.ShapeOutline,
'fill-opacity': [
'case',
['boolean', ['feature-state', 'hover'], false],
1,
0.5,
],
}
};
}
}
$: 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>