@opendata-ai/openchart-engine 8.0.0-rc.9 → 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.
Files changed (271) hide show
  1. package/dist/index.d.ts +149 -12
  2. package/dist/index.js +804 -155
  3. package/dist/index.js.map +1 -1
  4. package/package.json +3 -4
  5. package/src/__test-fixtures__/specs.ts +0 -182
  6. package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +0 -2401
  7. package/src/__tests__/annotation-behavior.test.ts +0 -852
  8. package/src/__tests__/axes.test.ts +0 -1579
  9. package/src/__tests__/compile-animation.test.ts +0 -161
  10. package/src/__tests__/compile-chart.test.ts +0 -1303
  11. package/src/__tests__/compile-footnotes.test.ts +0 -277
  12. package/src/__tests__/compile-layer.test.ts +0 -969
  13. package/src/__tests__/compile-snapshot.test.ts +0 -156
  14. package/src/__tests__/compound-labels.test.ts +0 -147
  15. package/src/__tests__/dimensions.test.ts +0 -918
  16. package/src/__tests__/encoding-sugar.test.ts +0 -390
  17. package/src/__tests__/facet.test.ts +0 -215
  18. package/src/__tests__/helpers/invariants.ts +0 -299
  19. package/src/__tests__/known-bugs.test.ts +0 -566
  20. package/src/__tests__/layout-fuzz.test.ts +0 -358
  21. package/src/__tests__/layout-invariants.test.ts +0 -535
  22. package/src/__tests__/legend.test.ts +0 -721
  23. package/src/__tests__/mark-keys.test.ts +0 -294
  24. package/src/__tests__/ranking-recipes.test.ts +0 -260
  25. package/src/__tests__/rotation.test.ts +0 -83
  26. package/src/__tests__/scales.test.ts +0 -323
  27. package/src/__tests__/series-search.test.ts +0 -92
  28. package/src/__tests__/spec-sugar.test.ts +0 -801
  29. package/src/__tests__/you-draw-it.test.ts +0 -160
  30. package/src/annotations/__tests__/compute.test.ts +0 -1672
  31. package/src/annotations/__tests__/geometry.test.ts +0 -66
  32. package/src/annotations/__tests__/placement-fuzz.test.ts +0 -256
  33. package/src/annotations/__tests__/placement-invariants.test.ts +0 -345
  34. package/src/annotations/__tests__/placement.test.ts +0 -316
  35. package/src/annotations/__tests__/resolve-text.test.ts +0 -788
  36. package/src/annotations/__tests__/rich-text.test.ts +0 -91
  37. package/src/annotations/__tests__/thinning.test.ts +0 -285
  38. package/src/annotations/collisions.ts +0 -326
  39. package/src/annotations/compute.ts +0 -333
  40. package/src/annotations/constants.ts +0 -162
  41. package/src/annotations/geometry.ts +0 -376
  42. package/src/annotations/move.ts +0 -135
  43. package/src/annotations/placement.ts +0 -636
  44. package/src/annotations/position.ts +0 -131
  45. package/src/annotations/resolve-range.ts +0 -117
  46. package/src/annotations/resolve-refline.ts +0 -157
  47. package/src/annotations/resolve-text.ts +0 -545
  48. package/src/annotations/rich-text.ts +0 -65
  49. package/src/annotations/thinning.ts +0 -190
  50. package/src/barlist/__tests__/compile-barlist.test.ts +0 -200
  51. package/src/barlist/compile-barlist.ts +0 -391
  52. package/src/barlist/types.ts +0 -28
  53. package/src/charts/__tests__/post-process.test.ts +0 -237
  54. package/src/charts/__tests__/registry.test.ts +0 -116
  55. package/src/charts/__tests__/utils.test.ts +0 -195
  56. package/src/charts/_shared/__tests__/density-filter.test.ts +0 -32
  57. package/src/charts/_shared/density-filter.ts +0 -26
  58. package/src/charts/_shared/format-label-value.ts +0 -16
  59. package/src/charts/bar/__tests__/compute.test.ts +0 -934
  60. package/src/charts/bar/__tests__/gradient-orient.test.ts +0 -127
  61. package/src/charts/bar/__tests__/labels.test.ts +0 -292
  62. package/src/charts/bar/compute.ts +0 -539
  63. package/src/charts/bar/index.ts +0 -60
  64. package/src/charts/bar/labels.ts +0 -311
  65. package/src/charts/beeswarm/__tests__/compute.test.ts +0 -280
  66. package/src/charts/beeswarm/__tests__/dodge.test.ts +0 -148
  67. package/src/charts/beeswarm/compute.ts +0 -203
  68. package/src/charts/beeswarm/dodge.ts +0 -155
  69. package/src/charts/beeswarm/index.ts +0 -32
  70. package/src/charts/builtin.ts +0 -75
  71. package/src/charts/calendar/__tests__/compute.test.ts +0 -417
  72. package/src/charts/calendar/compute.ts +0 -319
  73. package/src/charts/calendar/index.ts +0 -32
  74. package/src/charts/column/__tests__/compute.test.ts +0 -540
  75. package/src/charts/column/__tests__/labels.test.ts +0 -179
  76. package/src/charts/column/compute.ts +0 -493
  77. package/src/charts/column/index.ts +0 -53
  78. package/src/charts/column/labels.ts +0 -229
  79. package/src/charts/dot/__tests__/compute.test.ts +0 -380
  80. package/src/charts/dot/__tests__/labels.test.ts +0 -98
  81. package/src/charts/dot/compute.ts +0 -285
  82. package/src/charts/dot/index.ts +0 -62
  83. package/src/charts/dot/labels.ts +0 -121
  84. package/src/charts/line/__tests__/compute.test.ts +0 -1194
  85. package/src/charts/line/__tests__/curves.test.ts +0 -51
  86. package/src/charts/line/__tests__/labels.test.ts +0 -94
  87. package/src/charts/line/__tests__/mark-options.test.ts +0 -175
  88. package/src/charts/line/area.ts +0 -484
  89. package/src/charts/line/compute.ts +0 -356
  90. package/src/charts/line/curves.ts +0 -85
  91. package/src/charts/line/index.ts +0 -176
  92. package/src/charts/line/labels.ts +0 -177
  93. package/src/charts/parliament/__tests__/compute.test.ts +0 -411
  94. package/src/charts/parliament/compute.ts +0 -313
  95. package/src/charts/parliament/index.ts +0 -32
  96. package/src/charts/pie/__tests__/compute.test.ts +0 -384
  97. package/src/charts/pie/__tests__/labels.test.ts +0 -132
  98. package/src/charts/pie/compute.ts +0 -324
  99. package/src/charts/pie/index.ts +0 -49
  100. package/src/charts/pie/labels.ts +0 -143
  101. package/src/charts/post-process.ts +0 -229
  102. package/src/charts/range/__tests__/compute.test.ts +0 -486
  103. package/src/charts/range/compute.ts +0 -273
  104. package/src/charts/range/index.ts +0 -82
  105. package/src/charts/range/labels.ts +0 -169
  106. package/src/charts/rect/__tests__/compute.test.ts +0 -120
  107. package/src/charts/rect/index.ts +0 -127
  108. package/src/charts/registry.ts +0 -72
  109. package/src/charts/rule/index.ts +0 -139
  110. package/src/charts/scatter/__tests__/compute.test.ts +0 -488
  111. package/src/charts/scatter/__tests__/trendline.test.ts +0 -191
  112. package/src/charts/scatter/compute.ts +0 -192
  113. package/src/charts/scatter/index.ts +0 -46
  114. package/src/charts/scatter/trendline.ts +0 -100
  115. package/src/charts/text/__tests__/compute.test.ts +0 -307
  116. package/src/charts/text/index.ts +0 -141
  117. package/src/charts/tick/__tests__/compute.test.ts +0 -75
  118. package/src/charts/tick/index.ts +0 -99
  119. package/src/charts/utils.ts +0 -170
  120. package/src/charts/waffle/__tests__/compute.test.ts +0 -305
  121. package/src/charts/waffle/compute.ts +0 -197
  122. package/src/charts/waffle/index.ts +0 -31
  123. package/src/compile/__tests__/color-scale-range.test.ts +0 -158
  124. package/src/compile/__tests__/color-scale-reverse.test.ts +0 -89
  125. package/src/compile/__tests__/data-clip.test.ts +0 -127
  126. package/src/compile/__tests__/fill-patterns.test.ts +0 -154
  127. package/src/compile/__tests__/size-scale.test.ts +0 -108
  128. package/src/compile/__tests__/watermark-obstacle.test.ts +0 -93
  129. package/src/compile/color-scale-range.ts +0 -101
  130. package/src/compile/data-clip.ts +0 -48
  131. package/src/compile/fill-patterns.ts +0 -74
  132. package/src/compile/layer.ts +0 -630
  133. package/src/compile/size-scale.ts +0 -141
  134. package/src/compile/spec-sugar.ts +0 -758
  135. package/src/compile/watermark-obstacle.ts +0 -54
  136. package/src/compile/you-draw-it.ts +0 -123
  137. package/src/compile.ts +0 -1503
  138. package/src/compiler/__tests__/a11y-warnings.test.ts +0 -148
  139. package/src/compiler/__tests__/animation.test.ts +0 -177
  140. package/src/compiler/__tests__/compile.test.ts +0 -87
  141. package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +0 -19
  142. package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +0 -20
  143. package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +0 -22
  144. package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +0 -19
  145. package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +0 -14
  146. package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +0 -34
  147. package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +0 -22
  148. package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +0 -41
  149. package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +0 -38
  150. package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +0 -28
  151. package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +0 -28
  152. package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +0 -30
  153. package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +0 -29
  154. package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +0 -32
  155. package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +0 -25
  156. package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +0 -19
  157. package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +0 -19
  158. package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +0 -21
  159. package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +0 -23
  160. package/src/compiler/__tests__/keys.test.ts +0 -83
  161. package/src/compiler/__tests__/normalize-highlight.test.ts +0 -103
  162. package/src/compiler/__tests__/normalize.test.ts +0 -399
  163. package/src/compiler/__tests__/sparkline-defaults.test.ts +0 -153
  164. package/src/compiler/__tests__/validate-did-you-mean.test.ts +0 -170
  165. package/src/compiler/__tests__/validate-highlight.test.ts +0 -112
  166. package/src/compiler/__tests__/validate-near-miss.test.ts +0 -251
  167. package/src/compiler/__tests__/validate.test.ts +0 -629
  168. package/src/compiler/__tests__/vl-parity.test.ts +0 -68
  169. package/src/compiler/a11y-warnings.ts +0 -110
  170. package/src/compiler/animation.ts +0 -142
  171. package/src/compiler/index.ts +0 -47
  172. package/src/compiler/keys.ts +0 -55
  173. package/src/compiler/normalize.ts +0 -734
  174. package/src/compiler/sparkline-defaults.ts +0 -138
  175. package/src/compiler/types.ts +0 -241
  176. package/src/compiler/validate.ts +0 -2041
  177. package/src/endpoint-labels/__tests__/compute.test.ts +0 -587
  178. package/src/endpoint-labels/compute.ts +0 -621
  179. package/src/endpoint-labels/constants.ts +0 -54
  180. package/src/endpoint-labels/format.ts +0 -60
  181. package/src/endpoint-labels/predict.ts +0 -139
  182. package/src/format/__tests__/field-format.test.ts +0 -164
  183. package/src/format/field-format.ts +0 -27
  184. package/src/geo/__tests__/compile-map.test.ts +0 -1128
  185. package/src/geo/compile-map.ts +0 -1376
  186. package/src/geo/join.ts +0 -115
  187. package/src/geo/projections.ts +0 -65
  188. package/src/geo/types.ts +0 -26
  189. package/src/graphs/__tests__/community.test.ts +0 -228
  190. package/src/graphs/__tests__/compile-graph.test.ts +0 -396
  191. package/src/graphs/__tests__/encoding.test.ts +0 -415
  192. package/src/graphs/community.ts +0 -92
  193. package/src/graphs/compile-graph.ts +0 -329
  194. package/src/graphs/encoding.ts +0 -356
  195. package/src/graphs/types.ts +0 -106
  196. package/src/index.ts +0 -177
  197. package/src/layout/__tests__/metrics.test.ts +0 -97
  198. package/src/layout/__tests__/scales-highlight.test.ts +0 -192
  199. package/src/layout/axes/rotation.ts +0 -152
  200. package/src/layout/axes/thinning.ts +0 -98
  201. package/src/layout/axes/ticks.ts +0 -409
  202. package/src/layout/axes.ts +0 -757
  203. package/src/layout/dimensions.ts +0 -912
  204. package/src/layout/facet.ts +0 -91
  205. package/src/layout/gridlines.ts +0 -84
  206. package/src/layout/metrics.ts +0 -132
  207. package/src/layout/plan.ts +0 -631
  208. package/src/layout/scales.ts +0 -1053
  209. package/src/layout/shared.ts +0 -44
  210. package/src/legend/__tests__/continuous.test.ts +0 -318
  211. package/src/legend/__tests__/redundancy.test.ts +0 -108
  212. package/src/legend/__tests__/size.test.ts +0 -348
  213. package/src/legend/__tests__/suppression.test.ts +0 -382
  214. package/src/legend/compute.ts +0 -748
  215. package/src/legend/continuous.ts +0 -327
  216. package/src/legend/size.ts +0 -407
  217. package/src/legend/suppression.ts +0 -226
  218. package/src/legend/wrap.ts +0 -117
  219. package/src/sankey/__tests__/compile-sankey.test.ts +0 -458
  220. package/src/sankey/__tests__/layout.test.ts +0 -165
  221. package/src/sankey/__tests__/node-label-wrap.test.ts +0 -114
  222. package/src/sankey/__tests__/node-sort.test.ts +0 -45
  223. package/src/sankey/compile-sankey.ts +0 -706
  224. package/src/sankey/layout.ts +0 -184
  225. package/src/sankey/types.ts +0 -41
  226. package/src/tables/__tests__/bar-column.test.ts +0 -147
  227. package/src/tables/__tests__/category-colors.test.ts +0 -230
  228. package/src/tables/__tests__/compile-table.test.ts +0 -236
  229. package/src/tables/__tests__/format-cells.test.ts +0 -143
  230. package/src/tables/__tests__/heatmap.test.ts +0 -148
  231. package/src/tables/__tests__/pagination.test.ts +0 -78
  232. package/src/tables/__tests__/search.test.ts +0 -94
  233. package/src/tables/__tests__/sort.test.ts +0 -107
  234. package/src/tables/__tests__/sparkline.test.ts +0 -122
  235. package/src/tables/bar-column.ts +0 -94
  236. package/src/tables/category-colors.ts +0 -78
  237. package/src/tables/compile-table.ts +0 -427
  238. package/src/tables/format-cells.ts +0 -115
  239. package/src/tables/heatmap.ts +0 -141
  240. package/src/tables/pagination.ts +0 -46
  241. package/src/tables/search.ts +0 -66
  242. package/src/tables/sort.ts +0 -69
  243. package/src/tables/sparkline.ts +0 -113
  244. package/src/tables/utils.ts +0 -16
  245. package/src/tilemap/__tests__/compile-tilemap.test.ts +0 -576
  246. package/src/tilemap/compile-tilemap.ts +0 -755
  247. package/src/tilemap/layout.ts +0 -172
  248. package/src/tilemap/types.ts +0 -33
  249. package/src/tooltips/__tests__/compute.test.ts +0 -582
  250. package/src/tooltips/compute.ts +0 -700
  251. package/src/transforms/__tests__/aggregate.test.ts +0 -159
  252. package/src/transforms/__tests__/bin.test.ts +0 -88
  253. package/src/transforms/__tests__/calculate.test.ts +0 -146
  254. package/src/transforms/__tests__/conditional.test.ts +0 -127
  255. package/src/transforms/__tests__/filter-relative.test.ts +0 -202
  256. package/src/transforms/__tests__/filter.test.ts +0 -59
  257. package/src/transforms/__tests__/fold.test.ts +0 -79
  258. package/src/transforms/__tests__/index.test.ts +0 -93
  259. package/src/transforms/__tests__/predicates.test.ts +0 -193
  260. package/src/transforms/__tests__/timeunit.test.ts +0 -129
  261. package/src/transforms/__tests__/window.test.ts +0 -286
  262. package/src/transforms/aggregate.ts +0 -131
  263. package/src/transforms/bin.ts +0 -87
  264. package/src/transforms/calculate.ts +0 -60
  265. package/src/transforms/conditional.ts +0 -46
  266. package/src/transforms/filter.ts +0 -122
  267. package/src/transforms/fold.ts +0 -49
  268. package/src/transforms/index.ts +0 -60
  269. package/src/transforms/predicates.ts +0 -123
  270. package/src/transforms/timeunit.ts +0 -88
  271. package/src/transforms/window.ts +0 -185
