ggaction 0.0.23 → 0.0.26

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 (228) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +2 -2
  3. package/knowledge/action-cards.json +1 -1
  4. package/knowledge/intent-taxonomy.json +1 -1
  5. package/knowledge/mcp-resources.json +1 -1
  6. package/knowledge/task-resolver.js +1 -1
  7. package/package.json +1 -1
  8. package/src/accessibility.js +13 -0
  9. package/src/actions/canvas/actions.js +2 -1
  10. package/src/actions/canvas/fitting.js +68 -13
  11. package/src/actions/charts/beeswarm.js +2 -1
  12. package/src/actions/charts/ecdf.js +2 -1
  13. package/src/actions/charts/endpoints.js +25 -16
  14. package/src/actions/charts/heatmap.js +6 -4
  15. package/src/actions/charts/missing.js +17 -0
  16. package/src/actions/charts/radar.js +70 -33
  17. package/src/actions/charts/raincloud.js +18 -4
  18. package/src/actions/charts/rug-strip.js +2 -2
  19. package/src/actions/charts/shared.js +17 -0
  20. package/src/actions/composition/actions.js +24 -7
  21. package/src/actions/composition/adjacent.js +90 -0
  22. package/src/actions/composition/index.js +11 -1
  23. package/src/actions/composition/layer.js +42 -0
  24. package/src/actions/composition/layerPlan.js +176 -0
  25. package/src/actions/composition/targets.js +105 -0
  26. package/src/actions/data/basicBin2d.js +1 -1
  27. package/src/actions/data/bin2d.js +2 -2
  28. package/src/actions/data/derived.js +3 -0
  29. package/src/actions/data/filter.js +1 -1
  30. package/src/actions/data/raincloudFilter.js +3 -1
  31. package/src/actions/encodings/appearance.js +26 -7
  32. package/src/actions/encodings/channels.js +1 -0
  33. package/src/actions/encodings/color/continuous.js +37 -34
  34. package/src/actions/encodings/color/index.js +57 -43
  35. package/src/actions/encodings/color/policy.js +15 -0
  36. package/src/actions/encodings/density/resolve.js +3 -2
  37. package/src/actions/encodings/density.js +2 -2
  38. package/src/actions/encodings/offset.js +2 -0
  39. package/src/actions/encodings/pathOrder.js +2 -0
  40. package/src/actions/encodings/position/areaAlignment.js +42 -0
  41. package/src/actions/encodings/position/index.js +37 -3
  42. package/src/actions/encodings/position/policies/line.js +1 -1
  43. package/src/actions/encodings/ranged.js +4 -2
  44. package/src/actions/encodings/remove.js +13 -10
  45. package/src/actions/encodings/ruleAppearance.js +14 -2
  46. package/src/actions/facets/actions.js +1 -1
  47. package/src/actions/guides/axes/axes.js +2 -1
  48. package/src/actions/guides/axes/components.js +5 -0
  49. package/src/actions/guides/axes/edit.js +1 -188
  50. package/src/actions/guides/axes/labels.js +2 -537
  51. package/src/actions/guides/axes/parallel/lifecycle.js +11 -4
  52. package/src/actions/guides/axes/parallel/policy.js +5 -2
  53. package/src/actions/guides/axes/parallel/resolve.js +3 -1
  54. package/src/actions/guides/axes/parallel.js +9 -4
  55. package/src/actions/guides/axes/tickGroups.js +16 -1
  56. package/src/actions/guides/axes/ticks.js +33 -24
  57. package/src/actions/guides/axes/titles.js +1 -389
  58. package/src/actions/guides/facadeLegend.js +3 -2
  59. package/src/actions/guides/grids/grid.js +3 -2
  60. package/src/actions/guides/grids/resolve.js +2 -1
  61. package/src/actions/guides/guides.js +3 -2
  62. package/src/actions/guides/legends/automatic.js +28 -0
  63. package/src/actions/guides/legends/blocks.js +15 -21
  64. package/src/actions/guides/legends/categorical/actions.js +1 -1
  65. package/src/actions/guides/legends/categorical/components.js +4 -3
  66. package/src/actions/guides/legends/categorical/layout.js +5 -2
  67. package/src/actions/guides/legends/categorical/options.js +9 -14
  68. package/src/actions/guides/legends/categorical/recipes.js +3 -0
  69. package/src/actions/guides/legends/categorical/resolve.js +7 -5
  70. package/src/actions/guides/legends/colorSize.js +51 -14
  71. package/src/actions/guides/legends/continuous/common.js +1 -1
  72. package/src/actions/guides/legends/continuous/gradient.js +1 -1
  73. package/src/actions/guides/legends/continuous/opacity.js +2 -0
  74. package/src/actions/guides/legends/edit.js +1 -1
  75. package/src/actions/guides/legends/focused.js +2 -2
  76. package/src/actions/guides/legends/group.js +1 -1
  77. package/src/actions/guides/legends/lane.js +1 -1
  78. package/src/actions/guides/legends/lifecycle.js +2 -2
  79. package/src/actions/guides/legends/remove.js +18 -4
  80. package/src/actions/guides/legends/size.js +1 -1
  81. package/src/actions/guides/legends/transition.js +15 -6
  82. package/src/actions/guides/polar/axes/facade.js +4 -2
  83. package/src/actions/guides/polar/axes/labels.js +6 -1
  84. package/src/actions/guides/polar/axes/shared.js +4 -4
  85. package/src/actions/guides/polar/axes/titles.js +13 -2
  86. package/src/actions/guides/polar/grids.js +6 -3
  87. package/src/actions/guides/tickValues.js +7 -1
  88. package/src/actions/index.js +2 -0
  89. package/src/actions/marks/area/actions.js +9 -2
  90. package/src/actions/marks/area/materialize.js +1 -1
  91. package/src/actions/marks/bar/materialize.js +4 -4
  92. package/src/actions/marks/lifecycle.js +14 -0
  93. package/src/actions/marks/line/actions.js +3 -0
  94. package/src/actions/marks/line/materialize.js +4 -2
  95. package/src/actions/marks/point/edit.js +2 -0
  96. package/src/actions/marks/point/jitter.js +2 -0
  97. package/src/actions/marks/point/materialize.js +5 -4
  98. package/src/actions/marks/point/packing.js +2 -0
  99. package/src/actions/marks/references.js +5 -3
  100. package/src/actions/marks/remove.js +16 -0
  101. package/src/actions/marks/shared.js +2 -1
  102. package/src/actions/marks/text/actions.js +5 -2
  103. package/src/actions/overlays/index.js +168 -0
  104. package/src/actions/overlays/snapshot.js +45 -0
  105. package/src/actions/primitives/editGraphics.js +21 -27
  106. package/src/actions/primitives/semanticAction.js +4 -5
  107. package/src/actions/primitives/semanticValidation/guide.js +1 -0
  108. package/src/actions/primitives/semanticValidation/layer.js +7 -0
  109. package/src/actions/scales/consumers/common.js +10 -0
  110. package/src/actions/scales/consumers/families.js +1 -1
  111. package/src/actions/scales/preview.js +1 -1
  112. package/src/actions/selection/actions.js +63 -11
  113. package/src/actions/titles/actions.js +3 -14
  114. package/src/actions/titles/resolve.js +23 -10
  115. package/src/core/action.js +41 -25
  116. package/src/core/compositionScales.js +12 -0
  117. package/src/core/compositionState.js +20 -3
  118. package/src/core/facetGap.js +44 -0
  119. package/src/core/immutable.js +11 -6
  120. package/src/core/programState.js +16 -0
  121. package/src/core/resourceReferences.js +19 -3
  122. package/src/core/validation.js +7 -0
  123. package/src/core/vocabulary.js +8 -2
  124. package/src/grammar/actionTargets.js +24 -0
  125. package/src/grammar/axisTextPlacement.js +1 -1
  126. package/src/grammar/bin2d.js +41 -4
  127. package/src/grammar/colorSizeLegend.js +56 -5
  128. package/src/grammar/displayLabels.js +10 -0
  129. package/src/grammar/facetHeaders.js +1 -1
  130. package/src/grammar/facets/dependencies.js +10 -3
  131. package/src/grammar/facets/guides.js +2 -1
  132. package/src/grammar/facets/legendIntent.js +7 -5
  133. package/src/grammar/legendBlocks.js +1 -1
  134. package/src/grammar/legendCombinations.js +1 -1
  135. package/src/grammar/legendGroups.js +1 -1
  136. package/src/grammar/legendItemsOffset.js +12 -0
  137. package/src/grammar/legendOverflow.js +25 -0
  138. package/src/grammar/lineSeries.js +1 -1
  139. package/src/grammar/markSelection.js +23 -0
  140. package/src/grammar/overlay.js +51 -0
  141. package/src/grammar/parallelCoordinates.js +1 -208
  142. package/src/grammar/pathCommands.js +8 -2
  143. package/src/grammar/pathSeries.js +1 -1
  144. package/src/grammar/plotComposition.js +152 -0
  145. package/src/grammar/pointShapes.js +18 -0
  146. package/src/grammar/scales/transformed.js +1 -1
  147. package/src/grammar/schemas/concreteGraphic.js +8 -1
  148. package/src/grammar/schemas/graphicBounds.js +8 -1
  149. package/src/grammar/schemas/graphicTree.js +18 -2
  150. package/src/grammar/schemas/semanticPath.js +3 -0
  151. package/src/grammar/strokeStyle.js +14 -8
  152. package/src/grammar/text.js +72 -0
  153. package/src/inspection.js +1 -1
  154. package/src/inspectionDescriptors.js +1 -1
  155. package/src/layout/composition.js +32 -10
  156. package/src/layout/compositionLegend.js +91 -0
  157. package/src/layout/facetContentFlow.js +15 -7
  158. package/src/layout/facetHeaderTitles.js +1 -1
  159. package/src/layout/facets.js +1 -1
  160. package/src/layout/labels.js +55 -27
  161. package/src/layout/legend.js +3 -2
  162. package/src/layout/legendGroup.js +1 -1
  163. package/src/layout/legendItems.js +7 -6
  164. package/src/layout/legendLane.js +1 -1
  165. package/src/layout/legendText.js +1 -1
  166. package/src/layout/title.js +1 -1
  167. package/src/materialization/adjacentSource.js +27 -0
  168. package/src/materialization/composition.js +165 -52
  169. package/src/materialization/compositionLegends.js +120 -0
  170. package/src/materialization/compositionScales.js +125 -0
  171. package/src/materialization/compositionTitle.js +96 -0
  172. package/src/materialization/facetGuides/edit.js +22 -2
  173. package/src/materialization/facetGuides/layout.js +1 -1
  174. package/src/materialization/facetGuides/owner.js +4 -3
  175. package/src/materialization/facetGuides/ownerLayout.js +12 -6
  176. package/src/materialization/facetGuides/placement.js +19 -22
  177. package/src/materialization/facetGuides/preparation.js +17 -5
  178. package/src/materialization/facetHeaders.js +5 -1
  179. package/src/materialization/facets.js +1 -1
  180. package/src/materialization/guides/layout.js +1 -1
  181. package/src/materialization/guides/resources.js +1 -1
  182. package/src/materialization/guides/visibility.js +105 -0
  183. package/src/materialization/legendClip.js +1 -1
  184. package/src/materialization/legendGroups.js +1 -1
  185. package/src/materialization/legendItemsOffset.js +68 -0
  186. package/src/materialization/legendSuppression.js +22 -0
  187. package/src/materialization/legends.js +3 -2
  188. package/src/materialization/marks/capabilities.js +4 -0
  189. package/src/materialization/marks/index.js +5 -1
  190. package/src/materialization/marks/pathOrder.js +1 -1
  191. package/src/materialization/marks/policies.js +2 -1
  192. package/src/materialization/overlayItems.js +154 -0
  193. package/src/materialization/planner.js +19 -4
  194. package/src/materialization/scales/policies/binnedPosition.js +43 -5
  195. package/src/materialization/scales/policies/series.js +12 -4
  196. package/src/materialization/scales/resolve.js +2 -0
  197. package/src/materialization/selection/items/bar.js +21 -3
  198. package/src/materialization/selection/items/common.js +18 -5
  199. package/src/materialization/selection/items/overlay.js +15 -0
  200. package/src/materialization/selection/items/path.js +1 -0
  201. package/src/materialization/selection/items/point.js +2 -3
  202. package/src/materialization/selection/items/tick.js +3 -12
  203. package/src/materialization/selection/path.js +2 -2
  204. package/src/materialization/selection/policies/bar.js +2 -0
  205. package/src/materialization/selection/policies/index.js +35 -1
  206. package/src/materialization/selection/policies/point.js +2 -0
  207. package/src/materialization/selection/policies/text.js +17 -0
  208. package/src/materialization/selection/rows.js +103 -0
  209. package/src/materialization/selection/state.js +19 -3
  210. package/src/materialization/selection/styles.js +8 -0
  211. package/src/materialization/text.js +6 -489
  212. package/src/materialization/textGeometry.js +503 -0
  213. package/src/materialization/textLines.js +20 -0
  214. package/src/persistence/validation.js +116 -25
  215. package/src/renderers/canvas/index.js +12 -7
  216. package/src/renderers/canvas/text.js +5 -5
  217. package/src/renderers/nativeBackend.js +25 -0
  218. package/src/renderers/pdf.js +3 -3
  219. package/src/renderers/png.js +4 -3
  220. package/src/renderers/svgSerialization.js +1 -643
  221. package/src/scene.js +5 -3
  222. package/src/selectors/plotComposition.js +115 -0
  223. package/src/selectors/selectionTargets.js +19 -0
  224. package/src/version.js +1 -1
  225. package/types/basic.d.ts +4 -2
  226. package/types/index.d.ts +14 -0
  227. package/types/inspection.d.ts +34 -0
  228. package/types/program.d.ts +572 -372
