@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/tooltips/compute.ts
DELETED
|
@@ -1,700 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tooltip descriptor computation.
|
|
3
|
-
*
|
|
4
|
-
* Generates a Map of mark-id -> TooltipContent from the spec encoding and marks.
|
|
5
|
-
* Each mark gets a tooltip that shows relevant field values formatted for display.
|
|
6
|
-
* The mark-id keys match the data-mark-id attributes set by the SVG renderer.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
ArcMark,
|
|
11
|
-
AreaMark,
|
|
12
|
-
DataRow,
|
|
13
|
-
Encoding,
|
|
14
|
-
EncodingChannel,
|
|
15
|
-
LineMark,
|
|
16
|
-
Mark,
|
|
17
|
-
NumberFormatter,
|
|
18
|
-
PointMark,
|
|
19
|
-
RectMark,
|
|
20
|
-
TooltipContent,
|
|
21
|
-
TooltipField,
|
|
22
|
-
} from '@opendata-ai/openchart-core';
|
|
23
|
-
import {
|
|
24
|
-
buildTemporalFormatter,
|
|
25
|
-
defaultNumberFormatter,
|
|
26
|
-
formatDate,
|
|
27
|
-
formatPercent,
|
|
28
|
-
getRepresentativeColor,
|
|
29
|
-
} from '@opendata-ai/openchart-core';
|
|
30
|
-
|
|
31
|
-
import type { NormalizedChartSpec } from '../compiler/types';
|
|
32
|
-
import { resolveFieldFormatter } from '../format/field-format';
|
|
33
|
-
|
|
34
|
-
// ---------------------------------------------------------------------------
|
|
35
|
-
// Helpers
|
|
36
|
-
// ---------------------------------------------------------------------------
|
|
37
|
-
|
|
38
|
-
/** Format a raw data value for tooltip display. */
|
|
39
|
-
function formatValue(
|
|
40
|
-
value: unknown,
|
|
41
|
-
fieldType?: string,
|
|
42
|
-
formatter?: NumberFormatter | ((v: Date | string | number) => string) | null,
|
|
43
|
-
): string {
|
|
44
|
-
if (value == null) return '';
|
|
45
|
-
|
|
46
|
-
if (fieldType === 'temporal' || value instanceof Date) {
|
|
47
|
-
if (formatter)
|
|
48
|
-
return (formatter as (v: Date | string | number) => string)(value as Date | string | number);
|
|
49
|
-
return formatDate(value as Date | string | number);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (typeof value === 'number') {
|
|
53
|
-
if (formatter) return (formatter as NumberFormatter)(value);
|
|
54
|
-
return defaultNumberFormatter()(value);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return String(value);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Resolve the display label for an encoding channel: title > axis.title > field name. */
|
|
61
|
-
function resolveLabel(ch: EncodingChannel): string {
|
|
62
|
-
const ax = ch.axis || undefined;
|
|
63
|
-
return ch.title ?? ax?.title ?? ch.field;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Resolve the format string for an encoding channel: format > axis.format. */
|
|
67
|
-
function resolveFormat(ch: EncodingChannel): string | undefined {
|
|
68
|
-
const ax = ch.axis || undefined;
|
|
69
|
-
return ch.format ?? ax?.format;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
type ChannelFormatter = NumberFormatter | ((v: Date | string | number) => string) | null;
|
|
73
|
-
|
|
74
|
-
function cacheKey(ch: EncodingChannel): string {
|
|
75
|
-
const fmt = resolveFormat(ch);
|
|
76
|
-
return fmt ? `${ch.field}::${ch.type}::${fmt}` : `${ch.field}::${ch.type}`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Build a per-channel formatter cache for tooltip display. */
|
|
80
|
-
function buildFormatterCache(
|
|
81
|
-
data: DataRow[],
|
|
82
|
-
channels: EncodingChannel[],
|
|
83
|
-
): Map<string, ChannelFormatter> {
|
|
84
|
-
const cache = new Map<string, ChannelFormatter>();
|
|
85
|
-
for (const ch of channels) {
|
|
86
|
-
const key = cacheKey(ch);
|
|
87
|
-
if (cache.has(key)) continue;
|
|
88
|
-
const fmt = resolveFormat(ch);
|
|
89
|
-
if (ch.type === 'temporal') {
|
|
90
|
-
const temporalFmt = buildTemporalFormatter(fmt);
|
|
91
|
-
cache.set(key, temporalFmt ?? null);
|
|
92
|
-
} else if (ch.type === 'quantitative') {
|
|
93
|
-
cache.set(
|
|
94
|
-
key,
|
|
95
|
-
resolveFieldFormatter({
|
|
96
|
-
surfaceFormat: fmt,
|
|
97
|
-
values: data.map((r) => r[ch.field]),
|
|
98
|
-
}),
|
|
99
|
-
);
|
|
100
|
-
} else {
|
|
101
|
-
cache.set(key, null);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return cache;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function getFormatter(
|
|
108
|
-
cache: Map<string, ChannelFormatter>,
|
|
109
|
-
ch: EncodingChannel,
|
|
110
|
-
): ChannelFormatter | undefined {
|
|
111
|
-
return cache.get(cacheKey(ch));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** Build tooltip fields from explicit tooltip encoding channels. */
|
|
115
|
-
function buildExplicitTooltipFields(
|
|
116
|
-
row: DataRow,
|
|
117
|
-
channels: EncodingChannel[],
|
|
118
|
-
formatters: Map<string, ChannelFormatter>,
|
|
119
|
-
): TooltipField[] {
|
|
120
|
-
return channels.map((ch) => ({
|
|
121
|
-
label: resolveLabel(ch),
|
|
122
|
-
value: formatValue(row[ch.field], ch.type, getFormatter(formatters, ch)),
|
|
123
|
-
}));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** Build tooltip fields from a data row based on the spec encoding. */
|
|
127
|
-
function buildFields(
|
|
128
|
-
row: DataRow,
|
|
129
|
-
encoding: Encoding,
|
|
130
|
-
formatters: Map<string, ChannelFormatter>,
|
|
131
|
-
color?: string,
|
|
132
|
-
): TooltipField[] {
|
|
133
|
-
if (encoding.tooltip) {
|
|
134
|
-
const channels = Array.isArray(encoding.tooltip) ? encoding.tooltip : [encoding.tooltip];
|
|
135
|
-
return buildExplicitTooltipFields(row, channels, formatters);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const fields: TooltipField[] = [];
|
|
139
|
-
|
|
140
|
-
if (encoding.color && 'field' in encoding.color) {
|
|
141
|
-
fields.push({
|
|
142
|
-
label: resolveLabel(encoding.color),
|
|
143
|
-
value: formatValue(
|
|
144
|
-
row[encoding.color.field],
|
|
145
|
-
encoding.color.type,
|
|
146
|
-
getFormatter(formatters, encoding.color),
|
|
147
|
-
),
|
|
148
|
-
color,
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (encoding.y) {
|
|
153
|
-
fields.push({
|
|
154
|
-
label: resolveLabel(encoding.y),
|
|
155
|
-
value: formatValue(
|
|
156
|
-
row[encoding.y.field],
|
|
157
|
-
encoding.y.type,
|
|
158
|
-
getFormatter(formatters, encoding.y),
|
|
159
|
-
),
|
|
160
|
-
color: encoding.color ? undefined : color,
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (encoding.x) {
|
|
165
|
-
fields.push({
|
|
166
|
-
label: resolveLabel(encoding.x),
|
|
167
|
-
value: formatValue(
|
|
168
|
-
row[encoding.x.field],
|
|
169
|
-
encoding.x.type,
|
|
170
|
-
getFormatter(formatters, encoding.x),
|
|
171
|
-
),
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (encoding.size && 'field' in encoding.size) {
|
|
176
|
-
fields.push({
|
|
177
|
-
label: resolveLabel(encoding.size),
|
|
178
|
-
value: formatValue(
|
|
179
|
-
row[encoding.size.field],
|
|
180
|
-
encoding.size.type,
|
|
181
|
-
getFormatter(formatters, encoding.size),
|
|
182
|
-
),
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return fields;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** Determine the title for a tooltip based on encoding. */
|
|
190
|
-
function getTooltipTitle(row: DataRow, encoding: Encoding): string | undefined {
|
|
191
|
-
// Detail channel provides an explicit label (e.g. district name in scatter)
|
|
192
|
-
if (encoding.detail) {
|
|
193
|
-
return String(row[encoding.detail.field] ?? '');
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// For charts with a temporal x-axis, use the date as the title
|
|
197
|
-
if (encoding.x?.type === 'temporal') {
|
|
198
|
-
return formatValue(row[encoding.x.field], 'temporal');
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// For nominal x, use the category
|
|
202
|
-
if (encoding.x?.type === 'nominal' || encoding.x?.type === 'ordinal') {
|
|
203
|
-
return String(row[encoding.x.field] ?? '');
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// For nominal y (e.g. horizontal bar charts), use the category
|
|
207
|
-
if (encoding.y?.type === 'nominal' || encoding.y?.type === 'ordinal') {
|
|
208
|
-
return String(row[encoding.y.field] ?? '');
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// For scatter/bubble (both axes quantitative), find a name-like string field
|
|
212
|
-
// in the data row that isn't already used by an encoding channel
|
|
213
|
-
if (encoding.x?.type === 'quantitative' && encoding.y?.type === 'quantitative') {
|
|
214
|
-
const encodedFields = new Set(
|
|
215
|
-
[encoding.x, encoding.y, encoding.color, encoding.size, encoding.detail]
|
|
216
|
-
.filter((ch): ch is EncodingChannel => !!ch && 'field' in ch)
|
|
217
|
-
.map((ch) => ch.field),
|
|
218
|
-
);
|
|
219
|
-
for (const [key, value] of Object.entries(row)) {
|
|
220
|
-
if (!encodedFields.has(key) && typeof value === 'string') {
|
|
221
|
-
return value;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// For color-encoded series, use the series name (skip conditional defs)
|
|
227
|
-
if (encoding.color && 'field' in encoding.color) {
|
|
228
|
-
return String(row[encoding.color.field] ?? '');
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return undefined;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// ---------------------------------------------------------------------------
|
|
235
|
-
// Per-mark-type tooltip generation
|
|
236
|
-
// ---------------------------------------------------------------------------
|
|
237
|
-
|
|
238
|
-
function tooltipsForLine(
|
|
239
|
-
mark: LineMark,
|
|
240
|
-
encoding: Encoding,
|
|
241
|
-
formatters: Map<string, ChannelFormatter>,
|
|
242
|
-
_markIndex: number,
|
|
243
|
-
): Array<[string, TooltipContent]> {
|
|
244
|
-
if (mark.dataPoints) {
|
|
245
|
-
for (const dp of mark.dataPoints) {
|
|
246
|
-
dp.tooltip = {
|
|
247
|
-
title: getTooltipTitle(dp.datum, encoding),
|
|
248
|
-
fields: buildFields(dp.datum, encoding, formatters, mark.stroke),
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return [];
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function tooltipsForPoint(
|
|
256
|
-
mark: PointMark,
|
|
257
|
-
encoding: Encoding,
|
|
258
|
-
formatters: Map<string, ChannelFormatter>,
|
|
259
|
-
markIndex: number,
|
|
260
|
-
): Array<[string, TooltipContent]> {
|
|
261
|
-
const title = getTooltipTitle(mark.data, encoding);
|
|
262
|
-
const fields = buildFields(mark.data, encoding, formatters, getRepresentativeColor(mark.fill));
|
|
263
|
-
|
|
264
|
-
return [[`point-${markIndex}`, { title, fields }]];
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function tooltipsForRect(
|
|
268
|
-
mark: RectMark,
|
|
269
|
-
encoding: Encoding,
|
|
270
|
-
formatters: Map<string, ChannelFormatter>,
|
|
271
|
-
markIndex: number,
|
|
272
|
-
): Array<[string, TooltipContent]> {
|
|
273
|
-
const title = getTooltipTitle(mark.data, encoding);
|
|
274
|
-
const fields = buildFields(mark.data, encoding, formatters, getRepresentativeColor(mark.fill));
|
|
275
|
-
|
|
276
|
-
return [[`rect-${markIndex}`, { title, fields }]];
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
function tooltipsForArc(
|
|
280
|
-
mark: ArcMark,
|
|
281
|
-
encoding: Encoding,
|
|
282
|
-
formatters: Map<string, ChannelFormatter>,
|
|
283
|
-
markIndex: number,
|
|
284
|
-
): Array<[string, TooltipContent]> {
|
|
285
|
-
const row = mark.data;
|
|
286
|
-
const fields: TooltipField[] = [];
|
|
287
|
-
|
|
288
|
-
const colorEnc = encoding.color && 'field' in encoding.color ? encoding.color : undefined;
|
|
289
|
-
if (colorEnc) {
|
|
290
|
-
const categoryName = String(row[colorEnc.field] ?? '');
|
|
291
|
-
if (encoding.y) {
|
|
292
|
-
fields.push({
|
|
293
|
-
label: categoryName,
|
|
294
|
-
value: formatValue(
|
|
295
|
-
row[encoding.y.field],
|
|
296
|
-
encoding.y.type,
|
|
297
|
-
getFormatter(formatters, encoding.y),
|
|
298
|
-
),
|
|
299
|
-
color: getRepresentativeColor(mark.fill),
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
} else if (encoding.y) {
|
|
303
|
-
fields.push({
|
|
304
|
-
label: resolveLabel(encoding.y),
|
|
305
|
-
value: formatValue(
|
|
306
|
-
row[encoding.y.field],
|
|
307
|
-
encoding.y.type,
|
|
308
|
-
getFormatter(formatters, encoding.y),
|
|
309
|
-
),
|
|
310
|
-
color: getRepresentativeColor(mark.fill),
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const title = colorEnc ? String(row[colorEnc.field] ?? '') : undefined;
|
|
315
|
-
|
|
316
|
-
return [[`arc-${markIndex}`, { title, fields }]];
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function tooltipsForArea(
|
|
320
|
-
mark: AreaMark,
|
|
321
|
-
encoding: Encoding,
|
|
322
|
-
formatters: Map<string, ChannelFormatter>,
|
|
323
|
-
_markIndex: number,
|
|
324
|
-
): Array<[string, TooltipContent]> {
|
|
325
|
-
if (mark.dataPoints) {
|
|
326
|
-
for (const dp of mark.dataPoints) {
|
|
327
|
-
dp.tooltip = {
|
|
328
|
-
title: getTooltipTitle(dp.datum, encoding),
|
|
329
|
-
fields: buildFields(dp.datum, encoding, formatters, getRepresentativeColor(mark.fill)),
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
return [];
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// ---------------------------------------------------------------------------
|
|
337
|
-
// Range mark tooltips (start / end / delta)
|
|
338
|
-
// ---------------------------------------------------------------------------
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Build tooltip descriptors for range marks (dumbbell / arrow / range bar).
|
|
342
|
-
*
|
|
343
|
-
* Every mark of a range row (dots, connector rule, bar rect) shares one
|
|
344
|
-
* tooltip showing the start value, the end value, and the signed delta.
|
|
345
|
-
* An explicit `encoding.tooltip` overrides the default field set.
|
|
346
|
-
*/
|
|
347
|
-
function computeRangeTooltips(
|
|
348
|
-
spec: NormalizedChartSpec,
|
|
349
|
-
marks: Mark[],
|
|
350
|
-
): Map<string, TooltipContent> {
|
|
351
|
-
const encoding = spec.encoding as Encoding;
|
|
352
|
-
const descriptors = new Map<string, TooltipContent>();
|
|
353
|
-
|
|
354
|
-
const horizontal = encoding.y?.type === 'nominal' || encoding.y?.type === 'ordinal';
|
|
355
|
-
const startCh = horizontal ? encoding.x : encoding.y;
|
|
356
|
-
const endCh = horizontal ? encoding.x2 : encoding.y2;
|
|
357
|
-
if (!startCh || !endCh) return descriptors;
|
|
358
|
-
|
|
359
|
-
const allChannels = [startCh, endCh, encoding.color, encoding.tooltip]
|
|
360
|
-
.flat()
|
|
361
|
-
.filter((ch): ch is EncodingChannel => !!ch && 'field' in ch);
|
|
362
|
-
const fmtCache = buildFormatterCache(spec.data, allChannels);
|
|
363
|
-
const startFmt = getFormatter(fmtCache, startCh);
|
|
364
|
-
|
|
365
|
-
const contentFor = (row: DataRow): TooltipContent => {
|
|
366
|
-
const title = getTooltipTitle(row, encoding);
|
|
367
|
-
if (encoding.tooltip) {
|
|
368
|
-
const channels = Array.isArray(encoding.tooltip) ? encoding.tooltip : [encoding.tooltip];
|
|
369
|
-
return { title, fields: buildExplicitTooltipFields(row, channels, fmtCache) };
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
const fields: TooltipField[] = [
|
|
373
|
-
{
|
|
374
|
-
label: resolveLabel(startCh),
|
|
375
|
-
value: formatValue(row[startCh.field], startCh.type, getFormatter(fmtCache, startCh)),
|
|
376
|
-
},
|
|
377
|
-
{
|
|
378
|
-
label: resolveLabel(endCh),
|
|
379
|
-
value: formatValue(row[endCh.field], endCh.type, getFormatter(fmtCache, endCh)),
|
|
380
|
-
},
|
|
381
|
-
];
|
|
382
|
-
|
|
383
|
-
const startVal = Number(row[startCh.field]);
|
|
384
|
-
const endVal = Number(row[endCh.field]);
|
|
385
|
-
if (Number.isFinite(startVal) && Number.isFinite(endVal)) {
|
|
386
|
-
const delta = endVal - startVal;
|
|
387
|
-
const formatted = startFmt
|
|
388
|
-
? (startFmt as NumberFormatter)(delta)
|
|
389
|
-
: defaultNumberFormatter()(delta);
|
|
390
|
-
fields.push({ label: 'Change', value: delta > 0 ? `+${formatted}` : formatted });
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
return { title, fields };
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
for (let i = 0; i < marks.length; i++) {
|
|
397
|
-
const mark = marks[i];
|
|
398
|
-
if (mark.type !== 'point' && mark.type !== 'rect' && mark.type !== 'rule') continue;
|
|
399
|
-
descriptors.set(`${mark.type}-${i}`, contentFor(mark.data as DataRow));
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
return descriptors;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* Compute per-day calendar heatmap tooltips.
|
|
407
|
-
*
|
|
408
|
-
* Data cells get a formatted date title plus the color value; empty cells
|
|
409
|
-
* (missing days, marked decorative by the calendar renderer) get no
|
|
410
|
-
* descriptor at all so no tooltip fires on them. An explicit
|
|
411
|
-
* `encoding.tooltip` overrides the default field set.
|
|
412
|
-
*/
|
|
413
|
-
function computeCalendarTooltips(
|
|
414
|
-
spec: NormalizedChartSpec,
|
|
415
|
-
marks: Mark[],
|
|
416
|
-
): Map<string, TooltipContent> {
|
|
417
|
-
const encoding = spec.encoding as Encoding;
|
|
418
|
-
const descriptors = new Map<string, TooltipContent>();
|
|
419
|
-
|
|
420
|
-
const xEnc = encoding.x;
|
|
421
|
-
const colorEnc = encoding.color && 'field' in encoding.color ? encoding.color : undefined;
|
|
422
|
-
if (!xEnc || !colorEnc) return descriptors;
|
|
423
|
-
|
|
424
|
-
const allChannels = [
|
|
425
|
-
xEnc,
|
|
426
|
-
colorEnc,
|
|
427
|
-
...(encoding.tooltip
|
|
428
|
-
? Array.isArray(encoding.tooltip)
|
|
429
|
-
? encoding.tooltip
|
|
430
|
-
: [encoding.tooltip]
|
|
431
|
-
: []),
|
|
432
|
-
].filter((ch): ch is EncodingChannel => !!ch && 'field' in ch);
|
|
433
|
-
const fmtCache = buildFormatterCache(spec.data, allChannels);
|
|
434
|
-
|
|
435
|
-
for (let i = 0; i < marks.length; i++) {
|
|
436
|
-
const mark = marks[i];
|
|
437
|
-
if (mark.type !== 'rect' || mark.aria.decorative) continue;
|
|
438
|
-
const row = mark.data as DataRow;
|
|
439
|
-
|
|
440
|
-
const title = formatValue(row[xEnc.field], 'temporal', getFormatter(fmtCache, xEnc));
|
|
441
|
-
if (encoding.tooltip) {
|
|
442
|
-
const channels = Array.isArray(encoding.tooltip) ? encoding.tooltip : [encoding.tooltip];
|
|
443
|
-
descriptors.set(`rect-${i}`, {
|
|
444
|
-
title,
|
|
445
|
-
fields: buildExplicitTooltipFields(row, channels, fmtCache),
|
|
446
|
-
});
|
|
447
|
-
continue;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
descriptors.set(`rect-${i}`, {
|
|
451
|
-
title,
|
|
452
|
-
fields: [
|
|
453
|
-
{
|
|
454
|
-
label: resolveLabel(colorEnc),
|
|
455
|
-
value: formatValue(row[colorEnc.field], colorEnc.type, getFormatter(fmtCache, colorEnc)),
|
|
456
|
-
color: getRepresentativeColor(mark.fill),
|
|
457
|
-
},
|
|
458
|
-
],
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
return descriptors;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
// ---------------------------------------------------------------------------
|
|
466
|
-
// Waffle mark tooltips (one shared tooltip per category)
|
|
467
|
-
// ---------------------------------------------------------------------------
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
* Build tooltip descriptors for waffle marks.
|
|
471
|
-
*
|
|
472
|
-
* Every cell of a category shares literally one TooltipContent object, so
|
|
473
|
-
* the cells act as a single hover target: moving across a category's cells
|
|
474
|
-
* keeps showing the same tooltip. Content: the category's value plus its
|
|
475
|
-
* "x of N units" cell count. An explicit `encoding.tooltip` overrides the
|
|
476
|
-
* default field set.
|
|
477
|
-
*/
|
|
478
|
-
function computeWaffleTooltips(
|
|
479
|
-
spec: NormalizedChartSpec,
|
|
480
|
-
marks: Mark[],
|
|
481
|
-
): Map<string, TooltipContent> {
|
|
482
|
-
const encoding = spec.encoding as Encoding;
|
|
483
|
-
const descriptors = new Map<string, TooltipContent>();
|
|
484
|
-
|
|
485
|
-
const colorEnc = encoding.color && 'field' in encoding.color ? encoding.color : undefined;
|
|
486
|
-
const valueCh = encoding.y ?? encoding.x;
|
|
487
|
-
if (!colorEnc || !valueCh) return descriptors;
|
|
488
|
-
|
|
489
|
-
const allChannels = [
|
|
490
|
-
valueCh,
|
|
491
|
-
colorEnc,
|
|
492
|
-
...(encoding.tooltip
|
|
493
|
-
? Array.isArray(encoding.tooltip)
|
|
494
|
-
? encoding.tooltip
|
|
495
|
-
: [encoding.tooltip]
|
|
496
|
-
: []),
|
|
497
|
-
].filter((ch): ch is EncodingChannel => !!ch && 'field' in ch);
|
|
498
|
-
const fmtCache = buildFormatterCache(spec.data, allChannels);
|
|
499
|
-
|
|
500
|
-
const units = Math.max(1, Math.round(spec.markDef.units ?? 100));
|
|
501
|
-
|
|
502
|
-
const cellCounts = new Map<string, number>();
|
|
503
|
-
for (const mark of marks) {
|
|
504
|
-
if (mark.type !== 'rect') continue;
|
|
505
|
-
const category = String((mark.data as DataRow)[colorEnc.field] ?? '');
|
|
506
|
-
cellCounts.set(category, (cellCounts.get(category) ?? 0) + 1);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
const contentByCategory = new Map<string, TooltipContent>();
|
|
510
|
-
for (let i = 0; i < marks.length; i++) {
|
|
511
|
-
const mark = marks[i];
|
|
512
|
-
if (mark.type !== 'rect') continue;
|
|
513
|
-
|
|
514
|
-
const row = mark.data as DataRow;
|
|
515
|
-
const category = String(row[colorEnc.field] ?? '');
|
|
516
|
-
let content = contentByCategory.get(category);
|
|
517
|
-
if (!content) {
|
|
518
|
-
if (encoding.tooltip) {
|
|
519
|
-
const channels = Array.isArray(encoding.tooltip) ? encoding.tooltip : [encoding.tooltip];
|
|
520
|
-
content = { title: category, fields: buildExplicitTooltipFields(row, channels, fmtCache) };
|
|
521
|
-
} else {
|
|
522
|
-
content = {
|
|
523
|
-
title: category,
|
|
524
|
-
fields: [
|
|
525
|
-
{
|
|
526
|
-
label: resolveLabel(valueCh),
|
|
527
|
-
value: formatValue(row[valueCh.field], valueCh.type, getFormatter(fmtCache, valueCh)),
|
|
528
|
-
color: getRepresentativeColor(mark.fill),
|
|
529
|
-
},
|
|
530
|
-
{ label: 'Share', value: `${cellCounts.get(category) ?? 0} of ${units} units` },
|
|
531
|
-
],
|
|
532
|
-
};
|
|
533
|
-
}
|
|
534
|
-
contentByCategory.set(category, content);
|
|
535
|
-
}
|
|
536
|
-
descriptors.set(`rect-${i}`, content);
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
return descriptors;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
// ---------------------------------------------------------------------------
|
|
543
|
-
// Parliament mark tooltips (one shared tooltip per party)
|
|
544
|
-
// ---------------------------------------------------------------------------
|
|
545
|
-
|
|
546
|
-
/**
|
|
547
|
-
* Build tooltip descriptors for parliament (hemicycle) marks.
|
|
548
|
-
*
|
|
549
|
-
* Every seat dot of a party shares one TooltipContent object, so hovering any
|
|
550
|
-
* of a party's seats shows the same tooltip: the seat count and its share of
|
|
551
|
-
* the chamber. Keys match the renderer's `point-${index}` mark ids. The
|
|
552
|
-
* majority line/label (rule/textMark) get no descriptor. An explicit
|
|
553
|
-
* `encoding.tooltip` overrides the default field set.
|
|
554
|
-
*/
|
|
555
|
-
function computeParliamentTooltips(
|
|
556
|
-
spec: NormalizedChartSpec,
|
|
557
|
-
marks: Mark[],
|
|
558
|
-
): Map<string, TooltipContent> {
|
|
559
|
-
const encoding = spec.encoding as Encoding;
|
|
560
|
-
const descriptors = new Map<string, TooltipContent>();
|
|
561
|
-
|
|
562
|
-
const colorEnc = encoding.color && 'field' in encoding.color ? encoding.color : undefined;
|
|
563
|
-
const valueCh = encoding.y ?? encoding.x;
|
|
564
|
-
if (!colorEnc || !valueCh) return descriptors;
|
|
565
|
-
|
|
566
|
-
const allChannels = [
|
|
567
|
-
valueCh,
|
|
568
|
-
colorEnc,
|
|
569
|
-
...(encoding.tooltip
|
|
570
|
-
? Array.isArray(encoding.tooltip)
|
|
571
|
-
? encoding.tooltip
|
|
572
|
-
: [encoding.tooltip]
|
|
573
|
-
: []),
|
|
574
|
-
].filter((ch): ch is EncodingChannel => !!ch && 'field' in ch);
|
|
575
|
-
const fmtCache = buildFormatterCache(spec.data, allChannels);
|
|
576
|
-
|
|
577
|
-
const seatCounts = new Map<string, number>();
|
|
578
|
-
for (const mark of marks) {
|
|
579
|
-
if (mark.type !== 'point') continue;
|
|
580
|
-
const party = String((mark.data as DataRow)[colorEnc.field] ?? '');
|
|
581
|
-
seatCounts.set(party, (seatCounts.get(party) ?? 0) + 1);
|
|
582
|
-
}
|
|
583
|
-
const totalSeats = [...seatCounts.values()].reduce((s, c) => s + c, 0);
|
|
584
|
-
|
|
585
|
-
const contentByParty = new Map<string, TooltipContent>();
|
|
586
|
-
for (let i = 0; i < marks.length; i++) {
|
|
587
|
-
const mark = marks[i];
|
|
588
|
-
if (mark.type !== 'point') continue;
|
|
589
|
-
|
|
590
|
-
const row = mark.data as DataRow;
|
|
591
|
-
const party = String(row[colorEnc.field] ?? '');
|
|
592
|
-
let content = contentByParty.get(party);
|
|
593
|
-
if (!content) {
|
|
594
|
-
if (encoding.tooltip) {
|
|
595
|
-
const channels = Array.isArray(encoding.tooltip) ? encoding.tooltip : [encoding.tooltip];
|
|
596
|
-
content = { title: party, fields: buildExplicitTooltipFields(row, channels, fmtCache) };
|
|
597
|
-
} else {
|
|
598
|
-
const count = seatCounts.get(party) ?? 0;
|
|
599
|
-
const share = totalSeats > 0 ? formatPercent(count / totalSeats) : '';
|
|
600
|
-
content = {
|
|
601
|
-
title: party,
|
|
602
|
-
fields: [
|
|
603
|
-
{
|
|
604
|
-
label: resolveLabel(valueCh),
|
|
605
|
-
value: formatValue(row[valueCh.field], valueCh.type, getFormatter(fmtCache, valueCh)),
|
|
606
|
-
color: getRepresentativeColor(mark.fill),
|
|
607
|
-
},
|
|
608
|
-
{ label: 'Share', value: share },
|
|
609
|
-
],
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
|
-
contentByParty.set(party, content);
|
|
613
|
-
}
|
|
614
|
-
descriptors.set(`point-${i}`, content);
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
return descriptors;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
// ---------------------------------------------------------------------------
|
|
621
|
-
// Public API
|
|
622
|
-
// ---------------------------------------------------------------------------
|
|
623
|
-
|
|
624
|
-
/**
|
|
625
|
-
* Compute tooltip descriptors for all marks in the layout.
|
|
626
|
-
*
|
|
627
|
-
* Returns a Map keyed by data-mark-id (matching the SVG attribute)
|
|
628
|
-
* to TooltipContent objects. The vanilla adapter uses this to show
|
|
629
|
-
* tooltips on hover/tap/keyboard focus.
|
|
630
|
-
*/
|
|
631
|
-
export function computeTooltipDescriptors(
|
|
632
|
-
spec: NormalizedChartSpec,
|
|
633
|
-
marks: Mark[],
|
|
634
|
-
): Map<string, TooltipContent> {
|
|
635
|
-
// Range marks share one start/end/delta tooltip across every mark of a row.
|
|
636
|
-
if (spec.markType === 'range') {
|
|
637
|
-
return computeRangeTooltips(spec, marks);
|
|
638
|
-
}
|
|
639
|
-
// Calendar heatmaps get one date-titled tooltip per data cell; empty
|
|
640
|
-
// (missing-day) cells get none.
|
|
641
|
-
if (spec.markType === 'calendar') {
|
|
642
|
-
return computeCalendarTooltips(spec, marks);
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
// Waffle cells share one tooltip per category (the cells act as one target).
|
|
646
|
-
if (spec.markType === 'waffle') {
|
|
647
|
-
return computeWaffleTooltips(spec, marks);
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
// Parliament seats share one tooltip per party (all of a party's seats act
|
|
651
|
-
// as one target).
|
|
652
|
-
if (spec.markType === 'parliament') {
|
|
653
|
-
return computeParliamentTooltips(spec, marks);
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
const encoding = spec.encoding as Encoding;
|
|
657
|
-
const descriptors = new Map<string, TooltipContent>();
|
|
658
|
-
|
|
659
|
-
const allChannels = [
|
|
660
|
-
encoding.x,
|
|
661
|
-
encoding.y,
|
|
662
|
-
encoding.color && 'field' in encoding.color ? encoding.color : undefined,
|
|
663
|
-
encoding.size && 'field' in encoding.size ? encoding.size : undefined,
|
|
664
|
-
...(encoding.tooltip
|
|
665
|
-
? Array.isArray(encoding.tooltip)
|
|
666
|
-
? encoding.tooltip
|
|
667
|
-
: [encoding.tooltip]
|
|
668
|
-
: []),
|
|
669
|
-
].filter((ch): ch is EncodingChannel => !!ch && 'field' in ch);
|
|
670
|
-
const formatters = buildFormatterCache(spec.data, allChannels);
|
|
671
|
-
|
|
672
|
-
for (let i = 0; i < marks.length; i++) {
|
|
673
|
-
const mark = marks[i];
|
|
674
|
-
let entries: Array<[string, TooltipContent]> = [];
|
|
675
|
-
|
|
676
|
-
switch (mark.type) {
|
|
677
|
-
case 'line':
|
|
678
|
-
entries = tooltipsForLine(mark, encoding, formatters, i);
|
|
679
|
-
break;
|
|
680
|
-
case 'area':
|
|
681
|
-
entries = tooltipsForArea(mark, encoding, formatters, i);
|
|
682
|
-
break;
|
|
683
|
-
case 'point':
|
|
684
|
-
entries = tooltipsForPoint(mark, encoding, formatters, i);
|
|
685
|
-
break;
|
|
686
|
-
case 'rect':
|
|
687
|
-
entries = tooltipsForRect(mark, encoding, formatters, i);
|
|
688
|
-
break;
|
|
689
|
-
case 'arc':
|
|
690
|
-
entries = tooltipsForArc(mark, encoding, formatters, i);
|
|
691
|
-
break;
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
for (const [key, content] of entries) {
|
|
695
|
-
descriptors.set(key, content);
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
return descriptors;
|
|
700
|
-
}
|