@antv/g2 5.2.0 → 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/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/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/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/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/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/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/data/utils/d3-cloud/index.ts +6 -2
- package/src/data/wordCloud.ts +14 -2
- 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/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
|
}
|
|
@@ -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]);
|
|
@@ -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);
|
package/src/runtime/transform.ts
CHANGED
|
@@ -12,15 +12,14 @@ import { isFullTooltip } from '../utils/mark';
|
|
|
12
12
|
import { useLibrary } from './library';
|
|
13
13
|
import { createColumnOf } from './mark';
|
|
14
14
|
import { Data, DataComponent } from './types/data';
|
|
15
|
-
import { G2Mark, G2DataOptions } from './types/options';
|
|
16
|
-
import { TransformContext } from './types/transform';
|
|
15
|
+
import { G2Mark, G2DataOptions, G2Context } from './types/options';
|
|
17
16
|
import { isPosition } from './scale';
|
|
18
17
|
|
|
19
18
|
// @todo Add more defaults.
|
|
20
19
|
export function applyDefaults(
|
|
21
20
|
I: number[],
|
|
22
21
|
mark: G2Mark,
|
|
23
|
-
context:
|
|
22
|
+
context: G2Context,
|
|
24
23
|
): [number[], G2Mark] {
|
|
25
24
|
const { encode = {}, scale = {}, transform = [], ...rest } = mark;
|
|
26
25
|
return [I, { ...rest, encode, scale, transform }];
|
|
@@ -29,7 +28,7 @@ export function applyDefaults(
|
|
|
29
28
|
export async function applyDataTransform(
|
|
30
29
|
I: number[],
|
|
31
30
|
mark: G2Mark,
|
|
32
|
-
context:
|
|
31
|
+
context: G2Context,
|
|
33
32
|
): Promise<[number[], G2Mark]> {
|
|
34
33
|
const { library } = context;
|
|
35
34
|
const { data } = mark;
|
|
@@ -40,7 +39,7 @@ export async function applyDataTransform(
|
|
|
40
39
|
const descriptor = normalizedDataSource(data);
|
|
41
40
|
const { transform: T = [], ...connector } = descriptor;
|
|
42
41
|
const transform = [connector, ...T];
|
|
43
|
-
const transformFunctions = transform.map(useData);
|
|
42
|
+
const transformFunctions = transform.map((t) => useData(t, context));
|
|
44
43
|
const transformedData = await composeAsync(transformFunctions)(data);
|
|
45
44
|
|
|
46
45
|
// Maintain the consistency of shape between input and output data.
|
|
@@ -62,7 +61,7 @@ export async function applyDataTransform(
|
|
|
62
61
|
export function flatEncode(
|
|
63
62
|
I: number[],
|
|
64
63
|
mark: G2Mark,
|
|
65
|
-
context:
|
|
64
|
+
context: G2Context,
|
|
66
65
|
): [number[], G2Mark] {
|
|
67
66
|
const { encode } = mark;
|
|
68
67
|
if (!encode) return [I, mark];
|
|
@@ -83,7 +82,7 @@ export function flatEncode(
|
|
|
83
82
|
export function inferChannelsType(
|
|
84
83
|
I: number[],
|
|
85
84
|
mark: G2Mark,
|
|
86
|
-
context:
|
|
85
|
+
context: G2Context,
|
|
87
86
|
): [number[], G2Mark] {
|
|
88
87
|
const { encode, data } = mark;
|
|
89
88
|
if (!encode) return [I, mark];
|
|
@@ -98,7 +97,7 @@ export function inferChannelsType(
|
|
|
98
97
|
export function maybeVisualChannel(
|
|
99
98
|
I: number[],
|
|
100
99
|
mark: G2Mark,
|
|
101
|
-
context:
|
|
100
|
+
context: G2Context,
|
|
102
101
|
): [number[], G2Mark] {
|
|
103
102
|
const { encode } = mark;
|
|
104
103
|
if (!encode) return [I, mark];
|
|
@@ -113,7 +112,7 @@ export function maybeVisualChannel(
|
|
|
113
112
|
export function extractColumns(
|
|
114
113
|
I: number[],
|
|
115
114
|
mark: G2Mark,
|
|
116
|
-
context:
|
|
115
|
+
context: G2Context,
|
|
117
116
|
): [number[], G2Mark] {
|
|
118
117
|
const { encode, data } = mark;
|
|
119
118
|
if (!encode) return [I, mark];
|
|
@@ -129,7 +128,7 @@ export function extractColumns(
|
|
|
129
128
|
export function normalizeTooltip(
|
|
130
129
|
I: number[],
|
|
131
130
|
mark: G2Mark,
|
|
132
|
-
context:
|
|
131
|
+
context: G2Context,
|
|
133
132
|
): [number[], G2Mark] {
|
|
134
133
|
const { tooltip = {} } = mark;
|
|
135
134
|
if (isUnset(tooltip)) return [I, mark];
|
|
@@ -145,7 +144,7 @@ export function normalizeTooltip(
|
|
|
145
144
|
export function extractTooltip(
|
|
146
145
|
I: number[],
|
|
147
146
|
mark: G2Mark,
|
|
148
|
-
context:
|
|
147
|
+
context: G2Context,
|
|
149
148
|
): [number[], G2Mark] {
|
|
150
149
|
const { data, encode, tooltip = {} } = mark;
|
|
151
150
|
if (isUnset(tooltip)) return [I, mark];
|
|
@@ -212,7 +211,7 @@ export function extractTooltip(
|
|
|
212
211
|
export function maybeArrayField(
|
|
213
212
|
I: number[],
|
|
214
213
|
mark: G2Mark,
|
|
215
|
-
context:
|
|
214
|
+
context: G2Context,
|
|
216
215
|
): [number[], G2Mark] {
|
|
217
216
|
const { encode, ...rest } = mark;
|
|
218
217
|
if (!encode) return [I, mark];
|
|
@@ -250,7 +249,7 @@ export function maybeArrayField(
|
|
|
250
249
|
export function addGuideToScale(
|
|
251
250
|
I: number[],
|
|
252
251
|
mark: G2Mark,
|
|
253
|
-
context:
|
|
252
|
+
context: G2Context,
|
|
254
253
|
): [number[], G2Mark] {
|
|
255
254
|
const { axis = {}, legend = {}, slider = {}, scrollbar = {} } = mark;
|
|
256
255
|
const normalize = (guide: boolean | Record<string, any>, channel: string) => {
|
|
@@ -298,7 +297,7 @@ export function addGuideToScale(
|
|
|
298
297
|
export function maybeNonAnimate(
|
|
299
298
|
I: number[],
|
|
300
299
|
mark: G2Mark,
|
|
301
|
-
context:
|
|
300
|
+
context: G2Context,
|
|
302
301
|
): [number[], G2Mark] {
|
|
303
302
|
const { animate } = mark;
|
|
304
303
|
if (animate || animate === undefined) return [I, mark];
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { G2Context } from './options';
|
|
2
|
+
|
|
1
3
|
export type DataOptions = Record<string, any>;
|
|
2
4
|
|
|
3
5
|
export type DataProps = Record<string, never>;
|
|
4
6
|
|
|
5
7
|
export type DataComponent<O extends DataOptions = DataOptions> = {
|
|
6
|
-
(options?: O): Data;
|
|
8
|
+
(options?: O, context?: G2Context): Data;
|
|
7
9
|
props: DataProps;
|
|
8
10
|
};
|
|
9
11
|
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { ColumnValue, NormalizedEncodeSpec } from './encode';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export type TransformContext = {
|
|
5
|
-
library: G2Library;
|
|
6
|
-
};
|
|
2
|
+
import { G2Context, G2Mark } from './options';
|
|
7
3
|
|
|
8
4
|
export type TransformOptions = Record<string, any>;
|
|
9
5
|
|
|
@@ -18,7 +14,7 @@ export type TransformComponent<O extends TransformOptions = TransformOptions> =
|
|
|
18
14
|
export type Transform = (
|
|
19
15
|
I: number[],
|
|
20
16
|
mark: G2Mark,
|
|
21
|
-
context:
|
|
17
|
+
context: G2Context,
|
|
22
18
|
) => [number[], G2Mark] | Promise<[number[], G2Mark]>;
|
|
23
19
|
|
|
24
20
|
export type TransformSpec = {
|
package/src/transform/diffY.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { deepMix } from '@antv/util';
|
|
|
2
2
|
import { TransformComponent as TC } from '../runtime';
|
|
3
3
|
import { DiffYTransform } from '../spec';
|
|
4
4
|
import { column, columnOf, maybeColumnOf } from './utils/helper';
|
|
5
|
-
import {
|
|
5
|
+
import { createGroups } from './utils/order';
|
|
6
6
|
|
|
7
7
|
export type DiffYOptions = Omit<DiffYTransform, 'type'>;
|
|
8
8
|
|
|
@@ -34,7 +34,7 @@ export const DiffY: TC<DiffYOptions> = (options = {}) => {
|
|
|
34
34
|
// Get the max Y of current group with current Y exclude.
|
|
35
35
|
const max = Math.max(...YG.filter((_, _i) => _i !== idx));
|
|
36
36
|
// Diff Y value.
|
|
37
|
-
newY1[i] = Y[i] > max ? max : Y[i];
|
|
37
|
+
newY1[i] = +Y[i] > max ? max : Y[i];
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
package/src/transform/dodgeX.ts
CHANGED
|
@@ -24,14 +24,26 @@ export const DodgeX: TC<DodgeXOptions> = (options = {}) => {
|
|
|
24
24
|
const [Y] = columnOf(encode, 'y');
|
|
25
25
|
const [S] = maybeColumnOf(encode, 'series', 'color');
|
|
26
26
|
const domainSeries = domainOf(S, scaleSeries);
|
|
27
|
+
const newMark = deepMix({}, mark, {
|
|
28
|
+
scale: {
|
|
29
|
+
series: {
|
|
30
|
+
domain: domainSeries,
|
|
31
|
+
paddingInner: padding,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
});
|
|
27
35
|
|
|
28
36
|
// Create groups and apply specified order for each group.
|
|
29
37
|
const groups = createGroups(groupBy, I, mark);
|
|
30
38
|
const createComparator = normalizeComparator(orderBy);
|
|
39
|
+
|
|
40
|
+
if (!createComparator) {
|
|
41
|
+
return [I, deepMix(newMark, { encode: { series: column(S) } })];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Sort and Update series for each mark related to series domain.
|
|
31
45
|
const comparator = createComparator(data, Y, S);
|
|
32
46
|
if (comparator) applyOrder(groups, comparator);
|
|
33
|
-
|
|
34
|
-
// Update series for each mark related to series domain.
|
|
35
47
|
const newS = new Array(I.length);
|
|
36
48
|
for (const G of groups) {
|
|
37
49
|
if (reverse) G.reverse();
|
|
@@ -42,15 +54,9 @@ export const DodgeX: TC<DodgeXOptions> = (options = {}) => {
|
|
|
42
54
|
|
|
43
55
|
return [
|
|
44
56
|
I,
|
|
45
|
-
deepMix(
|
|
46
|
-
scale: {
|
|
47
|
-
series: {
|
|
48
|
-
domain: domainSeries,
|
|
49
|
-
paddingInner: padding,
|
|
50
|
-
},
|
|
51
|
-
},
|
|
57
|
+
deepMix(newMark, {
|
|
52
58
|
encode: {
|
|
53
|
-
series: column(newS),
|
|
59
|
+
series: column(orderBy ? newS : S),
|
|
54
60
|
},
|
|
55
61
|
}),
|
|
56
62
|
];
|
package/src/transform/stackY.ts
CHANGED
|
@@ -35,7 +35,7 @@ export const StackY: TC<StackYOptions> = (options = {}) => {
|
|
|
35
35
|
|
|
36
36
|
// Create groups and apply specified order for each group.
|
|
37
37
|
const groups = createGroups(groupBy, I, mark);
|
|
38
|
-
const createComparator = normalizeComparator(orderBy);
|
|
38
|
+
const createComparator = normalizeComparator(orderBy) ?? (() => null);
|
|
39
39
|
const comparator = createComparator(data, Y, S);
|
|
40
40
|
if (comparator) applyOrder(groups, comparator);
|
|
41
41
|
|
|
@@ -34,14 +34,16 @@ export function createGroups(
|
|
|
34
34
|
return Array.from(group(I, key).values());
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
export function normalizeComparator(
|
|
37
|
+
export function normalizeComparator(
|
|
38
|
+
order: Order,
|
|
39
|
+
): IndexComparatorFactory | null {
|
|
38
40
|
if (Array.isArray(order)) return createFieldsOrder(order);
|
|
39
41
|
if (typeof order === 'function') return createFunctionOrder(order);
|
|
40
42
|
if (order === 'series') return createSeriesOrder;
|
|
41
43
|
if (order === 'value') return createValueOrder;
|
|
42
44
|
if (order === 'sum') return createSumOrder;
|
|
43
45
|
if (order === 'maxIndex') return createMaxIndexOrder;
|
|
44
|
-
return
|
|
46
|
+
return null;
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
export function applyOrder(groups: number[][], comparator: IndexComparator) {
|