@@ -1,621 +0,0 @@
1
- /**
2
- * Endpoint labels: per-series right-side label column for multi-series
3
- * line/area charts. Each entry pairs the series name with its last formatted
4
- * value, optionally anchored to the line by an open-circle marker.
5
- *
6
- * Single-pass design: this is the only place that resolves entries to pixel
7
- * positions. `predictEndpointLabelsWidth` (in `predict.ts`) runs before marks
8
- * to reserve right margin space, but only computes width — never positions.
9
- *
10
- * Layout pipeline:
11
- * 1. Filter marks to line/area marks with seriesKey + dataPoints.
12
- * 2. For each series take the last data point, format the value, wrap the label.
13
- * 3. Bidirectional collision sweep on labelY: forward pass pushes overlapping
14
- * entries down, reverse pass pushes them up; final = midpoint, clamped.
15
- * 4. Mark `showLeader` true when displacement exceeds threshold.
16
- * 5. Optionally attach the open-circle marker on the line at the right edge.
17
- */
18
-
19
- import type {
20
- AreaMark,
21
- EndpointLabelEntry,
22
- EndpointLabelsLayout,
23
- LayoutStrategy,
24
- LineMark,
25
- Mark,
26
- Rect,
27
- ResolvedTheme,
28
- TextStyle,
29
- } from '@opendata-ai/openchart-core';
30
- import { estimateTextWidth, wrapText } from '@opendata-ai/openchart-core';
31
-
32
- import type { NormalizedChartSpec } from '../compiler/types';
33
- import { resolveFieldFormatter } from '../format/field-format';
34
- import {
35
- countColorSeries,
36
- endpointLabelsExplicitlyOn,
37
- resolveSuppression,
38
- } from '../legend/suppression';
39
- import {
40
- ENDPOINT_COLUMN_GAP,
41
- ENDPOINT_ENTRY_GAP,
42
- ENDPOINT_GAP,
43
- ENDPOINT_LABEL_FONT_SIZE,
44
- ENDPOINT_LABEL_FONT_WEIGHT,
45
- ENDPOINT_LEADER_THRESHOLD,
46
- ENDPOINT_LINE_HEIGHT,
47
- ENDPOINT_MARKER_RADIUS,
48
- ENDPOINT_MARKER_STROKE_WIDTH,
49
- ENDPOINT_SWATCH_SIZE,
50
- ENDPOINT_VALUE_FONT_SIZE,
51
- ENDPOINT_VALUE_FONT_WEIGHT,
52
- ENDPOINT_VALUE_GAP,
53
- ENDPOINT_WRAP_WIDTH_DEFAULT,
54
- } from './constants';
55
- import { composeEndpointText, formatEndpointValue, resolveEndpointContent } from './format';
56
-
57
- // ---------------------------------------------------------------------------
58
- // Helpers
59
- // ---------------------------------------------------------------------------
60
-
61
- /** Empty layout used as the "column suppressed" return value. */
62
- function emptyLayout(theme: ResolvedTheme): EndpointLabelsLayout {
63
- const labelStyle: TextStyle = {
64
- fontFamily: theme.fonts.family,
65
- fontSize: ENDPOINT_LABEL_FONT_SIZE,
66
- fontWeight: ENDPOINT_LABEL_FONT_WEIGHT,
67
- fill: theme.colors.text,
68
- lineHeight: ENDPOINT_LINE_HEIGHT,
69
- };
70
- const valueStyle: TextStyle = {
71
- fontFamily: theme.fonts.family,
72
- fontSize: ENDPOINT_VALUE_FONT_SIZE,
73
- fontWeight: ENDPOINT_VALUE_FONT_WEIGHT,
74
- fill: theme.colors.annotationText ?? theme.colors.text,
75
- lineHeight: ENDPOINT_LINE_HEIGHT,
76
- fontVariant: 'tabular-nums',
77
- };
78
- return {
79
- entries: [],
80
- bounds: { x: 0, y: 0, width: 0, height: 0 },
81
- labelStyle,
82
- valueStyle,
83
- swatchSize: ENDPOINT_SWATCH_SIZE,
84
- gap: ENDPOINT_GAP,
85
- valueGap: ENDPOINT_VALUE_GAP,
86
- swatchChipFill: theme.colors.annotationFill ?? theme.colors.background,
87
- };
88
- }
89
-
90
- function isLineOrArea(mark: Mark): mark is LineMark | AreaMark {
91
- return mark.type === 'line' || mark.type === 'area';
92
- }
93
-
94
- /** Get the last data-point pixel position for a line/area mark. */
95
- function lastDataPoint(mark: LineMark | AreaMark): { x: number; y: number } | null {
96
- if (mark.dataPoints && mark.dataPoints.length > 0) {
97
- const last = mark.dataPoints[mark.dataPoints.length - 1];
98
- return { x: last.x, y: last.y };
99
- }
100
- if (mark.type === 'line' && mark.points.length > 0) {
101
- const last = mark.points[mark.points.length - 1];
102
- return { x: last.x, y: last.y };
103
- }
104
- if (mark.type === 'area' && mark.topPoints.length > 0) {
105
- const last = mark.topPoints[mark.topPoints.length - 1];
106
- return { x: last.x, y: last.y };
107
- }
108
- return null;
109
- }
110
-
111
- /** Get the first data-point pixel position for a line/area mark. */
112
- function firstDataPoint(mark: LineMark | AreaMark): { x: number; y: number } | null {
113
- if (mark.dataPoints && mark.dataPoints.length > 0) {
114
- const first = mark.dataPoints[0];
115
- return { x: first.x, y: first.y };
116
- }
117
- if (mark.type === 'line' && mark.points.length > 0) {
118
- const first = mark.points[0];
119
- return { x: first.x, y: first.y };
120
- }
121
- if (mark.type === 'area' && mark.topPoints.length > 0) {
122
- const first = mark.topPoints[0];
123
- return { x: first.x, y: first.y };
124
- }
125
- return null;
126
- }
127
-
128
- /** Get the value to display from the last data row. */
129
- function readValue(
130
- mark: LineMark | AreaMark,
131
- valueField: string | undefined,
132
- ): number | string | null {
133
- if (!valueField) return null;
134
- const dp = mark.dataPoints;
135
- if (dp && dp.length > 0) {
136
- const datum = dp[dp.length - 1].datum;
137
- const v = datum[valueField];
138
- return typeof v === 'number' || typeof v === 'string' ? v : null;
139
- }
140
- if (mark.data.length > 0) {
141
- const v = mark.data[mark.data.length - 1][valueField];
142
- return typeof v === 'number' || typeof v === 'string' ? v : null;
143
- }
144
- return null;
145
- }
146
-
147
- /** Get the value to display from the first data row. */
148
- function readFirstValue(
149
- mark: LineMark | AreaMark,
150
- valueField: string | undefined,
151
- ): number | string | null {
152
- if (!valueField) return null;
153
- const dp = mark.dataPoints;
154
- if (dp && dp.length > 0) {
155
- const datum = dp[0].datum;
156
- const v = datum[valueField];
157
- return typeof v === 'number' || typeof v === 'string' ? v : null;
158
- }
159
- if (mark.data.length > 0) {
160
- const v = mark.data[0][valueField];
161
- return typeof v === 'number' || typeof v === 'string' ? v : null;
162
- }
163
- return null;
164
- }
165
-
166
- /**
167
- * Local collision sweep: keep each label anchored to its line's `dataY` and
168
- * only displace neighbors that actually overlap.
169
- *
170
- * Each entry's "natural" top is `naturalTop` (typically `dataY - fontSize/2`
171
- * so the label's first-line baseline-center aligns with the data point). We
172
- * only push neighbors apart when their stacks would collide, by the minimum
173
- * amount needed. This preserves the line-tracking behavior the design calls
174
- * for: when lines are well-separated, labels sit at their lines; when close
175
- * together, the sweep nudges them apart while staying near their data points.
176
- *
177
- * Algorithm:
178
- * 1. Sort by naturalTop ascending.
179
- * 2. Forward pass (top→bottom): push i down if it overlaps i-1, then clamp
180
- * to `areaBottom - height` so an entry never lands below the chart.
181
- * 3. Reverse pass (bottom→top): push i up if it overlaps i+1, then clamp
182
- * to `areaTop` so an entry never lands above the chart.
183
- *
184
- * Both passes cascade the clamp through neighbors, so a clamp at one end can
185
- * propagate all the way to the other when the chart is too short to fit every
186
- * label without overlap. (When the total stack is taller than the area, the
187
- * earliest entries get pinned to areaTop and overlap is unavoidable — the
188
- * caller has to drop entries or shrink them.)
189
- */
190
- export function bidirectionalSweep(
191
- entries: { naturalTop: number; height: number; index: number }[],
192
- areaTop: number,
193
- areaBottom: number,
194
- ): number[] {
195
- const n = entries.length;
196
- if (n === 0) return [];
197
-
198
- // Sort by naturalTop ascending so neighbors in the chart are neighbors here.
199
- const sorted = [...entries].sort((a, b) => a.naturalTop - b.naturalTop);
200
-
201
- // Initialize tops at the natural (anchored-to-dataY) positions.
202
- const tops = sorted.map((e) => e.naturalTop);
203
-
204
- // Forward pass: push down only when overlapping the previous entry, then
205
- // cap at the chart's bottom edge so we don't run off the canvas.
206
- for (let i = 0; i < n; i++) {
207
- if (i > 0) {
208
- const minTop = tops[i - 1] + sorted[i - 1].height;
209
- if (tops[i] < minTop) tops[i] = minTop;
210
- }
211
- const maxTop = areaBottom - sorted[i].height;
212
- if (tops[i] > maxTop) tops[i] = maxTop;
213
- }
214
-
215
- // Reverse pass: when the forward pass clamped a tail entry up to fit the
216
- // bottom edge, propagate that displacement back through the predecessors so
217
- // they don't overlap the now-raised tail. Then clamp at areaTop.
218
- for (let i = n - 1; i >= 0; i--) {
219
- if (i < n - 1) {
220
- const maxTop = tops[i + 1] - sorted[i].height;
221
- if (tops[i] > maxTop) tops[i] = maxTop;
222
- }
223
- if (tops[i] < areaTop) tops[i] = areaTop;
224
- }
225
-
226
- // Write back in original index order.
227
- const result = new Array<number>(n);
228
- for (let i = 0; i < n; i++) {
229
- result[sorted[i].index] = tops[i];
230
- }
231
- return result;
232
- }
233
-
234
- // ---------------------------------------------------------------------------
235
- // Public API
236
- // ---------------------------------------------------------------------------
237
-
238
- /**
239
- * Compute the resolved endpoint-labels layout for a chart.
240
- *
241
- * Returns an empty layout (entries: []) when:
242
- * - The chart isn't a multi-series line/area.
243
- * - The user opted out via `endpointLabels: false`.
244
- * - Responsive strategy strips inline labels (compact breakpoint).
245
- * - Suppression truth table resolves to "endpoint column off".
246
- *
247
- * Otherwise produces fully-positioned entries with bidirectional collision
248
- * sweep applied to labelY and optional open-circle markers on the line.
249
- */
250
- export function computeEndpointLabels(
251
- spec: NormalizedChartSpec,
252
- marks: Mark[],
253
- theme: ResolvedTheme,
254
- chartArea: Rect,
255
- strategy?: LayoutStrategy,
256
- endpointLabelsDemoted?: boolean,
257
- ): EndpointLabelsLayout {
258
- // Compact strategy: drop the column entirely. The traditional legend takes
259
- // over series identification at the compact breakpoint. Explicit user
260
- // opt-in overrides the strip: slope/bump recipes are unreadable without
261
- // their labels, so an author who wrote endpointLabels config keeps it.
262
- if (strategy?.labelMode === 'none' && !endpointLabelsExplicitlyOn(spec)) {
263
- return emptyLayout(theme);
264
- }
265
-
266
- const highlight = spec.highlight ?? [];
267
- const highlightActive = highlight.length > 0;
268
- const seriesCount = countColorSeries(spec);
269
- const sup = resolveSuppression(spec, {
270
- seriesCount,
271
- // The 'none' branch above returned unless the user explicitly opted in,
272
- // in which case resolveSuppression's explicit-on override applies anyway.
273
- labelsHiddenByStrategy: false,
274
- labelsDensityNone: spec.labels.density === 'none',
275
- endpointLabelsDemoted,
276
- });
277
- const epExplicitlyOff =
278
- spec.endpointLabels === false ||
279
- (typeof spec.endpointLabels === 'object' && spec.endpointLabels?.show === false);
280
- if (!sup.showEndpointLabels && !(highlightActive && !epExplicitlyOff)) return emptyLayout(theme);
281
-
282
- // Dedupe by seriesKey: for area charts, the engine emits BOTH an AreaMark
283
- // and a derived LineMark per series (see `linesFromAreas` in
284
- // `charts/line/index.ts`). Without dedupe each series would produce two
285
- // endpoint entries. Prefer the line mark — its `stroke` is the canonical
286
- // series color and matches the visible line, whereas the area mark's
287
- // `stroke` may be derived from a gradient via `getRepresentativeColor`.
288
- // Same-type collisions (area→area, line→line) keep the first mark; the
289
- // engine never emits two of the same type per seriesKey.
290
- const bySeriesKey = new Map<string, LineMark | AreaMark>();
291
- for (const mark of marks) {
292
- if (!isLineOrArea(mark) || !mark.seriesKey) continue;
293
- const existing = bySeriesKey.get(mark.seriesKey);
294
- if (!existing || (existing.type === 'area' && mark.type === 'line')) {
295
- bySeriesKey.set(mark.seriesKey, mark);
296
- }
297
- }
298
- const lineOrAreaMarks = Array.from(bySeriesKey.values());
299
- if (lineOrAreaMarks.length < 2) return emptyLayout(theme);
300
-
301
- const config = typeof spec.endpointLabels === 'object' ? spec.endpointLabels : undefined;
302
- const wrapWidth = config?.width ?? ENDPOINT_WRAP_WIDTH_DEFAULT;
303
- const valueField = config?.valueField ?? spec.encoding.y?.field;
304
- const surfaceFormat =
305
- config?.format ??
306
- ((spec.encoding.y?.axis as Record<string, unknown> | undefined)?.format as string | undefined);
307
- const endpointFormatter = resolveFieldFormatter({
308
- surfaceFormat,
309
- channelFormat: spec.encoding.y?.format,
310
- values: valueField ? spec.data.map((r) => r[valueField]) : [],
311
- });
312
- const showMarker = config?.showMarker !== false;
313
- const markerRadius = config?.markerStyle?.radius ?? ENDPOINT_MARKER_RADIUS;
314
- const markerStrokeWidth = config?.markerStyle?.strokeWidth ?? ENDPOINT_MARKER_STROKE_WIDTH;
315
- const markerFill = config?.markerStyle?.fill ?? theme.colors.background;
316
- const trailingContent = resolveEndpointContent(config?.content, 'trailing');
317
- const leadingContent = resolveEndpointContent(config?.content, 'leading');
318
-
319
- // Resolve the styles once.
320
- const labelStyle: TextStyle = {
321
- fontFamily: theme.fonts.family,
322
- fontSize: ENDPOINT_LABEL_FONT_SIZE,
323
- fontWeight: ENDPOINT_LABEL_FONT_WEIGHT,
324
- fill: theme.colors.text,
325
- lineHeight: ENDPOINT_LINE_HEIGHT,
326
- };
327
- const valueStyle: TextStyle = {
328
- fontFamily: theme.fonts.family,
329
- fontSize: ENDPOINT_VALUE_FONT_SIZE,
330
- fontWeight: ENDPOINT_VALUE_FONT_WEIGHT,
331
- fill: theme.colors.annotationText ?? theme.colors.text,
332
- lineHeight: ENDPOINT_LINE_HEIGHT,
333
- fontVariant: 'tabular-nums',
334
- };
335
-
336
- // First pass: build entries with wrapped labels and dataY (no positions yet).
337
- type Provisional = {
338
- seriesKey: string;
339
- labelLines: string[];
340
- value: string;
341
- color: string;
342
- dataX: number;
343
- dataY: number;
344
- height: number;
345
- width: number;
346
- };
347
- const provisional: Provisional[] = [];
348
- const labelLineHeight = ENDPOINT_LABEL_FONT_SIZE * ENDPOINT_LINE_HEIGHT;
349
- const valueLineHeight = ENDPOINT_VALUE_FONT_SIZE * ENDPOINT_LINE_HEIGHT;
350
-
351
- for (const mark of lineOrAreaMarks) {
352
- const seriesKey = mark.seriesKey;
353
- if (!seriesKey) continue;
354
-
355
- if (highlightActive && !highlight.includes(seriesKey)) continue;
356
-
357
- const last = lastDataPoint(mark);
358
- if (!last) continue;
359
-
360
- const rawValue = readValue(mark, valueField);
361
- const formatted = formatEndpointValue(rawValue, endpointFormatter);
362
-
363
- // Content composition: when `content` is set, the composed text becomes
364
- // the label lines and the second-line value is dropped.
365
- const labelText = trailingContent
366
- ? composeEndpointText(trailingContent, seriesKey, formatted)
367
- : seriesKey;
368
- const value = trailingContent ? '' : formatted;
369
- const labelLines = wrapText(
370
- labelText,
371
- ENDPOINT_LABEL_FONT_SIZE,
372
- ENDPOINT_LABEL_FONT_WEIGHT,
373
- wrapWidth,
374
- );
375
-
376
- // Width of the widest line in this entry (used to size the column).
377
- let entryWidth = 0;
378
- for (const line of labelLines) {
379
- const w = estimateTextWidth(line, ENDPOINT_LABEL_FONT_SIZE, ENDPOINT_LABEL_FONT_WEIGHT);
380
- if (w > entryWidth) entryWidth = w;
381
- }
382
- const valueWidth = value
383
- ? estimateTextWidth(value, ENDPOINT_VALUE_FONT_SIZE, ENDPOINT_VALUE_FONT_WEIGHT)
384
- : 0;
385
- if (valueWidth > entryWidth) entryWidth = valueWidth;
386
-
387
- const labelHeight = labelLines.length * labelLineHeight;
388
- const valueHeight = value ? valueLineHeight : 0;
389
- const entryHeight = labelHeight + (value ? ENDPOINT_VALUE_GAP : 0) + valueHeight;
390
-
391
- // Determine the line stroke color. AreaMark may have a string fill; prefer
392
- // mark.stroke for visual continuity with the line drawn on top of the area.
393
- const color =
394
- mark.type === 'line'
395
- ? mark.stroke
396
- : (mark.stroke ??
397
- (typeof mark.fill === 'string' ? mark.fill : theme.colors.categorical[0]));
398
-
399
- provisional.push({
400
- seriesKey,
401
- labelLines,
402
- value,
403
- color,
404
- dataX: last.x,
405
- dataY: last.y,
406
- height: entryHeight,
407
- width: entryWidth,
408
- });
409
- }
410
-
411
- if (provisional.length === 0) return emptyLayout(theme);
412
- if (provisional.length < 2 && !highlightActive) return emptyLayout(theme);
413
-
414
- // Bidirectional collision sweep. Each entry's natural top is `dataY - labelFontSize/2`
415
- // so the label's first-line baseline-center aligns with the line's last data point.
416
- // The marker sits on this same baseline-center row, putting the open ring directly
417
- // where the line terminates when undisplaced.
418
- // Add ENDPOINT_ENTRY_GAP to each entry's claimed height so the sweep leaves
419
- // breathing room between stacked labels. The renderer doesn't draw the gap,
420
- // so it's invisible when entries are well-separated.
421
- const sweepInput = provisional.map((p, i) => ({
422
- naturalTop: p.dataY - ENDPOINT_LABEL_FONT_SIZE / 2,
423
- height: p.height + ENDPOINT_ENTRY_GAP,
424
- index: i,
425
- }));
426
- const sweptTops = bidirectionalSweep(sweepInput, chartArea.y, chartArea.y + chartArea.height);
427
-
428
- // Build final entries.
429
- const columnWidth = Math.max(
430
- ENDPOINT_SWATCH_SIZE + ENDPOINT_GAP + Math.max(...provisional.map((p) => p.width), 0) + 4,
431
- 32,
432
- );
433
- const columnX = chartArea.x + chartArea.width + ENDPOINT_COLUMN_GAP;
434
-
435
- // The marker is the line's visual terminator — always at (chartRightX, dataY).
436
- // The swatch + label first-line baseline-center sit at `labelY + fontSize/2`.
437
- // The sweep uses naturalTop = `dataY - fontSize/2` so that, when undisplaced,
438
- // the swatch row and the marker share the same y, producing the clean
439
- // single-row look from the mocks.
440
- //
441
- // Leader lines are off by default. The marker on the line plus the swatch in
442
- // the column already tie label to data; a connecting line adds noise without
443
- // adding information for the small displacements the sweep produces. Users
444
- // who want them can opt in via `endpointLabels.showLeader: true`.
445
- const showLeader = config?.showLeader === true;
446
- const entries: EndpointLabelEntry[] = provisional.map((p, i) => {
447
- const labelY = sweptTops[i];
448
- const swatchRowY = labelY + ENDPOINT_LABEL_FONT_SIZE / 2;
449
- const displaced = Math.abs(swatchRowY - p.dataY) > ENDPOINT_LEADER_THRESHOLD;
450
- const entry: EndpointLabelEntry = {
451
- seriesKey: p.seriesKey,
452
- labelLines: p.labelLines,
453
- value: p.value,
454
- color: p.color,
455
- dataY: p.dataY,
456
- labelY,
457
- showLeader: showLeader && displaced,
458
- };
459
- if (showMarker) {
460
- // Offset cx right by markerRadius so the line terminates at the circle's
461
- // left edge rather than its center — prevents the line from piercing the ring.
462
- entry.marker = {
463
- x: p.dataX + markerRadius,
464
- y: p.dataY,
465
- dataX: p.dataX,
466
- fill: markerFill,
467
- stroke: config?.markerStyle?.stroke ?? p.color,
468
- strokeWidth: markerStrokeWidth,
469
- radius: markerRadius,
470
- };
471
- }
472
- return entry;
473
- });
474
-
475
- // Leading column (left side) for `ends: 'both'` mode.
476
- const endsBoth = config?.ends === 'both';
477
- let leading: EndpointLabelEntry[] | undefined;
478
- let leadingBounds: { x: number; y: number; width: number; height: number } | undefined;
479
-
480
- if (endsBoth) {
481
- type LeadingProvisional = {
482
- seriesKey: string;
483
- labelLines: string[];
484
- value: string;
485
- color: string;
486
- dataX: number;
487
- dataY: number;
488
- height: number;
489
- width: number;
490
- };
491
- const leadingProvisional: LeadingProvisional[] = [];
492
-
493
- for (const mark of lineOrAreaMarks) {
494
- const seriesKey = mark.seriesKey;
495
- if (!seriesKey) continue;
496
-
497
- if (highlightActive && !highlight.includes(seriesKey)) continue;
498
-
499
- const first = firstDataPoint(mark);
500
- if (!first) continue;
501
-
502
- const rawValue = readFirstValue(mark, valueField);
503
- const formatted = formatEndpointValue(rawValue, endpointFormatter);
504
- const labelText = leadingContent
505
- ? composeEndpointText(leadingContent, seriesKey, formatted)
506
- : seriesKey;
507
- const value = leadingContent ? '' : formatted;
508
- const labelLines = wrapText(
509
- labelText,
510
- ENDPOINT_LABEL_FONT_SIZE,
511
- ENDPOINT_LABEL_FONT_WEIGHT,
512
- wrapWidth,
513
- );
514
-
515
- let entryWidth = 0;
516
- for (const line of labelLines) {
517
- const w = estimateTextWidth(line, ENDPOINT_LABEL_FONT_SIZE, ENDPOINT_LABEL_FONT_WEIGHT);
518
- if (w > entryWidth) entryWidth = w;
519
- }
520
- const valueWidth = value
521
- ? estimateTextWidth(value, ENDPOINT_VALUE_FONT_SIZE, ENDPOINT_VALUE_FONT_WEIGHT)
522
- : 0;
523
- if (valueWidth > entryWidth) entryWidth = valueWidth;
524
-
525
- const labelHeight = labelLines.length * labelLineHeight;
526
- const valueHeight = value ? valueLineHeight : 0;
527
- const entryHeight = labelHeight + (value ? ENDPOINT_VALUE_GAP : 0) + valueHeight;
528
-
529
- const color =
530
- mark.type === 'line'
531
- ? mark.stroke
532
- : (mark.stroke ??
533
- (typeof mark.fill === 'string' ? mark.fill : theme.colors.categorical[0]));
534
-
535
- leadingProvisional.push({
536
- seriesKey,
537
- labelLines,
538
- value,
539
- color,
540
- dataX: first.x,
541
- dataY: first.y,
542
- height: entryHeight,
543
- width: entryWidth,
544
- });
545
- }
546
-
547
- if (leadingProvisional.length >= 2 || (leadingProvisional.length === 1 && highlightActive)) {
548
- const leadingSweepInput = leadingProvisional.map((p, i) => ({
549
- naturalTop: p.dataY - ENDPOINT_LABEL_FONT_SIZE / 2,
550
- height: p.height + ENDPOINT_ENTRY_GAP,
551
- index: i,
552
- }));
553
- const leadingSweptTops = bidirectionalSweep(
554
- leadingSweepInput,
555
- chartArea.y,
556
- chartArea.y + chartArea.height,
557
- );
558
-
559
- const leadingColumnWidth = Math.max(
560
- ENDPOINT_SWATCH_SIZE +
561
- ENDPOINT_GAP +
562
- Math.max(...leadingProvisional.map((p) => p.width), 0) +
563
- 4,
564
- 32,
565
- );
566
- const leadingColumnX = chartArea.x - ENDPOINT_COLUMN_GAP - leadingColumnWidth;
567
-
568
- leading = leadingProvisional.map((p, i) => {
569
- const labelY = leadingSweptTops[i];
570
- const swatchRowY = labelY + ENDPOINT_LABEL_FONT_SIZE / 2;
571
- const displaced = Math.abs(swatchRowY - p.dataY) > ENDPOINT_LEADER_THRESHOLD;
572
- const entry: EndpointLabelEntry = {
573
- seriesKey: p.seriesKey,
574
- labelLines: p.labelLines,
575
- value: p.value,
576
- color: p.color,
577
- dataY: p.dataY,
578
- labelY,
579
- showLeader: showLeader && displaced,
580
- };
581
- if (showMarker) {
582
- entry.marker = {
583
- x: p.dataX - markerRadius,
584
- y: p.dataY,
585
- dataX: p.dataX,
586
- fill: markerFill,
587
- stroke: config?.markerStyle?.stroke ?? p.color,
588
- strokeWidth: markerStrokeWidth,
589
- radius: markerRadius,
590
- };
591
- }
592
- return entry;
593
- });
594
-
595
- leadingBounds = {
596
- x: leadingColumnX,
597
- y: chartArea.y,
598
- width: leadingColumnWidth,
599
- height: chartArea.height,
600
- };
601
- }
602
- }
603
-
604
- return {
605
- entries,
606
- bounds: {
607
- x: columnX,
608
- y: chartArea.y,
609
- width: columnWidth,
610
- height: chartArea.height,
611
- },
612
- labelStyle,
613
- valueStyle,
614
- swatchSize: ENDPOINT_SWATCH_SIZE,
615
- gap: ENDPOINT_GAP,
616
- valueGap: ENDPOINT_VALUE_GAP,
617
- swatchChipFill: theme.colors.annotationFill ?? theme.colors.background,
618
- leading,
619
- leadingBounds,
620
- };
621
- }
@@ -1,54 +0,0 @@
1
- /**
2
- * Shared constants for endpoint-labels prediction and computation.
3
- *
4
- * Both `predict.ts` (width-only, runs before marks) and `compute.ts` (full
5
- * layout, runs after marks) read from these so the predicted width can never
6
- * drift from the eventual rendered geometry.
7
- */
8
-
9
- /** Series-name label font size. Matches body text for editorial prominence. */
10
- export const ENDPOINT_LABEL_FONT_SIZE = 13;
11
-
12
- /** Series-name label font weight (semibold for visual prominence). */
13
- export const ENDPOINT_LABEL_FONT_WEIGHT = 600;
14
-
15
- /** Last-value label font size (slightly smaller than the series name). */
16
- export const ENDPOINT_VALUE_FONT_SIZE = 12;
17
-
18
- /** Last-value label font weight (regular, muted text tone). */
19
- export const ENDPOINT_VALUE_FONT_WEIGHT = 400;
20
-
21
- /** Default wrap width for long series names, in pixels. */
22
- export const ENDPOINT_WRAP_WIDTH_DEFAULT = 110;
23
-
24
- /** Width of the colored swatch chip drawn left of the label. */
25
- export const ENDPOINT_SWATCH_SIZE = 18;
26
-
27
- /** Gap between swatch, label, and value. */
28
- export const ENDPOINT_GAP = 8;
29
-
30
- /** Line height multiplier for wrapped label lines. */
31
- export const ENDPOINT_LINE_HEIGHT = 1.25;
32
-
33
- /** Pixel padding between the chart area's right edge and the column. */
34
- export const ENDPOINT_COLUMN_GAP = 18;
35
-
36
- /** Vertical pad between the last wrapped label line and the value text. */
37
- export const ENDPOINT_VALUE_GAP = 4;
38
-
39
- /**
40
- * Vertical breathing room between adjacent stacked entries during the
41
- * collision sweep. The renderer doesn't draw this gap — it's purely a
42
- * sweep-time cushion so that the value text of entry N doesn't hug the
43
- * label text of entry N+1 when the sweep has packed them tightly.
44
- */
45
- export const ENDPOINT_ENTRY_GAP = 6;
46
-
47
- /** Default open-circle marker radius on the line. */
48
- export const ENDPOINT_MARKER_RADIUS = 4;
49
-
50
- /** Default open-circle marker stroke width. */
51
- export const ENDPOINT_MARKER_STROKE_WIDTH = 2;
52
-
53
- /** Threshold (px) above which a leader line connects label back to data point. */
54
- export const ENDPOINT_LEADER_THRESHOLD = 8;