@@ -1,493 +1,10 @@
1
- import { resolveBarChannels } from "../grammar/bars/policy.js";
2
- import {
3
- assertMarkLabelPlacementSupport,
4
- normalizeMarkLabelPlacement,
5
- resolveMarkLabelPlacement
6
- } from "../layout/labels.js";
7
- import { resolveMarkLabelValues } from "../grammar/markLabels.js";
8
- import { formatTextValue, resolveTextLines, resolveTextOffsetDataset, textOffsetValue } from "../grammar/text.js";
9
- import { normalizePositionDatum } from "../grammar/positionDatum.js";
10
- import { mapOrdinalPositionValues } from "../grammar/scales/index.js";
11
- import { findDataset } from "../selectors/datasets.js";
12
- import { findCoordinate } from "../selectors/coordinates.js";
13
- import { findLayer } from "../selectors/layers.js";
14
- import { unionConcreteGraphicBounds } from
15
- "../grammar/schemas/graphicBounds.js";
16
- import { polarDirection } from "../grammar/polar.js";
17
- import { resolveMarkItems } from "./selection/policies/index.js";
18
- import { resolveMarkSelection } from "./selection/state.js";
19
- import { mapScaleConsumerValues } from "./scales/map.js";
20
- import { resolveRowEncodingValues } from "./rowEncoding.js";
1
+ import { resolveMarkItems } from './selection/policies/index.js';
2
+ import { resolveTextGraphicEntries as resolveEntries } from './textGeometry.js';
3
+ export { sourceValue, validateSourceMarkLabelPlacement } from './textGeometry.js';
21
4
 
