ggaction 0.0.2 → 0.0.3
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/CHANGELOG.md +28 -0
- package/README.md +12 -1
- package/package.json +6 -1
- package/src/actions/boxPlots/components.js +3 -3
- package/src/actions/boxPlots/edit.js +277 -0
- package/src/actions/boxPlots/index.js +2 -0
- package/src/actions/boxPlots/materialize.js +12 -1
- package/src/actions/boxPlots/options.js +25 -14
- package/src/actions/canvas/actions.js +21 -17
- package/src/actions/composition/actions.js +247 -0
- package/src/actions/composition/index.js +1 -0
- package/src/actions/coordinates/actions.js +2 -5
- package/src/actions/data/density.js +7 -1
- package/src/actions/data/filter.js +3 -1
- package/src/actions/encodings/appearance.js +11 -0
- package/src/actions/encodings/barWidth.js +5 -3
- package/src/actions/encodings/color.js +120 -25
- package/src/actions/encodings/index.js +2 -0
- package/src/actions/encodings/offset.js +90 -72
- package/src/actions/encodings/position/apply.js +66 -0
- package/src/actions/encodings/position/policies/arc.js +26 -0
- package/src/actions/encodings/position/policies/bar.js +9 -5
- package/src/actions/encodings/position/policies/index.js +5 -1
- package/src/actions/encodings/position/policies/line.js +20 -3
- package/src/actions/encodings/position/resolve.js +57 -8
- package/src/actions/encodings/position.js +44 -90
- package/src/actions/encodings/ranged.js +28 -6
- package/src/actions/encodings/shared.js +17 -21
- package/src/actions/encodings/text.js +73 -0
- package/src/actions/errorBands/create.js +37 -29
- package/src/actions/errorBands/edit.js +206 -0
- package/src/actions/errorBands/index.js +9 -0
- package/src/actions/errorBands/options.js +39 -0
- package/src/actions/errorBars/create.js +50 -43
- package/src/actions/errorBars/edit.js +130 -0
- package/src/actions/errorBars/index.js +3 -0
- package/src/actions/errorBars/options.js +52 -0
- package/src/actions/facets/actions.js +162 -0
- package/src/actions/facets/derive.js +200 -0
- package/src/actions/facets/guides.js +16 -0
- package/src/actions/facets/index.js +1 -0
- package/src/actions/facets/replay.js +69 -0
- package/src/actions/guides/applicability.js +113 -0
- package/src/actions/guides/axes/axes.js +70 -16
- package/src/actions/guides/axes/edit.js +126 -0
- package/src/actions/guides/axes/index.js +12 -0
- package/src/actions/guides/axes/labels.js +21 -18
- package/src/actions/guides/axes/remove.js +60 -0
- package/src/actions/guides/axes/tickGroups.js +7 -5
- package/src/actions/guides/axes/ticks.js +9 -4
- package/src/actions/guides/axes/titles.js +7 -5
- package/src/actions/guides/grids/grid.js +144 -15
- package/src/actions/guides/grids/resolve.js +5 -13
- package/src/actions/guides/guides.js +17 -75
- package/src/actions/guides/index.js +6 -0
- package/src/actions/guides/legends/categorical/actions.js +48 -9
- package/src/actions/guides/legends/categorical/components.js +16 -16
- package/src/actions/guides/legends/categorical/index.js +14 -14
- package/src/actions/guides/legends/categorical/layout.js +3 -1
- package/src/actions/guides/legends/categorical/options.js +2 -5
- package/src/actions/guides/legends/categorical/resolve.js +2 -2
- package/src/actions/guides/legends/categorical/symbols.js +14 -14
- package/src/actions/guides/legends/continuous/common.js +7 -7
- package/src/actions/guides/legends/continuous/interval.js +5 -5
- package/src/actions/guides/legends/edit.js +2 -6
- package/src/actions/guides/legends/focused.js +63 -0
- package/src/actions/guides/legends/index.js +4 -0
- package/src/actions/guides/legends/remove.js +64 -0
- package/src/actions/guides/legends/size.js +12 -14
- package/src/actions/guides/polar/axes/facade.js +219 -0
- package/src/actions/guides/polar/axes/labels.js +188 -0
- package/src/actions/guides/polar/axes/lines.js +113 -0
- package/src/actions/guides/polar/axes/shared.js +112 -0
- package/src/actions/guides/polar/axes/ticks.js +164 -0
- package/src/actions/guides/polar/axes/titles.js +165 -0
- package/src/actions/guides/polar/axes.js +55 -0
- package/src/actions/guides/polar/grids.js +252 -0
- package/src/actions/guides/polar/index.js +2 -0
- package/src/actions/guides/polar/resolve.js +269 -0
- package/src/actions/index.js +4 -0
- package/src/actions/marks/arc/actions.js +239 -0
- package/src/actions/marks/arc/index.js +1 -0
- package/src/actions/marks/{area.js → area/actions.js} +51 -38
- package/src/actions/marks/area/index.js +4 -0
- package/src/actions/marks/bar/create.js +26 -5
- package/src/actions/marks/bar/materialize.js +25 -31
- package/src/actions/marks/index.js +12 -4
- package/src/actions/marks/lifecycle.js +27 -0
- package/src/actions/marks/line/actions.js +373 -0
- package/src/actions/marks/line/index.js +1 -0
- package/src/actions/marks/{point.js → point/actions.js} +67 -108
- package/src/actions/marks/point/index.js +1 -0
- package/src/actions/marks/rect/actions.js +165 -0
- package/src/actions/marks/rect/index.js +1 -0
- package/src/actions/marks/remove.js +214 -0
- package/src/actions/marks/{rule.js → rule/actions.js} +34 -28
- package/src/actions/marks/rule/index.js +1 -0
- package/src/actions/marks/shared.js +156 -25
- package/src/actions/marks/text/actions.js +192 -0
- package/src/actions/marks/text/index.js +1 -0
- package/src/actions/primitives/createGraphics.js +13 -6
- package/src/actions/primitives/editGraphics.js +10 -3
- package/src/actions/primitives/semantic.js +26 -3
- package/src/actions/primitives/semanticValue.js +25 -3
- package/src/actions/regression/components.js +1 -1
- package/src/actions/regression/create.js +18 -1
- package/src/actions/regression/edit.js +247 -0
- package/src/actions/regression/index.js +3 -0
- package/src/actions/regression/resolve.js +3 -1
- package/src/actions/scales/consumers.js +15 -6
- package/src/actions/scales/definitions.js +13 -4
- package/src/actions/scales/edit.js +42 -19
- package/src/actions/scales/materialize.js +57 -408
- package/src/actions/selection/actions.js +38 -24
- package/src/actions/selection/index.js +2 -0
- package/src/actions/titles/actions.js +63 -2
- package/src/actions/titles/index.js +2 -0
- package/src/actions/titles/resolve.js +12 -17
- package/src/composition.js +10 -0
- package/src/core/ChartProgram.js +200 -2
- package/src/core/action.js +10 -0
- package/src/core/validation.js +16 -0
- package/src/core/vocabulary.js +154 -4
- package/src/grammar/arcs.js +154 -0
- package/src/grammar/bars/aggregate.js +2 -2
- package/src/grammar/bars/geometry.js +3 -3
- package/src/grammar/bars/policy.js +7 -1
- package/src/grammar/coordinates.js +3 -7
- package/src/grammar/density.js +20 -1
- package/src/grammar/facets/dependencies.js +185 -0
- package/src/grammar/facets/guides.js +257 -0
- package/src/grammar/facets/scales.js +254 -0
- package/src/grammar/facets.js +122 -0
- package/src/grammar/lineSeries.js +111 -3
- package/src/grammar/polar.js +102 -0
- package/src/grammar/polarGuides.js +243 -0
- package/src/grammar/polarLineCommands.js +43 -0
- package/src/grammar/polarPaths.js +161 -0
- package/src/grammar/positionCompatibility.js +18 -2
- package/src/grammar/rects.js +26 -0
- package/src/grammar/scales/continuous.js +6 -1
- package/src/grammar/scales/fields.js +7 -0
- package/src/grammar/scales/ordinal.js +5 -2
- package/src/grammar/scales/policies.js +1 -1
- package/src/grammar/schemas/graphic.js +1 -1
- package/src/grammar/schemas/graphicBounds.js +64 -9
- package/src/grammar/schemas/graphicTree.js +44 -10
- package/src/grammar/schemas/semanticPath.js +31 -9
- package/src/grammar/seriesLayout.js +13 -2
- package/src/grammar/text.js +71 -0
- package/src/grammar/ticks.js +50 -13
- package/src/index.js +1 -0
- package/src/layout/composition.js +310 -0
- package/src/layout/facets.js +131 -0
- package/src/layout/title.js +6 -0
- package/src/materialization/bars/aggregate.js +5 -2
- package/src/materialization/bars/grouped.js +75 -37
- package/src/materialization/composition.js +152 -0
- package/src/materialization/compositionSnapshot.js +114 -0
- package/src/materialization/dependencies.js +71 -16
- package/src/materialization/encodings.js +14 -28
- package/src/materialization/facetGuides.js +426 -0
- package/src/materialization/facets.js +214 -0
- package/src/materialization/graphicHierarchy.js +15 -5
- package/src/materialization/guides/resources.js +46 -0
- package/src/materialization/marks.js +220 -14
- package/src/materialization/planner.js +105 -12
- package/src/materialization/rect.js +138 -0
- package/src/materialization/rectConfig.js +38 -0
- package/src/materialization/rowEncoding.js +33 -0
- package/src/materialization/scales/map.js +40 -0
- package/src/materialization/scales/policies/arc.js +37 -0
- package/src/materialization/scales/policies/bar.js +132 -0
- package/src/materialization/scales/policies/series.js +76 -0
- package/src/materialization/scales/resolve.js +326 -0
- package/src/materialization/selection/items/arc.js +29 -0
- package/src/materialization/selection/items/bar.js +255 -0
- package/src/materialization/selection/items/common.js +131 -0
- package/src/materialization/selection/items/index.js +6 -0
- package/src/materialization/selection/items/path.js +59 -0
- package/src/materialization/selection/items/point.js +55 -0
- package/src/materialization/selection/items/rect.js +23 -0
- package/src/materialization/selection/items/rule.js +25 -0
- package/src/materialization/selection/policies/arc.js +10 -0
- package/src/materialization/selection/policies/area.js +1 -1
- package/src/materialization/selection/policies/bar.js +1 -1
- package/src/materialization/selection/policies/index.js +5 -1
- package/src/materialization/selection/policies/line.js +1 -1
- package/src/materialization/selection/policies/point.js +1 -1
- package/src/materialization/selection/policies/rect.js +10 -0
- package/src/materialization/selection/policies/rule.js +1 -1
- package/src/materialization/selection/styles.js +24 -16
- package/src/materialization/text.js +156 -0
- package/src/renderers/canvas/index.js +68 -22
- package/types/extension.d.ts +1 -0
- package/types/index.d.ts +56 -1
- package/types/program.d.ts +664 -27
- package/src/actions/marks/line.js +0 -248
- package/src/materialization/selection/items.js +0 -503
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
import { isPlainObject } from "../core/immutable.js";
|
|
2
|
+
import { unionConcreteGraphicBounds } from
|
|
3
|
+
"../grammar/schemas/graphicBounds.js";
|
|
4
|
+
import {
|
|
5
|
+
planFacetGuideOwnership,
|
|
6
|
+
resolveSharedFacetLegends
|
|
7
|
+
} from "../grammar/facets/guides.js";
|
|
8
|
+
import { mapOrdinalValues } from "../grammar/scales.js";
|
|
9
|
+
import { findLayer } from "../selectors/layers.js";
|
|
10
|
+
import { findSemanticScale } from "../selectors/scales.js";
|
|
11
|
+
import { axisGraphicIds, allLegendGraphicIds } from
|
|
12
|
+
"./guides/resources.js";
|
|
13
|
+
import { attachSnapshotObject } from "./composition.js";
|
|
14
|
+
import { namespaceGraphicId } from "./compositionSnapshot.js";
|
|
15
|
+
import { DEFAULT_COLORS, DEFAULT_FONT_FAMILY } from "../theme/defaults.js";
|
|
16
|
+
|
|
17
|
+
const LEGEND_SOURCE_CANVAS = Object.freeze({
|
|
18
|
+
width: 640,
|
|
19
|
+
height: 400,
|
|
20
|
+
margin: Object.freeze({ top: 40, right: 170, bottom: 60, left: 70 })
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
function legendKinds(program) {
|
|
24
|
+
return Object.keys(program.guideConfigs.legend ?? {});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function textItem(text, x, y, options = {}) {
|
|
28
|
+
return {
|
|
29
|
+
type: "text",
|
|
30
|
+
properties: {
|
|
31
|
+
x,
|
|
32
|
+
y,
|
|
33
|
+
text: String(text),
|
|
34
|
+
fill: options.color ?? DEFAULT_COLORS.strongText,
|
|
35
|
+
fontSize: options.fontSize ?? 12,
|
|
36
|
+
fontFamily: options.fontFamily ?? DEFAULT_FONT_FAMILY,
|
|
37
|
+
fontWeight: options.fontWeight ?? "normal",
|
|
38
|
+
textAlign: options.textAlign ?? "left",
|
|
39
|
+
textBaseline: options.textBaseline ?? "middle"
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function collection(program, id, items) {
|
|
45
|
+
return program
|
|
46
|
+
.createGraphics({ id, type: "collection", parent: "canvas" })
|
|
47
|
+
.editGraphics({ target: id, property: "items", value: items });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function resolveLegacyCategoricalLegend(program) {
|
|
51
|
+
const encodings = program.semanticSpec.layers.flatMap(layer =>
|
|
52
|
+
layer.encoding?.color === undefined
|
|
53
|
+
? []
|
|
54
|
+
: [{ layer, encoding: layer.encoding.color }]
|
|
55
|
+
);
|
|
56
|
+
if (encodings.length === 0) return undefined;
|
|
57
|
+
const fields = new Set(encodings.map(entry => entry.encoding.field));
|
|
58
|
+
const scales = new Set(encodings.map(entry => entry.encoding.scale));
|
|
59
|
+
const marks = new Set(encodings.map(entry => entry.layer.mark?.type));
|
|
60
|
+
if (fields.size !== 1 || scales.size !== 1 || marks.size !== 1) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
"Shared facet legend requires one compatible color field, scale, and mark recipe."
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
if (encodings.some(entry =>
|
|
66
|
+
!["nominal", "ordinal"].includes(entry.encoding.fieldType)
|
|
67
|
+
)) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
const firstId = program.compositionSpec.children[0];
|
|
71
|
+
const scaleId = encodings[0].encoding.scale;
|
|
72
|
+
const scale = program.children[firstId].resolvedScales[scaleId];
|
|
73
|
+
if (scale?.type !== "ordinal") return undefined;
|
|
74
|
+
const config = {
|
|
75
|
+
kind: "color",
|
|
76
|
+
target: encodings[0].layer.id,
|
|
77
|
+
field: encodings[0].encoding.field,
|
|
78
|
+
scale: scaleId,
|
|
79
|
+
title: encodings[0].encoding.field
|
|
80
|
+
};
|
|
81
|
+
const compatibility = resolveSharedFacetLegends(
|
|
82
|
+
program.compositionSpec.children.map(id => ({
|
|
83
|
+
id,
|
|
84
|
+
guideConfigs: { legend: { color: config } },
|
|
85
|
+
resolvedScales: program.children[id].resolvedScales
|
|
86
|
+
}))
|
|
87
|
+
);
|
|
88
|
+
return {
|
|
89
|
+
mode: "legacyCategorical",
|
|
90
|
+
compatibility,
|
|
91
|
+
reservation: { gap: 18, width: 132 },
|
|
92
|
+
legend: {
|
|
93
|
+
field: config.field,
|
|
94
|
+
mark: encodings[0].layer.mark.type,
|
|
95
|
+
domain: scale.domain,
|
|
96
|
+
colors: mapOrdinalValues(scale.domain, scale.domain, scale.range)
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function materializeLegacyCategoricalLegend(program, prepared, layout) {
|
|
102
|
+
const legend = prepared.legend;
|
|
103
|
+
const items = [textItem(legend.field, layout.legend.x, layout.legend.y, {
|
|
104
|
+
fontSize: 12,
|
|
105
|
+
fontWeight: 700,
|
|
106
|
+
textBaseline: "top"
|
|
107
|
+
})];
|
|
108
|
+
legend.domain.forEach((value, index) => {
|
|
109
|
+
const y = layout.legend.y + 29 + index * 26;
|
|
110
|
+
items.push(legend.mark === "point"
|
|
111
|
+
? {
|
|
112
|
+
type: "circle",
|
|
113
|
+
properties: {
|
|
114
|
+
x: layout.legend.x + 7,
|
|
115
|
+
y,
|
|
116
|
+
radius: 5.5,
|
|
117
|
+
fill: legend.colors[index],
|
|
118
|
+
stroke: "#ffffff",
|
|
119
|
+
strokeWidth: 0.35,
|
|
120
|
+
opacity: 1
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
: {
|
|
124
|
+
type: "rect",
|
|
125
|
+
properties: {
|
|
126
|
+
x: layout.legend.x + 1,
|
|
127
|
+
y: y - 6,
|
|
128
|
+
width: 12,
|
|
129
|
+
height: 12,
|
|
130
|
+
fill: legend.colors[index],
|
|
131
|
+
stroke: "#ffffff",
|
|
132
|
+
strokeWidth: 0.6,
|
|
133
|
+
opacity: 1
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
items.push(textItem(value, layout.legend.x + 22, y, {
|
|
137
|
+
fontSize: 10.5
|
|
138
|
+
}));
|
|
139
|
+
});
|
|
140
|
+
return collection(
|
|
141
|
+
program,
|
|
142
|
+
`${program.compositionSpec.id}-legend`,
|
|
143
|
+
items
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function legendCandidates(program) {
|
|
148
|
+
const layers = program.semanticSpec.layers;
|
|
149
|
+
const color = layers.filter(layer => {
|
|
150
|
+
const encoding = layer.encoding?.color;
|
|
151
|
+
return encoding?.scale !== undefined &&
|
|
152
|
+
findSemanticScale(program, encoding.scale) !== undefined;
|
|
153
|
+
});
|
|
154
|
+
if (color.length === 1) {
|
|
155
|
+
const layer = color[0];
|
|
156
|
+
return {
|
|
157
|
+
target: layer.id,
|
|
158
|
+
channels: ["color", "shape", "strokeDash"].filter(
|
|
159
|
+
channel => layer.encoding?.[channel]?.scale !== undefined
|
|
160
|
+
)
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (color.length > 1) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
"Shared facet legend requires one unambiguous color legend target."
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
for (const channel of ["shape", "strokeDash", "size", "opacity"]) {
|
|
169
|
+
const candidates = layers.filter(
|
|
170
|
+
layer => layer.encoding?.[channel]?.scale !== undefined
|
|
171
|
+
);
|
|
172
|
+
if (candidates.length === 1) {
|
|
173
|
+
return { target: candidates[0].id, channels: [channel] };
|
|
174
|
+
}
|
|
175
|
+
if (candidates.length > 1) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
`Shared facet legend requires one unambiguous ${channel} legend target.`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
throw new Error("Shared facet legend requires one eligible encoded scale.");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function compactNumber(value) {
|
|
185
|
+
if (Math.abs(value) >= 1_000_000_000) {
|
|
186
|
+
return `${+(value / 1_000_000_000).toFixed(1)}B`;
|
|
187
|
+
}
|
|
188
|
+
if (Math.abs(value) >= 1_000_000) {
|
|
189
|
+
return `${Math.round(value / 1_000_000)}M`;
|
|
190
|
+
}
|
|
191
|
+
if (Math.abs(value) >= 1_000) {
|
|
192
|
+
return `${Math.round(value / 1_000)}K`;
|
|
193
|
+
}
|
|
194
|
+
return String(+value.toPrecision(3));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function continuousValues(domain, count) {
|
|
198
|
+
return Array.from(
|
|
199
|
+
{ length: count },
|
|
200
|
+
(_, index) => domain[0] + index / (count - 1) * (domain[1] - domain[0])
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function prepareAutoLegendSource(child) {
|
|
205
|
+
const request = legendCandidates(child);
|
|
206
|
+
const canvas = child.graphicSpec.objects.canvas;
|
|
207
|
+
let source = child
|
|
208
|
+
._withCanvasConfig({
|
|
209
|
+
margin: LEGEND_SOURCE_CANVAS.margin,
|
|
210
|
+
size: { width: "explicit", height: "explicit" }
|
|
211
|
+
})
|
|
212
|
+
.editGraphics({
|
|
213
|
+
target: "canvas",
|
|
214
|
+
property: "width",
|
|
215
|
+
value: LEGEND_SOURCE_CANVAS.width
|
|
216
|
+
})
|
|
217
|
+
.editGraphics({
|
|
218
|
+
target: "canvas",
|
|
219
|
+
property: "height",
|
|
220
|
+
value: LEGEND_SOURCE_CANVAS.height
|
|
221
|
+
});
|
|
222
|
+
if (canvas?.properties.background !== undefined) {
|
|
223
|
+
source = source.editGraphics({
|
|
224
|
+
target: "canvas",
|
|
225
|
+
property: "background",
|
|
226
|
+
value: canvas.properties.background
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
const scale = findSemanticScale(
|
|
230
|
+
source,
|
|
231
|
+
findLayer(source, request.target)?.encoding?.color?.scale
|
|
232
|
+
);
|
|
233
|
+
const gradient = scale?.type === "sequential";
|
|
234
|
+
source = source.createLegend({
|
|
235
|
+
target: request.target,
|
|
236
|
+
channels: request.channels,
|
|
237
|
+
...(gradient ? {
|
|
238
|
+
count: 5,
|
|
239
|
+
gradient: { length: 180, thickness: 12 },
|
|
240
|
+
labels: { offset: 9, fontSize: 10 },
|
|
241
|
+
titleStyle: { color: DEFAULT_COLORS.strongText, fontSize: 11 }
|
|
242
|
+
} : {})
|
|
243
|
+
});
|
|
244
|
+
if (gradient) {
|
|
245
|
+
const resolved = source.resolvedScales[scale.id];
|
|
246
|
+
source = source
|
|
247
|
+
.editGraphics({
|
|
248
|
+
target: "colorGradientLabels",
|
|
249
|
+
property: "text",
|
|
250
|
+
value: continuousValues(resolved.domain, 5).map(compactNumber)
|
|
251
|
+
})
|
|
252
|
+
.editGraphics({
|
|
253
|
+
target: "colorGradientTitle",
|
|
254
|
+
property: "y",
|
|
255
|
+
value: source.graphicSpec.objects.colorGradientTitle.properties.y + 8
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return source;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function compatibleLegendChildren(program, source, inferred) {
|
|
262
|
+
return program.compositionSpec.children.map(id => {
|
|
263
|
+
const child = program.children[id];
|
|
264
|
+
return {
|
|
265
|
+
id,
|
|
266
|
+
guideConfigs: inferred ? source.guideConfigs : child.guideConfigs,
|
|
267
|
+
resolvedScales: child.resolvedScales
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function prepareSharedFacetLegend(program) {
|
|
273
|
+
if (program.compositionSpec.facet.guides.legend !== "shared") {
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
const firstId = program.compositionSpec.children[0];
|
|
277
|
+
const first = program.children[firstId];
|
|
278
|
+
const inferred = legendKinds(first).length === 0;
|
|
279
|
+
if (inferred) {
|
|
280
|
+
const legacy = resolveLegacyCategoricalLegend(program);
|
|
281
|
+
if (legacy !== undefined) return legacy;
|
|
282
|
+
}
|
|
283
|
+
const source = inferred ? prepareAutoLegendSource(first) : first;
|
|
284
|
+
const kinds = legendKinds(source);
|
|
285
|
+
const compatibility = resolveSharedFacetLegends(
|
|
286
|
+
compatibleLegendChildren(program, source, inferred)
|
|
287
|
+
);
|
|
288
|
+
const roots = allLegendGraphicIds(kinds).filter(
|
|
289
|
+
id => source.graphicSpec.objects[id] !== undefined
|
|
290
|
+
);
|
|
291
|
+
if (roots.length === 0) {
|
|
292
|
+
throw new Error("Shared facet legend has no concrete guide graphics.");
|
|
293
|
+
}
|
|
294
|
+
const bounds = unionConcreteGraphicBounds(source.graphicSpec, roots);
|
|
295
|
+
if (bounds === undefined) {
|
|
296
|
+
throw new Error("Shared facet legend has no measurable concrete bounds.");
|
|
297
|
+
}
|
|
298
|
+
const gradient = kinds.includes("gradient");
|
|
299
|
+
return {
|
|
300
|
+
mode: "promoted",
|
|
301
|
+
source,
|
|
302
|
+
inferred,
|
|
303
|
+
kinds,
|
|
304
|
+
roots,
|
|
305
|
+
bounds,
|
|
306
|
+
compatibility,
|
|
307
|
+
reservation: {
|
|
308
|
+
gap: gradient ? 24 : 18,
|
|
309
|
+
width: gradient ? 112 : Math.max(132, Math.ceil(bounds.right - bounds.left))
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function copyLegendConfig(program, source) {
|
|
315
|
+
let next = program;
|
|
316
|
+
for (const [kind, config] of Object.entries(source.guideConfigs.legend ?? {})) {
|
|
317
|
+
next = next._withLegendConfig(kind, config);
|
|
318
|
+
}
|
|
319
|
+
return next;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function removeNamespacedGraphics(program, namespace, ids) {
|
|
323
|
+
let next = program;
|
|
324
|
+
for (const id of ids) {
|
|
325
|
+
const target = namespaceGraphicId(namespace, id);
|
|
326
|
+
if (next.graphicSpec.objects[target] !== undefined) {
|
|
327
|
+
next = next.editGraphics({ target, remove: true });
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return next;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function gradientAnchor(prepared, plot, layout) {
|
|
334
|
+
const strips = prepared.source.graphicSpec.objects.colorGradientStrips;
|
|
335
|
+
if (strips?.items?.[0] === undefined) return undefined;
|
|
336
|
+
const properties = strips.items[0].properties;
|
|
337
|
+
const length = prepared.source.guideConfigs.legend.gradient.gradient.length;
|
|
338
|
+
return {
|
|
339
|
+
sourceX: properties.x,
|
|
340
|
+
sourceY: properties.y,
|
|
341
|
+
targetX: layout.legend.x,
|
|
342
|
+
targetY: plot.y + (plot.height - length) / 2
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function legendTranslation(prepared, plot, layout) {
|
|
347
|
+
const gradient = gradientAnchor(prepared, plot, layout);
|
|
348
|
+
if (gradient !== undefined) {
|
|
349
|
+
return {
|
|
350
|
+
x: gradient.targetX - gradient.sourceX,
|
|
351
|
+
y: gradient.targetY - gradient.sourceY
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
const height = prepared.bounds.bottom - prepared.bounds.top;
|
|
355
|
+
return {
|
|
356
|
+
x: layout.legend.x - prepared.bounds.left,
|
|
357
|
+
y: plot.y + Math.max(0, (plot.height - height) / 2) - prepared.bounds.top
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function attachParentLegend(program, prepared, plot, layout) {
|
|
362
|
+
const translation = legendTranslation(prepared, plot, layout);
|
|
363
|
+
const canvas = prepared.source.graphicSpec.objects.canvas;
|
|
364
|
+
const id = `${program.compositionSpec.id}-shared-legend`;
|
|
365
|
+
let next = copyLegendConfig(program, prepared.source)
|
|
366
|
+
.createGraphics({ id, type: "canvas", parent: "canvas" });
|
|
367
|
+
for (const [property, value] of Object.entries({
|
|
368
|
+
x: translation.x,
|
|
369
|
+
y: translation.y,
|
|
370
|
+
width: canvas.properties.width,
|
|
371
|
+
height: canvas.properties.height,
|
|
372
|
+
background: "transparent"
|
|
373
|
+
})) {
|
|
374
|
+
next = next.editGraphics({ target: id, property, value });
|
|
375
|
+
}
|
|
376
|
+
for (const root of prepared.roots) {
|
|
377
|
+
next = attachSnapshotObject(next, prepared.source.graphicSpec, root, id);
|
|
378
|
+
}
|
|
379
|
+
return next;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function applyFacetGuideComposition(program, { layout, plot } = {}) {
|
|
383
|
+
if (!isPlainObject(layout) || !Array.isArray(layout.children)) {
|
|
384
|
+
throw new TypeError("Facet guide composition requires a resolved layout.");
|
|
385
|
+
}
|
|
386
|
+
if (!isPlainObject(plot)) {
|
|
387
|
+
throw new TypeError("Facet guide composition requires resolved plot bounds.");
|
|
388
|
+
}
|
|
389
|
+
const prepared = prepareSharedFacetLegend(program);
|
|
390
|
+
const children = layout.children.map(cell => {
|
|
391
|
+
const child = program.children[cell.id];
|
|
392
|
+
return {
|
|
393
|
+
id: cell.id,
|
|
394
|
+
axes: Object.keys(child.guideConfigs.axis ?? {})
|
|
395
|
+
.filter(channel => ["x", "y"].includes(channel)),
|
|
396
|
+
legendKinds: legendKinds(child)
|
|
397
|
+
};
|
|
398
|
+
});
|
|
399
|
+
const ownership = planFacetGuideOwnership({
|
|
400
|
+
placements: layout.children,
|
|
401
|
+
children,
|
|
402
|
+
axes: program.compositionSpec.facet.guides.axes,
|
|
403
|
+
legend: program.compositionSpec.facet.guides.legend,
|
|
404
|
+
...(prepared === undefined ? {} : { sharedLegends: prepared.compatibility })
|
|
405
|
+
});
|
|
406
|
+
let next = program;
|
|
407
|
+
for (const cell of layout.children) {
|
|
408
|
+
const namespace = `${program.compositionSpec.id}-${cell.id}`;
|
|
409
|
+
const childPlan = ownership.children[cell.id];
|
|
410
|
+
for (const channel of childPlan.removeAxes) {
|
|
411
|
+
next = removeNamespacedGraphics(next, namespace, axisGraphicIds(channel));
|
|
412
|
+
}
|
|
413
|
+
if (childPlan.removeLegends.length > 0) {
|
|
414
|
+
next = removeNamespacedGraphics(
|
|
415
|
+
next,
|
|
416
|
+
namespace,
|
|
417
|
+
allLegendGraphicIds(childPlan.removeLegends)
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (prepared === undefined) return next;
|
|
422
|
+
if (prepared.mode === "legacyCategorical") {
|
|
423
|
+
return materializeLegacyCategoricalLegend(next, prepared, layout);
|
|
424
|
+
}
|
|
425
|
+
return attachParentLegend(next, prepared, plot, layout);
|
|
426
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { resolveGraphicBounds } from "../layout/canvas.js";
|
|
2
|
+
import { resolvePlacedPlotBounds } from "../layout/composition.js";
|
|
3
|
+
import {
|
|
4
|
+
alignedTextAnchor,
|
|
5
|
+
buildTitleReadingBlock
|
|
6
|
+
} from "../layout/title.js";
|
|
7
|
+
import { resolveFacetLayout } from "../layout/facets.js";
|
|
8
|
+
import { namespaceGraphicSnapshot } from "./compositionSnapshot.js";
|
|
9
|
+
import { prepareSharedFacetLegend } from "./facetGuides.js";
|
|
10
|
+
import {
|
|
11
|
+
attachSnapshotObject,
|
|
12
|
+
clearCompositionChildren,
|
|
13
|
+
compositionChildDescriptor
|
|
14
|
+
} from "./composition.js";
|
|
15
|
+
import { DEFAULT_COLORS, DEFAULT_FONT_FAMILY } from "../theme/defaults.js";
|
|
16
|
+
|
|
17
|
+
const ZERO_MARGIN = Object.freeze({ top: 0, right: 0, bottom: 0, left: 0 });
|
|
18
|
+
|
|
19
|
+
function facetConfig(program) {
|
|
20
|
+
const config = program.materializationConfigs.facets?.[program.compositionSpec.id];
|
|
21
|
+
if (config === undefined) {
|
|
22
|
+
throw new Error(`Facet "${program.compositionSpec.id}" requires materialization config.`);
|
|
23
|
+
}
|
|
24
|
+
return config;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function titleLayout(program) {
|
|
28
|
+
if (program.semanticSpec.title.text === undefined) {
|
|
29
|
+
return { height: 0 };
|
|
30
|
+
}
|
|
31
|
+
const config = program.titleConfig;
|
|
32
|
+
if (config === undefined) {
|
|
33
|
+
throw new Error("Facet title requires chart title configuration.");
|
|
34
|
+
}
|
|
35
|
+
if (config.position !== "top") {
|
|
36
|
+
throw new Error("Facet parent title currently supports only top position.");
|
|
37
|
+
}
|
|
38
|
+
const block = buildTitleReadingBlock({
|
|
39
|
+
text: program.semanticSpec.title.text,
|
|
40
|
+
subtitle: program.semanticSpec.title.subtitle
|
|
41
|
+
}, config);
|
|
42
|
+
const top = 8 + config.offset;
|
|
43
|
+
if (top < 0) {
|
|
44
|
+
throw new Error("Facet parent title offset places text outside the Canvas.");
|
|
45
|
+
}
|
|
46
|
+
return { config, block, top, height: Math.ceil(top + block.height) };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function textItem(text, x, y, options = {}) {
|
|
50
|
+
return {
|
|
51
|
+
type: "text",
|
|
52
|
+
properties: {
|
|
53
|
+
x,
|
|
54
|
+
y,
|
|
55
|
+
text: String(text),
|
|
56
|
+
fill: options.color ?? DEFAULT_COLORS.strongText,
|
|
57
|
+
fontSize: options.fontSize ?? 12,
|
|
58
|
+
fontFamily: options.fontFamily ?? DEFAULT_FONT_FAMILY,
|
|
59
|
+
fontWeight: options.fontWeight ?? "normal",
|
|
60
|
+
textAlign: options.textAlign ?? "left",
|
|
61
|
+
textBaseline: options.textBaseline ?? "middle"
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function collection(program, id, items) {
|
|
67
|
+
return program
|
|
68
|
+
.createGraphics({ id, type: "collection", parent: "canvas" })
|
|
69
|
+
.editGraphics({ target: id, property: "items", value: items });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function materializeHeaders(program, layout, plots, config) {
|
|
73
|
+
const plotById = new Map(plots.map(plot => [plot.id, plot]));
|
|
74
|
+
const items = layout.children.map(cell => {
|
|
75
|
+
const plot = plotById.get(cell.id);
|
|
76
|
+
if (plot === undefined) {
|
|
77
|
+
throw new Error(`Facet header requires plot bounds for "${cell.id}".`);
|
|
78
|
+
}
|
|
79
|
+
return textItem(
|
|
80
|
+
cell.value,
|
|
81
|
+
cell.x + plot.x + plot.width / 2,
|
|
82
|
+
cell.y + config.offset,
|
|
83
|
+
{
|
|
84
|
+
color: config.color,
|
|
85
|
+
fontSize: config.fontSize,
|
|
86
|
+
fontFamily: config.fontFamily,
|
|
87
|
+
fontWeight: config.fontWeight,
|
|
88
|
+
textAlign: "center",
|
|
89
|
+
textBaseline: "top"
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
return collection(program, `${program.compositionSpec.id}-headers`, items);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function materializeTitleComponent(program, id, lines, centers, style, plot, top) {
|
|
97
|
+
if (lines.length === 0) return program;
|
|
98
|
+
const x = alignedTextAnchor(plot.x, plot.width, program.titleConfig.align);
|
|
99
|
+
const count = lines.length;
|
|
100
|
+
let next = program.createGraphics({
|
|
101
|
+
id,
|
|
102
|
+
type: "text",
|
|
103
|
+
...(count > 1 ? { length: count } : {}),
|
|
104
|
+
parent: "canvas"
|
|
105
|
+
});
|
|
106
|
+
for (const [property, value] of Object.entries({
|
|
107
|
+
x,
|
|
108
|
+
y: count === 1 ? top + centers[0] : centers.map(center => top + center),
|
|
109
|
+
text: count === 1 ? lines[0] : lines,
|
|
110
|
+
fill: style.color,
|
|
111
|
+
fontSize: style.fontSize,
|
|
112
|
+
fontFamily: style.fontFamily,
|
|
113
|
+
fontWeight: style.fontWeight,
|
|
114
|
+
textAlign: program.titleConfig.align,
|
|
115
|
+
textBaseline: "middle"
|
|
116
|
+
})) {
|
|
117
|
+
next = next.editGraphics({ target: id, property, value });
|
|
118
|
+
}
|
|
119
|
+
return next;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function materializeTitle(program, plot, title) {
|
|
123
|
+
let next = materializeTitleComponent(
|
|
124
|
+
program,
|
|
125
|
+
"chartTitle",
|
|
126
|
+
title.block.titleLines,
|
|
127
|
+
title.block.titleCenters,
|
|
128
|
+
title.config.titleStyle,
|
|
129
|
+
plot,
|
|
130
|
+
title.top
|
|
131
|
+
);
|
|
132
|
+
if (title.block.subtitleLines.length > 0) {
|
|
133
|
+
next = materializeTitleComponent(
|
|
134
|
+
next,
|
|
135
|
+
"chartSubtitle",
|
|
136
|
+
title.block.subtitleLines,
|
|
137
|
+
title.block.subtitleCenters,
|
|
138
|
+
title.config.subtitleStyle,
|
|
139
|
+
plot,
|
|
140
|
+
title.top
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
return next;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function resolveFacetProgramLayout(program, preparedLegend) {
|
|
147
|
+
program._assertCompositionProgram("resolveFacetProgramLayout");
|
|
148
|
+
if (program.compositionSpec.type !== "facet") {
|
|
149
|
+
throw new Error("resolveFacetProgramLayout requires a facet composition.");
|
|
150
|
+
}
|
|
151
|
+
const title = titleLayout(program);
|
|
152
|
+
const spec = program.compositionSpec;
|
|
153
|
+
const layout = resolveFacetLayout({
|
|
154
|
+
children: spec.children.map((id, index) => ({
|
|
155
|
+
...compositionChildDescriptor(id, program.children[id]),
|
|
156
|
+
value: spec.facet.values[index]
|
|
157
|
+
})),
|
|
158
|
+
columns: spec.columns,
|
|
159
|
+
gap: spec.gap,
|
|
160
|
+
align: spec.align,
|
|
161
|
+
padding: spec.padding,
|
|
162
|
+
titleHeight: title.height,
|
|
163
|
+
sharedLegend: spec.facet.guides.legend === "shared",
|
|
164
|
+
...(preparedLegend === undefined ? {} : {
|
|
165
|
+
sharedLegendGap: preparedLegend.reservation.gap,
|
|
166
|
+
sharedLegendWidth: preparedLegend.reservation.width
|
|
167
|
+
})
|
|
168
|
+
});
|
|
169
|
+
const plots = spec.children.map(id => ({
|
|
170
|
+
id,
|
|
171
|
+
...resolveGraphicBounds(program.children[id])
|
|
172
|
+
}));
|
|
173
|
+
const plot = resolvePlacedPlotBounds({
|
|
174
|
+
placements: layout.children,
|
|
175
|
+
plots
|
|
176
|
+
});
|
|
177
|
+
return { layout, title, plot, plots };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function materializeFacetGraphics(program) {
|
|
181
|
+
const preparedLegend = prepareSharedFacetLegend(program);
|
|
182
|
+
const { layout, title, plot, plots } = resolveFacetProgramLayout(
|
|
183
|
+
program,
|
|
184
|
+
preparedLegend
|
|
185
|
+
);
|
|
186
|
+
const config = facetConfig(program);
|
|
187
|
+
let next = clearCompositionChildren(program);
|
|
188
|
+
if (next.graphicSpec.objects.canvas === undefined) {
|
|
189
|
+
next = next.createGraphics({ id: "canvas", type: "canvas" });
|
|
190
|
+
}
|
|
191
|
+
for (const [property, value] of Object.entries({
|
|
192
|
+
width: layout.width,
|
|
193
|
+
height: layout.height,
|
|
194
|
+
background: "white"
|
|
195
|
+
})) {
|
|
196
|
+
next = next.editGraphics({ target: "canvas", property, value });
|
|
197
|
+
}
|
|
198
|
+
for (const placement of layout.children) {
|
|
199
|
+
const child = program.children[placement.id];
|
|
200
|
+
const snapshot = namespaceGraphicSnapshot(child.graphicSpec, {
|
|
201
|
+
namespace: `${program.compositionSpec.id}-${placement.id}`,
|
|
202
|
+
x: placement.x,
|
|
203
|
+
y: placement.y
|
|
204
|
+
});
|
|
205
|
+
next = attachSnapshotObject(next, snapshot, snapshot.order[0], "canvas");
|
|
206
|
+
}
|
|
207
|
+
next = materializeHeaders(next, layout, plots, config.headers);
|
|
208
|
+
next = next.composeFacetGuides({ layout, plot });
|
|
209
|
+
if (title.height > 0) next = materializeTitle(next, plot, title);
|
|
210
|
+
return next._withCanvasConfig({
|
|
211
|
+
margin: ZERO_MARGIN,
|
|
212
|
+
size: { width: "auto", height: "auto" }
|
|
213
|
+
});
|
|
214
|
+
}
|
|
@@ -7,6 +7,16 @@ import { findDataset } from "../selectors/datasets.js";
|
|
|
7
7
|
export const CANVAS_GRAPHIC_ID = "canvas";
|
|
8
8
|
export const PLOT_GRAPHIC_ID = "plot-main";
|
|
9
9
|
|
|
10
|
+
export function findCanvasGraphic(program) {
|
|
11
|
+
const graphic = program.graphicSpec.objects[CANVAS_GRAPHIC_ID];
|
|
12
|
+
return graphic?.type === "canvas" ? graphic : undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function findPlotGraphic(program) {
|
|
16
|
+
const graphic = program.graphicSpec.objects[PLOT_GRAPHIC_ID];
|
|
17
|
+
return graphic?.type === "collection" ? graphic : undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
10
20
|
export function assertCanvasHierarchyAvailable(program) {
|
|
11
21
|
const canvasByType = Object.entries(program.graphicSpec.objects).find(
|
|
12
22
|
([, graphic]) => graphic.type === "canvas"
|
|
@@ -22,8 +32,8 @@ export function assertCanvasHierarchyAvailable(program) {
|
|
|
22
32
|
}
|
|
23
33
|
|
|
24
34
|
export function resolvePlotGraphicPlacement(program, relative = {}) {
|
|
25
|
-
const canvas = program
|
|
26
|
-
const plot = program
|
|
35
|
+
const canvas = findCanvasGraphic(program);
|
|
36
|
+
const plot = findPlotGraphic(program);
|
|
27
37
|
|
|
28
38
|
if (canvas === undefined && plot === undefined) return relative;
|
|
29
39
|
if (canvas?.type !== "canvas") {
|
|
@@ -74,15 +84,15 @@ export function resolveMarkGraphicPlacement(program, { data, markType }) {
|
|
|
74
84
|
}
|
|
75
85
|
|
|
76
86
|
export function resolveCanvasGraphicPlacement(program, relative = {}) {
|
|
77
|
-
const canvas = program
|
|
78
|
-
if (canvas
|
|
87
|
+
const canvas = findCanvasGraphic(program);
|
|
88
|
+
if (canvas === undefined) {
|
|
79
89
|
throw new Error("Canvas-owned graphics require the canonical canvas.");
|
|
80
90
|
}
|
|
81
91
|
return { parent: CANVAS_GRAPHIC_ID, ...relative };
|
|
82
92
|
}
|
|
83
93
|
|
|
84
94
|
export function resolveLegendGraphicPlacement(program, relative = {}) {
|
|
85
|
-
const title = program
|
|
95
|
+
const title = findCanvasGraphic(program)?.children?.find(
|
|
86
96
|
id => id === "chartTitle" || id === "chartSubtitle"
|
|
87
97
|
);
|
|
88
98
|
const hasRelativeAnchor = relative.before !== undefined ||
|