@opendata-ai/openchart-engine 8.0.0-rc.2 → 8.0.0-rc.20
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 +140 -2
- package/dist/index.js +1403 -210
- 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 -721
- 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 -370
- 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 -539
- 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 -139
- 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 -86
- package/src/compiler/__tests__/validate-highlight.test.ts +0 -112
- package/src/compiler/__tests__/validate-near-miss.test.ts +0 -89
- 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 -733
- package/src/compiler/sparkline-defaults.ts +0 -138
- package/src/compiler/types.ts +0 -241
- package/src/compiler/validate.ts +0 -1810
- 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 -621
- package/src/geo/compile-map.ts +0 -856
- package/src/geo/join.ts +0 -115
- package/src/geo/projections.ts +0 -45
- package/src/geo/types.ts +0 -24
- 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,856 +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
|
-
ResolvedAnimation,
|
|
14
|
-
ResolvedTheme,
|
|
15
|
-
TextStyle,
|
|
16
|
-
TooltipContent,
|
|
17
|
-
TooltipField,
|
|
18
|
-
} from '@opendata-ai/openchart-core';
|
|
19
|
-
import {
|
|
20
|
-
adaptTheme,
|
|
21
|
-
buildD3Formatter,
|
|
22
|
-
CATEGORICAL_PALETTE,
|
|
23
|
-
computeChrome,
|
|
24
|
-
estimateTextWidth,
|
|
25
|
-
formatNumber,
|
|
26
|
-
getBreakpoint,
|
|
27
|
-
HPAD_COMPACT_FRACTION,
|
|
28
|
-
HPAD_COMPACT_MIN,
|
|
29
|
-
resolveTheme,
|
|
30
|
-
SEQUENTIAL_PALETTES,
|
|
31
|
-
} from '@opendata-ai/openchart-core';
|
|
32
|
-
import { geoArea, geoPath } from 'd3-geo';
|
|
33
|
-
import { scaleQuantile } from 'd3-scale';
|
|
34
|
-
import { feature as topoFeature, mesh as topoMesh } from 'topojson-client';
|
|
35
|
-
import type { GeometryCollection, Topology } from 'topojson-specification';
|
|
36
|
-
import { emitSpecWarnings, expandSpecSugar } from '../compile/spec-sugar';
|
|
37
|
-
import { resolveAnimation } from '../compiler/animation';
|
|
38
|
-
import { compile as compileSpec } from '../compiler/index';
|
|
39
|
-
import {
|
|
40
|
-
CONTINUOUS_LABEL_GAP,
|
|
41
|
-
computeContinuousLegendContentForChannel,
|
|
42
|
-
} from '../legend/continuous';
|
|
43
|
-
import { joinDataToFeatures } from './join';
|
|
44
|
-
import { createProjection } from './projections';
|
|
45
|
-
import type { NormalizedMapSpec } from './types';
|
|
46
|
-
|
|
47
|
-
function validateGeoFeatures(geo: NormalizedMapSpec['geo']): Topology {
|
|
48
|
-
if (!geo.features) {
|
|
49
|
-
throw new Error(
|
|
50
|
-
'Map spec error: geo.features is required but missing.\n\n' +
|
|
51
|
-
'The map needs a TopoJSON topology object as geo.features. ' +
|
|
52
|
-
'Install a standard atlas package and import it:\n\n' +
|
|
53
|
-
' npm i us-atlas\n' +
|
|
54
|
-
' import us from "us-atlas/states-albers-10m.json";\n\n' +
|
|
55
|
-
' // or for world maps:\n' +
|
|
56
|
-
' npm i world-atlas\n' +
|
|
57
|
-
' import world from "world-atlas/countries-110m.json";\n\n' +
|
|
58
|
-
'Then pass it as geo.features:\n\n' +
|
|
59
|
-
' { type: "map", geo: { features: us }, data: [...], encoding: { ... } }',
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const topo = geo.features as Record<string, unknown>;
|
|
64
|
-
if (typeof topo !== 'object' || topo === null || topo.type !== 'Topology' || !topo.objects) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
'Map spec error: geo.features must be a valid TopoJSON Topology ' +
|
|
67
|
-
'(expected an object with type: "Topology" and an "objects" property).\n\n' +
|
|
68
|
-
'Make sure you are passing the full TopoJSON file, not a GeoJSON FeatureCollection. ' +
|
|
69
|
-
'Atlas packages like us-atlas and world-atlas export TopoJSON by default:\n\n' +
|
|
70
|
-
' npm i us-atlas\n' +
|
|
71
|
-
' import us from "us-atlas/states-albers-10m.json";\n' +
|
|
72
|
-
' { type: "map", geo: { features: us }, ... }',
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const objects = topo.objects as Record<string, unknown>;
|
|
77
|
-
if (Object.keys(objects).length === 0) {
|
|
78
|
-
throw new Error(
|
|
79
|
-
'Map spec error: geo.features.objects is empty (no geometry collections found). ' +
|
|
80
|
-
'The TopoJSON file must contain at least one named geometry collection.',
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return topo as unknown as Topology;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function compileMap(spec: unknown, options: CompileOptions): MapLayout {
|
|
88
|
-
// 1. Sugar expansion + validate + normalize
|
|
89
|
-
const sugarWarnings: string[] = [];
|
|
90
|
-
const expandedSpec =
|
|
91
|
-
spec && typeof spec === 'object' && !Array.isArray(spec)
|
|
92
|
-
? expandSpecSugar(spec as Record<string, unknown>, sugarWarnings)
|
|
93
|
-
: spec;
|
|
94
|
-
const { spec: normalized, warnings } = compileSpec(expandedSpec);
|
|
95
|
-
emitSpecWarnings([...sugarWarnings, ...warnings], options.onWarn);
|
|
96
|
-
|
|
97
|
-
if (!('type' in normalized) || normalized.type !== 'map') {
|
|
98
|
-
throw new Error(
|
|
99
|
-
'compileMap received a non-map spec. Use compileChart, compileTileMap, compileSankey, or compileBarList instead.',
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const mapSpec = normalized as NormalizedMapSpec;
|
|
104
|
-
|
|
105
|
-
// 2. Validate geo features
|
|
106
|
-
const topology = validateGeoFeatures(mapSpec.geo);
|
|
107
|
-
|
|
108
|
-
// Resolve watermark
|
|
109
|
-
const rawWatermark = (spec as Record<string, unknown>).watermark;
|
|
110
|
-
const watermark = rawWatermark !== undefined ? mapSpec.watermark : (options.watermark ?? true);
|
|
111
|
-
|
|
112
|
-
// 3. Resolve theme
|
|
113
|
-
const mergedThemeConfig = options.theme ? { ...mapSpec.theme, ...options.theme } : mapSpec.theme;
|
|
114
|
-
const lightTheme: ResolvedTheme = resolveTheme(mergedThemeConfig);
|
|
115
|
-
let theme: ResolvedTheme = lightTheme;
|
|
116
|
-
if (options.darkMode) {
|
|
117
|
-
theme = adaptTheme(theme);
|
|
118
|
-
}
|
|
119
|
-
const isDarkMode = options.darkMode;
|
|
120
|
-
|
|
121
|
-
// 4. Chrome
|
|
122
|
-
const chrome = computeChrome(
|
|
123
|
-
{
|
|
124
|
-
title: mapSpec.chrome.title,
|
|
125
|
-
subtitle: mapSpec.chrome.subtitle,
|
|
126
|
-
source: mapSpec.chrome.source,
|
|
127
|
-
byline: mapSpec.chrome.byline,
|
|
128
|
-
footer: mapSpec.chrome.footer,
|
|
129
|
-
},
|
|
130
|
-
theme,
|
|
131
|
-
options.width,
|
|
132
|
-
options.measureText,
|
|
133
|
-
'full',
|
|
134
|
-
undefined,
|
|
135
|
-
watermark,
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
// 5. Drawing area
|
|
139
|
-
const padding =
|
|
140
|
-
getBreakpoint(options.width) === 'compact'
|
|
141
|
-
? Math.max(HPAD_COMPACT_MIN, Math.round(theme.spacing.padding * HPAD_COMPACT_FRACTION))
|
|
142
|
-
: theme.spacing.padding;
|
|
143
|
-
const fullArea = {
|
|
144
|
-
x: padding,
|
|
145
|
-
y: padding + chrome.topHeight,
|
|
146
|
-
width: options.width - padding * 2,
|
|
147
|
-
height: options.height - chrome.topHeight - chrome.bottomHeight - padding * 2,
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
if (fullArea.width <= 0 || fullArea.height <= 0) {
|
|
151
|
-
return emptyLayout(chrome, theme, options, watermark);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// 6. Parse TopoJSON -> GeoJSON
|
|
155
|
-
const objectKey = Object.keys(topology.objects)[0];
|
|
156
|
-
const geoCollection = topoFeature(topology, topology.objects[objectKey] as GeometryCollection);
|
|
157
|
-
const geoFeatures = geoCollection.features;
|
|
158
|
-
|
|
159
|
-
// 7. Compile warnings
|
|
160
|
-
const compileWarnings: CompileWarning[] = [];
|
|
161
|
-
|
|
162
|
-
// 8. Legend height reserve
|
|
163
|
-
const showLegend = mapSpec.legend?.show !== false;
|
|
164
|
-
const colorEncoding = mapSpec.encoding.color;
|
|
165
|
-
const isQuantitative = colorEncoding.type === 'quantitative';
|
|
166
|
-
const legendPosition = mapSpec.legend?.position === 'bottom' ? 'bottom' : 'top';
|
|
167
|
-
|
|
168
|
-
// Compute legend block height. For quantitative maps, use the continuous
|
|
169
|
-
// legend infrastructure; for categorical, estimate a single swatch row.
|
|
170
|
-
let legendBlockHeight = 0;
|
|
171
|
-
let continuousContent: ReturnType<typeof computeContinuousLegendContentForChannel> = null;
|
|
172
|
-
if (showLegend && isQuantitative) {
|
|
173
|
-
const colorValues: number[] = [];
|
|
174
|
-
for (const row of mapSpec.data) {
|
|
175
|
-
const v = row[colorEncoding.field];
|
|
176
|
-
if (v != null) {
|
|
177
|
-
const n = Number(v);
|
|
178
|
-
if (!Number.isNaN(n)) colorValues.push(n);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
// Maps always use a quantile color scale, so tell the legend infra
|
|
182
|
-
// to produce binned swatches rather than a gradient bar.
|
|
183
|
-
const legendChannel: EncodingChannel = {
|
|
184
|
-
...(colorEncoding as EncodingChannel),
|
|
185
|
-
scale: { ...(colorEncoding as EncodingChannel).scale, type: 'quantile' },
|
|
186
|
-
};
|
|
187
|
-
continuousContent = computeContinuousLegendContentForChannel(
|
|
188
|
-
colorValues,
|
|
189
|
-
legendChannel,
|
|
190
|
-
theme,
|
|
191
|
-
fullArea.width,
|
|
192
|
-
);
|
|
193
|
-
if (continuousContent) {
|
|
194
|
-
const labelRowHeight = Math.ceil(theme.fonts.sizes.small * 1.3);
|
|
195
|
-
legendBlockHeight = continuousContent.barHeight + CONTINUOUS_LABEL_GAP + labelRowHeight;
|
|
196
|
-
}
|
|
197
|
-
} else if (showLegend) {
|
|
198
|
-
const labelHeight = Math.ceil(theme.fonts.sizes.small * 1.3);
|
|
199
|
-
legendBlockHeight = Math.max(10, labelHeight) + 6;
|
|
200
|
-
}
|
|
201
|
-
const legendReserveGap = legendBlockHeight > 0 ? 8 : 0;
|
|
202
|
-
|
|
203
|
-
const mapAreaHeight = fullArea.height - legendBlockHeight - legendReserveGap;
|
|
204
|
-
if (mapAreaHeight <= 0) {
|
|
205
|
-
return emptyLayout(chrome, theme, options, watermark);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// 9. Projection + path generator
|
|
209
|
-
const projectionType = mapSpec.geo.projection;
|
|
210
|
-
const projection = createProjection(projectionType, fullArea.width, mapAreaHeight, geoCollection);
|
|
211
|
-
const pathGen = geoPath(projection);
|
|
212
|
-
|
|
213
|
-
// 10. Check winding order (skip for identity: geoArea uses spherical math
|
|
214
|
-
// and gives false positives on planar pre-projected coordinates)
|
|
215
|
-
if (projectionType !== 'identity') {
|
|
216
|
-
for (const feat of geoFeatures) {
|
|
217
|
-
const area = geoArea(feat);
|
|
218
|
-
if (area > 2 * Math.PI) {
|
|
219
|
-
const name =
|
|
220
|
-
((feat.properties as Record<string, unknown> | null)?.name as string) ??
|
|
221
|
-
((feat.properties as Record<string, unknown> | null)?.NAME as string) ??
|
|
222
|
-
String(feat.id);
|
|
223
|
-
compileWarnings.push({
|
|
224
|
-
code: 'INVERTED_WINDING',
|
|
225
|
-
message:
|
|
226
|
-
`Feature "${name}" has inverted winding order (geoArea > 2π). ` +
|
|
227
|
-
'This usually means the exterior ring is wound clockwise (RFC 7946) instead of ' +
|
|
228
|
-
'counter-clockwise (d3-geo convention). Fix with: npx topojson-rewind your-file.json',
|
|
229
|
-
context: { featureId: feat.id, featureName: name },
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// 11. Join data to features
|
|
236
|
-
const featureObjs = geoFeatures.map((f) => ({
|
|
237
|
-
id: f.id as string | number,
|
|
238
|
-
properties: (f.properties ?? {}) as Record<string, unknown>,
|
|
239
|
-
}));
|
|
240
|
-
const { joined, warnings: joinWarnings } = joinDataToFeatures(
|
|
241
|
-
featureObjs,
|
|
242
|
-
mapSpec.data,
|
|
243
|
-
mapSpec.encoding.key.field,
|
|
244
|
-
mapSpec.geo.idField,
|
|
245
|
-
);
|
|
246
|
-
compileWarnings.push(...joinWarnings);
|
|
247
|
-
|
|
248
|
-
// 12. Check for null-projecting features (albersUsa drops territories)
|
|
249
|
-
const droppedFeatures: string[] = [];
|
|
250
|
-
|
|
251
|
-
// 13. Build color scale + feature marks
|
|
252
|
-
const formatter = buildD3Formatter(mapSpec.valueFormat) ?? formatNumber;
|
|
253
|
-
const neutralFill = isDarkMode ? '#2a2a2a' : '#e8e8e8';
|
|
254
|
-
const neutralStroke = isDarkMode ? 'rgba(255,255,255,0.08)' : 'rgba(0,0,0,0.06)';
|
|
255
|
-
|
|
256
|
-
let featureMarks: MapFeatureMark[];
|
|
257
|
-
let continuousLegend: ContinuousLegendLayout | null = null;
|
|
258
|
-
let categoricalLegend: CategoricalLegendLayout | null = null;
|
|
259
|
-
|
|
260
|
-
if (isQuantitative) {
|
|
261
|
-
const result = buildQuantitativeMarks({
|
|
262
|
-
geoFeatures,
|
|
263
|
-
pathGen,
|
|
264
|
-
joined,
|
|
265
|
-
colorField: colorEncoding.field,
|
|
266
|
-
palette: mapSpec.encoding.color.scale?.scheme ?? 'blue',
|
|
267
|
-
isDarkMode: !!isDarkMode,
|
|
268
|
-
neutralFill,
|
|
269
|
-
neutralStroke,
|
|
270
|
-
formatter,
|
|
271
|
-
droppedFeatures,
|
|
272
|
-
});
|
|
273
|
-
featureMarks = result.marks;
|
|
274
|
-
|
|
275
|
-
// Build the continuous legend. Maps place it directly (not via placeLegend,
|
|
276
|
-
// which does cartesian-specific positioning above the chart area).
|
|
277
|
-
if (showLegend && continuousContent) {
|
|
278
|
-
const labelStyle: TextStyle = {
|
|
279
|
-
fontFamily: theme.fonts.family,
|
|
280
|
-
fontSize: theme.fonts.sizes.small,
|
|
281
|
-
fontWeight: theme.fonts.weights.normal,
|
|
282
|
-
fill: theme.colors.text,
|
|
283
|
-
lineHeight: 1.3,
|
|
284
|
-
fontVariant: 'tabular-nums',
|
|
285
|
-
};
|
|
286
|
-
const legendX = fullArea.x;
|
|
287
|
-
const legendY =
|
|
288
|
-
legendPosition === 'bottom'
|
|
289
|
-
? fullArea.y + mapAreaHeight + legendReserveGap
|
|
290
|
-
: fullArea.y + legendReserveGap;
|
|
291
|
-
const bounds = {
|
|
292
|
-
x: legendX,
|
|
293
|
-
y: legendY,
|
|
294
|
-
width: continuousContent.barWidth,
|
|
295
|
-
height: legendBlockHeight,
|
|
296
|
-
};
|
|
297
|
-
const bar = {
|
|
298
|
-
x: bounds.x,
|
|
299
|
-
y: bounds.y,
|
|
300
|
-
width: continuousContent.barWidth,
|
|
301
|
-
height: continuousContent.barHeight,
|
|
302
|
-
};
|
|
303
|
-
continuousLegend = {
|
|
304
|
-
type: 'continuous' as const,
|
|
305
|
-
mode: continuousContent.mode,
|
|
306
|
-
position: legendPosition,
|
|
307
|
-
bounds,
|
|
308
|
-
labelStyle,
|
|
309
|
-
bar,
|
|
310
|
-
colorStops: continuousContent.colorStops,
|
|
311
|
-
bins: continuousContent.bins.map((b) => ({ ...b, x: b.x + bar.x })),
|
|
312
|
-
ticks: continuousContent.ticks.map((t) => ({ ...t, x: t.x + bar.x })),
|
|
313
|
-
labelY: bar.y + bar.height + CONTINUOUS_LABEL_GAP + labelStyle.fontSize,
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
} else {
|
|
317
|
-
const result = buildCategoricalMarks({
|
|
318
|
-
geoFeatures,
|
|
319
|
-
pathGen,
|
|
320
|
-
joined,
|
|
321
|
-
colorField: colorEncoding.field,
|
|
322
|
-
isDarkMode: !!isDarkMode,
|
|
323
|
-
neutralFill,
|
|
324
|
-
neutralStroke,
|
|
325
|
-
theme,
|
|
326
|
-
fullArea,
|
|
327
|
-
mapAreaHeight,
|
|
328
|
-
legendReserveGap,
|
|
329
|
-
showLegend,
|
|
330
|
-
droppedFeatures,
|
|
331
|
-
});
|
|
332
|
-
featureMarks = result.marks;
|
|
333
|
-
categoricalLegend = result.legend;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// Report dropped features (null-projecting)
|
|
337
|
-
if (droppedFeatures.length > 0) {
|
|
338
|
-
compileWarnings.push({
|
|
339
|
-
code: 'NULL_PROJECTION',
|
|
340
|
-
message:
|
|
341
|
-
`${droppedFeatures.length} feature(s) project to null and were dropped: ${droppedFeatures.join(', ')}. ` +
|
|
342
|
-
'This is common with albersUsa which excludes territories (PR, GU, VI, AS, MP). ' +
|
|
343
|
-
'If your data includes these, consider using projection: "mercator" or "equalEarth".',
|
|
344
|
-
context: { features: droppedFeatures },
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// 15. Borders via topojson mesh
|
|
349
|
-
const interiorMesh = topoMesh(
|
|
350
|
-
topology,
|
|
351
|
-
topology.objects[objectKey] as GeometryCollection,
|
|
352
|
-
(a, b) => a !== b,
|
|
353
|
-
);
|
|
354
|
-
const outlineMesh = topoMesh(
|
|
355
|
-
topology,
|
|
356
|
-
topology.objects[objectKey] as GeometryCollection,
|
|
357
|
-
(a, b) => a === b,
|
|
358
|
-
);
|
|
359
|
-
|
|
360
|
-
const interiorPath = pathGen(interiorMesh) ?? '';
|
|
361
|
-
const outlinePath = pathGen(outlineMesh) ?? '';
|
|
362
|
-
|
|
363
|
-
const interiorStroke = isDarkMode ? 'rgba(255,255,255,0.3)' : 'rgba(0,0,0,0.15)';
|
|
364
|
-
const outlineStroke = isDarkMode ? 'rgba(255,255,255,0.5)' : 'rgba(0,0,0,0.3)';
|
|
365
|
-
const borders: MapBorders = { interiorPath, outlinePath, interiorStroke, outlineStroke };
|
|
366
|
-
|
|
367
|
-
// 16. Tooltips
|
|
368
|
-
const tooltipChannels = mapSpec.encoding.tooltip
|
|
369
|
-
? Array.isArray(mapSpec.encoding.tooltip)
|
|
370
|
-
? mapSpec.encoding.tooltip
|
|
371
|
-
: [mapSpec.encoding.tooltip]
|
|
372
|
-
: null;
|
|
373
|
-
|
|
374
|
-
const tooltipDescriptors = new Map<string, TooltipContent>();
|
|
375
|
-
for (const mark of featureMarks) {
|
|
376
|
-
if (!mark.data) continue;
|
|
377
|
-
const fields: TooltipField[] = [];
|
|
378
|
-
const name = mark.name ?? String(mark.id);
|
|
379
|
-
fields.push({ label: 'Region', value: name });
|
|
380
|
-
|
|
381
|
-
if (tooltipChannels) {
|
|
382
|
-
for (const ch of tooltipChannels) {
|
|
383
|
-
const val = mark.data[ch.field];
|
|
384
|
-
if (val != null) {
|
|
385
|
-
fields.push({
|
|
386
|
-
label: ch.title ?? ch.field,
|
|
387
|
-
value: ch.type === 'quantitative' ? formatter(Number(val)) : String(val),
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
} else if (isQuantitative && mark.data[colorEncoding.field] != null) {
|
|
392
|
-
fields.push({
|
|
393
|
-
label: colorEncoding.title ?? colorEncoding.field,
|
|
394
|
-
value: formatter(Number(mark.data[colorEncoding.field])),
|
|
395
|
-
});
|
|
396
|
-
} else if (!isQuantitative && mark.data[colorEncoding.field] != null) {
|
|
397
|
-
fields.push({
|
|
398
|
-
label: colorEncoding.title ?? colorEncoding.field,
|
|
399
|
-
value: String(mark.data[colorEncoding.field]),
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
tooltipDescriptors.set(String(mark.id), { fields });
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// 17. Accessibility
|
|
406
|
-
const a11y = {
|
|
407
|
-
altText: `Map showing ${geoFeatures.length} regions with ${isQuantitative ? 'quantitative' : 'categorical'} data`,
|
|
408
|
-
dataTableFallback: featureMarks
|
|
409
|
-
.filter((m) => m.data)
|
|
410
|
-
.map((m) => [m.name ?? String(m.id), String(m.data?.[colorEncoding.field] ?? '')]),
|
|
411
|
-
role: 'img' as const,
|
|
412
|
-
keyboardNavigable: featureMarks.length > 0,
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
// 18. Resolve focus
|
|
416
|
-
const resolvedFocus = resolveFocus(mapSpec.geo.focus, featureMarks, compileWarnings);
|
|
417
|
-
|
|
418
|
-
// 19. Animation
|
|
419
|
-
const resolvedAnimation: ResolvedAnimation | undefined = resolveAnimation(mapSpec.animation);
|
|
420
|
-
|
|
421
|
-
// 20. Compute the map drawing area (offset when legend is at top)
|
|
422
|
-
const mapAreaY =
|
|
423
|
-
legendPosition === 'top' && legendBlockHeight > 0
|
|
424
|
-
? fullArea.y + legendBlockHeight + legendReserveGap
|
|
425
|
-
: fullArea.y;
|
|
426
|
-
const mapArea = {
|
|
427
|
-
x: fullArea.x,
|
|
428
|
-
y: mapAreaY,
|
|
429
|
-
width: fullArea.width,
|
|
430
|
-
height: mapAreaHeight,
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
// 21. Anchor bottom chrome below the map + legend, then compute total height
|
|
434
|
-
chrome.bottomAnchorY = fullArea.y + fullArea.height;
|
|
435
|
-
const contentHeight =
|
|
436
|
-
fullArea.y +
|
|
437
|
-
mapAreaHeight +
|
|
438
|
-
legendReserveGap +
|
|
439
|
-
legendBlockHeight +
|
|
440
|
-
chrome.bottomHeight +
|
|
441
|
-
padding;
|
|
442
|
-
|
|
443
|
-
// 22. Emit compile warnings
|
|
444
|
-
for (const w of compileWarnings) {
|
|
445
|
-
if (options.onWarn) {
|
|
446
|
-
options.onWarn(w.message);
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
return {
|
|
451
|
-
area: mapArea,
|
|
452
|
-
chrome,
|
|
453
|
-
features: featureMarks,
|
|
454
|
-
borders,
|
|
455
|
-
continuousLegend,
|
|
456
|
-
categoricalLegend,
|
|
457
|
-
tooltipDescriptors,
|
|
458
|
-
a11y,
|
|
459
|
-
theme,
|
|
460
|
-
width: options.width,
|
|
461
|
-
height: contentHeight,
|
|
462
|
-
animation: resolvedAnimation,
|
|
463
|
-
watermark,
|
|
464
|
-
measureText:
|
|
465
|
-
options.measureText ??
|
|
466
|
-
((text, fontSize) => ({ width: estimateTextWidth(text, fontSize), height: fontSize })),
|
|
467
|
-
warnings: compileWarnings,
|
|
468
|
-
mapSize: { width: fullArea.width, height: mapAreaHeight },
|
|
469
|
-
focus: resolvedFocus,
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
// ---------------------------------------------------------------------------
|
|
474
|
-
// Quantitative marks (sequential color scale)
|
|
475
|
-
// ---------------------------------------------------------------------------
|
|
476
|
-
|
|
477
|
-
interface QuantitativeOptions {
|
|
478
|
-
geoFeatures: GeoJSON.Feature[];
|
|
479
|
-
pathGen: ReturnType<typeof geoPath>;
|
|
480
|
-
joined: Map<string | number, Record<string, unknown>>;
|
|
481
|
-
colorField: string;
|
|
482
|
-
palette: string;
|
|
483
|
-
isDarkMode: boolean;
|
|
484
|
-
neutralFill: string;
|
|
485
|
-
neutralStroke: string;
|
|
486
|
-
formatter: (n: number) => string;
|
|
487
|
-
droppedFeatures: string[];
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
function buildQuantitativeMarks(opts: QuantitativeOptions): {
|
|
491
|
-
marks: MapFeatureMark[];
|
|
492
|
-
} {
|
|
493
|
-
const {
|
|
494
|
-
geoFeatures,
|
|
495
|
-
pathGen,
|
|
496
|
-
joined,
|
|
497
|
-
colorField,
|
|
498
|
-
palette,
|
|
499
|
-
isDarkMode,
|
|
500
|
-
neutralFill,
|
|
501
|
-
neutralStroke,
|
|
502
|
-
formatter,
|
|
503
|
-
droppedFeatures,
|
|
504
|
-
} = opts;
|
|
505
|
-
|
|
506
|
-
// Collect values for scale domain
|
|
507
|
-
const values: number[] = [];
|
|
508
|
-
for (const row of joined.values()) {
|
|
509
|
-
const raw = row[colorField];
|
|
510
|
-
if (raw != null) {
|
|
511
|
-
const v = Number(raw);
|
|
512
|
-
if (!Number.isNaN(v)) values.push(v);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
const paletteStops = [...(SEQUENTIAL_PALETTES[palette] ?? SEQUENTIAL_PALETTES.blue)];
|
|
517
|
-
const colorScale = scaleQuantile<string>().domain(values).range(paletteStops);
|
|
518
|
-
|
|
519
|
-
const marks: MapFeatureMark[] = [];
|
|
520
|
-
let animIndex = 0;
|
|
521
|
-
|
|
522
|
-
for (const feat of geoFeatures) {
|
|
523
|
-
const pathD = pathGen(feat);
|
|
524
|
-
if (!pathD) {
|
|
525
|
-
const name =
|
|
526
|
-
((feat.properties as Record<string, unknown> | null)?.name as string) ?? String(feat.id);
|
|
527
|
-
droppedFeatures.push(name);
|
|
528
|
-
continue;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
const featureId = feat.id as string | number;
|
|
532
|
-
const dataRow = joined.get(featureId) ?? null;
|
|
533
|
-
const hasData = dataRow !== null;
|
|
534
|
-
|
|
535
|
-
let fill = neutralFill;
|
|
536
|
-
let stroke = neutralStroke;
|
|
537
|
-
if (hasData) {
|
|
538
|
-
const raw = dataRow[colorField];
|
|
539
|
-
if (raw != null) {
|
|
540
|
-
const v = Number(raw);
|
|
541
|
-
if (!Number.isNaN(v)) {
|
|
542
|
-
fill = colorScale(v);
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
stroke = isDarkMode ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)';
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
const props = feat.properties as Record<string, unknown> | null;
|
|
549
|
-
const name = (props?.name as string) ?? (props?.NAME as string);
|
|
550
|
-
|
|
551
|
-
// Compute projected geometry (guard non-finite bounds from degenerate features)
|
|
552
|
-
const b = pathGen.bounds(feat);
|
|
553
|
-
const bx = Number.isFinite(b[0][0]) ? b[0][0] : 0;
|
|
554
|
-
const by = Number.isFinite(b[0][1]) ? b[0][1] : 0;
|
|
555
|
-
const bw = Number.isFinite(b[1][0]) ? b[1][0] - bx : 0;
|
|
556
|
-
const bh = Number.isFinite(b[1][1]) ? b[1][1] - by : 0;
|
|
557
|
-
const bounds = { x: bx, y: by, width: bw, height: bh };
|
|
558
|
-
const rawCentroid = pathGen.centroid(feat);
|
|
559
|
-
const cx = Number.isFinite(rawCentroid[0]) ? rawCentroid[0] : bx + bw / 2;
|
|
560
|
-
const cy = Number.isFinite(rawCentroid[1]) ? rawCentroid[1] : by + bh / 2;
|
|
561
|
-
|
|
562
|
-
marks.push({
|
|
563
|
-
type: 'map-feature',
|
|
564
|
-
path: pathD,
|
|
565
|
-
fill,
|
|
566
|
-
stroke,
|
|
567
|
-
strokeWidth: 0.5,
|
|
568
|
-
id: featureId,
|
|
569
|
-
name,
|
|
570
|
-
data: dataRow,
|
|
571
|
-
aria: {
|
|
572
|
-
role: 'img',
|
|
573
|
-
label: name
|
|
574
|
-
? `${name}: ${hasData && dataRow[colorField] != null && !Number.isNaN(Number(dataRow[colorField])) ? formatter(Number(dataRow[colorField])) : 'no data'}`
|
|
575
|
-
: String(featureId),
|
|
576
|
-
},
|
|
577
|
-
animationIndex: animIndex++,
|
|
578
|
-
bounds,
|
|
579
|
-
centroid: [cx, cy],
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
return { marks };
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
// ---------------------------------------------------------------------------
|
|
587
|
-
// Categorical marks (distinct fill colors)
|
|
588
|
-
// ---------------------------------------------------------------------------
|
|
589
|
-
|
|
590
|
-
interface CategoricalOptions {
|
|
591
|
-
geoFeatures: GeoJSON.Feature[];
|
|
592
|
-
pathGen: ReturnType<typeof geoPath>;
|
|
593
|
-
joined: Map<string | number, Record<string, unknown>>;
|
|
594
|
-
colorField: string;
|
|
595
|
-
isDarkMode: boolean;
|
|
596
|
-
neutralFill: string;
|
|
597
|
-
neutralStroke: string;
|
|
598
|
-
theme: ResolvedTheme;
|
|
599
|
-
fullArea: { x: number; y: number; width: number; height: number };
|
|
600
|
-
mapAreaHeight: number;
|
|
601
|
-
legendReserveGap: number;
|
|
602
|
-
showLegend: boolean;
|
|
603
|
-
droppedFeatures: string[];
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
function buildCategoricalMarks(opts: CategoricalOptions): {
|
|
607
|
-
marks: MapFeatureMark[];
|
|
608
|
-
legend: CategoricalLegendLayout | null;
|
|
609
|
-
} {
|
|
610
|
-
const {
|
|
611
|
-
geoFeatures,
|
|
612
|
-
pathGen,
|
|
613
|
-
joined,
|
|
614
|
-
colorField,
|
|
615
|
-
isDarkMode,
|
|
616
|
-
neutralFill,
|
|
617
|
-
neutralStroke,
|
|
618
|
-
theme,
|
|
619
|
-
fullArea,
|
|
620
|
-
mapAreaHeight,
|
|
621
|
-
legendReserveGap,
|
|
622
|
-
showLegend,
|
|
623
|
-
droppedFeatures,
|
|
624
|
-
} = opts;
|
|
625
|
-
|
|
626
|
-
// Collect unique categories in data order
|
|
627
|
-
const categories: string[] = [];
|
|
628
|
-
const seen = new Set<string>();
|
|
629
|
-
for (const row of joined.values()) {
|
|
630
|
-
const raw = row[colorField];
|
|
631
|
-
if (raw != null) {
|
|
632
|
-
const cat = String(raw);
|
|
633
|
-
if (!seen.has(cat)) {
|
|
634
|
-
seen.add(cat);
|
|
635
|
-
categories.push(cat);
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
const categoryColors = new Map<string, string>();
|
|
641
|
-
for (let i = 0; i < categories.length; i++) {
|
|
642
|
-
categoryColors.set(categories[i], CATEGORICAL_PALETTE[i % CATEGORICAL_PALETTE.length]);
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
const marks: MapFeatureMark[] = [];
|
|
646
|
-
let animIndex = 0;
|
|
647
|
-
|
|
648
|
-
for (const feat of geoFeatures) {
|
|
649
|
-
const pathD = pathGen(feat);
|
|
650
|
-
if (!pathD) {
|
|
651
|
-
const name =
|
|
652
|
-
((feat.properties as Record<string, unknown> | null)?.name as string) ?? String(feat.id);
|
|
653
|
-
droppedFeatures.push(name);
|
|
654
|
-
continue;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
const featureId = feat.id as string | number;
|
|
658
|
-
const dataRow = joined.get(featureId) ?? null;
|
|
659
|
-
const hasData = dataRow !== null;
|
|
660
|
-
|
|
661
|
-
let fill = neutralFill;
|
|
662
|
-
let stroke = neutralStroke;
|
|
663
|
-
if (hasData) {
|
|
664
|
-
const raw = dataRow[colorField];
|
|
665
|
-
if (raw != null) {
|
|
666
|
-
const cat = String(raw);
|
|
667
|
-
fill = categoryColors.get(cat) ?? neutralFill;
|
|
668
|
-
}
|
|
669
|
-
stroke = isDarkMode ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)';
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
const props = feat.properties as Record<string, unknown> | null;
|
|
673
|
-
const name = (props?.name as string) ?? (props?.NAME as string);
|
|
674
|
-
|
|
675
|
-
// Compute projected geometry (guard non-finite bounds from degenerate features)
|
|
676
|
-
const b = pathGen.bounds(feat);
|
|
677
|
-
const bx = Number.isFinite(b[0][0]) ? b[0][0] : 0;
|
|
678
|
-
const by = Number.isFinite(b[0][1]) ? b[0][1] : 0;
|
|
679
|
-
const bw = Number.isFinite(b[1][0]) ? b[1][0] - bx : 0;
|
|
680
|
-
const bh = Number.isFinite(b[1][1]) ? b[1][1] - by : 0;
|
|
681
|
-
const bounds = { x: bx, y: by, width: bw, height: bh };
|
|
682
|
-
const rawCentroid = pathGen.centroid(feat);
|
|
683
|
-
const cx = Number.isFinite(rawCentroid[0]) ? rawCentroid[0] : bx + bw / 2;
|
|
684
|
-
const cy = Number.isFinite(rawCentroid[1]) ? rawCentroid[1] : by + bh / 2;
|
|
685
|
-
|
|
686
|
-
marks.push({
|
|
687
|
-
type: 'map-feature',
|
|
688
|
-
path: pathD,
|
|
689
|
-
fill,
|
|
690
|
-
stroke,
|
|
691
|
-
strokeWidth: 0.5,
|
|
692
|
-
id: featureId,
|
|
693
|
-
name,
|
|
694
|
-
data: dataRow,
|
|
695
|
-
aria: {
|
|
696
|
-
role: 'img',
|
|
697
|
-
label: name
|
|
698
|
-
? `${name}: ${hasData ? String(dataRow[colorField]) : 'no data'}`
|
|
699
|
-
: String(featureId),
|
|
700
|
-
},
|
|
701
|
-
animationIndex: animIndex++,
|
|
702
|
-
bounds,
|
|
703
|
-
centroid: [cx, cy],
|
|
704
|
-
});
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
// Legend
|
|
708
|
-
let legend: CategoricalLegendLayout | null = null;
|
|
709
|
-
if (showLegend && categories.length > 0) {
|
|
710
|
-
const labelStyle: TextStyle = {
|
|
711
|
-
fontFamily: theme.fonts.family,
|
|
712
|
-
fontSize: 11,
|
|
713
|
-
fontWeight: 400,
|
|
714
|
-
fill: theme.colors.text,
|
|
715
|
-
lineHeight: 1.2,
|
|
716
|
-
};
|
|
717
|
-
|
|
718
|
-
const entries: LegendEntry[] = categories.map((cat) => ({
|
|
719
|
-
label: cat,
|
|
720
|
-
color: categoryColors.get(cat)!,
|
|
721
|
-
shape: 'square' as const,
|
|
722
|
-
}));
|
|
723
|
-
|
|
724
|
-
const legendX = fullArea.x;
|
|
725
|
-
const legendY = fullArea.y + mapAreaHeight + legendReserveGap;
|
|
726
|
-
const legendWidth = fullArea.width;
|
|
727
|
-
const swatchSize = 10;
|
|
728
|
-
const swatchGap = 6;
|
|
729
|
-
const entryGap = 16;
|
|
730
|
-
|
|
731
|
-
legend = {
|
|
732
|
-
type: 'categorical',
|
|
733
|
-
position: 'bottom',
|
|
734
|
-
bounds: { x: legendX, y: legendY, width: legendWidth, height: swatchSize + 6 },
|
|
735
|
-
labelStyle,
|
|
736
|
-
entries,
|
|
737
|
-
swatchSize,
|
|
738
|
-
swatchGap,
|
|
739
|
-
entryGap,
|
|
740
|
-
swatchChipFill: isDarkMode ? 'rgba(255,255,255,0.06)' : 'rgba(0,0,0,0.04)',
|
|
741
|
-
};
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
return { marks, legend };
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
// ---------------------------------------------------------------------------
|
|
748
|
-
// Focus resolution
|
|
749
|
-
// ---------------------------------------------------------------------------
|
|
750
|
-
|
|
751
|
-
function resolveFocus(
|
|
752
|
-
focus: MapFocus | null,
|
|
753
|
-
features: MapFeatureMark[],
|
|
754
|
-
warnings: CompileWarning[],
|
|
755
|
-
): MapFocusLayout | null {
|
|
756
|
-
if (focus === null || focus === undefined) return null;
|
|
757
|
-
|
|
758
|
-
// Normalize to { ids, padding }
|
|
759
|
-
let ids: Array<string | number>;
|
|
760
|
-
let padding = 16;
|
|
761
|
-
|
|
762
|
-
if (typeof focus === 'string' || typeof focus === 'number') {
|
|
763
|
-
ids = [focus];
|
|
764
|
-
} else if (Array.isArray(focus)) {
|
|
765
|
-
ids = focus;
|
|
766
|
-
} else {
|
|
767
|
-
// Object form: { features, padding? }
|
|
768
|
-
const feats = focus.features;
|
|
769
|
-
ids = typeof feats === 'string' || typeof feats === 'number' ? [feats] : feats;
|
|
770
|
-
padding = focus.padding ?? 16;
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
// Find matching features and compute union bounds
|
|
774
|
-
const idSet = new Set(ids.map(String));
|
|
775
|
-
let minX = Infinity;
|
|
776
|
-
let minY = Infinity;
|
|
777
|
-
let maxX = -Infinity;
|
|
778
|
-
let maxY = -Infinity;
|
|
779
|
-
const matchedIds: Array<string | number> = [];
|
|
780
|
-
|
|
781
|
-
for (const f of features) {
|
|
782
|
-
if (idSet.has(String(f.id))) {
|
|
783
|
-
matchedIds.push(f.id);
|
|
784
|
-
minX = Math.min(minX, f.bounds.x);
|
|
785
|
-
minY = Math.min(minY, f.bounds.y);
|
|
786
|
-
maxX = Math.max(maxX, f.bounds.x + f.bounds.width);
|
|
787
|
-
maxY = Math.max(maxY, f.bounds.y + f.bounds.height);
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
// Warn on unmatched ids
|
|
792
|
-
const unmatchedIds = ids.filter((id) => !matchedIds.some((m) => String(m) === String(id)));
|
|
793
|
-
if (unmatchedIds.length > 0) {
|
|
794
|
-
warnings.push({
|
|
795
|
-
code: 'FOCUS_UNMATCHED',
|
|
796
|
-
message: `geo.focus references feature id(s) not found in the map: ${unmatchedIds.join(', ')}`,
|
|
797
|
-
context: { unmatchedIds },
|
|
798
|
-
});
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
if (matchedIds.length === 0) return null;
|
|
802
|
-
|
|
803
|
-
return {
|
|
804
|
-
target: {
|
|
805
|
-
x: minX,
|
|
806
|
-
y: minY,
|
|
807
|
-
width: maxX - minX,
|
|
808
|
-
height: maxY - minY,
|
|
809
|
-
padding,
|
|
810
|
-
},
|
|
811
|
-
ids: matchedIds,
|
|
812
|
-
};
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
// ---------------------------------------------------------------------------
|
|
816
|
-
// Empty layout fallback
|
|
817
|
-
// ---------------------------------------------------------------------------
|
|
818
|
-
|
|
819
|
-
function emptyLayout(
|
|
820
|
-
chrome: ReturnType<typeof computeChrome>,
|
|
821
|
-
theme: ResolvedTheme,
|
|
822
|
-
options: CompileOptions,
|
|
823
|
-
watermark: boolean,
|
|
824
|
-
): MapLayout {
|
|
825
|
-
return {
|
|
826
|
-
area: { x: 0, y: 0, width: 0, height: 0 },
|
|
827
|
-
chrome,
|
|
828
|
-
features: [],
|
|
829
|
-
borders: {
|
|
830
|
-
interiorPath: '',
|
|
831
|
-
outlinePath: '',
|
|
832
|
-
interiorStroke: 'rgba(0,0,0,0.15)',
|
|
833
|
-
outlineStroke: 'rgba(0,0,0,0.3)',
|
|
834
|
-
},
|
|
835
|
-
continuousLegend: null,
|
|
836
|
-
categoricalLegend: null,
|
|
837
|
-
tooltipDescriptors: new Map(),
|
|
838
|
-
a11y: {
|
|
839
|
-
altText: 'Empty map',
|
|
840
|
-
dataTableFallback: [],
|
|
841
|
-
role: 'img',
|
|
842
|
-
keyboardNavigable: false,
|
|
843
|
-
},
|
|
844
|
-
theme,
|
|
845
|
-
width: options.width,
|
|
846
|
-
height: options.height,
|
|
847
|
-
animation: undefined,
|
|
848
|
-
watermark,
|
|
849
|
-
measureText:
|
|
850
|
-
options.measureText ??
|
|
851
|
-
((text, fontSize) => ({ width: estimateTextWidth(text, fontSize), height: fontSize })),
|
|
852
|
-
warnings: [],
|
|
853
|
-
mapSize: { width: 0, height: 0 },
|
|
854
|
-
focus: null,
|
|
855
|
-
};
|
|
856
|
-
}
|