22
- function sourceValue(item, source, field) {
23
- const measure = source.mark.type === "bar" ? resolveBarChannels(source).measure
24
- : source.mark.type === "arc" ? "radius" : undefined;
25
- const encoding = source.encoding?.[measure];
26
- if (encoding?.field === field && encoding.aggregate != null && item.channels[measure] !== undefined) {
27
- return source.mark.type === "bar"
28
- ? item.channels[`${measure}2`] ?? item.channels[measure]
29
- : item.channels[measure];
30
- }
31
- if (Object.hasOwn(item.fields, field)) return item.fields[field];
32
- for (const [channel, encoding] of Object.entries(source.encoding ?? {})) {
33
- if (encoding?.field !== field || item.channels[channel] === undefined) continue;
34
- if (source.mark.type === "bar" && item.channels[`${channel}2`] !== undefined) {
35
- return item.channels[`${channel}2`];
36
- }
37
- return item.channels[channel];
38
- }
39
- if (item.members.length === 1 && Object.hasOwn(item.members[0], field)) {
40
- return item.members[0][field];
41
- }
42
- if (source.mark.type === "line" && item.members.length > 0) {
43
- const endpoint = item.members.at(-1);
44
- if (Object.hasOwn(endpoint, field)) return endpoint[field];
45
- }
46
- return undefined;
5
+ export function resolveTextGraphicEntries(program, layer, config, options = {}) {
6
+ return resolveEntries(program, layer, config, { ...options, resolveMarkItems });
47
7
  }
48
-
49
- function barAnchor(program, source, item) {
50
- const channels = resolveBarChannels(source);
51
- const measure = channels.measure;
52
- const endpoint = item.channels[`${measure}2`];
53
- const scale = program.resolvedScales[source.encoding[measure].scale];
54
- const mapped = endpoint === undefined || scale === undefined
55
- ? undefined
56
- : mapScaleConsumerValues([endpoint], scale, measure)[0];
57
- return measure === "x"
58
- ? {
59
- x: mapped ?? item.properties.x + item.properties.width,
60
- y: item.properties.y + item.properties.height / 2
61
- }
62
- : {
63
- x: item.properties.x + item.properties.width / 2,
64
- y: mapped ?? item.properties.y
65
- };
66
- }
67
-
68
- function sourceAnchor(program, source, item) {
69
- if (source.mark.type === "bar") return barAnchor(program, source, item);
70
- if (source.mark.type === "rect") {
71
- return {
72
- x: item.properties.x + item.properties.width / 2,
73
- y: item.properties.y + item.properties.height / 2
74
- };
75
- }
76
- if (source.mark.type === "rule") {
77
- return {
78
- x: item.properties.x2,
79
- y: item.properties.y2
80
- };
81
- }
82
- if (source.mark.type === "line") {
83
- const graphicId = item.graphicIds?.[0];
84
- const graphic = program.graphicSpec.objects[source.id]?.items?.find(
85
- candidate => candidate.id === graphicId
86
- );
87
- const commands = graphic?.properties?.commands;
88
- const endpoint = Array.isArray(commands)
89
- ? [...commands].reverse().find(command => Number.isFinite(command.x) && Number.isFinite(command.y))
90
- : undefined;
91
- return { x: endpoint?.x, y: endpoint?.y };
92
- }
93
- return { x: item.properties.x, y: item.properties.y };
94
- }
95
-
96
- function explicitIntervalAxis(source) {
97
- const axes = ["x", "y"].filter(axis =>
98
- source.encoding?.[axis] !== undefined &&
99
- source.encoding?.[`${axis}2`] !== undefined
100
- );
101
- return axes.length === 1 ? axes[0] : undefined;
102
- }
103
-
104
- function sourcePlacementContext(program, source) {
105
- const coordinateType = findCoordinate(program, source.coordinate)?.type;
106
- return {
107
- markType: source.mark.type,
108
- coordinateType,
109
- intervalAxis: source.mark.type === "bar"
110
- ? resolveBarChannels(source).measure
111
- : source.mark.type === "rect"
112
- ? explicitIntervalAxis(source)
113
- : undefined
114
- };
115
- }
116
-
117
- export function validateSourceMarkLabelPlacement(program, sourceId, value) {
118
- const source = findLayer(program, sourceId);
119
- if (source === undefined) {
120
- throw new Error(`Unknown mark label placement source "${sourceId}".`);
121
- }
122
- const placement = normalizeMarkLabelPlacement(value);
123
- assertMarkLabelPlacementSupport(sourcePlacementContext(program, source), placement);
124
- return placement;
125
- }
126
-
127
- function rectangleBounds(item) {
128
- const { x, y, width, height } = item.properties;
129
- if (![x, y, width, height].every(Number.isFinite)) {
130
- throw new TypeError("Interval mark label placement requires finite rectangle geometry.");
131
- }
132
- return {
133
- left: Math.min(x, x + width),
134
- right: Math.max(x, x + width),
135
- top: Math.min(y, y + height),
136
- bottom: Math.max(y, y + height)
137
- };
138
- }
139
-
140
- function mappedChannel(program, source, item, channel) {
141
- const value = item.channels[channel];
142
- const primary = channel === "x2" ? "x" : channel === "y2" ? "y" : channel;
143
- const scale = program.resolvedScales[
144
- source.encoding?.[channel]?.scale ?? source.encoding?.[primary]?.scale
145
- ];
146
- return value === undefined || scale === undefined
147
- ? undefined
148
- : mapScaleConsumerValues([value], scale, channel)[0];
149
- }
150
-
151
- function scaleIncreaseDirection(program, source, axis) {
152
- const scale = program.resolvedScales[source.encoding?.[axis]?.scale];
153
- const domain = scale?.domain;
154
- if (!Array.isArray(domain) || domain.length < 2) {
155
- return axis === "x" ? { x: 1, y: 0 } : { x: 0, y: -1 };
156
- }
157
- const mapped = mapScaleConsumerValues(
158
- [domain[0], domain.at(-1)],
159
- scale,
160
- axis
161
- );
162
- const delta = mapped[1] - mapped[0];
163
- if (!Number.isFinite(delta) || delta === 0) {
164
- return axis === "x" ? { x: 1, y: 0 } : { x: 0, y: -1 };
165
- }
166
- return axis === "x"
167
- ? { x: Math.sign(delta), y: 0 }
168
- : { x: 0, y: Math.sign(delta) };
169
- }
170
-
171
- function intervalPlacementGeometry(program, source, item, axis) {
172
- const bounds = rectangleBounds(item);
173
- const primary = mappedChannel(program, source, item, axis);
174
- const secondary = mappedChannel(program, source, item, `${axis}2`);
175
- if (![primary, secondary].every(Number.isFinite)) {
176
- throw new TypeError(
177
- `${source.mark.type === "bar" ? "Bar" : "Rect"} semantic label placement requires resolved ${axis}/${axis}2 endpoints.`
178
- );
179
- }
180
- const cross = axis === "x"
181
- ? (bounds.top + bounds.bottom) / 2
182
- : (bounds.left + bounds.right) / 2;
183
- const start = axis === "x" ? { x: primary, y: cross } : { x: cross, y: primary };
184
- const end = axis === "x" ? { x: secondary, y: cross } : { x: cross, y: secondary };
185
- const delta = { x: end.x - start.x, y: end.y - start.y };
186
- const length = Math.hypot(delta.x, delta.y);
187
- const endOutward = length > 0
188
- ? { x: delta.x / length, y: delta.y / length }
189
- : scaleIncreaseDirection(program, source, axis);
190
- return {
191
- kind: "interval",
192
- center: {
193
- x: (bounds.left + bounds.right) / 2,
194
- y: (bounds.top + bounds.bottom) / 2
195
- },
196
- start: {
197
- point: start,
198
- outward: { x: -endOutward.x, y: -endOutward.y }
199
- },
200
- end: { point: end, outward: endOutward },
201
- bounds
202
- };
203
- }
204
-
205
- function pointPlacementGeometry(program, source, item) {
206
- const center = { x: item.properties.x, y: item.properties.y };
207
- const coordinateType = findCoordinate(program, source.coordinate)?.type;
208
- if (coordinateType !== "polar") return { kind: "point", center };
209
- const thetaScale = program.resolvedScales[source.encoding?.theta?.scale];
210
- const theta = item.channels.theta === undefined || thetaScale === undefined
211
- ? undefined
212
- : mapScaleConsumerValues([item.channels.theta], thetaScale, "theta")[0];
213
- const outward = Number.isFinite(theta) ? polarDirection(theta) : { x: 0, y: -1 };
214
- const objectBounds = unionConcreteGraphicBounds(
215
- program.graphicSpec,
216
- item.graphicIds
217
- , program.materializationConfigs.textMetrics);
218
- const corners = objectBounds === undefined ? [] : [
219
- { x: objectBounds.left, y: objectBounds.top },
220
- { x: objectBounds.right, y: objectBounds.top },
221
- { x: objectBounds.right, y: objectBounds.bottom },
222
- { x: objectBounds.left, y: objectBounds.bottom }
223
- ];
224
- const support = Math.max(0, ...corners.map(point =>
225
- (point.x - center.x) * outward.x + (point.y - center.y) * outward.y
226
- ));
227
- return {
228
- kind: "point",
229
- center,
230
- end: {
231
- point: {
232
- x: center.x + outward.x * support,
233
- y: center.y + outward.y * support
234
- },
235
- outward
236
- }
237
- };
238
- }
239
-
240
- function sourcePlacementGeometry(program, source, item) {
241
- if (source.mark.type === "bar") {
242
- return intervalPlacementGeometry(
243
- program,
244
- source,
245
- item,
246
- resolveBarChannels(source).measure
247
- );
248
- }
249
- if (source.mark.type === "rect") {
250
- const axis = explicitIntervalAxis(source);
251
- return axis === undefined
252
- ? { kind: "point", center: sourceAnchor(program, source, item) }
253
- : intervalPlacementGeometry(program, source, item, axis);
254
- }
255
- if (source.mark.type === "arc") return item.geometry;
256
- if (source.mark.type === "point") {
257
- return pointPlacementGeometry(program, source, item);
258
- }
259
- return { kind: "point", center: sourceAnchor(program, source, item) };
260
- }
261
-
262
- function contentValue(encoding, rowOrItem, source) {
263
- return Object.hasOwn(encoding, "field")
264
- ? source === undefined
265
- ? rowOrItem[encoding.field]
266
- : sourceValue(rowOrItem, source, encoding.field)
267
- : encoding.datum;
268
- }
269
-
270
- function concreteTextItem(config, position, text, { offsets = true } = {}) {
271
- const x = position.x;
272
- const y = position.y;
273
- if (text === undefined || !Number.isFinite(x) || !Number.isFinite(y)) {
274
- return undefined;
275
- }
276
- const lines = resolveTextLines(text, config);
277
- return {
278
- type: "text",
279
- properties: {
280
- x: x + (offsets ? config.dx : 0),
281
- y: y + (offsets ? config.dy : 0),
282
- text,
283
- ...(lines === undefined ? {} : { lines }),
284
- fill: config.fill,
285
- opacity: config.opacity,
286
- fontSize: config.fontSize,
287
- fontFamily: config.fontFamily,
288
- fontWeight: config.fontWeight,
289
- textAlign: config.align,
290
- textBaseline: config.baseline,
291
- rotation: config.rotation
292
- }
293
- };
294
- }
295
-
296
- function concreteItem(config, position, value, format) {
297
- return concreteTextItem(config, position, formatTextValue(value, format));
298
- }
299
-
300
- function relativeLuminance(color) {
301
- if (typeof color !== "string" || !/^#[0-9a-f]{6}$/i.test(color)) {
302
- return undefined;
303
- }
304
- const channels = [1, 3, 5].map(offset =>
305
- Number.parseInt(color.slice(offset, offset + 2), 16) / 255
306
- ).map(value => value <= 0.04045
307
- ? value / 12.92
308
- : ((value + 0.055) / 1.055) ** 2.4
309
- );
310
- return 0.2126 * channels[0] + 0.7152 * channels[1] + 0.0722 * channels[2];
311
- }
312
-
313
- function sourceTextConfig(config, source, item) {
314
- if (config.inheritColor && config.fillExplicit !== true) {
315
- const fill = item.properties[config.inheritColor];
316
- if (typeof fill !== "string") throw new Error(`Source mark "${source.id}" has no resolved ${config.inheritColor} color.`);
317
- return { ...config, fill };
318
- }
319
- if (
320
- !["arc", "rect"].includes(source.mark.type) ||
321
- config.fillExplicit === true
322
- ) return config;
323
- const luminance = relativeLuminance(item.properties.fill);
324
- if (luminance === undefined) return config;
325
- return {
326
- ...config,
327
- fill: luminance >= 0.26 ? "#0f172a" : "#f8fafc"
328
- };
329
- }
330
-
331
- function resolveSourceTextItems(program, layer, config) {
332
- const source = findLayer(program, layer.source);
333
- if (source === undefined) {
334
- throw new Error(`Text mark "${layer.id}" requires source layer "${layer.source}".`);
335
- }
336
- const requested = program.markConfigs[layer.id]?.labelAuthoring?.selection ?? {
337
- kind: "all"
338
- };
339
- let allItems;
340
- let items;
341
- if (requested.kind === "all") {
342
- allItems = resolveMarkItems(program, source.id);
343
- items = allItems;
344
- } else if (requested.kind === "inline") {
345
- const resolved = resolveMarkSelection(program, source.id, requested.selector);
346
- allItems = resolved.items;
347
- const selected = new Set(resolved.keys);
348
- items = allItems.filter(item => selected.has(item.key));
349
- } else if (requested.kind === "named") {
350
- const definition = program.materializationConfigs.selections?.[requested.id];
351
- if (definition === undefined) {
352
- throw new Error(`Unknown label selection "${requested.id}".`);
353
- }
354
- if (definition.target !== source.id) {
355
- throw new Error(
356
- `Label selection "${requested.id}" must target source mark "${source.id}".`
357
- );
358
- }
359
- const resolved = resolveMarkSelection(program, source.id, definition.selector);
360
- allItems = resolved.items;
361
- const selected = new Set(resolved.keys);
362
- items = allItems.filter(item => selected.has(item.key));
363
- } else {
364
- throw new Error(`Unknown label selection kind "${requested.kind}".`);
365
- }
366
- const resolvedValues = layer.encoding.text.content === undefined ? undefined
367
- : resolveMarkLabelValues(source, allItems, layer.encoding.text);
368
- const values = resolvedValues === undefined ? undefined : new Map(
369
- allItems.map((item, index) => [item.key, resolvedValues[index]])
370
- );
371
- return items.flatMap(item => {
372
- let resolvedConfig = sourceTextConfig(config, source, item);
373
- const text = formatTextValue(
374
- values === undefined
375
- ? contentValue(layer.encoding.text, item, source)
376
- : values.get(item.key),
377
- layer.encoding.text.format
378
- );
379
- if (text === undefined) return [];
380
- const placement = program.markConfigs[layer.id]?.labelAuthoring?.placement;
381
- if (placement === undefined) {
382
- const anchor = sourceAnchor(program, source, item);
383
- const concrete = concreteTextItem(resolvedConfig, anchor, text);
384
- return concrete === undefined ? [] : [{ graphic: concrete, anchor }];
385
- }
386
- assertMarkLabelPlacementSupport(sourcePlacementContext(program, source), placement);
387
- const resolved = resolveMarkLabelPlacement({
388
- placement,
389
- geometry: sourcePlacementGeometry(program, source, item),
390
- text: {
391
- text,
392
- lines: resolveTextLines(text, resolvedConfig),
393
- fontSize: resolvedConfig.fontSize,
394
- fontFamily: resolvedConfig.fontFamily,
395
- fontWeight: resolvedConfig.fontWeight,
396
- textAlign: resolvedConfig.align,
397
- textBaseline: resolvedConfig.baseline,
398
- rotation: resolvedConfig.rotation,
399
- dx: resolvedConfig.dx,
400
- dy: resolvedConfig.dy
401
- }
402
- }, program.materializationConfigs.textMetrics);
403
- if (!resolved.visible) return [];
404
- if (
405
- resolved.anchor.startsWith("outside") &&
406
- config.fillExplicit !== true && !config.inheritColor
407
- ) {
408
- resolvedConfig = config;
409
- }
410
- const anchor = { x: resolved.sourceX, y: resolved.sourceY };
411
- return [{
412
- graphic: concreteTextItem(
413
- resolvedConfig,
414
- { x: resolved.x, y: resolved.y },
415
- text,
416
- { offsets: false }
417
- ),
418
- anchor,
419
- placement: resolved
420
- }];
421
- });
422
- }
423
-
424
- function resolveRowTextItems(program, layer, config) {
425
- const sourceDataset = findDataset(program, layer.data);
426
- if (sourceDataset === undefined) {
427
- throw new Error(`Text mark "${layer.id}" requires an existing dataset.`);
428
- }
429
- const dataset = resolveTextOffsetDataset(layer, sourceDataset);
430
- const encodings = [layer.encoding?.x, layer.encoding?.y, layer.encoding?.text, layer.encoding?.color,
431
- layer.encoding?.dx, layer.encoding?.dy];
432
- const length = encodings.some(encoding => encoding !== undefined && Object.hasOwn(encoding, "field"))
433
- ? dataset.values.length
434
- : 1;
435
- const positionValues = channel => {
436
- const encoding = layer.encoding?.[channel];
437
- if (encoding === undefined) return undefined;
438
- if (Object.hasOwn(encoding, "field")) {
439
- return resolveRowEncodingValues(program, layer, dataset, channel);
440
- }
441
- const scale = program.resolvedScales[encoding.scale];
442
- if (scale === undefined) {
443
- throw new Error(
444
- `text mark "${layer.id}" requires resolved ${channel} scale "${encoding.scale}".`
445
- );
446
- }
447
- const datum = normalizePositionDatum(
448
- encoding.datum,
449
- encoding.fieldType,
450
- channel,
451
- encoding.temporalUnit,
452
- "Text"
453
- );
454
- const mapped = ["nominal", "ordinal"].includes(encoding.fieldType)
455
- ? mapOrdinalPositionValues([datum], scale)
456
- : mapScaleConsumerValues([datum], scale, channel);
457
- return Array.from({ length }, () => mapped[0]);
458
- };
459
- const x = positionValues("x");
460
- const y = positionValues("y");
461
- if (x === undefined || y === undefined) return [];
462
- const rows = length === 1 && dataset.values.length === 0
463
- ? [undefined]
464
- : dataset.values.slice(0, length);
465
- const fills = resolveRowEncodingValues(program, layer, dataset, "color");
466
- return rows.flatMap((row, index) => {
467
- const concrete = concreteItem(
468
- {
469
- ...config,
470
- ...(fills === undefined ? {} : { fill: fills[index] }),
471
- dx: textOffsetValue(layer.encoding?.dx, row, config.dx),
472
- dy: textOffsetValue(layer.encoding?.dy, row, config.dy)
473
- },
474
- { x: x[index], y: y[index] },
475
- contentValue(layer.encoding.text, row),
476
- layer.encoding.text.format
477
- );
478
- return concrete === undefined
479
- ? []
480
- : [{ graphic: concrete, anchor: { x: x[index], y: y[index] } }];
481
- });
482
- }
483
-
484
- export function resolveTextGraphicEntries(program, layer, config) {
485
- return layer.source === undefined
486
- ? resolveRowTextItems(program, layer, config)
487
- : resolveSourceTextItems(program, layer, config);
488
- }
489
-
490
8
  export function resolveTextGraphicItems(program, layer, config) {
491
- return resolveTextGraphicEntries(program, layer, config)
492
- .map(entry => entry.graphic);
9
+ return resolveTextGraphicEntries(program, layer, config).map(entry => entry.graphic);
493
10
  }