@antv/g2 5.2.0 → 5.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/README.md +6 -5
  2. package/README.zh-CN.md +2 -1
  3. package/dist/g2.lite.min.js +1 -1
  4. package/dist/g2.min.js +1 -1
  5. package/esm/api/runtime.d.ts +1 -0
  6. package/esm/api/runtime.js +2 -1
  7. package/esm/api/runtime.js.map +1 -1
  8. package/esm/composition/facetRect.js +4 -6
  9. package/esm/composition/facetRect.js.map +1 -1
  10. package/esm/composition/utils.d.ts +2 -0
  11. package/esm/composition/utils.js +10 -2
  12. package/esm/composition/utils.js.map +1 -1
  13. package/esm/data/utils/d3-cloud/index.js +4 -2
  14. package/esm/data/utils/d3-cloud/index.js.map +1 -1
  15. package/esm/data/wordCloud.d.ts +7 -0
  16. package/esm/data/wordCloud.js +5 -2
  17. package/esm/data/wordCloud.js.map +1 -1
  18. package/esm/interaction/legendFilter.js +0 -2
  19. package/esm/interaction/legendFilter.js.map +1 -1
  20. package/esm/interaction/sliderFilter.js +0 -2
  21. package/esm/interaction/sliderFilter.js.map +1 -1
  22. package/esm/interaction/tooltip.js +1 -0
  23. package/esm/interaction/tooltip.js.map +1 -1
  24. package/esm/label-transform/exceedAdjust.js +5 -0
  25. package/esm/label-transform/exceedAdjust.js.map +1 -1
  26. package/esm/runtime/coordinate.js +5 -1
  27. package/esm/runtime/coordinate.js.map +1 -1
  28. package/esm/runtime/mark.d.ts +1 -1
  29. package/esm/runtime/mark.js +1 -2
  30. package/esm/runtime/mark.js.map +1 -1
  31. package/esm/runtime/plot.d.ts +2 -2
  32. package/esm/runtime/plot.js +50 -35
  33. package/esm/runtime/plot.js.map +1 -1
  34. package/esm/runtime/render.js +2 -2
  35. package/esm/runtime/render.js.map +1 -1
  36. package/esm/runtime/scale.d.ts +1 -0
  37. package/esm/runtime/scale.js +13 -2
  38. package/esm/runtime/scale.js.map +1 -1
  39. package/esm/runtime/transform.d.ts +12 -13
  40. package/esm/runtime/transform.js +1 -1
  41. package/esm/runtime/transform.js.map +1 -1
  42. package/esm/runtime/types/data.d.ts +2 -1
  43. package/esm/runtime/types/transform.d.ts +2 -5
  44. package/esm/shape/label/label.js +4 -4
  45. package/esm/shape/label/label.js.map +1 -1
  46. package/esm/shape/label/position/spider.d.ts +3 -2
  47. package/esm/shape/label/position/spider.js +22 -6
  48. package/esm/shape/label/position/spider.js.map +1 -1
  49. package/esm/shape/label/position/utils.d.ts +2 -0
  50. package/esm/shape/label/position/utils.js +68 -0
  51. package/esm/shape/label/position/utils.js.map +1 -0
  52. package/esm/shape/text/advance.js +65 -32
  53. package/esm/shape/text/advance.js.map +1 -1
  54. package/esm/spec/scale.d.ts +4 -1
  55. package/esm/transform/diffY.js +1 -1
  56. package/esm/transform/diffY.js.map +1 -1
  57. package/esm/transform/dodgeX.js +14 -9
  58. package/esm/transform/dodgeX.js.map +1 -1
  59. package/esm/transform/filter.js +25 -3
  60. package/esm/transform/filter.js.map +1 -1
  61. package/esm/transform/stackY.js +2 -1
  62. package/esm/transform/stackY.js.map +1 -1
  63. package/esm/transform/utils/order.d.ts +1 -1
  64. package/esm/transform/utils/order.js +1 -1
  65. package/esm/transform/utils/order.js.map +1 -1
  66. package/lib/api/runtime.d.ts +1 -0
  67. package/lib/api/runtime.js +2 -1
  68. package/lib/api/runtime.js.map +1 -1
  69. package/lib/composition/facetRect.js +4 -6
  70. package/lib/composition/facetRect.js.map +1 -1
  71. package/lib/composition/utils.d.ts +2 -0
  72. package/lib/composition/utils.js +12 -3
  73. package/lib/composition/utils.js.map +1 -1
  74. package/lib/data/utils/d3-cloud/index.js +4 -2
  75. package/lib/data/utils/d3-cloud/index.js.map +1 -1
  76. package/lib/data/wordCloud.d.ts +7 -0
  77. package/lib/data/wordCloud.js +5 -2
  78. package/lib/data/wordCloud.js.map +1 -1
  79. package/lib/interaction/legendFilter.js +0 -2
  80. package/lib/interaction/legendFilter.js.map +1 -1
  81. package/lib/interaction/sliderFilter.js +0 -2
  82. package/lib/interaction/sliderFilter.js.map +1 -1
  83. package/lib/interaction/tooltip.js +1 -0
  84. package/lib/interaction/tooltip.js.map +1 -1
  85. package/lib/label-transform/exceedAdjust.js +5 -0
  86. package/lib/label-transform/exceedAdjust.js.map +1 -1
  87. package/lib/runtime/coordinate.js +5 -1
  88. package/lib/runtime/coordinate.js.map +1 -1
  89. package/lib/runtime/mark.d.ts +1 -1
  90. package/lib/runtime/mark.js +1 -2
  91. package/lib/runtime/mark.js.map +1 -1
  92. package/lib/runtime/plot.d.ts +2 -2
  93. package/lib/runtime/plot.js +49 -34
  94. package/lib/runtime/plot.js.map +1 -1
  95. package/lib/runtime/render.js +2 -2
  96. package/lib/runtime/render.js.map +1 -1
  97. package/lib/runtime/scale.d.ts +1 -0
  98. package/lib/runtime/scale.js +14 -2
  99. package/lib/runtime/scale.js.map +1 -1
  100. package/lib/runtime/transform.d.ts +12 -13
  101. package/lib/runtime/transform.js +1 -1
  102. package/lib/runtime/transform.js.map +1 -1
  103. package/lib/runtime/types/data.d.ts +2 -1
  104. package/lib/runtime/types/transform.d.ts +2 -5
  105. package/lib/shape/label/label.js +4 -4
  106. package/lib/shape/label/label.js.map +1 -1
  107. package/lib/shape/label/position/spider.d.ts +3 -2
  108. package/lib/shape/label/position/spider.js +22 -6
  109. package/lib/shape/label/position/spider.js.map +1 -1
  110. package/lib/shape/label/position/utils.d.ts +2 -0
  111. package/lib/shape/label/position/utils.js +73 -0
  112. package/lib/shape/label/position/utils.js.map +1 -0
  113. package/lib/shape/text/advance.js +65 -32
  114. package/lib/shape/text/advance.js.map +1 -1
  115. package/lib/spec/scale.d.ts +4 -1
  116. package/lib/transform/diffY.js +1 -1
  117. package/lib/transform/diffY.js.map +1 -1
  118. package/lib/transform/dodgeX.js +14 -9
  119. package/lib/transform/dodgeX.js.map +1 -1
  120. package/lib/transform/filter.js +25 -3
  121. package/lib/transform/filter.js.map +1 -1
  122. package/lib/transform/stackY.js +2 -1
  123. package/lib/transform/stackY.js.map +1 -1
  124. package/lib/transform/utils/order.d.ts +1 -1
  125. package/lib/transform/utils/order.js +1 -1
  126. package/lib/transform/utils/order.js.map +1 -1
  127. package/package.json +1 -1
  128. package/src/api/runtime.ts +4 -1
  129. package/src/composition/facetRect.ts +7 -4
  130. package/src/composition/utils.ts +10 -2
  131. package/src/data/utils/d3-cloud/index.ts +6 -2
  132. package/src/data/wordCloud.ts +14 -2
  133. package/src/interaction/legendFilter.ts +0 -1
  134. package/src/interaction/sliderFilter.ts +0 -3
  135. package/src/interaction/tooltip.ts +1 -0
  136. package/src/label-transform/exceedAdjust.ts +5 -0
  137. package/src/runtime/coordinate.ts +6 -1
  138. package/src/runtime/mark.ts +4 -9
  139. package/src/runtime/plot.ts +54 -45
  140. package/src/runtime/render.ts +3 -4
  141. package/src/runtime/scale.ts +28 -2
  142. package/src/runtime/transform.ts +13 -14
  143. package/src/runtime/types/data.ts +3 -1
  144. package/src/runtime/types/transform.ts +2 -6
  145. package/src/shape/label/label.ts +4 -3
  146. package/src/shape/label/position/spider.ts +29 -10
  147. package/src/shape/label/position/utils.ts +77 -0
  148. package/src/shape/text/advance.ts +76 -38
  149. package/src/spec/scale.ts +8 -0
  150. package/src/transform/diffY.ts +2 -2
  151. package/src/transform/dodgeX.ts +16 -10
  152. package/src/transform/filter.ts +55 -5
  153. package/src/transform/stackY.ts +1 -1
  154. package/src/transform/utils/order.ts +4 -2
