@antv/g2 5.1.23 → 5.2.1
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/README.md +6 -5
- package/README.zh-CN.md +2 -1
- package/dist/g2.lite.min.js +1 -1
- package/dist/g2.min.js +1 -1
- package/esm/api/runtime.d.ts +1 -0
- package/esm/api/runtime.js +2 -1
- package/esm/api/runtime.js.map +1 -1
- package/esm/component/slider.js +2 -1
- package/esm/component/slider.js.map +1 -1
- package/esm/data/utils/d3-cloud/index.js +4 -2
- package/esm/data/utils/d3-cloud/index.js.map +1 -1
- package/esm/data/wordCloud.d.ts +7 -0
- package/esm/data/wordCloud.js +5 -2
- package/esm/data/wordCloud.js.map +1 -1
- package/esm/interaction/tooltip.d.ts +2 -2
- package/esm/interaction/tooltip.js +47 -28
- package/esm/interaction/tooltip.js.map +1 -1
- package/esm/runtime/coordinate.js +5 -1
- package/esm/runtime/coordinate.js.map +1 -1
- package/esm/runtime/mark.d.ts +1 -1
- package/esm/runtime/mark.js +1 -2
- package/esm/runtime/mark.js.map +1 -1
- package/esm/runtime/plot.d.ts +2 -2
- package/esm/runtime/plot.js +35 -28
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/render.js +2 -2
- package/esm/runtime/render.js.map +1 -1
- package/esm/runtime/transform.d.ts +12 -13
- package/esm/runtime/transform.js +1 -1
- package/esm/runtime/transform.js.map +1 -1
- package/esm/runtime/types/data.d.ts +2 -1
- package/esm/runtime/types/transform.d.ts +2 -5
- package/esm/spec/interaction.d.ts +2 -0
- package/esm/transform/diffY.js +1 -1
- package/esm/transform/diffY.js.map +1 -1
- package/esm/transform/dodgeX.js +14 -9
- package/esm/transform/dodgeX.js.map +1 -1
- package/esm/transform/stackY.js +2 -1
- package/esm/transform/stackY.js.map +1 -1
- package/esm/transform/utils/order.d.ts +1 -1
- package/esm/transform/utils/order.js +1 -1
- package/esm/transform/utils/order.js.map +1 -1
- package/lib/api/runtime.d.ts +1 -0
- package/lib/api/runtime.js +2 -1
- package/lib/api/runtime.js.map +1 -1
- package/lib/component/slider.js +2 -1
- package/lib/component/slider.js.map +1 -1
- package/lib/data/utils/d3-cloud/index.js +4 -2
- package/lib/data/utils/d3-cloud/index.js.map +1 -1
- package/lib/data/wordCloud.d.ts +7 -0
- package/lib/data/wordCloud.js +5 -2
- package/lib/data/wordCloud.js.map +1 -1
- package/lib/interaction/tooltip.d.ts +2 -2
- package/lib/interaction/tooltip.js +47 -28
- package/lib/interaction/tooltip.js.map +1 -1
- package/lib/runtime/coordinate.js +5 -1
- package/lib/runtime/coordinate.js.map +1 -1
- package/lib/runtime/mark.d.ts +1 -1
- package/lib/runtime/mark.js +1 -2
- package/lib/runtime/mark.js.map +1 -1
- package/lib/runtime/plot.d.ts +2 -2
- package/lib/runtime/plot.js +35 -28
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/render.js +2 -2
- package/lib/runtime/render.js.map +1 -1
- package/lib/runtime/transform.d.ts +12 -13
- package/lib/runtime/transform.js +1 -1
- package/lib/runtime/transform.js.map +1 -1
- package/lib/runtime/types/data.d.ts +2 -1
- package/lib/runtime/types/transform.d.ts +2 -5
- package/lib/spec/interaction.d.ts +2 -0
- package/lib/transform/diffY.js +1 -1
- package/lib/transform/diffY.js.map +1 -1
- package/lib/transform/dodgeX.js +14 -9
- package/lib/transform/dodgeX.js.map +1 -1
- package/lib/transform/stackY.js +2 -1
- package/lib/transform/stackY.js.map +1 -1
- package/lib/transform/utils/order.d.ts +1 -1
- package/lib/transform/utils/order.js +1 -1
- package/lib/transform/utils/order.js.map +1 -1
- package/package.json +2 -2
- package/src/api/runtime.ts +4 -1
- package/src/component/slider.ts +2 -1
- package/src/data/utils/d3-cloud/index.ts +6 -2
- package/src/data/wordCloud.ts +14 -2
- package/src/interaction/tooltip.ts +50 -24
- package/src/runtime/coordinate.ts +6 -1
- package/src/runtime/mark.ts +4 -9
- package/src/runtime/plot.ts +36 -39
- package/src/runtime/render.ts +3 -4
- package/src/runtime/transform.ts +13 -14
- package/src/runtime/types/data.ts +3 -1
- package/src/runtime/types/transform.ts +2 -6
- package/src/spec/interaction.ts +2 -0
- package/src/transform/diffY.ts +2 -2
- package/src/transform/dodgeX.ts +16 -10
- package/src/transform/stackY.ts +1 -1
- package/src/transform/utils/order.ts +4 -2
package/src/api/runtime.ts
CHANGED
|
@@ -32,6 +32,7 @@ export type RuntimeOptions = ViewComposition & {
|
|
|
32
32
|
plugins?: RendererPlugin[];
|
|
33
33
|
theme?: string;
|
|
34
34
|
lib?: G2Library;
|
|
35
|
+
createCanvas?: () => HTMLCanvasElement;
|
|
35
36
|
};
|
|
36
37
|
|
|
37
38
|
export class Runtime<Spec extends G2Spec = G2Spec> extends CompositionNode {
|
|
@@ -53,7 +54,8 @@ export class Runtime<Spec extends G2Spec = G2Spec> extends CompositionNode {
|
|
|
53
54
|
private _compositions: Record<string, new () => Node>;
|
|
54
55
|
|
|
55
56
|
constructor(options: RuntimeOptions) {
|
|
56
|
-
const { container, canvas, renderer, plugins, lib, ...rest } =
|
|
57
|
+
const { container, canvas, renderer, plugins, lib, createCanvas, ...rest } =
|
|
58
|
+
options;
|
|
57
59
|
super(rest, 'view');
|
|
58
60
|
this._renderer = renderer || new CanvasRenderer();
|
|
59
61
|
this._plugins = plugins || [];
|
|
@@ -63,6 +65,7 @@ export class Runtime<Spec extends G2Spec = G2Spec> extends CompositionNode {
|
|
|
63
65
|
library: { ...lib, ...library },
|
|
64
66
|
emitter: this._emitter,
|
|
65
67
|
canvas,
|
|
68
|
+
createCanvas,
|
|
66
69
|
};
|
|
67
70
|
this._create();
|
|
68
71
|
}
|
package/src/component/slider.ts
CHANGED
|
@@ -81,9 +81,10 @@ export const Slider: GCC<SliderOptions> = (options) => {
|
|
|
81
81
|
function markValue(markState, channels: string[]) {
|
|
82
82
|
const [value] = Array.from(markState.entries())
|
|
83
83
|
.filter(([mark]) => mark.type === 'line' || mark.type === 'area')
|
|
84
|
+
.filter(([mark]) => mark.slider)
|
|
84
85
|
.map(([mark]) => {
|
|
85
86
|
const { encode, slider } = mark;
|
|
86
|
-
if (slider?.x
|
|
87
|
+
if (slider?.x) {
|
|
87
88
|
const channel = (name) => {
|
|
88
89
|
const channel = encode[name];
|
|
89
90
|
return [name, channel ? channel.value : undefined];
|
|
@@ -256,10 +256,10 @@ export function tagCloud() {
|
|
|
256
256
|
event = cloudDispatch,
|
|
257
257
|
words = [],
|
|
258
258
|
timer = null,
|
|
259
|
-
timeInterval = Infinity
|
|
259
|
+
timeInterval = Infinity,
|
|
260
|
+
canvas = cloudCanvas;
|
|
260
261
|
|
|
261
262
|
const fontStyle = cloudFontNormal;
|
|
262
|
-
const canvas = cloudCanvas;
|
|
263
263
|
const cloud: any = {};
|
|
264
264
|
|
|
265
265
|
cloud.start = function () {
|
|
@@ -476,6 +476,10 @@ export function tagCloud() {
|
|
|
476
476
|
rotate = functor(_);
|
|
477
477
|
};
|
|
478
478
|
|
|
479
|
+
cloud.canvas = function (_) {
|
|
480
|
+
canvas = functor(_);
|
|
481
|
+
};
|
|
482
|
+
|
|
479
483
|
cloud.spiral = function (_) {
|
|
480
484
|
spiral = spirals[_] || _;
|
|
481
485
|
};
|
package/src/data/wordCloud.ts
CHANGED
|
@@ -90,7 +90,16 @@ export type WordCloudOptions = {
|
|
|
90
90
|
* If not specified, returns the current random number generator, which defaults to Math.random.
|
|
91
91
|
*/
|
|
92
92
|
random: () => number;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* If specified, sets the spiral used for positioning words.
|
|
96
|
+
*/
|
|
93
97
|
spiral: any;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* If specified, sets the image mask used for positioning words.
|
|
101
|
+
*/
|
|
102
|
+
canvas: HTMLCanvasElement;
|
|
94
103
|
};
|
|
95
104
|
|
|
96
105
|
const DEFAULT_OPTIONS = {
|
|
@@ -151,9 +160,11 @@ export function normalizeFontSize(fontSize: any, range?: [number, number]) {
|
|
|
151
160
|
return () => fontSize;
|
|
152
161
|
}
|
|
153
162
|
|
|
154
|
-
export const WordCloud: DC<Partial<WordCloudOptions>> = (options) => {
|
|
163
|
+
export const WordCloud: DC<Partial<WordCloudOptions>> = (options, context) => {
|
|
155
164
|
return async (data) => {
|
|
156
|
-
const cloudOptions = Object.assign({}, DEFAULT_OPTIONS, options
|
|
165
|
+
const cloudOptions = Object.assign({}, DEFAULT_OPTIONS, options, {
|
|
166
|
+
canvas: context.createCanvas,
|
|
167
|
+
});
|
|
157
168
|
const layout = tagCloud();
|
|
158
169
|
|
|
159
170
|
await flow(layout, cloudOptions)
|
|
@@ -172,6 +183,7 @@ export const WordCloud: DC<Partial<WordCloudOptions>> = (options) => {
|
|
|
172
183
|
.set('random')
|
|
173
184
|
.set('text')
|
|
174
185
|
.set('on')
|
|
186
|
+
.set('canvas')
|
|
175
187
|
.setAsync('imageMask', processImageMask, layout.createMask);
|
|
176
188
|
|
|
177
189
|
layout.words([...data]);
|
|
@@ -69,6 +69,7 @@ function createTooltip(
|
|
|
69
69
|
bounding,
|
|
70
70
|
containerOffset,
|
|
71
71
|
css = {},
|
|
72
|
+
offset: [number, number] = [10, 10],
|
|
72
73
|
) {
|
|
73
74
|
const defaults = {
|
|
74
75
|
'.g2-tooltip': {},
|
|
@@ -89,7 +90,7 @@ function createTooltip(
|
|
|
89
90
|
position,
|
|
90
91
|
enterable,
|
|
91
92
|
title: '',
|
|
92
|
-
offset
|
|
93
|
+
offset,
|
|
93
94
|
template: {
|
|
94
95
|
prefixCls: 'g2-',
|
|
95
96
|
},
|
|
@@ -113,6 +114,7 @@ function showTooltip({
|
|
|
113
114
|
css,
|
|
114
115
|
mount,
|
|
115
116
|
bounding,
|
|
117
|
+
offset,
|
|
116
118
|
}) {
|
|
117
119
|
const container = getContainer(root, mount);
|
|
118
120
|
const canvasContainer = getContainer(root);
|
|
@@ -130,6 +132,7 @@ function showTooltip({
|
|
|
130
132
|
b,
|
|
131
133
|
containerOffset,
|
|
132
134
|
css,
|
|
135
|
+
offset,
|
|
133
136
|
),
|
|
134
137
|
} = parent as any;
|
|
135
138
|
const { items, title = '' } = data;
|
|
@@ -572,6 +575,7 @@ export function seriesTooltip(
|
|
|
572
575
|
mount,
|
|
573
576
|
bounding,
|
|
574
577
|
theme,
|
|
578
|
+
offset,
|
|
575
579
|
disableNative = false,
|
|
576
580
|
marker = true,
|
|
577
581
|
preserve = false,
|
|
@@ -798,6 +802,7 @@ export function seriesTooltip(
|
|
|
798
802
|
mount,
|
|
799
803
|
bounding,
|
|
800
804
|
css,
|
|
805
|
+
offset,
|
|
801
806
|
});
|
|
802
807
|
}
|
|
803
808
|
|
|
@@ -881,17 +886,21 @@ export function seriesTooltip(
|
|
|
881
886
|
destroyTooltip({ root, single });
|
|
882
887
|
};
|
|
883
888
|
|
|
884
|
-
const onTooltipShow = ({ nativeEvent, data }) => {
|
|
889
|
+
const onTooltipShow = ({ nativeEvent, data, offsetX, offsetY, ...rest }) => {
|
|
885
890
|
if (nativeEvent) return;
|
|
886
|
-
const
|
|
887
|
-
const
|
|
891
|
+
const x = data?.data?.x;
|
|
892
|
+
const scaleX = scale.x;
|
|
888
893
|
const x1 = scaleX.map(x);
|
|
889
894
|
const [x2, y2] = coordinate.map([x1, 0.5]);
|
|
890
|
-
const
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
+
const rootBounds = root.getRenderBounds();
|
|
896
|
+
const minX = rootBounds.min[0];
|
|
897
|
+
const minY = rootBounds.min[1];
|
|
898
|
+
update({
|
|
899
|
+
...rest,
|
|
900
|
+
offsetX: offsetX !== undefined ? offsetX : minX + x2,
|
|
901
|
+
offsetY: offsetY !== undefined ? offsetY : minY + y2,
|
|
902
|
+
_x: x, // a hint, to lookup for the right element if multiple element in the same abstractX.
|
|
903
|
+
});
|
|
895
904
|
};
|
|
896
905
|
|
|
897
906
|
const onTooltipHide = () => {
|
|
@@ -974,6 +983,7 @@ export function tooltip(
|
|
|
974
983
|
mount,
|
|
975
984
|
bounding,
|
|
976
985
|
theme,
|
|
986
|
+
offset,
|
|
977
987
|
shared = false,
|
|
978
988
|
body = true,
|
|
979
989
|
disableNative = false,
|
|
@@ -985,30 +995,43 @@ export function tooltip(
|
|
|
985
995
|
const keyGroup = group(elements, groupKey);
|
|
986
996
|
const inInterval = (d) => d.markType === 'interval';
|
|
987
997
|
const isBar = elements.every(inInterval) && !isPolar(coordinate);
|
|
988
|
-
const xof = (d) => d.__data__.x;
|
|
989
998
|
const scaleX = scale.x;
|
|
999
|
+
const scaleSeries = scale.series;
|
|
1000
|
+
const bandWidth = scaleX?.getBandWidth?.() ?? 0;
|
|
1001
|
+
const xof = scaleSeries
|
|
1002
|
+
? (d) => d.__data__.x + d.__data__.series * bandWidth
|
|
1003
|
+
: (d) => d.__data__.x + bandWidth / 2;
|
|
990
1004
|
|
|
991
1005
|
// Sort for bisector search.
|
|
992
1006
|
if (isBar) elements.sort((a, b) => xof(a) - xof(b));
|
|
993
1007
|
|
|
1008
|
+
const findElementByTarget = (event) => {
|
|
1009
|
+
const { target } = event;
|
|
1010
|
+
return maybeRoot(target, (node) => {
|
|
1011
|
+
if (!node.classList) return false;
|
|
1012
|
+
return node.classList.includes('element');
|
|
1013
|
+
});
|
|
1014
|
+
};
|
|
1015
|
+
|
|
994
1016
|
const findElement = isBar
|
|
995
1017
|
? (event) => {
|
|
996
1018
|
const mouse = mousePosition(root, event);
|
|
997
1019
|
if (!mouse) return;
|
|
998
|
-
const offsetX = scaleX?.getBandWidth ? scaleX.getBandWidth() / 2 : 0;
|
|
999
1020
|
const [normalizedX] = coordinate.invert(mouse);
|
|
1000
|
-
const abstractX = normalizedX
|
|
1021
|
+
const abstractX = normalizedX;
|
|
1001
1022
|
const search = bisector(xof).center;
|
|
1002
1023
|
const i = search(elements, abstractX);
|
|
1003
|
-
|
|
1024
|
+
const target = elements[i];
|
|
1025
|
+
if (!shared) {
|
|
1026
|
+
// For grouped bar chart without shared options.
|
|
1027
|
+
const isGrouped = elements.find(
|
|
1028
|
+
(d) => d !== target && xof(d) === xof(target),
|
|
1029
|
+
);
|
|
1030
|
+
if (isGrouped) return findElementByTarget(event);
|
|
1031
|
+
}
|
|
1032
|
+
return target;
|
|
1004
1033
|
}
|
|
1005
|
-
:
|
|
1006
|
-
const { target } = event;
|
|
1007
|
-
return maybeRoot(target, (node) => {
|
|
1008
|
-
if (!node.classList) return false;
|
|
1009
|
-
return node.classList.includes('element');
|
|
1010
|
-
});
|
|
1011
|
-
};
|
|
1034
|
+
: findElementByTarget;
|
|
1012
1035
|
|
|
1013
1036
|
const pointermove = throttle(
|
|
1014
1037
|
(event) => {
|
|
@@ -1055,6 +1078,7 @@ export function tooltip(
|
|
|
1055
1078
|
mount,
|
|
1056
1079
|
bounding,
|
|
1057
1080
|
css,
|
|
1081
|
+
offset,
|
|
1058
1082
|
});
|
|
1059
1083
|
}
|
|
1060
1084
|
|
|
@@ -1090,16 +1114,18 @@ export function tooltip(
|
|
|
1090
1114
|
}
|
|
1091
1115
|
};
|
|
1092
1116
|
|
|
1093
|
-
const onTooltipShow = ({ nativeEvent, data }) => {
|
|
1117
|
+
const onTooltipShow = ({ nativeEvent, offsetX, offsetY, data: raw }) => {
|
|
1094
1118
|
if (nativeEvent) return;
|
|
1095
|
-
const
|
|
1119
|
+
const { data } = raw;
|
|
1120
|
+
const element = selectElementByData(elements, data, datum);
|
|
1096
1121
|
if (!element) return;
|
|
1097
1122
|
const bbox = element.getBBox();
|
|
1098
1123
|
const { x, y, width, height } = bbox;
|
|
1124
|
+
const rootBBox = root.getBBox();
|
|
1099
1125
|
pointermove({
|
|
1100
1126
|
target: element,
|
|
1101
|
-
offsetX: x + width / 2,
|
|
1102
|
-
offsetY: y + height / 2,
|
|
1127
|
+
offsetX: offsetX !== undefined ? offsetX + rootBBox.x : x + width / 2,
|
|
1128
|
+
offsetY: offsetY !== undefined ? offsetY + rootBBox.y : y + height / 2,
|
|
1103
1129
|
});
|
|
1104
1130
|
};
|
|
1105
1131
|
|
|
@@ -46,7 +46,12 @@ export function createCoordinate(
|
|
|
46
46
|
|
|
47
47
|
export function coordinate2Transform(node: G2View, library: G2Library): G2View {
|
|
48
48
|
// @ts-ignore
|
|
49
|
-
const { coordinate = {}, ...rest } = node;
|
|
49
|
+
const { coordinate = {}, coordinates, ...rest } = node;
|
|
50
|
+
|
|
51
|
+
// If coordinates are already set, it means that the coordinate has been processed
|
|
52
|
+
// during the initialization. There is not need to process it during update.
|
|
53
|
+
if (coordinates) return node;
|
|
54
|
+
|
|
50
55
|
const { type, transform = [], ...options } = coordinate;
|
|
51
56
|
if (!type) return { ...rest, coordinates: transform };
|
|
52
57
|
const [, createCoordinate] = useLibrary<
|
package/src/runtime/mark.ts
CHANGED
|
@@ -7,15 +7,11 @@ import {
|
|
|
7
7
|
G2Mark,
|
|
8
8
|
G2TransformOptions,
|
|
9
9
|
G2EncodeOptions,
|
|
10
|
+
G2Context,
|
|
10
11
|
} from './types/options';
|
|
11
12
|
import { MarkProps } from './types/mark';
|
|
12
13
|
import { NormalizedEncodeSpec, EncodeComponent, Encode } from './types/encode';
|
|
13
|
-
import {
|
|
14
|
-
ColumnOf,
|
|
15
|
-
TransformContext,
|
|
16
|
-
Transform,
|
|
17
|
-
TransformComponent,
|
|
18
|
-
} from './types/transform';
|
|
14
|
+
import { ColumnOf, Transform, TransformComponent } from './types/transform';
|
|
19
15
|
import {
|
|
20
16
|
applyDefaults,
|
|
21
17
|
applyDataTransform,
|
|
@@ -33,10 +29,9 @@ import {
|
|
|
33
29
|
export async function initializeMark(
|
|
34
30
|
partialMark: G2Mark,
|
|
35
31
|
partialProps: MarkProps,
|
|
36
|
-
|
|
32
|
+
context: G2Library,
|
|
37
33
|
): Promise<[G2Mark, G2MarkState]> {
|
|
38
34
|
// Apply transform to mark to derive indices, data, encode, etc,.
|
|
39
|
-
const context = { library };
|
|
40
35
|
const [I, transformedMark] = await applyMarkTransform(
|
|
41
36
|
partialMark,
|
|
42
37
|
partialProps,
|
|
@@ -143,7 +138,7 @@ export function createColumnOf(library: G2Library): ColumnOf {
|
|
|
143
138
|
async function applyMarkTransform(
|
|
144
139
|
mark: G2Mark,
|
|
145
140
|
props: MarkProps,
|
|
146
|
-
context:
|
|
141
|
+
context: G2Context,
|
|
147
142
|
): Promise<[number[], G2Mark]> {
|
|
148
143
|
const { library } = context;
|
|
149
144
|
const [useTransform] = useLibrary<
|
package/src/runtime/plot.ts
CHANGED
|
@@ -96,9 +96,10 @@ type Store = Map<any, (options: G2ViewTree) => G2ViewTree>;
|
|
|
96
96
|
export async function plot<T extends G2ViewTree>(
|
|
97
97
|
options: T,
|
|
98
98
|
selection: Selection,
|
|
99
|
-
library: G2Library,
|
|
100
99
|
context: G2Context,
|
|
101
100
|
): Promise<any> {
|
|
101
|
+
const { library } = context;
|
|
102
|
+
|
|
102
103
|
const [useComposition] = useLibrary<
|
|
103
104
|
G2CompositionOptions,
|
|
104
105
|
CompositionComponent,
|
|
@@ -168,7 +169,7 @@ export async function plot<T extends G2ViewTree>(
|
|
|
168
169
|
const state = nodeState.get(node);
|
|
169
170
|
const [view, children] = state
|
|
170
171
|
? initializeState(state, node, library)
|
|
171
|
-
: await initializeView(node,
|
|
172
|
+
: await initializeView(node, context);
|
|
172
173
|
viewNode.set(view, node);
|
|
173
174
|
views.push(view);
|
|
174
175
|
|
|
@@ -183,7 +184,7 @@ export async function plot<T extends G2ViewTree>(
|
|
|
183
184
|
// should sync position scales among facets normally.
|
|
184
185
|
if (transformedNodes.every(isStandardView)) {
|
|
185
186
|
const states = await Promise.all(
|
|
186
|
-
transformedNodes.map((d) => initializeMarks(d,
|
|
187
|
+
transformedNodes.map((d) => initializeMarks(d, context)),
|
|
187
188
|
);
|
|
188
189
|
// Note!!!
|
|
189
190
|
// This will mutate scales for marks.
|
|
@@ -198,7 +199,7 @@ export async function plot<T extends G2ViewTree>(
|
|
|
198
199
|
// Apply transform to get data in advance for non-mark composition
|
|
199
200
|
// node, which makes sure that composition node can preprocess the
|
|
200
201
|
// data to produce more nodes based on it.
|
|
201
|
-
const n = isMark(node) ? node : await applyTransform(node,
|
|
202
|
+
const n = isMark(node) ? node : await applyTransform(node, context);
|
|
202
203
|
const N = transform(n);
|
|
203
204
|
if (Array.isArray(N)) discovered.push(...N);
|
|
204
205
|
else if (typeof N === 'function') nodeGenerators.push(N());
|
|
@@ -222,12 +223,12 @@ export async function plot<T extends G2ViewTree>(
|
|
|
222
223
|
.attr('id', (view) => view.key)
|
|
223
224
|
.call(applyTranslate)
|
|
224
225
|
.each(function (view, i, element) {
|
|
225
|
-
plotView(view, select(element), transitions,
|
|
226
|
+
plotView(view, select(element), transitions, context);
|
|
226
227
|
enterContainer.set(view, element);
|
|
227
228
|
}),
|
|
228
229
|
(update) =>
|
|
229
230
|
update.call(applyTranslate).each(function (view, i, element) {
|
|
230
|
-
plotView(view, select(element), transitions,
|
|
231
|
+
plotView(view, select(element), transitions, context);
|
|
231
232
|
updateContainer.set(view, element);
|
|
232
233
|
}),
|
|
233
234
|
(exit) =>
|
|
@@ -260,12 +261,7 @@ export async function plot<T extends G2ViewTree>(
|
|
|
260
261
|
oldStore || new Map<any, (options: G2ViewTree) => G2ViewTree>();
|
|
261
262
|
const setState = (key, reducer = (x) => x) => store.set(key, reducer);
|
|
262
263
|
const options = viewNode.get(view);
|
|
263
|
-
const update = createUpdateView(
|
|
264
|
-
select(container),
|
|
265
|
-
options,
|
|
266
|
-
library,
|
|
267
|
-
context,
|
|
268
|
-
);
|
|
264
|
+
const update = createUpdateView(select(container), options, context);
|
|
269
265
|
return {
|
|
270
266
|
view,
|
|
271
267
|
container,
|
|
@@ -372,7 +368,7 @@ export async function plot<T extends G2ViewTree>(
|
|
|
372
368
|
const keyframe = new Promise<void>(async (resolve) => {
|
|
373
369
|
for (const node of nodeGenerator) {
|
|
374
370
|
const sizedNode = { width, height, ...node };
|
|
375
|
-
await plot(sizedNode, selection,
|
|
371
|
+
await plot(sizedNode, selection, context);
|
|
376
372
|
}
|
|
377
373
|
resolve();
|
|
378
374
|
});
|
|
@@ -424,9 +420,9 @@ function definedInteraction(library: G2Library) {
|
|
|
424
420
|
function createUpdateView(
|
|
425
421
|
selection: Selection,
|
|
426
422
|
options: G2ViewTree,
|
|
427
|
-
library: G2Library,
|
|
428
423
|
context: G2Context,
|
|
429
424
|
): G2ViewInstance['update'] {
|
|
425
|
+
const { library } = context;
|
|
430
426
|
const createDefinedInteraction = definedInteraction(library);
|
|
431
427
|
const filter = (d) => d[1] && d[1].props && d[1].props.reapplyWhenUpdate;
|
|
432
428
|
const interactions = inferInteraction(options);
|
|
@@ -437,16 +433,16 @@ function createUpdateView(
|
|
|
437
433
|
|
|
438
434
|
return async (newOptions, source, callback) => {
|
|
439
435
|
const transitions = [];
|
|
440
|
-
const [newView, newChildren] = await initializeView(newOptions,
|
|
441
|
-
plotView(newView, selection, transitions,
|
|
436
|
+
const [newView, newChildren] = await initializeView(newOptions, context);
|
|
437
|
+
plotView(newView, selection, transitions, context);
|
|
442
438
|
|
|
443
439
|
// Update interaction need to reapply when update.
|
|
444
440
|
for (const name of updates.filter((d) => d !== source)) {
|
|
445
|
-
updateInteraction(name, selection, newOptions, newView,
|
|
441
|
+
updateInteraction(name, selection, newOptions, newView, context);
|
|
446
442
|
}
|
|
447
443
|
|
|
448
444
|
for (const child of newChildren) {
|
|
449
|
-
plot(child, selection,
|
|
445
|
+
plot(child, selection, context);
|
|
450
446
|
}
|
|
451
447
|
callback();
|
|
452
448
|
return { options: newOptions, view: newView };
|
|
@@ -458,9 +454,10 @@ function updateInteraction(
|
|
|
458
454
|
selection: Selection,
|
|
459
455
|
options: G2ViewTree,
|
|
460
456
|
view: G2ViewDescriptor,
|
|
461
|
-
library: G2Library,
|
|
462
457
|
context: G2Context,
|
|
463
458
|
) {
|
|
459
|
+
const { library } = context;
|
|
460
|
+
|
|
464
461
|
const [useInteraction] = useLibrary<
|
|
465
462
|
G2InteractionOptions,
|
|
466
463
|
InteractionComponent,
|
|
@@ -500,9 +497,11 @@ function updateInteraction(
|
|
|
500
497
|
|
|
501
498
|
async function initializeView(
|
|
502
499
|
options: G2View,
|
|
503
|
-
|
|
500
|
+
context: G2Context,
|
|
504
501
|
): Promise<[G2ViewDescriptor, G2ViewTree[]]> {
|
|
505
|
-
const
|
|
502
|
+
const { library } = context;
|
|
503
|
+
|
|
504
|
+
const flattenOptions = await transformMarks(options, context);
|
|
506
505
|
|
|
507
506
|
const mergedOptions = bubbleOptions(flattenOptions);
|
|
508
507
|
|
|
@@ -515,7 +514,7 @@ async function initializeView(
|
|
|
515
514
|
options.marks = [...mergedOptions.marks, ...mergedOptions.components];
|
|
516
515
|
|
|
517
516
|
const transformedOptions = coordinate2Transform(mergedOptions, library);
|
|
518
|
-
const state = await initializeMarks(transformedOptions,
|
|
517
|
+
const state = await initializeMarks(transformedOptions, context);
|
|
519
518
|
return initializeState(state, transformedOptions, library);
|
|
520
519
|
}
|
|
521
520
|
|
|
@@ -553,8 +552,10 @@ function bubbleOptions(options: G2View): G2View {
|
|
|
553
552
|
|
|
554
553
|
async function transformMarks(
|
|
555
554
|
options: G2View,
|
|
556
|
-
|
|
555
|
+
context: G2Context,
|
|
557
556
|
): Promise<G2View> {
|
|
557
|
+
const { library } = context;
|
|
558
|
+
|
|
558
559
|
const [useMark, createMark] = useLibrary<G2MarkOptions, MarkComponent, Mark>(
|
|
559
560
|
'mark',
|
|
560
561
|
library,
|
|
@@ -570,13 +571,13 @@ async function transformMarks(
|
|
|
570
571
|
const components = [];
|
|
571
572
|
const discovered = [...marks];
|
|
572
573
|
const { width, height } = computeRoughPlotSize(options);
|
|
573
|
-
const
|
|
574
|
+
const markOptions = { options, width, height };
|
|
574
575
|
|
|
575
576
|
// Pre order traversal.
|
|
576
577
|
while (discovered.length) {
|
|
577
578
|
const [node] = discovered.splice(0, 1);
|
|
578
579
|
// Apply data transform to get data.
|
|
579
|
-
const mark = (await applyTransform(node,
|
|
580
|
+
const mark = (await applyTransform(node, context)) as G2Mark;
|
|
580
581
|
const { type = error('G2Mark type is required.'), key } = mark;
|
|
581
582
|
|
|
582
583
|
// For components.
|
|
@@ -595,7 +596,7 @@ async function transformMarks(
|
|
|
595
596
|
};
|
|
596
597
|
|
|
597
598
|
// Convert composite mark to marks.
|
|
598
|
-
const marks = await useMark(newMark,
|
|
599
|
+
const marks = await useMark(newMark, markOptions);
|
|
599
600
|
const M = Array.isArray(marks) ? marks : [marks];
|
|
600
601
|
discovered.unshift(...M.map((d, i) => ({ ...d, key: `${key}-${i}` })));
|
|
601
602
|
}
|
|
@@ -607,8 +608,10 @@ async function transformMarks(
|
|
|
607
608
|
|
|
608
609
|
async function initializeMarks(
|
|
609
610
|
options: G2View,
|
|
610
|
-
|
|
611
|
+
context: G2Context,
|
|
611
612
|
): Promise<Map<G2Mark, G2MarkState>> {
|
|
613
|
+
const { library } = context;
|
|
614
|
+
|
|
612
615
|
const [useTheme] = useLibrary<G2ThemeOptions, ThemeComponent, Theme>(
|
|
613
616
|
'theme',
|
|
614
617
|
library,
|
|
@@ -630,7 +633,7 @@ async function initializeMarks(
|
|
|
630
633
|
for (const markOptions of partialMarks) {
|
|
631
634
|
const { type } = markOptions;
|
|
632
635
|
const { props = {} } = createMark(type);
|
|
633
|
-
const markAndState = await initializeMark(markOptions, props,
|
|
636
|
+
const markAndState = await initializeMark(markOptions, props, context);
|
|
634
637
|
if (markAndState) {
|
|
635
638
|
const [initializedMark, state] = markAndState;
|
|
636
639
|
markState.set(initializedMark, state);
|
|
@@ -862,9 +865,9 @@ async function plotView(
|
|
|
862
865
|
view: G2ViewDescriptor,
|
|
863
866
|
selection: Selection,
|
|
864
867
|
transitions: GAnimation[],
|
|
865
|
-
library: G2Library,
|
|
866
868
|
context: G2Context,
|
|
867
869
|
): Promise<void> {
|
|
870
|
+
const { library } = context;
|
|
868
871
|
const {
|
|
869
872
|
components,
|
|
870
873
|
theme,
|
|
@@ -1022,13 +1025,7 @@ async function plotView(
|
|
|
1022
1025
|
const { data } = state;
|
|
1023
1026
|
const { key, class: cls, type } = mark;
|
|
1024
1027
|
const viewNode = selection.select(`#${key}`);
|
|
1025
|
-
const shapeFunction = createMarkShapeFunction(
|
|
1026
|
-
mark,
|
|
1027
|
-
state,
|
|
1028
|
-
view,
|
|
1029
|
-
library,
|
|
1030
|
-
context,
|
|
1031
|
-
);
|
|
1028
|
+
const shapeFunction = createMarkShapeFunction(mark, state, view, context);
|
|
1032
1029
|
const enterFunction = createEnterFunction(mark, state, view, library);
|
|
1033
1030
|
const updateFunction = createUpdateFunction(mark, state, view, library);
|
|
1034
1031
|
const exitFunction = createExitFunction(mark, state, view, library);
|
|
@@ -1488,13 +1485,14 @@ function createMarkShapeFunction(
|
|
|
1488
1485
|
mark: G2Mark,
|
|
1489
1486
|
state: G2MarkState,
|
|
1490
1487
|
view: G2ViewDescriptor,
|
|
1491
|
-
library: G2Library,
|
|
1492
1488
|
context: G2Context,
|
|
1493
1489
|
): (
|
|
1494
1490
|
data: Record<string, any>,
|
|
1495
1491
|
index: number,
|
|
1496
1492
|
element?: DisplayObject,
|
|
1497
1493
|
) => DisplayObject {
|
|
1494
|
+
const { library } = context;
|
|
1495
|
+
|
|
1498
1496
|
const [useShape] = useLibrary<G2ShapeOptions, ShapeComponent, Shape>(
|
|
1499
1497
|
'shape',
|
|
1500
1498
|
library,
|
|
@@ -1692,9 +1690,8 @@ function inferInteraction(
|
|
|
1692
1690
|
|
|
1693
1691
|
async function applyTransform<T extends G2ViewTree>(
|
|
1694
1692
|
node: T,
|
|
1695
|
-
|
|
1693
|
+
context: G2Context,
|
|
1696
1694
|
): Promise<G2ViewTree> {
|
|
1697
|
-
const context = { library };
|
|
1698
1695
|
const { data, ...rest } = node;
|
|
1699
1696
|
if (data == undefined) return node;
|
|
1700
1697
|
const [, { data: newData }] = await applyDataTransform([], { data }, context);
|
package/src/runtime/render.ts
CHANGED
|
@@ -77,6 +77,7 @@ export function render<T extends G2ViewTree = G2ViewTree>(
|
|
|
77
77
|
emitter = new EventEmitter(),
|
|
78
78
|
library,
|
|
79
79
|
} = context;
|
|
80
|
+
|
|
80
81
|
context.canvas = canvas;
|
|
81
82
|
context.emitter = emitter;
|
|
82
83
|
|
|
@@ -91,9 +92,7 @@ export function render<T extends G2ViewTree = G2ViewTree>(
|
|
|
91
92
|
// Make sure that plot chart after container is ready for every time.
|
|
92
93
|
const selection = select(canvas.document.documentElement);
|
|
93
94
|
canvas.ready
|
|
94
|
-
.then(() =>
|
|
95
|
-
plot<T>({ ...keyed, width, height, depth }, selection, library, context),
|
|
96
|
-
)
|
|
95
|
+
.then(() => plot<T>({ ...keyed, width, height, depth }, selection, context))
|
|
97
96
|
.then(() => {
|
|
98
97
|
// Place the center of whole scene at z axis' origin.
|
|
99
98
|
if (depth) {
|
|
@@ -146,7 +145,7 @@ export function renderToMountedElement<T extends G2ViewTree = G2ViewTree>(
|
|
|
146
145
|
emitter.emit(ChartEvent.BEFORE_RENDER);
|
|
147
146
|
// Plot the chart and mutate context.
|
|
148
147
|
// Make sure that plot chart after container is ready for every time.
|
|
149
|
-
plot<T>({ ...keyed, width, height }, selection,
|
|
148
|
+
plot<T>({ ...keyed, width, height }, selection, context)
|
|
150
149
|
.then(() => {
|
|
151
150
|
context.canvas?.requestAnimationFrame(() => {
|
|
152
151
|
emitter.emit(ChartEvent.AFTER_RENDER);
|