@opendata-ai/openchart-engine 8.0.0-rc.8 → 8.0.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/dist/index.d.ts +149 -12
- package/dist/index.js +894 -171
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/__test-fixtures__/specs.ts +0 -182
- package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +0 -2339
- package/src/__tests__/annotation-behavior.test.ts +0 -852
- package/src/__tests__/axes.test.ts +0 -1579
- package/src/__tests__/compile-animation.test.ts +0 -161
- package/src/__tests__/compile-chart.test.ts +0 -1303
- package/src/__tests__/compile-footnotes.test.ts +0 -277
- package/src/__tests__/compile-layer.test.ts +0 -969
- package/src/__tests__/compile-snapshot.test.ts +0 -156
- package/src/__tests__/compound-labels.test.ts +0 -147
- package/src/__tests__/dimensions.test.ts +0 -918
- package/src/__tests__/encoding-sugar.test.ts +0 -390
- package/src/__tests__/facet.test.ts +0 -215
- package/src/__tests__/helpers/invariants.ts +0 -299
- package/src/__tests__/known-bugs.test.ts +0 -566
- package/src/__tests__/layout-fuzz.test.ts +0 -358
- package/src/__tests__/layout-invariants.test.ts +0 -535
- package/src/__tests__/legend.test.ts +0 -721
- package/src/__tests__/mark-keys.test.ts +0 -294
- package/src/__tests__/ranking-recipes.test.ts +0 -260
- package/src/__tests__/rotation.test.ts +0 -83
- package/src/__tests__/scales.test.ts +0 -323
- package/src/__tests__/series-search.test.ts +0 -92
- package/src/__tests__/spec-sugar.test.ts +0 -801
- package/src/__tests__/you-draw-it.test.ts +0 -160
- package/src/annotations/__tests__/compute.test.ts +0 -1672
- package/src/annotations/__tests__/geometry.test.ts +0 -66
- package/src/annotations/__tests__/placement-fuzz.test.ts +0 -256
- package/src/annotations/__tests__/placement-invariants.test.ts +0 -345
- package/src/annotations/__tests__/placement.test.ts +0 -316
- package/src/annotations/__tests__/resolve-text.test.ts +0 -788
- package/src/annotations/__tests__/rich-text.test.ts +0 -91
- package/src/annotations/__tests__/thinning.test.ts +0 -285
- package/src/annotations/collisions.ts +0 -326
- package/src/annotations/compute.ts +0 -333
- package/src/annotations/constants.ts +0 -162
- package/src/annotations/geometry.ts +0 -376
- package/src/annotations/move.ts +0 -135
- package/src/annotations/placement.ts +0 -636
- package/src/annotations/position.ts +0 -131
- package/src/annotations/resolve-range.ts +0 -117
- package/src/annotations/resolve-refline.ts +0 -157
- package/src/annotations/resolve-text.ts +0 -545
- package/src/annotations/rich-text.ts +0 -65
- package/src/annotations/thinning.ts +0 -190
- package/src/barlist/__tests__/compile-barlist.test.ts +0 -200
- package/src/barlist/compile-barlist.ts +0 -391
- package/src/barlist/types.ts +0 -28
- package/src/charts/__tests__/post-process.test.ts +0 -237
- package/src/charts/__tests__/registry.test.ts +0 -116
- package/src/charts/__tests__/utils.test.ts +0 -195
- package/src/charts/_shared/__tests__/density-filter.test.ts +0 -32
- package/src/charts/_shared/density-filter.ts +0 -26
- package/src/charts/_shared/format-label-value.ts +0 -16
- package/src/charts/bar/__tests__/compute.test.ts +0 -934
- package/src/charts/bar/__tests__/gradient-orient.test.ts +0 -127
- package/src/charts/bar/__tests__/labels.test.ts +0 -292
- package/src/charts/bar/compute.ts +0 -539
- package/src/charts/bar/index.ts +0 -60
- package/src/charts/bar/labels.ts +0 -311
- package/src/charts/beeswarm/__tests__/compute.test.ts +0 -280
- package/src/charts/beeswarm/__tests__/dodge.test.ts +0 -148
- package/src/charts/beeswarm/compute.ts +0 -203
- package/src/charts/beeswarm/dodge.ts +0 -155
- package/src/charts/beeswarm/index.ts +0 -32
- package/src/charts/builtin.ts +0 -75
- package/src/charts/calendar/__tests__/compute.test.ts +0 -417
- package/src/charts/calendar/compute.ts +0 -319
- package/src/charts/calendar/index.ts +0 -32
- package/src/charts/column/__tests__/compute.test.ts +0 -540
- package/src/charts/column/__tests__/labels.test.ts +0 -179
- package/src/charts/column/compute.ts +0 -493
- package/src/charts/column/index.ts +0 -53
- package/src/charts/column/labels.ts +0 -229
- package/src/charts/dot/__tests__/compute.test.ts +0 -380
- package/src/charts/dot/__tests__/labels.test.ts +0 -98
- package/src/charts/dot/compute.ts +0 -285
- package/src/charts/dot/index.ts +0 -62
- package/src/charts/dot/labels.ts +0 -121
- package/src/charts/line/__tests__/compute.test.ts +0 -1194
- package/src/charts/line/__tests__/curves.test.ts +0 -51
- package/src/charts/line/__tests__/labels.test.ts +0 -94
- package/src/charts/line/__tests__/mark-options.test.ts +0 -175
- package/src/charts/line/area.ts +0 -484
- package/src/charts/line/compute.ts +0 -356
- package/src/charts/line/curves.ts +0 -85
- package/src/charts/line/index.ts +0 -176
- package/src/charts/line/labels.ts +0 -177
- package/src/charts/parliament/__tests__/compute.test.ts +0 -411
- package/src/charts/parliament/compute.ts +0 -313
- package/src/charts/parliament/index.ts +0 -32
- package/src/charts/pie/__tests__/compute.test.ts +0 -384
- package/src/charts/pie/__tests__/labels.test.ts +0 -132
- package/src/charts/pie/compute.ts +0 -324
- package/src/charts/pie/index.ts +0 -49
- package/src/charts/pie/labels.ts +0 -143
- package/src/charts/post-process.ts +0 -229
- package/src/charts/range/__tests__/compute.test.ts +0 -486
- package/src/charts/range/compute.ts +0 -273
- package/src/charts/range/index.ts +0 -82
- package/src/charts/range/labels.ts +0 -169
- package/src/charts/rect/__tests__/compute.test.ts +0 -120
- package/src/charts/rect/index.ts +0 -127
- package/src/charts/registry.ts +0 -72
- package/src/charts/rule/index.ts +0 -139
- package/src/charts/scatter/__tests__/compute.test.ts +0 -488
- package/src/charts/scatter/__tests__/trendline.test.ts +0 -191
- package/src/charts/scatter/compute.ts +0 -192
- package/src/charts/scatter/index.ts +0 -46
- package/src/charts/scatter/trendline.ts +0 -100
- package/src/charts/text/__tests__/compute.test.ts +0 -307
- package/src/charts/text/index.ts +0 -141
- package/src/charts/tick/__tests__/compute.test.ts +0 -75
- package/src/charts/tick/index.ts +0 -99
- package/src/charts/utils.ts +0 -170
- package/src/charts/waffle/__tests__/compute.test.ts +0 -305
- package/src/charts/waffle/compute.ts +0 -197
- package/src/charts/waffle/index.ts +0 -31
- package/src/compile/__tests__/color-scale-range.test.ts +0 -158
- package/src/compile/__tests__/color-scale-reverse.test.ts +0 -89
- package/src/compile/__tests__/data-clip.test.ts +0 -127
- package/src/compile/__tests__/fill-patterns.test.ts +0 -154
- package/src/compile/__tests__/size-scale.test.ts +0 -108
- package/src/compile/__tests__/watermark-obstacle.test.ts +0 -93
- package/src/compile/color-scale-range.ts +0 -101
- package/src/compile/data-clip.ts +0 -48
- package/src/compile/fill-patterns.ts +0 -74
- package/src/compile/layer.ts +0 -626
- package/src/compile/size-scale.ts +0 -141
- package/src/compile/spec-sugar.ts +0 -758
- package/src/compile/watermark-obstacle.ts +0 -54
- package/src/compile/you-draw-it.ts +0 -123
- package/src/compile.ts +0 -1450
- package/src/compiler/__tests__/a11y-warnings.test.ts +0 -148
- package/src/compiler/__tests__/animation.test.ts +0 -177
- package/src/compiler/__tests__/compile.test.ts +0 -87
- package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +0 -20
- package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +0 -22
- package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +0 -14
- package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +0 -34
- package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +0 -22
- package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +0 -41
- package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +0 -38
- package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +0 -28
- package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +0 -28
- package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +0 -30
- package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +0 -29
- package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +0 -32
- package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +0 -25
- package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +0 -21
- package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +0 -23
- package/src/compiler/__tests__/keys.test.ts +0 -83
- package/src/compiler/__tests__/normalize-highlight.test.ts +0 -103
- package/src/compiler/__tests__/normalize.test.ts +0 -399
- package/src/compiler/__tests__/sparkline-defaults.test.ts +0 -153
- package/src/compiler/__tests__/validate-did-you-mean.test.ts +0 -170
- package/src/compiler/__tests__/validate-highlight.test.ts +0 -112
- package/src/compiler/__tests__/validate-near-miss.test.ts +0 -251
- package/src/compiler/__tests__/validate.test.ts +0 -629
- package/src/compiler/__tests__/vl-parity.test.ts +0 -68
- package/src/compiler/a11y-warnings.ts +0 -110
- package/src/compiler/animation.ts +0 -142
- package/src/compiler/index.ts +0 -47
- package/src/compiler/keys.ts +0 -55
- package/src/compiler/normalize.ts +0 -734
- package/src/compiler/sparkline-defaults.ts +0 -138
- package/src/compiler/types.ts +0 -241
- package/src/compiler/validate.ts +0 -2041
- package/src/endpoint-labels/__tests__/compute.test.ts +0 -587
- package/src/endpoint-labels/compute.ts +0 -621
- package/src/endpoint-labels/constants.ts +0 -54
- package/src/endpoint-labels/format.ts +0 -60
- package/src/endpoint-labels/predict.ts +0 -139
- package/src/format/__tests__/field-format.test.ts +0 -164
- package/src/format/field-format.ts +0 -27
- package/src/geo/__tests__/compile-map.test.ts +0 -1004
- package/src/geo/compile-map.ts +0 -1341
- package/src/geo/join.ts +0 -115
- package/src/geo/projections.ts +0 -45
- package/src/geo/types.ts +0 -26
- package/src/graphs/__tests__/community.test.ts +0 -228
- package/src/graphs/__tests__/compile-graph.test.ts +0 -396
- package/src/graphs/__tests__/encoding.test.ts +0 -415
- package/src/graphs/community.ts +0 -92
- package/src/graphs/compile-graph.ts +0 -329
- package/src/graphs/encoding.ts +0 -356
- package/src/graphs/types.ts +0 -106
- package/src/index.ts +0 -177
- package/src/layout/__tests__/metrics.test.ts +0 -97
- package/src/layout/__tests__/scales-highlight.test.ts +0 -192
- package/src/layout/axes/rotation.ts +0 -152
- package/src/layout/axes/thinning.ts +0 -98
- package/src/layout/axes/ticks.ts +0 -409
- package/src/layout/axes.ts +0 -757
- package/src/layout/dimensions.ts +0 -912
- package/src/layout/facet.ts +0 -91
- package/src/layout/gridlines.ts +0 -84
- package/src/layout/metrics.ts +0 -132
- package/src/layout/plan.ts +0 -631
- package/src/layout/scales.ts +0 -1053
- package/src/layout/shared.ts +0 -44
- package/src/legend/__tests__/continuous.test.ts +0 -318
- package/src/legend/__tests__/redundancy.test.ts +0 -108
- package/src/legend/__tests__/size.test.ts +0 -348
- package/src/legend/__tests__/suppression.test.ts +0 -382
- package/src/legend/compute.ts +0 -747
- package/src/legend/continuous.ts +0 -327
- package/src/legend/size.ts +0 -407
- package/src/legend/suppression.ts +0 -226
- package/src/legend/wrap.ts +0 -117
- package/src/sankey/__tests__/compile-sankey.test.ts +0 -458
- package/src/sankey/__tests__/layout.test.ts +0 -165
- package/src/sankey/__tests__/node-label-wrap.test.ts +0 -114
- package/src/sankey/__tests__/node-sort.test.ts +0 -45
- package/src/sankey/compile-sankey.ts +0 -706
- package/src/sankey/layout.ts +0 -184
- package/src/sankey/types.ts +0 -41
- package/src/tables/__tests__/bar-column.test.ts +0 -147
- package/src/tables/__tests__/category-colors.test.ts +0 -230
- package/src/tables/__tests__/compile-table.test.ts +0 -236
- package/src/tables/__tests__/format-cells.test.ts +0 -143
- package/src/tables/__tests__/heatmap.test.ts +0 -148
- package/src/tables/__tests__/pagination.test.ts +0 -78
- package/src/tables/__tests__/search.test.ts +0 -94
- package/src/tables/__tests__/sort.test.ts +0 -107
- package/src/tables/__tests__/sparkline.test.ts +0 -122
- package/src/tables/bar-column.ts +0 -94
- package/src/tables/category-colors.ts +0 -78
- package/src/tables/compile-table.ts +0 -427
- package/src/tables/format-cells.ts +0 -115
- package/src/tables/heatmap.ts +0 -141
- package/src/tables/pagination.ts +0 -46
- package/src/tables/search.ts +0 -66
- package/src/tables/sort.ts +0 -69
- package/src/tables/sparkline.ts +0 -113
- package/src/tables/utils.ts +0 -16
- package/src/tilemap/__tests__/compile-tilemap.test.ts +0 -576
- package/src/tilemap/compile-tilemap.ts +0 -755
- package/src/tilemap/layout.ts +0 -172
- package/src/tilemap/types.ts +0 -33
- package/src/tooltips/__tests__/compute.test.ts +0 -582
- package/src/tooltips/compute.ts +0 -700
- package/src/transforms/__tests__/aggregate.test.ts +0 -159
- package/src/transforms/__tests__/bin.test.ts +0 -88
- package/src/transforms/__tests__/calculate.test.ts +0 -146
- package/src/transforms/__tests__/conditional.test.ts +0 -127
- package/src/transforms/__tests__/filter-relative.test.ts +0 -202
- package/src/transforms/__tests__/filter.test.ts +0 -59
- package/src/transforms/__tests__/fold.test.ts +0 -79
- package/src/transforms/__tests__/index.test.ts +0 -93
- package/src/transforms/__tests__/predicates.test.ts +0 -193
- package/src/transforms/__tests__/timeunit.test.ts +0 -129
- package/src/transforms/__tests__/window.test.ts +0 -286
- package/src/transforms/aggregate.ts +0 -131
- package/src/transforms/bin.ts +0 -87
- package/src/transforms/calculate.ts +0 -60
- package/src/transforms/conditional.ts +0 -46
- package/src/transforms/filter.ts +0 -122
- package/src/transforms/fold.ts +0 -49
- package/src/transforms/index.ts +0 -60
- package/src/transforms/predicates.ts +0 -123
- package/src/transforms/timeunit.ts +0 -88
- package/src/transforms/window.ts +0 -185
package/src/geo/compile-map.ts
DELETED
|
@@ -1,1341 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CategoricalLegendLayout,
|
|
3
|
-
CompileOptions,
|
|
4
|
-
CompileWarning,
|
|
5
|
-
ContinuousLegendLayout,
|
|
6
|
-
EncodingChannel,
|
|
7
|
-
LegendEntry,
|
|
8
|
-
MapBorders,
|
|
9
|
-
MapFeatureMark,
|
|
10
|
-
MapFocus,
|
|
11
|
-
MapFocusLayout,
|
|
12
|
-
MapLayout,
|
|
13
|
-
MapPointMark,
|
|
14
|
-
ResolvedAnimation,
|
|
15
|
-
ResolvedTheme,
|
|
16
|
-
TextStyle,
|
|
17
|
-
TooltipContent,
|
|
18
|
-
TooltipField,
|
|
19
|
-
} from '@opendata-ai/openchart-core';
|
|
20
|
-
import {
|
|
21
|
-
adaptTheme,
|
|
22
|
-
buildD3Formatter,
|
|
23
|
-
CATEGORICAL_PALETTE,
|
|
24
|
-
computeChrome,
|
|
25
|
-
estimateTextWidth,
|
|
26
|
-
formatNumber,
|
|
27
|
-
getBreakpoint,
|
|
28
|
-
HPAD_COMPACT_FRACTION,
|
|
29
|
-
HPAD_COMPACT_MIN,
|
|
30
|
-
resolveTheme,
|
|
31
|
-
SEQUENTIAL_PALETTES,
|
|
32
|
-
} from '@opendata-ai/openchart-core';
|
|
33
|
-
import { geoArea, geoPath } from 'd3-geo';
|
|
34
|
-
import { scaleQuantile } from 'd3-scale';
|
|
35
|
-
import { feature as topoFeature, mesh as topoMesh } from 'topojson-client';
|
|
36
|
-
import type { GeometryCollection, Topology } from 'topojson-specification';
|
|
37
|
-
import { buildSizeScale, SIZE_SCALE_DEFAULTS } from '../compile/size-scale';
|
|
38
|
-
import { emitSpecWarnings, expandSpecSugar } from '../compile/spec-sugar';
|
|
39
|
-
import { resolveAnimation } from '../compiler/animation';
|
|
40
|
-
import { compile as compileSpec } from '../compiler/index';
|
|
41
|
-
import {
|
|
42
|
-
CONTINUOUS_BAR_HEIGHT,
|
|
43
|
-
CONTINUOUS_LABEL_GAP,
|
|
44
|
-
computeContinuousLegendContentForChannel,
|
|
45
|
-
} from '../legend/continuous';
|
|
46
|
-
import { joinDataToFeatures } from './join';
|
|
47
|
-
import { createProjection } from './projections';
|
|
48
|
-
import type { NormalizedMapSpec } from './types';
|
|
49
|
-
|
|
50
|
-
function validateGeoFeatures(geo: NormalizedMapSpec['geo']): Topology {
|
|
51
|
-
if (!geo.features) {
|
|
52
|
-
throw new Error(
|
|
53
|
-
'Map spec error: geo.features is required but missing.\n\n' +
|
|
54
|
-
'The map needs a TopoJSON topology object as geo.features. ' +
|
|
55
|
-
'Install a standard atlas package and import it:\n\n' +
|
|
56
|
-
' npm i us-atlas\n' +
|
|
57
|
-
' import us from "us-atlas/states-albers-10m.json";\n\n' +
|
|
58
|
-
' // or for world maps:\n' +
|
|
59
|
-
' npm i world-atlas\n' +
|
|
60
|
-
' import world from "world-atlas/countries-110m.json";\n\n' +
|
|
61
|
-
'Then pass it as geo.features:\n\n' +
|
|
62
|
-
' { type: "map", geo: { features: us }, data: [...], encoding: { ... } }',
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const topo = geo.features as Record<string, unknown>;
|
|
67
|
-
if (typeof topo !== 'object' || topo === null || topo.type !== 'Topology' || !topo.objects) {
|
|
68
|
-
throw new Error(
|
|
69
|
-
'Map spec error: geo.features must be a valid TopoJSON Topology ' +
|
|
70
|
-
'(expected an object with type: "Topology" and an "objects" property).\n\n' +
|
|
71
|
-
'Make sure you are passing the full TopoJSON file, not a GeoJSON FeatureCollection. ' +
|
|
72
|
-
'Atlas packages like us-atlas and world-atlas export TopoJSON by default:\n\n' +
|
|
73
|
-
' npm i us-atlas\n' +
|
|
74
|
-
' import us from "us-atlas/states-albers-10m.json";\n' +
|
|
75
|
-
' { type: "map", geo: { features: us }, ... }',
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const objects = topo.objects as Record<string, unknown>;
|
|
80
|
-
if (Object.keys(objects).length === 0) {
|
|
81
|
-
throw new Error(
|
|
82
|
-
'Map spec error: geo.features.objects is empty (no geometry collections found). ' +
|
|
83
|
-
'The TopoJSON file must contain at least one named geometry collection.',
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return topo as unknown as Topology;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function compileMap(spec: unknown, options: CompileOptions): MapLayout {
|
|
91
|
-
// 1. Sugar expansion + validate + normalize
|
|
92
|
-
const sugarWarnings: string[] = [];
|
|
93
|
-
const expandedSpec =
|
|
94
|
-
spec && typeof spec === 'object' && !Array.isArray(spec)
|
|
95
|
-
? expandSpecSugar(spec as Record<string, unknown>, sugarWarnings)
|
|
96
|
-
: spec;
|
|
97
|
-
const { spec: normalized, warnings } = compileSpec(expandedSpec);
|
|
98
|
-
emitSpecWarnings([...sugarWarnings, ...warnings], options.onWarn);
|
|
99
|
-
|
|
100
|
-
if (!('type' in normalized) || normalized.type !== 'map') {
|
|
101
|
-
throw new Error(
|
|
102
|
-
'compileMap received a non-map spec. Use compileChart, compileTileMap, compileSankey, or compileBarList instead.',
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const mapSpec = normalized as NormalizedMapSpec;
|
|
107
|
-
|
|
108
|
-
// 2. Validate geo features
|
|
109
|
-
const topology = validateGeoFeatures(mapSpec.geo);
|
|
110
|
-
|
|
111
|
-
// Resolve watermark
|
|
112
|
-
const rawWatermark = (spec as Record<string, unknown>).watermark;
|
|
113
|
-
const watermark = rawWatermark !== undefined ? mapSpec.watermark : (options.watermark ?? true);
|
|
114
|
-
|
|
115
|
-
// 3. Resolve theme
|
|
116
|
-
const mergedThemeConfig = options.theme ? { ...mapSpec.theme, ...options.theme } : mapSpec.theme;
|
|
117
|
-
const lightTheme: ResolvedTheme = resolveTheme(mergedThemeConfig);
|
|
118
|
-
let theme: ResolvedTheme = lightTheme;
|
|
119
|
-
if (options.darkMode) {
|
|
120
|
-
theme = adaptTheme(theme);
|
|
121
|
-
}
|
|
122
|
-
const isDarkMode = options.darkMode;
|
|
123
|
-
|
|
124
|
-
// 4. Chrome
|
|
125
|
-
const chrome = computeChrome(
|
|
126
|
-
{
|
|
127
|
-
title: mapSpec.chrome.title,
|
|
128
|
-
subtitle: mapSpec.chrome.subtitle,
|
|
129
|
-
source: mapSpec.chrome.source,
|
|
130
|
-
byline: mapSpec.chrome.byline,
|
|
131
|
-
footer: mapSpec.chrome.footer,
|
|
132
|
-
},
|
|
133
|
-
theme,
|
|
134
|
-
options.width,
|
|
135
|
-
options.measureText,
|
|
136
|
-
'full',
|
|
137
|
-
undefined,
|
|
138
|
-
watermark,
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
// 5. Drawing area
|
|
142
|
-
const padding =
|
|
143
|
-
getBreakpoint(options.width) === 'compact'
|
|
144
|
-
? Math.max(HPAD_COMPACT_MIN, Math.round(theme.spacing.padding * HPAD_COMPACT_FRACTION))
|
|
145
|
-
: theme.spacing.padding;
|
|
146
|
-
const fullArea = {
|
|
147
|
-
x: padding,
|
|
148
|
-
y: padding + chrome.topHeight,
|
|
149
|
-
width: options.width - padding * 2,
|
|
150
|
-
height: options.height - chrome.topHeight - chrome.bottomHeight - padding * 2,
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
if (fullArea.width <= 0 || fullArea.height <= 0) {
|
|
154
|
-
return emptyLayout(chrome, theme, options, watermark);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// 6. Parse TopoJSON -> GeoJSON
|
|
158
|
-
const objectKey = Object.keys(topology.objects)[0];
|
|
159
|
-
const geoCollection = topoFeature(topology, topology.objects[objectKey] as GeometryCollection);
|
|
160
|
-
const geoFeatures = geoCollection.features;
|
|
161
|
-
|
|
162
|
-
// 7. Compile warnings
|
|
163
|
-
const compileWarnings: CompileWarning[] = [];
|
|
164
|
-
|
|
165
|
-
// 8. Legend height reserve
|
|
166
|
-
const showLegend = mapSpec.legend?.show !== false;
|
|
167
|
-
const colorEncoding = mapSpec.encoding.color;
|
|
168
|
-
const isQuantitative = colorEncoding?.type === 'quantitative';
|
|
169
|
-
const legendPosition = mapSpec.legend?.position === 'bottom' ? 'bottom' : 'top';
|
|
170
|
-
|
|
171
|
-
// Compute legend block height. For quantitative maps, use the continuous
|
|
172
|
-
// legend infrastructure; for categorical, estimate a single swatch row.
|
|
173
|
-
let legendBlockHeight = 0;
|
|
174
|
-
let continuousContent: ReturnType<typeof computeContinuousLegendContentForChannel> = null;
|
|
175
|
-
if (showLegend && colorEncoding && isQuantitative) {
|
|
176
|
-
const colorValues: number[] = [];
|
|
177
|
-
for (const row of mapSpec.data) {
|
|
178
|
-
const v = row[colorEncoding.field];
|
|
179
|
-
if (v != null) {
|
|
180
|
-
const n = Number(v);
|
|
181
|
-
if (!Number.isNaN(n)) colorValues.push(n);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
// Maps always use a quantile color scale, so tell the legend infra
|
|
185
|
-
// to produce binned swatches rather than a gradient bar.
|
|
186
|
-
const legendChannel: EncodingChannel = {
|
|
187
|
-
...colorEncoding,
|
|
188
|
-
scale: { ...colorEncoding.scale, type: 'quantile' },
|
|
189
|
-
};
|
|
190
|
-
continuousContent = computeContinuousLegendContentForChannel(
|
|
191
|
-
colorValues,
|
|
192
|
-
legendChannel,
|
|
193
|
-
theme,
|
|
194
|
-
fullArea.width,
|
|
195
|
-
);
|
|
196
|
-
if (continuousContent) {
|
|
197
|
-
const labelRowHeight = Math.ceil(theme.fonts.sizes.small * 1.3);
|
|
198
|
-
legendBlockHeight = continuousContent.barHeight + CONTINUOUS_LABEL_GAP + labelRowHeight;
|
|
199
|
-
}
|
|
200
|
-
} else if (showLegend && colorEncoding) {
|
|
201
|
-
const labelHeight = Math.ceil(theme.fonts.sizes.small * 1.3);
|
|
202
|
-
legendBlockHeight = Math.max(10, labelHeight) + 6;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Reserve height for point color legend if applicable
|
|
206
|
-
if (showLegend && mapSpec.points?.color) {
|
|
207
|
-
if (mapSpec.points.color.type === 'quantitative') {
|
|
208
|
-
const labelRowHeight = Math.ceil(theme.fonts.sizes.small * 1.3);
|
|
209
|
-
legendBlockHeight += CONTINUOUS_BAR_HEIGHT + CONTINUOUS_LABEL_GAP + labelRowHeight + 8;
|
|
210
|
-
} else {
|
|
211
|
-
const ptLabelHeight = Math.ceil(theme.fonts.sizes.small * 1.3);
|
|
212
|
-
legendBlockHeight += Math.max(10, ptLabelHeight) + 6 + 8; // swatch row + gap
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const legendReserveGap = legendBlockHeight > 0 ? 8 : 0;
|
|
217
|
-
|
|
218
|
-
const mapAreaHeight = fullArea.height - legendBlockHeight - legendReserveGap;
|
|
219
|
-
if (mapAreaHeight <= 0) {
|
|
220
|
-
return emptyLayout(chrome, theme, options, watermark);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// 9. Projection + path generator
|
|
224
|
-
const projectionType = mapSpec.geo.projection;
|
|
225
|
-
const projection = createProjection(projectionType, fullArea.width, mapAreaHeight, geoCollection);
|
|
226
|
-
const pathGen = geoPath(projection);
|
|
227
|
-
|
|
228
|
-
// 10. Check winding order (skip for identity: geoArea uses spherical math
|
|
229
|
-
// and gives false positives on planar pre-projected coordinates)
|
|
230
|
-
if (projectionType !== 'identity') {
|
|
231
|
-
for (const feat of geoFeatures) {
|
|
232
|
-
const area = geoArea(feat);
|
|
233
|
-
if (area > 2 * Math.PI) {
|
|
234
|
-
const name =
|
|
235
|
-
((feat.properties as Record<string, unknown> | null)?.name as string) ??
|
|
236
|
-
((feat.properties as Record<string, unknown> | null)?.NAME as string) ??
|
|
237
|
-
String(feat.id);
|
|
238
|
-
compileWarnings.push({
|
|
239
|
-
code: 'INVERTED_WINDING',
|
|
240
|
-
message:
|
|
241
|
-
`Feature "${name}" has inverted winding order (geoArea > 2π). ` +
|
|
242
|
-
'This usually means the exterior ring is wound clockwise (RFC 7946) instead of ' +
|
|
243
|
-
'counter-clockwise (d3-geo convention). Fix with: npx topojson-rewind your-file.json',
|
|
244
|
-
context: { featureId: feat.id, featureName: name },
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// 11. Join data to features (skip when no key channel, e.g. basemap-only mode)
|
|
251
|
-
let joined = new Map<string | number, Record<string, unknown>>();
|
|
252
|
-
if (mapSpec.encoding.key) {
|
|
253
|
-
const featureObjs = geoFeatures.map((f) => ({
|
|
254
|
-
id: f.id as string | number,
|
|
255
|
-
properties: (f.properties ?? {}) as Record<string, unknown>,
|
|
256
|
-
}));
|
|
257
|
-
const joinResult = joinDataToFeatures(
|
|
258
|
-
featureObjs,
|
|
259
|
-
mapSpec.data,
|
|
260
|
-
mapSpec.encoding.key.field,
|
|
261
|
-
mapSpec.geo.idField,
|
|
262
|
-
);
|
|
263
|
-
joined = joinResult.joined;
|
|
264
|
-
// In basemap-only mode (empty data + points layer), all features are intentionally unmatched
|
|
265
|
-
const filteredJoinWarnings =
|
|
266
|
-
mapSpec.data.length === 0 && mapSpec.points
|
|
267
|
-
? joinResult.warnings.filter((w) => w.code !== 'UNMATCHED_FEATURES')
|
|
268
|
-
: joinResult.warnings;
|
|
269
|
-
compileWarnings.push(...filteredJoinWarnings);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// 12. Check for null-projecting features (albersUsa drops territories)
|
|
273
|
-
const droppedFeatures: string[] = [];
|
|
274
|
-
|
|
275
|
-
// 13. Build color scale + feature marks
|
|
276
|
-
const formatter = buildD3Formatter(mapSpec.valueFormat) ?? formatNumber;
|
|
277
|
-
const neutralFill = isDarkMode ? '#2a2a2a' : '#e8e8e8';
|
|
278
|
-
const neutralStroke = isDarkMode ? 'rgba(255,255,255,0.08)' : 'rgba(0,0,0,0.06)';
|
|
279
|
-
|
|
280
|
-
let featureMarks: MapFeatureMark[];
|
|
281
|
-
let continuousLegend: ContinuousLegendLayout | null = null;
|
|
282
|
-
let categoricalLegend: CategoricalLegendLayout | null = null;
|
|
283
|
-
|
|
284
|
-
if (!colorEncoding) {
|
|
285
|
-
// Basemap-only mode: all features get neutral fill, no legend
|
|
286
|
-
featureMarks = buildBasemapMarks({
|
|
287
|
-
geoFeatures,
|
|
288
|
-
pathGen,
|
|
289
|
-
neutralFill,
|
|
290
|
-
neutralStroke,
|
|
291
|
-
droppedFeatures,
|
|
292
|
-
});
|
|
293
|
-
} else if (isQuantitative) {
|
|
294
|
-
const result = buildQuantitativeMarks({
|
|
295
|
-
geoFeatures,
|
|
296
|
-
pathGen,
|
|
297
|
-
joined,
|
|
298
|
-
colorField: colorEncoding.field,
|
|
299
|
-
palette: colorEncoding.scale?.scheme ?? 'blue',
|
|
300
|
-
isDarkMode: !!isDarkMode,
|
|
301
|
-
neutralFill,
|
|
302
|
-
neutralStroke,
|
|
303
|
-
formatter,
|
|
304
|
-
droppedFeatures,
|
|
305
|
-
});
|
|
306
|
-
featureMarks = result.marks;
|
|
307
|
-
|
|
308
|
-
// Build the continuous legend. Maps place it directly (not via placeLegend,
|
|
309
|
-
// which does cartesian-specific positioning above the chart area).
|
|
310
|
-
if (showLegend && continuousContent) {
|
|
311
|
-
const labelStyle: TextStyle = {
|
|
312
|
-
fontFamily: theme.fonts.family,
|
|
313
|
-
fontSize: theme.fonts.sizes.small,
|
|
314
|
-
fontWeight: theme.fonts.weights.normal,
|
|
315
|
-
fill: theme.colors.text,
|
|
316
|
-
lineHeight: 1.3,
|
|
317
|
-
fontVariant: 'tabular-nums',
|
|
318
|
-
};
|
|
319
|
-
const legendX = fullArea.x;
|
|
320
|
-
const legendY =
|
|
321
|
-
legendPosition === 'bottom'
|
|
322
|
-
? fullArea.y + mapAreaHeight + legendReserveGap
|
|
323
|
-
: fullArea.y + legendReserveGap;
|
|
324
|
-
const bounds = {
|
|
325
|
-
x: legendX,
|
|
326
|
-
y: legendY,
|
|
327
|
-
width: continuousContent.barWidth,
|
|
328
|
-
height: legendBlockHeight,
|
|
329
|
-
};
|
|
330
|
-
const bar = {
|
|
331
|
-
x: bounds.x,
|
|
332
|
-
y: bounds.y,
|
|
333
|
-
width: continuousContent.barWidth,
|
|
334
|
-
height: continuousContent.barHeight,
|
|
335
|
-
};
|
|
336
|
-
continuousLegend = {
|
|
337
|
-
type: 'continuous' as const,
|
|
338
|
-
mode: continuousContent.mode,
|
|
339
|
-
position: legendPosition,
|
|
340
|
-
bounds,
|
|
341
|
-
labelStyle,
|
|
342
|
-
bar,
|
|
343
|
-
colorStops: continuousContent.colorStops,
|
|
344
|
-
bins: continuousContent.bins.map((b) => ({ ...b, x: b.x + bar.x })),
|
|
345
|
-
ticks: continuousContent.ticks.map((t) => ({ ...t, x: t.x + bar.x })),
|
|
346
|
-
labelY: bar.y + bar.height + CONTINUOUS_LABEL_GAP + labelStyle.fontSize,
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
} else {
|
|
350
|
-
const result = buildCategoricalMarks({
|
|
351
|
-
geoFeatures,
|
|
352
|
-
pathGen,
|
|
353
|
-
joined,
|
|
354
|
-
colorField: colorEncoding.field,
|
|
355
|
-
colorScale: colorEncoding.scale,
|
|
356
|
-
isDarkMode: !!isDarkMode,
|
|
357
|
-
neutralFill,
|
|
358
|
-
neutralStroke,
|
|
359
|
-
theme,
|
|
360
|
-
fullArea,
|
|
361
|
-
mapAreaHeight,
|
|
362
|
-
legendReserveGap,
|
|
363
|
-
legendPosition,
|
|
364
|
-
showLegend,
|
|
365
|
-
droppedFeatures,
|
|
366
|
-
});
|
|
367
|
-
featureMarks = result.marks;
|
|
368
|
-
categoricalLegend = result.legend;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// Report dropped features (null-projecting)
|
|
372
|
-
if (droppedFeatures.length > 0) {
|
|
373
|
-
compileWarnings.push({
|
|
374
|
-
code: 'NULL_PROJECTION',
|
|
375
|
-
message:
|
|
376
|
-
`${droppedFeatures.length} feature(s) project to null and were dropped: ${droppedFeatures.join(', ')}. ` +
|
|
377
|
-
'This is common with albersUsa which excludes territories (PR, GU, VI, AS, MP). ' +
|
|
378
|
-
'If your data includes these, consider using projection: "mercator" or "equalEarth".',
|
|
379
|
-
context: { features: droppedFeatures },
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
// 15. Borders via topojson mesh
|
|
384
|
-
const interiorMesh = topoMesh(
|
|
385
|
-
topology,
|
|
386
|
-
topology.objects[objectKey] as GeometryCollection,
|
|
387
|
-
(a, b) => a !== b,
|
|
388
|
-
);
|
|
389
|
-
const outlineMesh = topoMesh(
|
|
390
|
-
topology,
|
|
391
|
-
topology.objects[objectKey] as GeometryCollection,
|
|
392
|
-
(a, b) => a === b,
|
|
393
|
-
);
|
|
394
|
-
|
|
395
|
-
const interiorPath = pathGen(interiorMesh) ?? '';
|
|
396
|
-
const outlinePath = pathGen(outlineMesh) ?? '';
|
|
397
|
-
|
|
398
|
-
const interiorStroke = isDarkMode ? 'rgba(255,255,255,0.3)' : 'rgba(0,0,0,0.15)';
|
|
399
|
-
const outlineStroke = isDarkMode ? 'rgba(255,255,255,0.5)' : 'rgba(0,0,0,0.3)';
|
|
400
|
-
const borders: MapBorders = { interiorPath, outlinePath, interiorStroke, outlineStroke };
|
|
401
|
-
|
|
402
|
-
// 15b. Point marks (symbol layer above choropleth)
|
|
403
|
-
const pointMarks: MapPointMark[] = [];
|
|
404
|
-
let pointCategoricalLegend: CategoricalLegendLayout | null = null;
|
|
405
|
-
let pointContinuousLegend: ContinuousLegendLayout | null = null;
|
|
406
|
-
|
|
407
|
-
if (mapSpec.points) {
|
|
408
|
-
const pts = mapSpec.points;
|
|
409
|
-
const lonField = pts.longitude.field;
|
|
410
|
-
const latField = pts.latitude.field;
|
|
411
|
-
const keyChannel = pts.key;
|
|
412
|
-
|
|
413
|
-
// Size scale
|
|
414
|
-
const sizeScale = buildSizeScale(pts.size, pts.data, SIZE_SCALE_DEFAULTS.mapPoint);
|
|
415
|
-
|
|
416
|
-
// Color scale (independent from choropleth)
|
|
417
|
-
let pointColorScale: ((val: unknown) => string) | null = null;
|
|
418
|
-
const pointCategories: string[] = [];
|
|
419
|
-
const pointCategoryColors = new Map<string, string>();
|
|
420
|
-
|
|
421
|
-
if (pts.color) {
|
|
422
|
-
if (pts.color.type === 'quantitative') {
|
|
423
|
-
const colorValues: number[] = [];
|
|
424
|
-
for (const row of pts.data) {
|
|
425
|
-
const raw = row[pts.color.field];
|
|
426
|
-
if (raw != null) {
|
|
427
|
-
const v = Number(raw);
|
|
428
|
-
if (Number.isFinite(v)) colorValues.push(v);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
if (colorValues.length > 0) {
|
|
432
|
-
const palette = pts.color.scale?.scheme ?? 'blue';
|
|
433
|
-
const paletteStops = [...(SEQUENTIAL_PALETTES[palette] ?? SEQUENTIAL_PALETTES.blue)];
|
|
434
|
-
const qScale = scaleQuantile<string>().domain(colorValues).range(paletteStops);
|
|
435
|
-
pointColorScale = (val: unknown) => {
|
|
436
|
-
const v = Number(val);
|
|
437
|
-
return Number.isFinite(v) ? qScale(v) : CATEGORICAL_PALETTE[0];
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
} else {
|
|
441
|
-
// nominal / ordinal: honor scale.domain (category order) and scale.range (explicit colors)
|
|
442
|
-
const explicitDomain = pts.color.scale?.domain as string[] | undefined;
|
|
443
|
-
const explicitRange = pts.color.scale?.range as string[] | undefined;
|
|
444
|
-
|
|
445
|
-
if (explicitDomain && Array.isArray(explicitDomain)) {
|
|
446
|
-
for (const cat of explicitDomain) {
|
|
447
|
-
pointCategories.push(String(cat));
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
// Also pick up any categories in the data that aren't in the explicit domain
|
|
451
|
-
const seen = new Set<string>(pointCategories);
|
|
452
|
-
for (const row of pts.data) {
|
|
453
|
-
const raw = row[pts.color.field];
|
|
454
|
-
if (raw != null) {
|
|
455
|
-
const cat = String(raw);
|
|
456
|
-
if (!seen.has(cat)) {
|
|
457
|
-
seen.add(cat);
|
|
458
|
-
pointCategories.push(cat);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const colorSource =
|
|
464
|
-
explicitRange && Array.isArray(explicitRange) ? explicitRange : CATEGORICAL_PALETTE;
|
|
465
|
-
for (let i = 0; i < pointCategories.length; i++) {
|
|
466
|
-
pointCategoryColors.set(pointCategories[i], colorSource[i % colorSource.length]);
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
pointColorScale = (val: unknown) => {
|
|
470
|
-
if (val == null) return colorSource[0];
|
|
471
|
-
return pointCategoryColors.get(String(val)) ?? colorSource[0];
|
|
472
|
-
};
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
const defaultFill = CATEGORICAL_PALETTE[0];
|
|
477
|
-
const pointOpacity = pts.opacity ?? 0.65;
|
|
478
|
-
let animIndex = 0;
|
|
479
|
-
const droppedPoints: Array<[number, number]> = [];
|
|
480
|
-
|
|
481
|
-
for (let i = 0; i < pts.data.length; i++) {
|
|
482
|
-
const row = pts.data[i];
|
|
483
|
-
const lon = Number(row[lonField]);
|
|
484
|
-
const lat = Number(row[latField]);
|
|
485
|
-
if (!Number.isFinite(lon) || !Number.isFinite(lat)) continue;
|
|
486
|
-
|
|
487
|
-
const projected = projection([lon, lat]);
|
|
488
|
-
if (!projected) {
|
|
489
|
-
droppedPoints.push([lon, lat]);
|
|
490
|
-
continue;
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
const r = sizeScale ? sizeScale.scale(Number(row[sizeScale.field])) : 5;
|
|
494
|
-
const fill =
|
|
495
|
-
pointColorScale && pts.color ? pointColorScale(row[pts.color.field]) : defaultFill;
|
|
496
|
-
const key = keyChannel ? String(row[keyChannel.field] ?? i) : String(i);
|
|
497
|
-
const stroke = isDarkMode ? 'rgba(0,0,0,0.5)' : 'rgba(255,255,255,0.8)';
|
|
498
|
-
|
|
499
|
-
pointMarks.push({
|
|
500
|
-
type: 'map-point',
|
|
501
|
-
cx: projected[0],
|
|
502
|
-
cy: projected[1],
|
|
503
|
-
r,
|
|
504
|
-
fill,
|
|
505
|
-
stroke,
|
|
506
|
-
strokeWidth: 1,
|
|
507
|
-
fillOpacity: pointOpacity,
|
|
508
|
-
key,
|
|
509
|
-
data: row as Record<string, unknown>,
|
|
510
|
-
aria: { role: 'img', label: key },
|
|
511
|
-
animationIndex: animIndex++,
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
if (droppedPoints.length > 0) {
|
|
516
|
-
const sample = droppedPoints
|
|
517
|
-
.slice(0, 3)
|
|
518
|
-
.map(([lo, la]) => `[${lo}, ${la}]`)
|
|
519
|
-
.join(', ');
|
|
520
|
-
compileWarnings.push({
|
|
521
|
-
code: 'POINT_NULL_PROJECTION',
|
|
522
|
-
message:
|
|
523
|
-
`${droppedPoints.length} point(s) project to null and were dropped (e.g. ${sample}). ` +
|
|
524
|
-
'This is common with albersUsa which excludes territories. ' +
|
|
525
|
-
'Consider using projection: "mercator" or "equalEarth".',
|
|
526
|
-
context: { count: droppedPoints.length, samples: droppedPoints.slice(0, 5) },
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
// Point categorical legend
|
|
531
|
-
if (
|
|
532
|
-
pts.color &&
|
|
533
|
-
pts.color.type !== 'quantitative' &&
|
|
534
|
-
pointCategories.length > 0 &&
|
|
535
|
-
showLegend
|
|
536
|
-
) {
|
|
537
|
-
const labelStyle: TextStyle = {
|
|
538
|
-
fontFamily: theme.fonts.family,
|
|
539
|
-
fontSize: theme.fonts.sizes.small,
|
|
540
|
-
fontWeight: theme.fonts.weights.normal,
|
|
541
|
-
fill: theme.colors.text,
|
|
542
|
-
lineHeight: 1.2,
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
const entries: LegendEntry[] = pointCategories.map((cat) => ({
|
|
546
|
-
label: cat,
|
|
547
|
-
color: pointCategoryColors.get(cat)!,
|
|
548
|
-
shape: 'circle' as const,
|
|
549
|
-
}));
|
|
550
|
-
|
|
551
|
-
// Position below the map. Only offset by choropleth legend height when
|
|
552
|
-
// the choropleth legend is also at the bottom (not 'top').
|
|
553
|
-
const bottomChoroplethHeight =
|
|
554
|
-
legendPosition === 'bottom'
|
|
555
|
-
? (continuousLegend?.bounds.height ?? categoricalLegend?.bounds.height ?? 0)
|
|
556
|
-
: 0;
|
|
557
|
-
const choroplethLegendGap = bottomChoroplethHeight > 0 ? 8 : 0;
|
|
558
|
-
const legendX = fullArea.x;
|
|
559
|
-
const legendY =
|
|
560
|
-
fullArea.y +
|
|
561
|
-
mapAreaHeight +
|
|
562
|
-
legendReserveGap +
|
|
563
|
-
bottomChoroplethHeight +
|
|
564
|
-
choroplethLegendGap;
|
|
565
|
-
const legendWidth = fullArea.width;
|
|
566
|
-
const swatchSize = 10;
|
|
567
|
-
const swatchGap = 6;
|
|
568
|
-
const entryGap = 16;
|
|
569
|
-
|
|
570
|
-
pointCategoricalLegend = {
|
|
571
|
-
type: 'categorical',
|
|
572
|
-
position: 'bottom',
|
|
573
|
-
bounds: { x: legendX, y: legendY, width: legendWidth, height: swatchSize + 6 },
|
|
574
|
-
labelStyle,
|
|
575
|
-
entries,
|
|
576
|
-
swatchSize,
|
|
577
|
-
swatchGap,
|
|
578
|
-
entryGap,
|
|
579
|
-
swatchChipFill: isDarkMode ? 'rgba(255,255,255,0.06)' : 'rgba(0,0,0,0.04)',
|
|
580
|
-
};
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
// Point continuous legend (quantitative color)
|
|
584
|
-
if (pts.color && pts.color.type === 'quantitative' && showLegend) {
|
|
585
|
-
const ptColorValues: number[] = [];
|
|
586
|
-
for (const row of pts.data) {
|
|
587
|
-
const v = row[pts.color.field];
|
|
588
|
-
if (v != null) {
|
|
589
|
-
const n = Number(v);
|
|
590
|
-
if (Number.isFinite(n)) ptColorValues.push(n);
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
const ptLegendChannel: EncodingChannel = {
|
|
594
|
-
...pts.color,
|
|
595
|
-
scale: { ...pts.color.scale, type: 'quantile' },
|
|
596
|
-
};
|
|
597
|
-
const ptContinuousContent = computeContinuousLegendContentForChannel(
|
|
598
|
-
ptColorValues,
|
|
599
|
-
ptLegendChannel,
|
|
600
|
-
theme,
|
|
601
|
-
fullArea.width,
|
|
602
|
-
);
|
|
603
|
-
if (ptContinuousContent) {
|
|
604
|
-
const labelStyle: TextStyle = {
|
|
605
|
-
fontFamily: theme.fonts.family,
|
|
606
|
-
fontSize: theme.fonts.sizes.small,
|
|
607
|
-
fontWeight: theme.fonts.weights.normal,
|
|
608
|
-
fill: theme.colors.text,
|
|
609
|
-
lineHeight: 1.3,
|
|
610
|
-
fontVariant: 'tabular-nums',
|
|
611
|
-
};
|
|
612
|
-
const bottomChoroplethH =
|
|
613
|
-
legendPosition === 'bottom'
|
|
614
|
-
? (continuousLegend?.bounds.height ?? categoricalLegend?.bounds.height ?? 0)
|
|
615
|
-
: 0;
|
|
616
|
-
const choroplethLegendGap = bottomChoroplethH > 0 ? 8 : 0;
|
|
617
|
-
const legendX = fullArea.x;
|
|
618
|
-
const legendY =
|
|
619
|
-
fullArea.y + mapAreaHeight + legendReserveGap + bottomChoroplethH + choroplethLegendGap;
|
|
620
|
-
const ptLabelRowHeight = Math.ceil(theme.fonts.sizes.small * 1.3);
|
|
621
|
-
const ptLegendHeight =
|
|
622
|
-
ptContinuousContent.barHeight + CONTINUOUS_LABEL_GAP + ptLabelRowHeight;
|
|
623
|
-
const bounds = {
|
|
624
|
-
x: legendX,
|
|
625
|
-
y: legendY,
|
|
626
|
-
width: ptContinuousContent.barWidth,
|
|
627
|
-
height: ptLegendHeight,
|
|
628
|
-
};
|
|
629
|
-
const bar = {
|
|
630
|
-
x: bounds.x,
|
|
631
|
-
y: bounds.y,
|
|
632
|
-
width: ptContinuousContent.barWidth,
|
|
633
|
-
height: ptContinuousContent.barHeight,
|
|
634
|
-
};
|
|
635
|
-
pointContinuousLegend = {
|
|
636
|
-
type: 'continuous' as const,
|
|
637
|
-
mode: ptContinuousContent.mode,
|
|
638
|
-
position: 'bottom',
|
|
639
|
-
bounds,
|
|
640
|
-
labelStyle,
|
|
641
|
-
bar,
|
|
642
|
-
colorStops: ptContinuousContent.colorStops,
|
|
643
|
-
bins: ptContinuousContent.bins.map((b) => ({ ...b, x: b.x + bar.x })),
|
|
644
|
-
ticks: ptContinuousContent.ticks.map((t) => ({ ...t, x: t.x + bar.x })),
|
|
645
|
-
labelY: bar.y + bar.height + CONTINUOUS_LABEL_GAP + labelStyle.fontSize,
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
// 16. Tooltips
|
|
652
|
-
const tooltipChannels = mapSpec.encoding.tooltip
|
|
653
|
-
? Array.isArray(mapSpec.encoding.tooltip)
|
|
654
|
-
? mapSpec.encoding.tooltip
|
|
655
|
-
: [mapSpec.encoding.tooltip]
|
|
656
|
-
: null;
|
|
657
|
-
|
|
658
|
-
const tooltipDescriptors = new Map<string, TooltipContent>();
|
|
659
|
-
for (const mark of featureMarks) {
|
|
660
|
-
if (!mark.data) continue;
|
|
661
|
-
const fields: TooltipField[] = [];
|
|
662
|
-
const name = mark.name ?? String(mark.id);
|
|
663
|
-
fields.push({ label: 'Region', value: name });
|
|
664
|
-
|
|
665
|
-
if (tooltipChannels) {
|
|
666
|
-
for (const ch of tooltipChannels) {
|
|
667
|
-
const val = mark.data[ch.field];
|
|
668
|
-
if (val != null) {
|
|
669
|
-
fields.push({
|
|
670
|
-
label: ch.title ?? ch.field,
|
|
671
|
-
value: ch.type === 'quantitative' ? formatter(Number(val)) : String(val),
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
} else if (colorEncoding && isQuantitative && mark.data[colorEncoding.field] != null) {
|
|
676
|
-
fields.push({
|
|
677
|
-
label: colorEncoding.title ?? colorEncoding.field,
|
|
678
|
-
value: formatter(Number(mark.data[colorEncoding.field])),
|
|
679
|
-
});
|
|
680
|
-
} else if (colorEncoding && !isQuantitative && mark.data[colorEncoding.field] != null) {
|
|
681
|
-
fields.push({
|
|
682
|
-
label: colorEncoding.title ?? colorEncoding.field,
|
|
683
|
-
value: String(mark.data[colorEncoding.field]),
|
|
684
|
-
});
|
|
685
|
-
}
|
|
686
|
-
tooltipDescriptors.set(String(mark.id), { fields });
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
// Point tooltips (keyed with 'point:' prefix to disambiguate from feature IDs)
|
|
690
|
-
if (mapSpec.points && pointMarks.length > 0) {
|
|
691
|
-
const ptTooltipChannels = mapSpec.points.tooltip
|
|
692
|
-
? Array.isArray(mapSpec.points.tooltip)
|
|
693
|
-
? mapSpec.points.tooltip
|
|
694
|
-
: [mapSpec.points.tooltip]
|
|
695
|
-
: null;
|
|
696
|
-
|
|
697
|
-
for (const pm of pointMarks) {
|
|
698
|
-
const fields: TooltipField[] = [];
|
|
699
|
-
if (ptTooltipChannels) {
|
|
700
|
-
for (const ch of ptTooltipChannels) {
|
|
701
|
-
const val = pm.data[ch.field];
|
|
702
|
-
if (val != null) {
|
|
703
|
-
fields.push({
|
|
704
|
-
label: ch.title ?? ch.field,
|
|
705
|
-
value: ch.type === 'quantitative' ? formatter(Number(val)) : String(val),
|
|
706
|
-
});
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
} else {
|
|
710
|
-
// Auto-populate from key, color, and size channels
|
|
711
|
-
const pts = mapSpec.points!;
|
|
712
|
-
if (pts.key) {
|
|
713
|
-
const val = pm.data[pts.key.field];
|
|
714
|
-
if (val != null)
|
|
715
|
-
fields.push({ label: pts.key.title ?? pts.key.field, value: String(val) });
|
|
716
|
-
}
|
|
717
|
-
if (pts.color) {
|
|
718
|
-
const val = pm.data[pts.color.field];
|
|
719
|
-
if (val != null)
|
|
720
|
-
fields.push({
|
|
721
|
-
label: pts.color.title ?? pts.color.field,
|
|
722
|
-
value: pts.color.type === 'quantitative' ? formatter(Number(val)) : String(val),
|
|
723
|
-
});
|
|
724
|
-
}
|
|
725
|
-
if (pts.size) {
|
|
726
|
-
const val = pm.data[pts.size.field];
|
|
727
|
-
if (val != null)
|
|
728
|
-
fields.push({ label: pts.size.title ?? pts.size.field, value: formatter(Number(val)) });
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
if (fields.length > 0) {
|
|
732
|
-
tooltipDescriptors.set(`point:${pm.key}`, { fields });
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
// 17. Accessibility
|
|
738
|
-
const a11yDataField = colorEncoding?.field;
|
|
739
|
-
const a11y = {
|
|
740
|
-
altText:
|
|
741
|
-
pointMarks.length > 0
|
|
742
|
-
? colorEncoding
|
|
743
|
-
? `Map showing ${geoFeatures.length} regions with ${isQuantitative ? 'quantitative' : 'categorical'} data and ${pointMarks.length} point markers`
|
|
744
|
-
: `Map showing ${geoFeatures.length} regions with ${pointMarks.length} point markers`
|
|
745
|
-
: colorEncoding
|
|
746
|
-
? `Map showing ${geoFeatures.length} regions with ${isQuantitative ? 'quantitative' : 'categorical'} data`
|
|
747
|
-
: `Map showing ${geoFeatures.length} regions`,
|
|
748
|
-
dataTableFallback: [
|
|
749
|
-
...featureMarks
|
|
750
|
-
.filter((m) => m.data)
|
|
751
|
-
.map((m) => [
|
|
752
|
-
m.name ?? String(m.id),
|
|
753
|
-
a11yDataField ? String(m.data?.[a11yDataField] ?? '') : '',
|
|
754
|
-
]),
|
|
755
|
-
...pointMarks.map((pm) => [
|
|
756
|
-
pm.key,
|
|
757
|
-
mapSpec.points?.color ? String(pm.data[mapSpec.points.color.field] ?? '') : '',
|
|
758
|
-
]),
|
|
759
|
-
],
|
|
760
|
-
role: 'img' as const,
|
|
761
|
-
keyboardNavigable: featureMarks.length > 0 || pointMarks.length > 0,
|
|
762
|
-
};
|
|
763
|
-
|
|
764
|
-
// 18. Resolve focus
|
|
765
|
-
const resolvedFocus = resolveFocus(mapSpec.geo.focus, featureMarks, pointMarks, compileWarnings);
|
|
766
|
-
|
|
767
|
-
// 19. Animation
|
|
768
|
-
const resolvedAnimation: ResolvedAnimation | undefined = resolveAnimation(mapSpec.animation);
|
|
769
|
-
|
|
770
|
-
// 20. Compute the map drawing area (offset when legend is at top)
|
|
771
|
-
const mapAreaY =
|
|
772
|
-
legendPosition === 'top' && legendBlockHeight > 0
|
|
773
|
-
? fullArea.y + legendBlockHeight + legendReserveGap
|
|
774
|
-
: fullArea.y;
|
|
775
|
-
const mapArea = {
|
|
776
|
-
x: fullArea.x,
|
|
777
|
-
y: mapAreaY,
|
|
778
|
-
width: fullArea.width,
|
|
779
|
-
height: mapAreaHeight,
|
|
780
|
-
};
|
|
781
|
-
|
|
782
|
-
// 21. Anchor bottom chrome below the map + legend, then compute total height
|
|
783
|
-
chrome.bottomAnchorY = fullArea.y + fullArea.height;
|
|
784
|
-
const contentHeight =
|
|
785
|
-
fullArea.y +
|
|
786
|
-
mapAreaHeight +
|
|
787
|
-
legendReserveGap +
|
|
788
|
-
legendBlockHeight +
|
|
789
|
-
chrome.bottomHeight +
|
|
790
|
-
padding;
|
|
791
|
-
|
|
792
|
-
// 22. Emit compile warnings
|
|
793
|
-
for (const w of compileWarnings) {
|
|
794
|
-
if (options.onWarn) {
|
|
795
|
-
options.onWarn(w.message);
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
return {
|
|
800
|
-
area: mapArea,
|
|
801
|
-
chrome,
|
|
802
|
-
features: featureMarks,
|
|
803
|
-
borders,
|
|
804
|
-
continuousLegend,
|
|
805
|
-
categoricalLegend,
|
|
806
|
-
pointMarks,
|
|
807
|
-
pointCategoricalLegend,
|
|
808
|
-
pointContinuousLegend,
|
|
809
|
-
tooltipDescriptors,
|
|
810
|
-
a11y,
|
|
811
|
-
theme,
|
|
812
|
-
width: options.width,
|
|
813
|
-
height: contentHeight,
|
|
814
|
-
animation: resolvedAnimation,
|
|
815
|
-
watermark,
|
|
816
|
-
measureText:
|
|
817
|
-
options.measureText ??
|
|
818
|
-
((text, fontSize) => ({ width: estimateTextWidth(text, fontSize), height: fontSize })),
|
|
819
|
-
warnings: compileWarnings,
|
|
820
|
-
mapSize: { width: fullArea.width, height: mapAreaHeight },
|
|
821
|
-
focus: resolvedFocus,
|
|
822
|
-
};
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
// ---------------------------------------------------------------------------
|
|
826
|
-
// Basemap-only marks (neutral fill, no color encoding)
|
|
827
|
-
// ---------------------------------------------------------------------------
|
|
828
|
-
|
|
829
|
-
interface BasemapOptions {
|
|
830
|
-
geoFeatures: GeoJSON.Feature[];
|
|
831
|
-
pathGen: ReturnType<typeof geoPath>;
|
|
832
|
-
neutralFill: string;
|
|
833
|
-
neutralStroke: string;
|
|
834
|
-
droppedFeatures: string[];
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
function buildBasemapMarks(opts: BasemapOptions): MapFeatureMark[] {
|
|
838
|
-
const { geoFeatures, pathGen, neutralFill, neutralStroke, droppedFeatures } = opts;
|
|
839
|
-
const marks: MapFeatureMark[] = [];
|
|
840
|
-
let animIndex = 0;
|
|
841
|
-
|
|
842
|
-
for (const feat of geoFeatures) {
|
|
843
|
-
const pathD = pathGen(feat);
|
|
844
|
-
if (!pathD) {
|
|
845
|
-
const name =
|
|
846
|
-
((feat.properties as Record<string, unknown> | null)?.name as string) ?? String(feat.id);
|
|
847
|
-
droppedFeatures.push(name);
|
|
848
|
-
continue;
|
|
849
|
-
}
|
|
850
|
-
|
|
851
|
-
const featureId = feat.id as string | number;
|
|
852
|
-
const props = feat.properties as Record<string, unknown> | null;
|
|
853
|
-
const name = (props?.name as string) ?? (props?.NAME as string);
|
|
854
|
-
|
|
855
|
-
const b = pathGen.bounds(feat);
|
|
856
|
-
const bx = Number.isFinite(b[0][0]) ? b[0][0] : 0;
|
|
857
|
-
const by = Number.isFinite(b[0][1]) ? b[0][1] : 0;
|
|
858
|
-
const bw = Number.isFinite(b[1][0]) ? b[1][0] - bx : 0;
|
|
859
|
-
const bh = Number.isFinite(b[1][1]) ? b[1][1] - by : 0;
|
|
860
|
-
const bounds = { x: bx, y: by, width: bw, height: bh };
|
|
861
|
-
const rawCentroid = pathGen.centroid(feat);
|
|
862
|
-
const cx = Number.isFinite(rawCentroid[0]) ? rawCentroid[0] : bx + bw / 2;
|
|
863
|
-
const cy = Number.isFinite(rawCentroid[1]) ? rawCentroid[1] : by + bh / 2;
|
|
864
|
-
|
|
865
|
-
marks.push({
|
|
866
|
-
type: 'map-feature',
|
|
867
|
-
path: pathD,
|
|
868
|
-
fill: neutralFill,
|
|
869
|
-
stroke: neutralStroke,
|
|
870
|
-
strokeWidth: 0.5,
|
|
871
|
-
id: featureId,
|
|
872
|
-
name,
|
|
873
|
-
data: null,
|
|
874
|
-
aria: { role: 'img', label: name ?? String(featureId) },
|
|
875
|
-
animationIndex: animIndex++,
|
|
876
|
-
bounds,
|
|
877
|
-
centroid: [cx, cy],
|
|
878
|
-
});
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
return marks;
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
// ---------------------------------------------------------------------------
|
|
885
|
-
// Quantitative marks (sequential color scale)
|
|
886
|
-
// ---------------------------------------------------------------------------
|
|
887
|
-
|
|
888
|
-
interface QuantitativeOptions {
|
|
889
|
-
geoFeatures: GeoJSON.Feature[];
|
|
890
|
-
pathGen: ReturnType<typeof geoPath>;
|
|
891
|
-
joined: Map<string | number, Record<string, unknown>>;
|
|
892
|
-
colorField: string;
|
|
893
|
-
palette: string;
|
|
894
|
-
isDarkMode: boolean;
|
|
895
|
-
neutralFill: string;
|
|
896
|
-
neutralStroke: string;
|
|
897
|
-
formatter: (n: number) => string;
|
|
898
|
-
droppedFeatures: string[];
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
function buildQuantitativeMarks(opts: QuantitativeOptions): {
|
|
902
|
-
marks: MapFeatureMark[];
|
|
903
|
-
} {
|
|
904
|
-
const {
|
|
905
|
-
geoFeatures,
|
|
906
|
-
pathGen,
|
|
907
|
-
joined,
|
|
908
|
-
colorField,
|
|
909
|
-
palette,
|
|
910
|
-
isDarkMode,
|
|
911
|
-
neutralFill,
|
|
912
|
-
neutralStroke,
|
|
913
|
-
formatter,
|
|
914
|
-
droppedFeatures,
|
|
915
|
-
} = opts;
|
|
916
|
-
|
|
917
|
-
// Collect values for scale domain
|
|
918
|
-
const values: number[] = [];
|
|
919
|
-
for (const row of joined.values()) {
|
|
920
|
-
const raw = row[colorField];
|
|
921
|
-
if (raw != null) {
|
|
922
|
-
const v = Number(raw);
|
|
923
|
-
if (!Number.isNaN(v)) values.push(v);
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
const paletteStops = [...(SEQUENTIAL_PALETTES[palette] ?? SEQUENTIAL_PALETTES.blue)];
|
|
928
|
-
const colorScale = scaleQuantile<string>().domain(values).range(paletteStops);
|
|
929
|
-
|
|
930
|
-
const marks: MapFeatureMark[] = [];
|
|
931
|
-
let animIndex = 0;
|
|
932
|
-
|
|
933
|
-
for (const feat of geoFeatures) {
|
|
934
|
-
const pathD = pathGen(feat);
|
|
935
|
-
if (!pathD) {
|
|
936
|
-
const name =
|
|
937
|
-
((feat.properties as Record<string, unknown> | null)?.name as string) ?? String(feat.id);
|
|
938
|
-
droppedFeatures.push(name);
|
|
939
|
-
continue;
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
const featureId = feat.id as string | number;
|
|
943
|
-
const dataRow = joined.get(featureId) ?? null;
|
|
944
|
-
const hasData = dataRow !== null;
|
|
945
|
-
|
|
946
|
-
let fill = neutralFill;
|
|
947
|
-
let stroke = neutralStroke;
|
|
948
|
-
if (hasData) {
|
|
949
|
-
const raw = dataRow[colorField];
|
|
950
|
-
if (raw != null) {
|
|
951
|
-
const v = Number(raw);
|
|
952
|
-
if (!Number.isNaN(v)) {
|
|
953
|
-
fill = colorScale(v);
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
stroke = isDarkMode ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)';
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
const props = feat.properties as Record<string, unknown> | null;
|
|
960
|
-
const name = (props?.name as string) ?? (props?.NAME as string);
|
|
961
|
-
|
|
962
|
-
// Compute projected geometry (guard non-finite bounds from degenerate features)
|
|
963
|
-
const b = pathGen.bounds(feat);
|
|
964
|
-
const bx = Number.isFinite(b[0][0]) ? b[0][0] : 0;
|
|
965
|
-
const by = Number.isFinite(b[0][1]) ? b[0][1] : 0;
|
|
966
|
-
const bw = Number.isFinite(b[1][0]) ? b[1][0] - bx : 0;
|
|
967
|
-
const bh = Number.isFinite(b[1][1]) ? b[1][1] - by : 0;
|
|
968
|
-
const bounds = { x: bx, y: by, width: bw, height: bh };
|
|
969
|
-
const rawCentroid = pathGen.centroid(feat);
|
|
970
|
-
const cx = Number.isFinite(rawCentroid[0]) ? rawCentroid[0] : bx + bw / 2;
|
|
971
|
-
const cy = Number.isFinite(rawCentroid[1]) ? rawCentroid[1] : by + bh / 2;
|
|
972
|
-
|
|
973
|
-
marks.push({
|
|
974
|
-
type: 'map-feature',
|
|
975
|
-
path: pathD,
|
|
976
|
-
fill,
|
|
977
|
-
stroke,
|
|
978
|
-
strokeWidth: 0.5,
|
|
979
|
-
id: featureId,
|
|
980
|
-
name,
|
|
981
|
-
data: dataRow,
|
|
982
|
-
aria: {
|
|
983
|
-
role: 'img',
|
|
984
|
-
label: name
|
|
985
|
-
? `${name}: ${hasData && dataRow[colorField] != null && !Number.isNaN(Number(dataRow[colorField])) ? formatter(Number(dataRow[colorField])) : 'no data'}`
|
|
986
|
-
: String(featureId),
|
|
987
|
-
},
|
|
988
|
-
animationIndex: animIndex++,
|
|
989
|
-
bounds,
|
|
990
|
-
centroid: [cx, cy],
|
|
991
|
-
});
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
return { marks };
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
// ---------------------------------------------------------------------------
|
|
998
|
-
// Categorical marks (distinct fill colors)
|
|
999
|
-
// ---------------------------------------------------------------------------
|
|
1000
|
-
|
|
1001
|
-
interface CategoricalOptions {
|
|
1002
|
-
geoFeatures: GeoJSON.Feature[];
|
|
1003
|
-
pathGen: ReturnType<typeof geoPath>;
|
|
1004
|
-
joined: Map<string | number, Record<string, unknown>>;
|
|
1005
|
-
colorField: string;
|
|
1006
|
-
colorScale?: { domain?: unknown; range?: unknown };
|
|
1007
|
-
isDarkMode: boolean;
|
|
1008
|
-
neutralFill: string;
|
|
1009
|
-
neutralStroke: string;
|
|
1010
|
-
theme: ResolvedTheme;
|
|
1011
|
-
fullArea: { x: number; y: number; width: number; height: number };
|
|
1012
|
-
mapAreaHeight: number;
|
|
1013
|
-
legendReserveGap: number;
|
|
1014
|
-
legendPosition: 'top' | 'bottom';
|
|
1015
|
-
showLegend: boolean;
|
|
1016
|
-
droppedFeatures: string[];
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
|
-
function buildCategoricalMarks(opts: CategoricalOptions): {
|
|
1020
|
-
marks: MapFeatureMark[];
|
|
1021
|
-
legend: CategoricalLegendLayout | null;
|
|
1022
|
-
} {
|
|
1023
|
-
const {
|
|
1024
|
-
geoFeatures,
|
|
1025
|
-
pathGen,
|
|
1026
|
-
joined,
|
|
1027
|
-
colorField,
|
|
1028
|
-
colorScale: scaleConfig,
|
|
1029
|
-
isDarkMode,
|
|
1030
|
-
neutralFill,
|
|
1031
|
-
neutralStroke,
|
|
1032
|
-
theme,
|
|
1033
|
-
fullArea,
|
|
1034
|
-
mapAreaHeight,
|
|
1035
|
-
legendReserveGap,
|
|
1036
|
-
legendPosition,
|
|
1037
|
-
showLegend,
|
|
1038
|
-
droppedFeatures,
|
|
1039
|
-
} = opts;
|
|
1040
|
-
|
|
1041
|
-
// Collect unique categories: honor scale.domain for order, then append any unseen from data
|
|
1042
|
-
const categories: string[] = [];
|
|
1043
|
-
const explicitDomain = scaleConfig?.domain;
|
|
1044
|
-
if (explicitDomain && Array.isArray(explicitDomain)) {
|
|
1045
|
-
for (const cat of explicitDomain) {
|
|
1046
|
-
categories.push(String(cat));
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
const seen = new Set<string>(categories);
|
|
1050
|
-
for (const row of joined.values()) {
|
|
1051
|
-
const raw = row[colorField];
|
|
1052
|
-
if (raw != null) {
|
|
1053
|
-
const cat = String(raw);
|
|
1054
|
-
if (!seen.has(cat)) {
|
|
1055
|
-
seen.add(cat);
|
|
1056
|
-
categories.push(cat);
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
|
-
const explicitRange = scaleConfig?.range;
|
|
1062
|
-
const colorSource =
|
|
1063
|
-
explicitRange && Array.isArray(explicitRange) ? explicitRange : CATEGORICAL_PALETTE;
|
|
1064
|
-
const categoryColors = new Map<string, string>();
|
|
1065
|
-
for (let i = 0; i < categories.length; i++) {
|
|
1066
|
-
categoryColors.set(categories[i], colorSource[i % colorSource.length]);
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
const marks: MapFeatureMark[] = [];
|
|
1070
|
-
let animIndex = 0;
|
|
1071
|
-
|
|
1072
|
-
for (const feat of geoFeatures) {
|
|
1073
|
-
const pathD = pathGen(feat);
|
|
1074
|
-
if (!pathD) {
|
|
1075
|
-
const name =
|
|
1076
|
-
((feat.properties as Record<string, unknown> | null)?.name as string) ?? String(feat.id);
|
|
1077
|
-
droppedFeatures.push(name);
|
|
1078
|
-
continue;
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
const featureId = feat.id as string | number;
|
|
1082
|
-
const dataRow = joined.get(featureId) ?? null;
|
|
1083
|
-
const hasData = dataRow !== null;
|
|
1084
|
-
|
|
1085
|
-
let fill = neutralFill;
|
|
1086
|
-
let stroke = neutralStroke;
|
|
1087
|
-
if (hasData) {
|
|
1088
|
-
const raw = dataRow[colorField];
|
|
1089
|
-
if (raw != null) {
|
|
1090
|
-
const cat = String(raw);
|
|
1091
|
-
fill = categoryColors.get(cat) ?? neutralFill;
|
|
1092
|
-
}
|
|
1093
|
-
stroke = isDarkMode ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)';
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
const props = feat.properties as Record<string, unknown> | null;
|
|
1097
|
-
const name = (props?.name as string) ?? (props?.NAME as string);
|
|
1098
|
-
|
|
1099
|
-
// Compute projected geometry (guard non-finite bounds from degenerate features)
|
|
1100
|
-
const b = pathGen.bounds(feat);
|
|
1101
|
-
const bx = Number.isFinite(b[0][0]) ? b[0][0] : 0;
|
|
1102
|
-
const by = Number.isFinite(b[0][1]) ? b[0][1] : 0;
|
|
1103
|
-
const bw = Number.isFinite(b[1][0]) ? b[1][0] - bx : 0;
|
|
1104
|
-
const bh = Number.isFinite(b[1][1]) ? b[1][1] - by : 0;
|
|
1105
|
-
const bounds = { x: bx, y: by, width: bw, height: bh };
|
|
1106
|
-
const rawCentroid = pathGen.centroid(feat);
|
|
1107
|
-
const cx = Number.isFinite(rawCentroid[0]) ? rawCentroid[0] : bx + bw / 2;
|
|
1108
|
-
const cy = Number.isFinite(rawCentroid[1]) ? rawCentroid[1] : by + bh / 2;
|
|
1109
|
-
|
|
1110
|
-
marks.push({
|
|
1111
|
-
type: 'map-feature',
|
|
1112
|
-
path: pathD,
|
|
1113
|
-
fill,
|
|
1114
|
-
stroke,
|
|
1115
|
-
strokeWidth: 0.5,
|
|
1116
|
-
id: featureId,
|
|
1117
|
-
name,
|
|
1118
|
-
data: dataRow,
|
|
1119
|
-
aria: {
|
|
1120
|
-
role: 'img',
|
|
1121
|
-
label: name
|
|
1122
|
-
? `${name}: ${hasData ? String(dataRow[colorField]) : 'no data'}`
|
|
1123
|
-
: String(featureId),
|
|
1124
|
-
},
|
|
1125
|
-
animationIndex: animIndex++,
|
|
1126
|
-
bounds,
|
|
1127
|
-
centroid: [cx, cy],
|
|
1128
|
-
});
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
// Legend
|
|
1132
|
-
let legend: CategoricalLegendLayout | null = null;
|
|
1133
|
-
if (showLegend && categories.length > 0) {
|
|
1134
|
-
const labelStyle: TextStyle = {
|
|
1135
|
-
fontFamily: theme.fonts.family,
|
|
1136
|
-
fontSize: 11,
|
|
1137
|
-
fontWeight: 400,
|
|
1138
|
-
fill: theme.colors.text,
|
|
1139
|
-
lineHeight: 1.2,
|
|
1140
|
-
};
|
|
1141
|
-
|
|
1142
|
-
const entries: LegendEntry[] = categories.map((cat) => ({
|
|
1143
|
-
label: cat,
|
|
1144
|
-
color: categoryColors.get(cat)!,
|
|
1145
|
-
shape: 'square' as const,
|
|
1146
|
-
}));
|
|
1147
|
-
|
|
1148
|
-
// Honor legend.position like the continuous path does. The map drawing
|
|
1149
|
-
// area (mapAreaY in compileMap) already shifts down for top legends, so
|
|
1150
|
-
// the legend row here must agree with it — a bottom-anchored legend with
|
|
1151
|
-
// a top-shifted map is how legends used to land on top of Alaska.
|
|
1152
|
-
const legendX = fullArea.x;
|
|
1153
|
-
const legendY =
|
|
1154
|
-
legendPosition === 'bottom'
|
|
1155
|
-
? fullArea.y + mapAreaHeight + legendReserveGap
|
|
1156
|
-
: fullArea.y + legendReserveGap;
|
|
1157
|
-
const legendWidth = fullArea.width;
|
|
1158
|
-
const swatchSize = 10;
|
|
1159
|
-
const swatchGap = 6;
|
|
1160
|
-
const entryGap = 16;
|
|
1161
|
-
|
|
1162
|
-
legend = {
|
|
1163
|
-
type: 'categorical',
|
|
1164
|
-
position: legendPosition,
|
|
1165
|
-
bounds: { x: legendX, y: legendY, width: legendWidth, height: swatchSize + 6 },
|
|
1166
|
-
labelStyle,
|
|
1167
|
-
entries,
|
|
1168
|
-
swatchSize,
|
|
1169
|
-
swatchGap,
|
|
1170
|
-
entryGap,
|
|
1171
|
-
swatchChipFill: isDarkMode ? 'rgba(255,255,255,0.06)' : 'rgba(0,0,0,0.04)',
|
|
1172
|
-
};
|
|
1173
|
-
}
|
|
1174
|
-
|
|
1175
|
-
return { marks, legend };
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
// ---------------------------------------------------------------------------
|
|
1179
|
-
// Focus resolution
|
|
1180
|
-
// ---------------------------------------------------------------------------
|
|
1181
|
-
|
|
1182
|
-
function resolveFocus(
|
|
1183
|
-
focus: MapFocus | null,
|
|
1184
|
-
features: MapFeatureMark[],
|
|
1185
|
-
pointMarks: MapPointMark[],
|
|
1186
|
-
warnings: CompileWarning[],
|
|
1187
|
-
): MapFocusLayout | null {
|
|
1188
|
-
if (focus === null || focus === undefined) return null;
|
|
1189
|
-
|
|
1190
|
-
// Points form fits the union of the point layer's circle bounds (cx +/- r)
|
|
1191
|
-
// rather than any feature. Use it when the points cluster in a small part of a
|
|
1192
|
-
// large feature, so fitting the feature would leave the cluster small and
|
|
1193
|
-
// off-center. `points: true` fits every point; `points: { field, value }`
|
|
1194
|
-
// fits only the matching subset, so a story can pan between sub-clusters.
|
|
1195
|
-
if (typeof focus === 'object' && !Array.isArray(focus) && 'points' in focus) {
|
|
1196
|
-
const filter = focus.points;
|
|
1197
|
-
const matched =
|
|
1198
|
-
filter === true
|
|
1199
|
-
? pointMarks
|
|
1200
|
-
: pointMarks.filter((p) => p.data[filter.field] === filter.value);
|
|
1201
|
-
|
|
1202
|
-
if (matched.length === 0) {
|
|
1203
|
-
const detail =
|
|
1204
|
-
filter === true
|
|
1205
|
-
? 'the map has no points to fit'
|
|
1206
|
-
: `no points match { field: "${filter.field}", value: ${JSON.stringify(filter.value)} }`;
|
|
1207
|
-
warnings.push({
|
|
1208
|
-
code: 'FOCUS_UNMATCHED',
|
|
1209
|
-
message: `geo.focus.points is set but ${detail}`,
|
|
1210
|
-
context: {},
|
|
1211
|
-
});
|
|
1212
|
-
return null;
|
|
1213
|
-
}
|
|
1214
|
-
const padding = focus.padding ?? 16;
|
|
1215
|
-
let minX = Infinity;
|
|
1216
|
-
let minY = Infinity;
|
|
1217
|
-
let maxX = -Infinity;
|
|
1218
|
-
let maxY = -Infinity;
|
|
1219
|
-
for (const p of matched) {
|
|
1220
|
-
// Use center points only, not cx ± r: the camera counter-scales point
|
|
1221
|
-
// radii to constant screen size, so the base radius doesn't occupy
|
|
1222
|
-
// projected space after the transform. Including it inflates the
|
|
1223
|
-
// bounding box and produces a much shallower zoom than expected.
|
|
1224
|
-
minX = Math.min(minX, p.cx);
|
|
1225
|
-
minY = Math.min(minY, p.cy);
|
|
1226
|
-
maxX = Math.max(maxX, p.cx);
|
|
1227
|
-
maxY = Math.max(maxY, p.cy);
|
|
1228
|
-
}
|
|
1229
|
-
// Guard against degenerate bounding box (single point or coincident points)
|
|
1230
|
-
const w = Math.max(maxX - minX, 1);
|
|
1231
|
-
const h = Math.max(maxY - minY, 1);
|
|
1232
|
-
return {
|
|
1233
|
-
target: { x: minX, y: minY, width: w, height: h, padding },
|
|
1234
|
-
// No feature ids: focus-dim is a feature concept, so a points focus
|
|
1235
|
-
// dims no features (there are no "other" features to mute).
|
|
1236
|
-
ids: [],
|
|
1237
|
-
};
|
|
1238
|
-
}
|
|
1239
|
-
|
|
1240
|
-
// Normalize to { ids, padding }
|
|
1241
|
-
let ids: Array<string | number>;
|
|
1242
|
-
let padding = 16;
|
|
1243
|
-
|
|
1244
|
-
if (typeof focus === 'string' || typeof focus === 'number') {
|
|
1245
|
-
ids = [focus];
|
|
1246
|
-
} else if (Array.isArray(focus)) {
|
|
1247
|
-
ids = focus;
|
|
1248
|
-
} else {
|
|
1249
|
-
// Object form: { features, padding? }
|
|
1250
|
-
const feats = focus.features;
|
|
1251
|
-
ids = typeof feats === 'string' || typeof feats === 'number' ? [feats] : feats;
|
|
1252
|
-
padding = focus.padding ?? 16;
|
|
1253
|
-
}
|
|
1254
|
-
|
|
1255
|
-
// Find matching features and compute union bounds
|
|
1256
|
-
const idSet = new Set(ids.map(String));
|
|
1257
|
-
let minX = Infinity;
|
|
1258
|
-
let minY = Infinity;
|
|
1259
|
-
let maxX = -Infinity;
|
|
1260
|
-
let maxY = -Infinity;
|
|
1261
|
-
const matchedIds: Array<string | number> = [];
|
|
1262
|
-
|
|
1263
|
-
for (const f of features) {
|
|
1264
|
-
if (idSet.has(String(f.id))) {
|
|
1265
|
-
matchedIds.push(f.id);
|
|
1266
|
-
minX = Math.min(minX, f.bounds.x);
|
|
1267
|
-
minY = Math.min(minY, f.bounds.y);
|
|
1268
|
-
maxX = Math.max(maxX, f.bounds.x + f.bounds.width);
|
|
1269
|
-
maxY = Math.max(maxY, f.bounds.y + f.bounds.height);
|
|
1270
|
-
}
|
|
1271
|
-
}
|
|
1272
|
-
|
|
1273
|
-
// Warn on unmatched ids
|
|
1274
|
-
const unmatchedIds = ids.filter((id) => !matchedIds.some((m) => String(m) === String(id)));
|
|
1275
|
-
if (unmatchedIds.length > 0) {
|
|
1276
|
-
warnings.push({
|
|
1277
|
-
code: 'FOCUS_UNMATCHED',
|
|
1278
|
-
message: `geo.focus references feature id(s) not found in the map: ${unmatchedIds.join(', ')}`,
|
|
1279
|
-
context: { unmatchedIds },
|
|
1280
|
-
});
|
|
1281
|
-
}
|
|
1282
|
-
|
|
1283
|
-
if (matchedIds.length === 0) return null;
|
|
1284
|
-
|
|
1285
|
-
return {
|
|
1286
|
-
target: {
|
|
1287
|
-
x: minX,
|
|
1288
|
-
y: minY,
|
|
1289
|
-
width: maxX - minX,
|
|
1290
|
-
height: maxY - minY,
|
|
1291
|
-
padding,
|
|
1292
|
-
},
|
|
1293
|
-
ids: matchedIds,
|
|
1294
|
-
};
|
|
1295
|
-
}
|
|
1296
|
-
|
|
1297
|
-
// ---------------------------------------------------------------------------
|
|
1298
|
-
// Empty layout fallback
|
|
1299
|
-
// ---------------------------------------------------------------------------
|
|
1300
|
-
|
|
1301
|
-
function emptyLayout(
|
|
1302
|
-
chrome: ReturnType<typeof computeChrome>,
|
|
1303
|
-
theme: ResolvedTheme,
|
|
1304
|
-
options: CompileOptions,
|
|
1305
|
-
watermark: boolean,
|
|
1306
|
-
): MapLayout {
|
|
1307
|
-
return {
|
|
1308
|
-
area: { x: 0, y: 0, width: 0, height: 0 },
|
|
1309
|
-
chrome,
|
|
1310
|
-
features: [],
|
|
1311
|
-
borders: {
|
|
1312
|
-
interiorPath: '',
|
|
1313
|
-
outlinePath: '',
|
|
1314
|
-
interiorStroke: 'rgba(0,0,0,0.15)',
|
|
1315
|
-
outlineStroke: 'rgba(0,0,0,0.3)',
|
|
1316
|
-
},
|
|
1317
|
-
continuousLegend: null,
|
|
1318
|
-
categoricalLegend: null,
|
|
1319
|
-
pointMarks: [],
|
|
1320
|
-
pointCategoricalLegend: null,
|
|
1321
|
-
pointContinuousLegend: null,
|
|
1322
|
-
tooltipDescriptors: new Map(),
|
|
1323
|
-
a11y: {
|
|
1324
|
-
altText: 'Empty map',
|
|
1325
|
-
dataTableFallback: [],
|
|
1326
|
-
role: 'img',
|
|
1327
|
-
keyboardNavigable: false,
|
|
1328
|
-
},
|
|
1329
|
-
theme,
|
|
1330
|
-
width: options.width,
|
|
1331
|
-
height: options.height,
|
|
1332
|
-
animation: undefined,
|
|
1333
|
-
watermark,
|
|
1334
|
-
measureText:
|
|
1335
|
-
options.measureText ??
|
|
1336
|
-
((text, fontSize) => ({ width: estimateTextWidth(text, fontSize), height: fontSize })),
|
|
1337
|
-
warnings: [],
|
|
1338
|
-
mapSize: { width: 0, height: 0 },
|
|
1339
|
-
focus: null,
|
|
1340
|
-
};
|
|
1341
|
-
}
|