@keneth80/k-chart 1.26.1 → 1.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +79 -0
- package/README.md +131 -1100
- package/docs/code-graph-handoff.md +19 -37
- package/docs/configuration-reference.md +16 -1
- package/docs/functional-api.md +87 -0
- package/docs/react-nextjs.md +62 -8
- package/docs/refactor-plan.md +39 -23
- package/docs/release.md +9 -4
- package/examples/stackblitz-simple-api-basic/package-lock.json +96 -4
- package/examples/stackblitz-simple-api-basic/package.json +1 -1
- package/examples/stackblitz-subcell-heatmap-basic/README.md +48 -0
- package/examples/stackblitz-subcell-heatmap-basic/index.html +13 -0
- package/examples/stackblitz-subcell-heatmap-basic/package-lock.json +1788 -0
- package/examples/stackblitz-subcell-heatmap-basic/package.json +23 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/app.ts +212 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/component.css +169 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/heatmap-demo-core.ts +505 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/heatmap-demo.ts +23 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/main.ts +15 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/maplibre-compat.ts +6 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/model.ts +242 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/raster.ts +121 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/style.css +15 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/synthetic.ts +72 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/vendor.d.ts +3 -0
- package/examples/stackblitz-subcell-heatmap-basic/src/vite-env.d.ts +1 -0
- package/examples/stackblitz-topology-basic/package-lock.json +96 -4
- package/examples/stackblitz-topology-basic/package.json +1 -1
- package/examples/stackblitz-webgl-line-basic/package-lock.json +96 -4
- package/examples/stackblitz-webgl-line-basic/package.json +1 -1
- package/examples/stackblitz-world-activity-map/package.json +1 -1
- package/lib/core/contracts.d.ts +14 -2
- package/lib/core/contracts.d.ts.map +1 -1
- package/lib/internal/map-move-end.d.ts +8 -0
- package/lib/internal/map-move-end.d.ts.map +1 -0
- package/lib/internal/map-move-end.js +33 -0
- package/lib/internal/map-move-end.js.map +1 -0
- package/lib/series/svg-globe.d.ts.map +1 -1
- package/lib/series/svg-globe.js +66 -8
- package/lib/series/svg-globe.js.map +1 -1
- package/lib/series/svg-graph.d.ts +29 -0
- package/lib/series/svg-graph.d.ts.map +1 -1
- package/lib/series/svg-graph.js +509 -80
- package/lib/series/svg-graph.js.map +1 -1
- package/lib/series/svg-region-map.d.ts.map +1 -1
- package/lib/series/svg-region-map.js +72 -1
- package/lib/series/svg-region-map.js.map +1 -1
- package/package.json +4 -3
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CanvasSource,
|
|
3
|
+
GeoJSONSource,
|
|
4
|
+
Map as MapLibreMap,
|
|
5
|
+
MapMouseEvent
|
|
6
|
+
} from 'maplibre-gl';
|
|
7
|
+
import type {FeatureCollection, LineString, Point, Polygon} from 'geojson';
|
|
8
|
+
import {
|
|
9
|
+
type Bounds,
|
|
10
|
+
type FieldFrame,
|
|
11
|
+
type SubcellField,
|
|
12
|
+
contains,
|
|
13
|
+
mixFrames,
|
|
14
|
+
sampleFrame,
|
|
15
|
+
unionBounds,
|
|
16
|
+
validateFrame
|
|
17
|
+
} from './model';
|
|
18
|
+
import {paintRaster} from './raster';
|
|
19
|
+
import {makeFrame, PROBES, SELECTED_CELL_ID} from './synthetic';
|
|
20
|
+
|
|
21
|
+
export interface HeatmapPick {
|
|
22
|
+
longitude: number;
|
|
23
|
+
latitude: number;
|
|
24
|
+
value: number | null;
|
|
25
|
+
cellId: string | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface HeatmapOptions {
|
|
29
|
+
showGrid?: boolean;
|
|
30
|
+
showSamples?: boolean;
|
|
31
|
+
showCellOutline?: boolean;
|
|
32
|
+
/** Keep low values opaque, or fade their alpha from zero to the high-value maximum. */
|
|
33
|
+
showLowValueBackground?: boolean;
|
|
34
|
+
style?: string;
|
|
35
|
+
/** Cancels adapter initialization when a host switches examples mid-load. */
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
/** Example-local callback, not a KChart adapter configuration field. */
|
|
38
|
+
onPick?: (pick: HeatmapPick) => void;
|
|
39
|
+
onFrame?: (frame: FieldFrame) => void;
|
|
40
|
+
onError?: (error: Error) => void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface FlatMapControllerLike {
|
|
44
|
+
show(options: {lat: number; lon: number; zoom?: number}): Promise<void>;
|
|
45
|
+
resize(): void;
|
|
46
|
+
destroy(): void;
|
|
47
|
+
/** MapLibre major versions have incompatible nominal types; validate after this boundary. */
|
|
48
|
+
getMap(): unknown;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type FlatMapFactory = (configuration: {
|
|
52
|
+
container: string | HTMLElement;
|
|
53
|
+
style: string;
|
|
54
|
+
initialZoom?: number;
|
|
55
|
+
minZoom?: number;
|
|
56
|
+
maxZoom?: number;
|
|
57
|
+
renderWorldCopies?: boolean;
|
|
58
|
+
maxBounds?: [[number, number], [number, number]];
|
|
59
|
+
toolbar?: {visible?: boolean};
|
|
60
|
+
cluster?: boolean;
|
|
61
|
+
}) => FlatMapControllerLike;
|
|
62
|
+
|
|
63
|
+
export interface SubcellHeatmapController {
|
|
64
|
+
setFrame(frame: FieldFrame, animate?: boolean): void;
|
|
65
|
+
setGridVisible(visible: boolean): void;
|
|
66
|
+
setSamplesVisible(visible: boolean): void;
|
|
67
|
+
setCellOutlineVisible(visible: boolean): void;
|
|
68
|
+
setLowValueBackgroundVisible(visible: boolean): void;
|
|
69
|
+
focus(mode: 'world' | 'region' | 'cell'): void;
|
|
70
|
+
getState(): {
|
|
71
|
+
frame: FieldFrame;
|
|
72
|
+
selectedId: string;
|
|
73
|
+
showGrid: boolean;
|
|
74
|
+
showSamples: boolean;
|
|
75
|
+
showCellOutline: boolean;
|
|
76
|
+
showLowValueBackground: boolean;
|
|
77
|
+
};
|
|
78
|
+
getMap(): MapLibreMap;
|
|
79
|
+
destroy(): void;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let nextId = 0;
|
|
83
|
+
|
|
84
|
+
function abortError(): Error {
|
|
85
|
+
const error = new Error('Subcell heatmap initialization was cancelled.');
|
|
86
|
+
error.name = 'AbortError';
|
|
87
|
+
return error;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function ring(bounds: Bounds): number[][] {
|
|
91
|
+
return [
|
|
92
|
+
[bounds.west, bounds.south],
|
|
93
|
+
[bounds.east, bounds.south],
|
|
94
|
+
[bounds.east, bounds.north],
|
|
95
|
+
[bounds.west, bounds.north],
|
|
96
|
+
[bounds.west, bounds.south]
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function selectionGeo(cell: SubcellField): FeatureCollection<Polygon> {
|
|
101
|
+
return {
|
|
102
|
+
type: 'FeatureCollection',
|
|
103
|
+
features: [{
|
|
104
|
+
type: 'Feature',
|
|
105
|
+
properties: {id: cell.id},
|
|
106
|
+
geometry: {type: 'Polygon', coordinates: [ring(cell.bounds)]}
|
|
107
|
+
}]
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function gridGeo(bounds: Bounds): FeatureCollection<LineString> {
|
|
112
|
+
const features: FeatureCollection<LineString>['features'] = [];
|
|
113
|
+
for (let longitude = bounds.west; longitude <= bounds.east + 1e-9; longitude += 0.5) {
|
|
114
|
+
features.push({
|
|
115
|
+
type: 'Feature',
|
|
116
|
+
properties: {},
|
|
117
|
+
geometry: {
|
|
118
|
+
type: 'LineString',
|
|
119
|
+
coordinates: [[longitude, bounds.south], [longitude, bounds.north]]
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
for (let latitude = bounds.south; latitude <= bounds.north + 1e-9; latitude += 0.5) {
|
|
124
|
+
features.push({
|
|
125
|
+
type: 'Feature',
|
|
126
|
+
properties: {},
|
|
127
|
+
geometry: {
|
|
128
|
+
type: 'LineString',
|
|
129
|
+
coordinates: [[bounds.west, latitude], [bounds.east, latitude]]
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return {type: 'FeatureCollection', features};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function samplesGeo(cell: SubcellField): FeatureCollection<Point> {
|
|
137
|
+
const features: FeatureCollection<Point>['features'] = [];
|
|
138
|
+
// All 2,601 nodes feed the field; only a representative subset is drawn as points.
|
|
139
|
+
for (let row = 0; row < cell.rows; row += 5) {
|
|
140
|
+
for (let column = 0; column < cell.columns; column += 5) {
|
|
141
|
+
features.push({
|
|
142
|
+
type: 'Feature',
|
|
143
|
+
properties: {value: cell.values[row * cell.columns + column]},
|
|
144
|
+
geometry: {
|
|
145
|
+
type: 'Point',
|
|
146
|
+
coordinates: [
|
|
147
|
+
cell.bounds.west + column / (cell.columns - 1) * 0.5,
|
|
148
|
+
cell.bounds.north - row / (cell.rows - 1) * 0.5
|
|
149
|
+
]
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return {type: 'FeatureCollection', features};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Example renderer built on an injected, real KChart MapLibre adapter factory.
|
|
159
|
+
* The injection lets the repository demo exercise local adapter source while
|
|
160
|
+
* the standalone example exercises the published adapter package.
|
|
161
|
+
*/
|
|
162
|
+
export async function createSubcellHeatmapDemoWithAdapter(
|
|
163
|
+
container: string | HTMLElement,
|
|
164
|
+
createFlatMap: FlatMapFactory,
|
|
165
|
+
options: HeatmapOptions = {}
|
|
166
|
+
): Promise<SubcellHeatmapController> {
|
|
167
|
+
let current = makeFrame(0);
|
|
168
|
+
let selectedId = SELECTED_CELL_ID;
|
|
169
|
+
const bounds = unionBounds(current.cells);
|
|
170
|
+
const prefix = `kchart-subcell-${++nextId}`;
|
|
171
|
+
const id = (suffix: string): string => `${prefix}-${suffix}`;
|
|
172
|
+
let destroyed = false;
|
|
173
|
+
let raf = 0;
|
|
174
|
+
let animationEpoch = 0;
|
|
175
|
+
let pauseTimer = 0;
|
|
176
|
+
let lowValueBackgroundVisible = options.showLowValueBackground ?? true;
|
|
177
|
+
|
|
178
|
+
const canvas = document.createElement('canvas');
|
|
179
|
+
// MapLibre 5 renders 64 * 2^n square CanvasSources black (maplibre-gl-js#6607).
|
|
180
|
+
canvas.width = 511;
|
|
181
|
+
canvas.height = 511;
|
|
182
|
+
canvas.id = prefix;
|
|
183
|
+
// Keep a drawable layout box for MapLibre 5 while placing the source canvas off-screen.
|
|
184
|
+
canvas.style.position = 'fixed';
|
|
185
|
+
canvas.style.left = '-10000px';
|
|
186
|
+
canvas.style.top = '-10000px';
|
|
187
|
+
canvas.style.pointerEvents = 'none';
|
|
188
|
+
canvas.dataset.subcellHeatmap = prefix;
|
|
189
|
+
let controller: FlatMapControllerLike | undefined;
|
|
190
|
+
let rejectAbort: ((error: Error) => void) | undefined;
|
|
191
|
+
let initializationCleaned = false;
|
|
192
|
+
const cleanupInitialization = (): void => {
|
|
193
|
+
if (initializationCleaned) return;
|
|
194
|
+
initializationCleaned = true;
|
|
195
|
+
controller?.destroy();
|
|
196
|
+
canvas.remove();
|
|
197
|
+
};
|
|
198
|
+
const onInitializationAbort = (): void => {
|
|
199
|
+
destroyed = true;
|
|
200
|
+
cleanupInitialization();
|
|
201
|
+
rejectAbort?.(abortError());
|
|
202
|
+
};
|
|
203
|
+
try {
|
|
204
|
+
if (options.signal?.aborted) throw abortError();
|
|
205
|
+
options.signal?.addEventListener('abort', onInitializationAbort, {once: true});
|
|
206
|
+
document.body.appendChild(canvas);
|
|
207
|
+
paintRaster(canvas, current, bounds, lowValueBackgroundVisible);
|
|
208
|
+
controller = createFlatMap({
|
|
209
|
+
container,
|
|
210
|
+
style: options.style ?? 'https://tiles.openfreemap.org/styles/liberty',
|
|
211
|
+
initialZoom: 9,
|
|
212
|
+
minZoom: 1,
|
|
213
|
+
maxZoom: 15,
|
|
214
|
+
renderWorldCopies: false,
|
|
215
|
+
toolbar: {visible: false},
|
|
216
|
+
cluster: false
|
|
217
|
+
});
|
|
218
|
+
const show = controller.show({lat: 37.75, lon: 126.75, zoom: 9});
|
|
219
|
+
if (options.signal) {
|
|
220
|
+
const aborted = new Promise<never>((_resolve, reject) => {
|
|
221
|
+
rejectAbort = reject;
|
|
222
|
+
});
|
|
223
|
+
await Promise.race([show, aborted]);
|
|
224
|
+
} else {
|
|
225
|
+
await show;
|
|
226
|
+
}
|
|
227
|
+
} catch (error) {
|
|
228
|
+
options.signal?.removeEventListener('abort', onInitializationAbort);
|
|
229
|
+
cleanupInitialization();
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
options.signal?.removeEventListener('abort', onInitializationAbort);
|
|
233
|
+
if (!controller) {
|
|
234
|
+
cleanupInitialization();
|
|
235
|
+
throw new Error('KChart adapter was not created.');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const maybeMap = controller.getMap();
|
|
239
|
+
if (!maybeMap) {
|
|
240
|
+
controller.destroy();
|
|
241
|
+
canvas.remove();
|
|
242
|
+
throw new Error('KChart adapter returned no MapLibre instance.');
|
|
243
|
+
}
|
|
244
|
+
const map = maybeMap as MapLibreMap;
|
|
245
|
+
const onMapError = (event: unknown): void => {
|
|
246
|
+
const cause = (event as {error?: unknown}).error;
|
|
247
|
+
options.onError?.(cause instanceof Error ? cause : new Error(String(cause ?? 'MapLibre runtime error')));
|
|
248
|
+
};
|
|
249
|
+
map.on('error', onMapError);
|
|
250
|
+
const selected = (): SubcellField => current.cells.find((cell) => cell.id === selectedId)!;
|
|
251
|
+
let gridVisible = options.showGrid ?? false;
|
|
252
|
+
let samplesVisible = options.showSamples ?? false;
|
|
253
|
+
let cellOutlineVisible = options.showCellOutline ?? false;
|
|
254
|
+
|
|
255
|
+
const addGeo = (name: string, data: FeatureCollection): void => {
|
|
256
|
+
map.addSource(id(name), {type: 'geojson', data});
|
|
257
|
+
};
|
|
258
|
+
const pauseCanvas = (): void => {
|
|
259
|
+
if (!destroyed) {
|
|
260
|
+
(map.getSource(id('raster')) as CanvasSource | undefined)?.pause();
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
const refreshCanvas = (): void => {
|
|
264
|
+
(map.getSource(id('raster')) as CanvasSource).play();
|
|
265
|
+
map.triggerRepaint();
|
|
266
|
+
window.clearTimeout(pauseTimer);
|
|
267
|
+
// Keep the source active long enough for MapLibre 5 to allocate its source tile.
|
|
268
|
+
pauseTimer = window.setTimeout(pauseCanvas, 500);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
map.addSource(id('raster'), {
|
|
273
|
+
type: 'canvas',
|
|
274
|
+
canvas: canvas.id,
|
|
275
|
+
animate: false,
|
|
276
|
+
coordinates: [
|
|
277
|
+
[bounds.west, bounds.north],
|
|
278
|
+
[bounds.east, bounds.north],
|
|
279
|
+
[bounds.east, bounds.south],
|
|
280
|
+
[bounds.west, bounds.south]
|
|
281
|
+
]
|
|
282
|
+
});
|
|
283
|
+
// Scalars are interpolated before color mapping. Nearest avoids filling missing gaps.
|
|
284
|
+
map.addLayer({
|
|
285
|
+
id: id('field'),
|
|
286
|
+
type: 'raster',
|
|
287
|
+
source: id('raster'),
|
|
288
|
+
paint: {'raster-opacity': 1, 'raster-fade-duration': 0, 'raster-resampling': 'nearest'}
|
|
289
|
+
});
|
|
290
|
+
addGeo('grid', gridGeo(bounds));
|
|
291
|
+
map.addLayer({
|
|
292
|
+
id: id('grid-lines'),
|
|
293
|
+
type: 'line',
|
|
294
|
+
source: id('grid'),
|
|
295
|
+
layout: {visibility: gridVisible ? 'visible' : 'none'},
|
|
296
|
+
paint: {'line-color': '#d8eef2', 'line-width': 0.65, 'line-opacity': 0.26}
|
|
297
|
+
});
|
|
298
|
+
addGeo('selection', selectionGeo(selected()));
|
|
299
|
+
map.addLayer({
|
|
300
|
+
id: id('selected-line'),
|
|
301
|
+
type: 'line',
|
|
302
|
+
source: id('selection'),
|
|
303
|
+
layout: {visibility: cellOutlineVisible ? 'visible' : 'none'},
|
|
304
|
+
paint: {'line-color': '#73cfc3', 'line-width': 1, 'line-opacity': 0.55}
|
|
305
|
+
});
|
|
306
|
+
addGeo('samples', samplesGeo(selected()));
|
|
307
|
+
map.addLayer({
|
|
308
|
+
id: id('sample-points'),
|
|
309
|
+
type: 'circle',
|
|
310
|
+
source: id('samples'),
|
|
311
|
+
minzoom: 7,
|
|
312
|
+
layout: {visibility: samplesVisible ? 'visible' : 'none'},
|
|
313
|
+
paint: {
|
|
314
|
+
'circle-radius': 2,
|
|
315
|
+
'circle-color': '#0d2637',
|
|
316
|
+
'circle-stroke-width': 0.5,
|
|
317
|
+
'circle-stroke-color': '#ffffff'
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
addGeo('probes', {
|
|
321
|
+
type: 'FeatureCollection',
|
|
322
|
+
features: PROBES.map((probe) => ({
|
|
323
|
+
type: 'Feature',
|
|
324
|
+
properties: {name: probe.name},
|
|
325
|
+
geometry: {type: 'Point', coordinates: [probe.longitude, probe.latitude]}
|
|
326
|
+
}))
|
|
327
|
+
});
|
|
328
|
+
map.addLayer({
|
|
329
|
+
id: id('probe-points'),
|
|
330
|
+
type: 'circle',
|
|
331
|
+
source: id('probes'),
|
|
332
|
+
minzoom: 6,
|
|
333
|
+
paint: {
|
|
334
|
+
'circle-radius': 6,
|
|
335
|
+
'circle-color': '#ffffff',
|
|
336
|
+
'circle-stroke-width': 2,
|
|
337
|
+
'circle-stroke-color': '#153c58'
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
addGeo('locator', {
|
|
341
|
+
type: 'FeatureCollection',
|
|
342
|
+
features: [{
|
|
343
|
+
type: 'Feature',
|
|
344
|
+
properties: {},
|
|
345
|
+
geometry: {type: 'Point', coordinates: [126.75, 37.75]}
|
|
346
|
+
}]
|
|
347
|
+
});
|
|
348
|
+
map.addLayer({
|
|
349
|
+
id: id('world-location'),
|
|
350
|
+
type: 'circle',
|
|
351
|
+
source: id('locator'),
|
|
352
|
+
maxzoom: 6,
|
|
353
|
+
paint: {
|
|
354
|
+
'circle-radius': 7,
|
|
355
|
+
'circle-color': '#ed7353',
|
|
356
|
+
'circle-stroke-color': '#ffffff',
|
|
357
|
+
'circle-stroke-width': 2
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
// MapLibre 5 does not upload an idle CanvasSource until it is explicitly played once.
|
|
361
|
+
refreshCanvas();
|
|
362
|
+
} catch (error) {
|
|
363
|
+
map.off('error', onMapError);
|
|
364
|
+
controller.destroy();
|
|
365
|
+
canvas.remove();
|
|
366
|
+
throw error;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const updateSelection = (): void => {
|
|
370
|
+
(map.getSource(id('selection')) as GeoJSONSource).setData(selectionGeo(selected()));
|
|
371
|
+
(map.getSource(id('samples')) as GeoJSONSource).setData(samplesGeo(selected()));
|
|
372
|
+
};
|
|
373
|
+
const focus = (mode: 'world' | 'region' | 'cell'): void => {
|
|
374
|
+
if (destroyed) return;
|
|
375
|
+
const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
376
|
+
if (mode === 'world') {
|
|
377
|
+
map.flyTo({center: [25, 20], zoom: 1.5, duration: reduced ? 0 : 700});
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const target = mode === 'cell' ? selected().bounds : bounds;
|
|
381
|
+
map.fitBounds(
|
|
382
|
+
[[target.west, target.south], [target.east, target.north]],
|
|
383
|
+
{padding: 55, duration: reduced ? 0 : 600, maxZoom: 12}
|
|
384
|
+
);
|
|
385
|
+
};
|
|
386
|
+
const onClick = (event: MapMouseEvent): void => {
|
|
387
|
+
if (destroyed) return;
|
|
388
|
+
if (map.getZoom() < 6) {
|
|
389
|
+
const hits = map.queryRenderedFeatures(event.point, {layers: [id('world-location')]});
|
|
390
|
+
if (hits.length) {
|
|
391
|
+
focus('cell');
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
const longitude = event.lngLat.lng;
|
|
396
|
+
const latitude = event.lngLat.lat;
|
|
397
|
+
const cell = current.cells.find((candidate) => contains(candidate.bounds, longitude, latitude));
|
|
398
|
+
if (cell) {
|
|
399
|
+
selectedId = cell.id;
|
|
400
|
+
updateSelection();
|
|
401
|
+
}
|
|
402
|
+
options.onPick?.({
|
|
403
|
+
longitude,
|
|
404
|
+
latitude,
|
|
405
|
+
value: sampleFrame(current, longitude, latitude),
|
|
406
|
+
cellId: cell?.id ?? null
|
|
407
|
+
});
|
|
408
|
+
};
|
|
409
|
+
map.on('click', onClick);
|
|
410
|
+
|
|
411
|
+
const host = typeof container === 'string'
|
|
412
|
+
? document.querySelector<HTMLElement>(container)
|
|
413
|
+
: container;
|
|
414
|
+
const observer = new ResizeObserver(() => {
|
|
415
|
+
if (!destroyed) controller.resize();
|
|
416
|
+
});
|
|
417
|
+
if (host) observer.observe(host);
|
|
418
|
+
|
|
419
|
+
const apply = (frame: FieldFrame): void => {
|
|
420
|
+
current = frame;
|
|
421
|
+
paintRaster(canvas, current, bounds, lowValueBackgroundVisible);
|
|
422
|
+
refreshCanvas();
|
|
423
|
+
updateSelection();
|
|
424
|
+
options.onFrame?.(current);
|
|
425
|
+
};
|
|
426
|
+
const setFrame = (next: FieldFrame, animate = true): void => {
|
|
427
|
+
if (destroyed) throw new Error('Demo has been destroyed.');
|
|
428
|
+
validateFrame(next);
|
|
429
|
+
// Validate compatibility before interrupting an active transition.
|
|
430
|
+
mixFrames(current, next, 0);
|
|
431
|
+
cancelAnimationFrame(raf);
|
|
432
|
+
const epoch = ++animationEpoch;
|
|
433
|
+
const previous = current;
|
|
434
|
+
if (!animate || window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
435
|
+
apply(next);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
const started = performance.now();
|
|
439
|
+
let lastPaint = -Infinity;
|
|
440
|
+
const tick = (now: number): void => {
|
|
441
|
+
if (destroyed || epoch !== animationEpoch) return;
|
|
442
|
+
const progress = Math.min(1, (now - started) / 700);
|
|
443
|
+
// Repaint at roughly 10 Hz; the idle canvas has no permanent animation loop.
|
|
444
|
+
if (now - lastPaint >= 100 || progress === 1) {
|
|
445
|
+
apply(mixFrames(previous, next, progress));
|
|
446
|
+
lastPaint = now;
|
|
447
|
+
}
|
|
448
|
+
if (progress < 1) raf = requestAnimationFrame(tick);
|
|
449
|
+
};
|
|
450
|
+
raf = requestAnimationFrame(tick);
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
focus('cell');
|
|
454
|
+
options.onFrame?.(current);
|
|
455
|
+
|
|
456
|
+
return {
|
|
457
|
+
setFrame,
|
|
458
|
+
setGridVisible(visible) {
|
|
459
|
+
if (destroyed) return;
|
|
460
|
+
gridVisible = visible;
|
|
461
|
+
// Grid visibility never mutates the raster, probes, or selected cell layers.
|
|
462
|
+
map.setLayoutProperty(id('grid-lines'), 'visibility', visible ? 'visible' : 'none');
|
|
463
|
+
},
|
|
464
|
+
setSamplesVisible(visible) {
|
|
465
|
+
if (destroyed) return;
|
|
466
|
+
samplesVisible = visible;
|
|
467
|
+
map.setLayoutProperty(id('sample-points'), 'visibility', visible ? 'visible' : 'none');
|
|
468
|
+
},
|
|
469
|
+
setCellOutlineVisible(visible) {
|
|
470
|
+
if (destroyed) return;
|
|
471
|
+
cellOutlineVisible = visible;
|
|
472
|
+
map.setLayoutProperty(id('selected-line'), 'visibility', visible ? 'visible' : 'none');
|
|
473
|
+
},
|
|
474
|
+
setLowValueBackgroundVisible(visible) {
|
|
475
|
+
if (destroyed) return;
|
|
476
|
+
lowValueBackgroundVisible = visible;
|
|
477
|
+
paintRaster(canvas, current, bounds, lowValueBackgroundVisible);
|
|
478
|
+
refreshCanvas();
|
|
479
|
+
},
|
|
480
|
+
focus,
|
|
481
|
+
getState: () => ({
|
|
482
|
+
frame: current,
|
|
483
|
+
selectedId,
|
|
484
|
+
showGrid: gridVisible,
|
|
485
|
+
showSamples: samplesVisible,
|
|
486
|
+
showCellOutline: cellOutlineVisible,
|
|
487
|
+
showLowValueBackground: lowValueBackgroundVisible
|
|
488
|
+
}),
|
|
489
|
+
getMap: () => map,
|
|
490
|
+
destroy() {
|
|
491
|
+
if (destroyed) return;
|
|
492
|
+
destroyed = true;
|
|
493
|
+
animationEpoch += 1;
|
|
494
|
+
cancelAnimationFrame(raf);
|
|
495
|
+
window.clearTimeout(pauseTimer);
|
|
496
|
+
observer.disconnect();
|
|
497
|
+
map.off('click', onClick);
|
|
498
|
+
map.off('error', onMapError);
|
|
499
|
+
(map.getSource(id('raster')) as CanvasSource | undefined)?.pause();
|
|
500
|
+
// The adapter owns the map and releases every custom source/layer with it.
|
|
501
|
+
controller.destroy();
|
|
502
|
+
canvas.remove();
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {createMapLibreFlatMap} from '@keneth80/k-chart-maplibre';
|
|
2
|
+
import type {KChartMapLibrePlace} from '@keneth80/k-chart-maplibre';
|
|
3
|
+
import {
|
|
4
|
+
createSubcellHeatmapDemoWithAdapter,
|
|
5
|
+
type HeatmapOptions,
|
|
6
|
+
type SubcellHeatmapController
|
|
7
|
+
} from './heatmap-demo-core.js';
|
|
8
|
+
import '@keneth80/k-chart-maplibre/style.css';
|
|
9
|
+
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
10
|
+
|
|
11
|
+
/** Published-package entry used by the standalone and StackBlitz example. */
|
|
12
|
+
export function createSubcellHeatmapDemo(
|
|
13
|
+
container: string | HTMLElement,
|
|
14
|
+
options: HeatmapOptions = {}
|
|
15
|
+
): Promise<SubcellHeatmapController> {
|
|
16
|
+
return createSubcellHeatmapDemoWithAdapter(
|
|
17
|
+
container,
|
|
18
|
+
(configuration) => createMapLibreFlatMap<KChartMapLibrePlace>(configuration),
|
|
19
|
+
options
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type {HeatmapOptions, SubcellHeatmapController};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {mountSubcellHeatmapApp} from './app.js';
|
|
2
|
+
import {createSubcellHeatmapDemo} from './heatmap-demo.js';
|
|
3
|
+
import './style.css';
|
|
4
|
+
|
|
5
|
+
async function main(): Promise<void> {
|
|
6
|
+
const host = document.querySelector<HTMLElement>('#app');
|
|
7
|
+
if (!host) throw new Error('Missing #app host.');
|
|
8
|
+
const mounted = await mountSubcellHeatmapApp(host, createSubcellHeatmapDemo);
|
|
9
|
+
window.addEventListener('pagehide', () => mounted.destroy(), {once: true});
|
|
10
|
+
(window as typeof window & {subcellDemo?: typeof mounted}).subcellDemo = mounted;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
void main().catch((error) => {
|
|
14
|
+
console.error('[KChart subcell heat field]', error);
|
|
15
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as maplibregl from 'maplibre-gl/dist/maplibre-gl.mjs';
|
|
2
|
+
|
|
3
|
+
// Adapter 0.2.0 expects the MapLibre namespace as a CommonJS default export.
|
|
4
|
+
// Pin the matching module worker because adapter 0.2.0's CommonJS bundle hides import.meta.url.
|
|
5
|
+
maplibregl.setWorkerUrl('https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl-worker.mjs');
|
|
6
|
+
export default maplibregl;
|