@@ -56,9 +56,9 @@ const setScale = useDefaultAdaptor<G2ViewTree>((options) => {
56
56
  * color encoding, but it also can be override with explicity
57
57
  * encode and scale specification.
58
58
  */
59
- export const inferColor = useDefaultAdaptor<G2ViewTree>(
59
+ export const inferColor = useOverrideAdaptor<G2ViewTree>(
60
60
  (options: G2ViewTree) => {
61
- const { data, scale } = options;
61
+ const { data, scale, legend } = options;
62
62
  const discovered = [options];
63
63
  let encodeColor;
64
64
  let scaleColor;
@@ -90,10 +90,13 @@ export const inferColor = useDefaultAdaptor<G2ViewTree>(
90
90
 
91
91
  const title = typeof encodeColor === 'string' ? encodeColor : '';
92
92
  const [domain, type] = domainColor();
93
+
93
94
  return {
94
- encode: { color: encodeColor },
95
+ encode: { color: { type: 'column', value: domain ?? [] } },
95
96
  scale: { color: deepMix({}, scaleColor, { domain, type }) },
96
- legend: { color: deepMix({ title }, legendColor) },
97
+ ...(legend === undefined && {
98
+ legend: { color: deepMix({ title }, legendColor) },
99
+ }),
97
100
  };
98
101
  },
99
102
  );
@@ -16,12 +16,20 @@ export function useOverrideAdaptor<T>(adaptor: Adapter<T>): Adapter<T> {
16
16
  return (options?, ...rest) => deepMix({}, options, adaptor(options, ...rest));
17
17
  }
18
18
 
19
+ export function isObject(d) {
20
+ if (d instanceof Date) return false;
21
+ return typeof d === 'object';
22
+ }
23
+
19
24
  export function mergeData(
20
25
  dataDescriptor: any[] | { value: any; [key: string]: any },
21
26
  dataValue: any[],
22
27
  ) {
23
28
  if (!dataDescriptor) return dataValue;
24
29
  if (Array.isArray(dataDescriptor)) return dataDescriptor;
25
- const { value = dataValue, ...rest } = dataDescriptor;
26
- return { ...rest, value };
30
+ if (isObject(dataDescriptor)) {
31
+ const { value = dataValue, ...rest } = dataDescriptor;
32
+ return { ...rest, value };
33
+ }
34
+ return dataDescriptor;
27
35
  }
@@ -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
  };
@@ -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]);
@@ -126,7 +126,6 @@ function legendFilterOrdinal(
126
126
  const index = selectedValues.indexOf(value);
127
127
  if (index === -1) selectedValues.push(value);
128
128
  else selectedValues.splice(index, 1);
129
- if (selectedValues.length === 0) selectedValues.push(...items.map(datum));
130
129
  await filter(selectedValues);
131
130
  updateLegendState();
132
131
 
@@ -227,9 +227,6 @@ export function SliderFilter({
227
227
  }
228
228
  }
229
229
 
230
- // Update to async state such as scale.
231
- update();
232
-
233
230
  return () => {
234
231
  for (const [slider, handler] of sliderHandler) {
235
232
  slider.removeEventListener('valuechange', handler);
@@ -499,6 +499,7 @@ function updateMarker(root, { data, style, theme }) {
499
499
  const fill = type === 'hollow' ? 'transparent' : originColor;
500
500
  const stroke = type === 'hollow' ? originColor : '#fff';
501
501
  const shape = new Circle({
502
+ className: 'g2-tooltip-marker',
502
503
  style: {
503
504
  cx: point[0],
504
505
  cy: point[1],
@@ -52,6 +52,11 @@ export const ExceedAdjust: LLC<ExceedAdjustOptions> = () => {
52
52
  [width, height],
53
53
  ],
54
54
  );
55
+ // For label with connectorPoints
56
+ if (l.style.connector && l.style.connectorPoints) {
57
+ l.style.connectorPoints[0][0] -= changeValue[0];
58
+ l.style.connectorPoints[0][1] -= changeValue[1];
59
+ }
55
60
  l.style.x += changeValue[0];
56
61
  l.style.y += changeValue[1];
57
62
  });
@@ -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<
@@ -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
- library: G2Library,
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: TransformContext,
141
+ context: G2Context,
147
142
  ): Promise<[number[], G2Mark]> {
148
143
  const { library } = context;
149
144
  const [useTransform] = useLibrary<
@@ -49,6 +49,7 @@ import {
49
49
  inferScale,
50
50
  syncFacetsScales,
51
51
  useRelationScale,
52
+ groupTransform,
52
53
  } from './scale';
53
54
  import { applyDataTransform } from './transform';
54
55
  import {
@@ -96,9 +97,10 @@ type Store = Map<any, (options: G2ViewTree) => G2ViewTree>;
96
97
  export async function plot<T extends G2ViewTree>(
97
98
  options: T,
98
99
  selection: Selection,
99
- library: G2Library,
100
100
  context: G2Context,
101
101
  ): Promise<any> {
102
+ const { library } = context;
103
+
102
104
  const [useComposition] = useLibrary<
103
105
  G2CompositionOptions,
104
106
  CompositionComponent,
@@ -168,7 +170,7 @@ export async function plot<T extends G2ViewTree>(
168
170
  const state = nodeState.get(node);
169
171
  const [view, children] = state
170
172
  ? initializeState(state, node, library)
171
- : await initializeView(node, library);
173
+ : await initializeView(node, context);
172
174
  viewNode.set(view, node);
173
175
  views.push(view);
174
176
 
@@ -183,7 +185,7 @@ export async function plot<T extends G2ViewTree>(
183
185
  // should sync position scales among facets normally.
184
186
  if (transformedNodes.every(isStandardView)) {
185
187
  const states = await Promise.all(
186
- transformedNodes.map((d) => initializeMarks(d, library)),
188
+ transformedNodes.map((d) => initializeMarks(d, context)),
187
189
  );
188
190
  // Note!!!
189
191
  // This will mutate scales for marks.
@@ -198,7 +200,7 @@ export async function plot<T extends G2ViewTree>(
198
200
  // Apply transform to get data in advance for non-mark composition
199
201
  // node, which makes sure that composition node can preprocess the
200
202
  // data to produce more nodes based on it.
201
- const n = isMark(node) ? node : await applyTransform(node, library);
203
+ const n = isMark(node) ? node : await applyTransform(node, context);
202
204
  const N = transform(n);
203
205
  if (Array.isArray(N)) discovered.push(...N);
204
206
  else if (typeof N === 'function') nodeGenerators.push(N());
@@ -222,12 +224,12 @@ export async function plot<T extends G2ViewTree>(
222
224
  .attr('id', (view) => view.key)
223
225
  .call(applyTranslate)
224
226
  .each(function (view, i, element) {
225
- plotView(view, select(element), transitions, library, context);
227
+ plotView(view, select(element), transitions, context);
226
228
  enterContainer.set(view, element);
227
229
  }),
228
230
  (update) =>
229
231
  update.call(applyTranslate).each(function (view, i, element) {
230
- plotView(view, select(element), transitions, library, context);
232
+ plotView(view, select(element), transitions, context);
231
233
  updateContainer.set(view, element);
232
234
  }),
233
235
  (exit) =>
@@ -260,12 +262,7 @@ export async function plot<T extends G2ViewTree>(
260
262
  oldStore || new Map<any, (options: G2ViewTree) => G2ViewTree>();
261
263
  const setState = (key, reducer = (x) => x) => store.set(key, reducer);
262
264
  const options = viewNode.get(view);
263
- const update = createUpdateView(
264
- select(container),
265
- options,
266
- library,
267
- context,
268
- );
265
+ const update = createUpdateView(select(container), options, context);
269
266
  return {
270
267
  view,
271
268
  container,
@@ -372,7 +369,7 @@ export async function plot<T extends G2ViewTree>(
372
369
  const keyframe = new Promise<void>(async (resolve) => {
373
370
  for (const node of nodeGenerator) {
374
371
  const sizedNode = { width, height, ...node };
375
- await plot(sizedNode, selection, library, context);
372
+ await plot(sizedNode, selection, context);
376
373
  }
377
374
  resolve();
378
375
  });
@@ -424,9 +421,9 @@ function definedInteraction(library: G2Library) {
424
421
  function createUpdateView(
425
422
  selection: Selection,
426
423
  options: G2ViewTree,
427
- library: G2Library,
428
424
  context: G2Context,
429
425
  ): G2ViewInstance['update'] {
426
+ const { library } = context;
430
427
  const createDefinedInteraction = definedInteraction(library);
431
428
  const filter = (d) => d[1] && d[1].props && d[1].props.reapplyWhenUpdate;
432
429
  const interactions = inferInteraction(options);
@@ -437,16 +434,16 @@ function createUpdateView(
437
434
 
438
435
  return async (newOptions, source, callback) => {
439
436
  const transitions = [];
440
- const [newView, newChildren] = await initializeView(newOptions, library);
441
- plotView(newView, selection, transitions, library, context);
437
+ const [newView, newChildren] = await initializeView(newOptions, context);
438
+ plotView(newView, selection, transitions, context);
442
439
 
443
440
  // Update interaction need to reapply when update.
444
441
  for (const name of updates.filter((d) => d !== source)) {
445
- updateInteraction(name, selection, newOptions, newView, library, context);
442
+ updateInteraction(name, selection, newOptions, newView, context);
446
443
  }
447
444
 
448
445
  for (const child of newChildren) {
449
- plot(child, selection, library, context);
446
+ plot(child, selection, context);
450
447
  }
451
448
  callback();
452
449
  return { options: newOptions, view: newView };
@@ -458,9 +455,10 @@ function updateInteraction(
458
455
  selection: Selection,
459
456
  options: G2ViewTree,
460
457
  view: G2ViewDescriptor,
461
- library: G2Library,
462
458
  context: G2Context,
463
459
  ) {
460
+ const { library } = context;
461
+
464
462
  const [useInteraction] = useLibrary<
465
463
  G2InteractionOptions,
466
464
  InteractionComponent,
@@ -500,9 +498,11 @@ function updateInteraction(
500
498
 
501
499
  async function initializeView(
502
500
  options: G2View,
503
- library: G2Library,
501
+ context: G2Context,
504
502
  ): Promise<[G2ViewDescriptor, G2ViewTree[]]> {
505
- const flattenOptions = await transformMarks(options, library);
503
+ const { library } = context;
504
+
505
+ const flattenOptions = await transformMarks(options, context);
506
506
 
507
507
  const mergedOptions = bubbleOptions(flattenOptions);
508
508
 
@@ -515,7 +515,7 @@ async function initializeView(
515
515
  options.marks = [...mergedOptions.marks, ...mergedOptions.components];
516
516
 
517
517
  const transformedOptions = coordinate2Transform(mergedOptions, library);
518
- const state = await initializeMarks(transformedOptions, library);
518
+ const state = await initializeMarks(transformedOptions, context);
519
519
  return initializeState(state, transformedOptions, library);
520
520
  }
521
521
 
@@ -553,8 +553,10 @@ function bubbleOptions(options: G2View): G2View {
553
553
 
554
554
  async function transformMarks(
555
555
  options: G2View,
556
- library: G2Library,
556
+ context: G2Context,
557
557
  ): Promise<G2View> {
558
+ const { library } = context;
559
+
558
560
  const [useMark, createMark] = useLibrary<G2MarkOptions, MarkComponent, Mark>(
559
561
  'mark',
560
562
  library,
@@ -570,13 +572,13 @@ async function transformMarks(
570
572
  const components = [];
571
573
  const discovered = [...marks];
572
574
  const { width, height } = computeRoughPlotSize(options);
573
- const context = { options, width, height };
575
+ const markOptions = { options, width, height };
574
576
 
575
577
  // Pre order traversal.
576
578
  while (discovered.length) {
577
579
  const [node] = discovered.splice(0, 1);
578
580
  // Apply data transform to get data.
579
- const mark = (await applyTransform(node, library)) as G2Mark;
581
+ const mark = (await applyTransform(node, context)) as G2Mark;
580
582
  const { type = error('G2Mark type is required.'), key } = mark;
581
583
 
582
584
  // For components.
@@ -595,7 +597,7 @@ async function transformMarks(
595
597
  };
596
598
 
597
599
  // Convert composite mark to marks.
598
- const marks = await useMark(newMark, context);
600
+ const marks = await useMark(newMark, markOptions);
599
601
  const M = Array.isArray(marks) ? marks : [marks];
600
602
  discovered.unshift(...M.map((d, i) => ({ ...d, key: `${key}-${i}` })));
601
603
  }
@@ -607,8 +609,10 @@ async function transformMarks(
607
609
 
608
610
  async function initializeMarks(
609
611
  options: G2View,
610
- library: G2Library,
612
+ context: G2Context,
611
613
  ): Promise<Map<G2Mark, G2MarkState>> {
614
+ const { library } = context;
615
+
612
616
  const [useTheme] = useLibrary<G2ThemeOptions, ThemeComponent, Theme>(
613
617
  'theme',
614
618
  library,
@@ -630,7 +634,7 @@ async function initializeMarks(
630
634
  for (const markOptions of partialMarks) {
631
635
  const { type } = markOptions;
632
636
  const { props = {} } = createMark(type);
633
- const markAndState = await initializeMark(markOptions, props, library);
637
+ const markAndState = await initializeMark(markOptions, props, context);
634
638
  if (markAndState) {
635
639
  const [initializedMark, state] = markAndState;
636
640
  markState.set(initializedMark, state);
@@ -668,6 +672,7 @@ async function initializeMarks(
668
672
  const values = channels.flatMap(({ values }) => values.map((d) => d.value));
669
673
  const scale = {
670
674
  ...inferScale(name, values, options, coordinates, theme, library),
675
+ uid: Symbol('scale'),
671
676
  key: scaleKey,
672
677
  };
673
678
  channels.forEach((channel) => (channel.scale = scale));
@@ -742,6 +747,19 @@ function initializeState(
742
747
  // AxisZ need a copy of axisX and axisY to show grids in X-Z & Y-Z planes.
743
748
  processAxisZ(components);
744
749
 
750
+ // Index scale instance by uid.
751
+ const uidScale = new Map(
752
+ Array.from(markState.values()).flatMap((state) => {
753
+ const { channels } = state;
754
+ return channels.map(({ scale }) => [
755
+ scale.uid,
756
+ useRelationScale(scale, library),
757
+ ]);
758
+ }),
759
+ );
760
+
761
+ groupTransform(markState, uidScale);
762
+
745
763
  // Scale from marks and components.
746
764
  const scaleInstance: Record<string, Scale> = {};
747
765
 
@@ -750,8 +768,8 @@ function initializeState(
750
768
  const { scales: scaleDescriptors = [] } = component;
751
769
  const scales = [];
752
770
  for (const descriptor of scaleDescriptors) {
753
- const { name } = descriptor;
754
- const scale = useRelationScale(descriptor, library);
771
+ const { name, uid } = descriptor;
772
+ const scale = uidScale.get(uid) ?? useRelationScale(descriptor, library);
755
773
  scales.push(scale);
756
774
  // Delivery the scale of axisX to the AxisY,
757
775
  // in order to calculate the angle of axisY component when rendering radar chart.
@@ -763,7 +781,6 @@ function initializeState(
763
781
  }
764
782
  assignScale(scaleInstance, { [name]: scale });
765
783
  }
766
-
767
784
  component.scaleInstances = scales;
768
785
  }
769
786
 
@@ -788,9 +805,7 @@ function initializeState(
788
805
  channels.map(({ name, scale }) => [name, scale]),
789
806
  );
790
807
  // Transform abstract value to visual value by scales.
791
- const markScaleInstance = mapObject(scale, (options) => {
792
- return useRelationScale(options, library);
793
- });
808
+ const markScaleInstance = mapObject(scale, ({ uid }) => uidScale.get(uid));
794
809
  assignScale(scaleInstance, markScaleInstance);
795
810
  const value = applyScale(channels, markScaleInstance);
796
811
 
@@ -862,9 +877,9 @@ async function plotView(
862
877
  view: G2ViewDescriptor,
863
878
  selection: Selection,
864
879
  transitions: GAnimation[],
865
- library: G2Library,
866
880
  context: G2Context,
867
881
  ): Promise<void> {
882
+ const { library } = context;
868
883
  const {
869
884
  components,
870
885
  theme,
@@ -1022,13 +1037,7 @@ async function plotView(
1022
1037
  const { data } = state;
1023
1038
  const { key, class: cls, type } = mark;
1024
1039
  const viewNode = selection.select(`#${key}`);
1025
- const shapeFunction = createMarkShapeFunction(
1026
- mark,
1027
- state,
1028
- view,
1029
- library,
1030
- context,
1031
- );
1040
+ const shapeFunction = createMarkShapeFunction(mark, state, view, context);
1032
1041
  const enterFunction = createEnterFunction(mark, state, view, library);
1033
1042
  const updateFunction = createUpdateFunction(mark, state, view, library);
1034
1043
  const exitFunction = createExitFunction(mark, state, view, library);
@@ -1488,13 +1497,14 @@ function createMarkShapeFunction(
1488
1497
  mark: G2Mark,
1489
1498
  state: G2MarkState,
1490
1499
  view: G2ViewDescriptor,
1491
- library: G2Library,
1492
1500
  context: G2Context,
1493
1501
  ): (
1494
1502
  data: Record<string, any>,
1495
1503
  index: number,
1496
1504
  element?: DisplayObject,
1497
1505
  ) => DisplayObject {
1506
+ const { library } = context;
1507
+
1498
1508
  const [useShape] = useLibrary<G2ShapeOptions, ShapeComponent, Shape>(
1499
1509
  'shape',
1500
1510
  library,
@@ -1692,9 +1702,8 @@ function inferInteraction(
1692
1702
 
1693
1703
  async function applyTransform<T extends G2ViewTree>(
1694
1704
  node: T,
1695
- library: G2Library,
1705
+ context: G2Context,
1696
1706
  ): Promise<G2ViewTree> {
1697
- const context = { library };
1698
1707
  const { data, ...rest } = node;
1699
1708
  if (data == undefined) return node;
1700
1709
  const [, { data: newData }] = await applyDataTransform([], { data }, context);
@@ -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, library, context)
148
+ plot<T>({ ...keyed, width, height }, selection, context)
150
149
  .then(() => {
151
150
  context.canvas?.requestAnimationFrame(() => {
152
151
  emitter.emit(ChartEvent.AFTER_RENDER);
@@ -1,5 +1,5 @@
1
1
  import { Linear, createInterpolateValue } from '@antv/scale';
2
- import { extent, max } from 'd3-array';
2
+ import { extent, max, rollups } from 'd3-array';
3
3
  import * as d3ScaleChromatic from 'd3-scale-chromatic';
4
4
  import { deepMix, omit, upperFirst } from '@antv/util';
5
5
  import { firstOf, lastOf, unique } from '../utils/array';
@@ -72,6 +72,32 @@ export function applyScale(
72
72
  return scaledValue;
73
73
  }
74
74
 
75
+ export function groupTransform(
76
+ markState: Map<G2Mark, G2MarkState>,
77
+ uidScale: Map<symbol, Scale>,
78
+ ) {
79
+ const channels = Array.from(markState.values()).flatMap((d) => d.channels);
80
+
81
+ const scaleGroups = rollups(
82
+ channels,
83
+ (channels) => channels.map((d) => uidScale.get(d.scale.uid)),
84
+ (d) => d.name,
85
+ )
86
+ .filter(
87
+ ([, scales]) =>
88
+ scales.some(
89
+ (d) => typeof d.getOptions().groupTransform === 'function',
90
+ ) && // only sync scales with groupTransform options
91
+ scales.every((d) => d.getTicks), // only sync quantitative scales
92
+ )
93
+ .map((d) => d[1]);
94
+
95
+ scaleGroups.forEach((group) => {
96
+ const groupTransform = group.map((d) => d.getOptions().groupTransform)[0];
97
+ groupTransform(group);
98
+ });
99
+ }
100
+
75
101
  export function collectScales(states: G2MarkState[], options: G2View) {
76
102
  const { components = [] } = options;
77
103
 
@@ -416,7 +442,7 @@ function categoricalColors(
416
442
  library,
417
443
  );
418
444
  const { category10: c10, category20: c20 } = theme;
419
- const defaultPalette = unique(values.flat()).length <= c10.length ? c10 : c20;
445
+ const defaultPalette = unique(domain).length <= c10.length ? c10 : c20;
420
446
  const { palette = defaultPalette, offset } = options;
421
447
  if (Array.isArray(palette)) return palette;
422
448
  // Built-in palettes have higher priority.