@antv/g2 5.0.3 → 5.0.5

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 (146) hide show
  1. package/dist/g2-lite.min.js +3 -3
  2. package/dist/g2.min.js +3 -3
  3. package/esm/api/chart.d.ts +7 -5
  4. package/esm/api/chart.js +39 -25
  5. package/esm/api/chart.js.map +1 -1
  6. package/esm/api/composition/base.d.ts +1 -0
  7. package/esm/api/composition/base.js +2 -2
  8. package/esm/api/composition/base.js.map +1 -1
  9. package/esm/component/axis.d.ts +5 -2
  10. package/esm/component/axis.js +30 -19
  11. package/esm/component/axis.js.map +1 -1
  12. package/esm/component/constant.d.ts +14 -0
  13. package/esm/component/constant.js +235 -0
  14. package/esm/component/constant.js.map +1 -0
  15. package/esm/component/legendContinuousBlockSize.js +2 -1
  16. package/esm/component/legendContinuousBlockSize.js.map +1 -1
  17. package/esm/component/legendContinuousSize.js +14 -5
  18. package/esm/component/legendContinuousSize.js.map +1 -1
  19. package/esm/component/slider.js +8 -5
  20. package/esm/component/slider.js.map +1 -1
  21. package/esm/interaction/brushFilter.d.ts +1 -1
  22. package/esm/interaction/brushFilter.js +22 -8
  23. package/esm/interaction/brushFilter.js.map +1 -1
  24. package/esm/interaction/brushHighlight.js +1 -1
  25. package/esm/interaction/brushHighlight.js.map +1 -1
  26. package/esm/interaction/brushXFilter.d.ts +1 -1
  27. package/esm/interaction/brushYFilter.d.ts +1 -1
  28. package/esm/interaction/legendFilter.js +6 -3
  29. package/esm/interaction/legendFilter.js.map +1 -1
  30. package/esm/interaction/sliderFilter.js +16 -7
  31. package/esm/interaction/sliderFilter.js.map +1 -1
  32. package/esm/interaction/tooltip.js +38 -17
  33. package/esm/interaction/tooltip.js.map +1 -1
  34. package/esm/runtime/component.js +2 -92
  35. package/esm/runtime/component.js.map +1 -1
  36. package/esm/runtime/plot.js +4 -2
  37. package/esm/runtime/plot.js.map +1 -1
  38. package/esm/runtime/render.js +3 -1
  39. package/esm/runtime/render.js.map +1 -1
  40. package/esm/runtime/transform.js +5 -7
  41. package/esm/runtime/transform.js.map +1 -1
  42. package/esm/shape/label/label.js +3 -2
  43. package/esm/shape/label/label.js.map +1 -1
  44. package/esm/shape/lineXY/line.d.ts +22 -1
  45. package/esm/shape/lineXY/line.js +12 -1
  46. package/esm/shape/lineXY/line.js.map +1 -1
  47. package/esm/shape/link/link.d.ts +2 -1
  48. package/esm/shape/link/link.js +5 -41
  49. package/esm/shape/link/link.js.map +1 -1
  50. package/esm/shape/text/advance.js +2 -4
  51. package/esm/shape/text/advance.js.map +1 -1
  52. package/esm/shape/utils.d.ts +4 -0
  53. package/esm/shape/utils.js.map +1 -1
  54. package/esm/shape/vector/vector.d.ts +1 -0
  55. package/esm/shape/vector/vector.js +12 -8
  56. package/esm/shape/vector/vector.js.map +1 -1
  57. package/esm/transform/sort.js +13 -3
  58. package/esm/transform/sort.js.map +1 -1
  59. package/esm/utils/size.d.ts +0 -10
  60. package/esm/utils/size.js +0 -23
  61. package/esm/utils/size.js.map +1 -1
  62. package/lib/api/chart.d.ts +7 -5
  63. package/lib/api/chart.js +38 -24
  64. package/lib/api/chart.js.map +1 -1
  65. package/lib/api/composition/base.d.ts +1 -0
  66. package/lib/api/composition/base.js +2 -2
  67. package/lib/api/composition/base.js.map +1 -1
  68. package/lib/component/axis.d.ts +5 -2
  69. package/lib/component/axis.js +30 -19
  70. package/lib/component/axis.js.map +1 -1
  71. package/lib/component/constant.d.ts +14 -0
  72. package/lib/component/constant.js +238 -0
  73. package/lib/component/constant.js.map +1 -0
  74. package/lib/component/legendContinuousBlockSize.js +2 -1
  75. package/lib/component/legendContinuousBlockSize.js.map +1 -1
  76. package/lib/component/legendContinuousSize.js +14 -5
  77. package/lib/component/legendContinuousSize.js.map +1 -1
  78. package/lib/component/slider.js +8 -5
  79. package/lib/component/slider.js.map +1 -1
  80. package/lib/interaction/brushFilter.d.ts +1 -1
  81. package/lib/interaction/brushFilter.js +22 -8
  82. package/lib/interaction/brushFilter.js.map +1 -1
  83. package/lib/interaction/brushHighlight.js +1 -1
  84. package/lib/interaction/brushHighlight.js.map +1 -1
  85. package/lib/interaction/brushXFilter.d.ts +1 -1
  86. package/lib/interaction/brushYFilter.d.ts +1 -1
  87. package/lib/interaction/legendFilter.js +6 -3
  88. package/lib/interaction/legendFilter.js.map +1 -1
  89. package/lib/interaction/sliderFilter.js +16 -7
  90. package/lib/interaction/sliderFilter.js.map +1 -1
  91. package/lib/interaction/tooltip.js +37 -16
  92. package/lib/interaction/tooltip.js.map +1 -1
  93. package/lib/runtime/component.js +2 -92
  94. package/lib/runtime/component.js.map +1 -1
  95. package/lib/runtime/plot.js +4 -2
  96. package/lib/runtime/plot.js.map +1 -1
  97. package/lib/runtime/render.js +3 -1
  98. package/lib/runtime/render.js.map +1 -1
  99. package/lib/runtime/transform.js +5 -7
  100. package/lib/runtime/transform.js.map +1 -1
  101. package/lib/shape/label/label.js +3 -2
  102. package/lib/shape/label/label.js.map +1 -1
  103. package/lib/shape/lineXY/line.d.ts +22 -1
  104. package/lib/shape/lineXY/line.js +12 -1
  105. package/lib/shape/lineXY/line.js.map +1 -1
  106. package/lib/shape/link/link.d.ts +2 -1
  107. package/lib/shape/link/link.js +5 -41
  108. package/lib/shape/link/link.js.map +1 -1
  109. package/lib/shape/text/advance.js +2 -4
  110. package/lib/shape/text/advance.js.map +1 -1
  111. package/lib/shape/utils.d.ts +4 -0
  112. package/lib/shape/utils.js.map +1 -1
  113. package/lib/shape/vector/vector.d.ts +1 -0
  114. package/lib/shape/vector/vector.js +12 -8
  115. package/lib/shape/vector/vector.js.map +1 -1
  116. package/lib/transform/sort.js +13 -3
  117. package/lib/transform/sort.js.map +1 -1
  118. package/lib/utils/size.d.ts +0 -10
  119. package/lib/utils/size.js +1 -25
  120. package/lib/utils/size.js.map +1 -1
  121. package/package.json +2 -2
  122. package/src/api/chart.ts +49 -42
  123. package/src/api/composition/base.ts +4 -2
  124. package/src/component/axis.ts +55 -23
  125. package/src/component/constant.ts +237 -0
  126. package/src/component/legendContinuousBlockSize.ts +2 -3
  127. package/src/component/legendContinuousSize.ts +21 -12
  128. package/src/component/slider.ts +10 -5
  129. package/src/interaction/brushFilter.ts +25 -8
  130. package/src/interaction/brushHighlight.ts +1 -1
  131. package/src/interaction/legendFilter.ts +6 -3
  132. package/src/interaction/sliderFilter.ts +21 -7
  133. package/src/interaction/tooltip.ts +64 -28
  134. package/src/runtime/component.ts +2 -98
  135. package/src/runtime/plot.ts +4 -1
  136. package/src/runtime/render.ts +3 -1
  137. package/src/runtime/transform.ts +5 -7
  138. package/src/shape/label/label.ts +13 -2
  139. package/src/shape/lineXY/line.ts +45 -2
  140. package/src/shape/link/link.ts +6 -38
  141. package/src/shape/text/advance.ts +11 -2
  142. package/src/shape/utils.ts +4 -0
  143. package/src/shape/vector/vector.ts +12 -9
  144. package/src/transform/sort.ts +12 -3
  145. package/src/utils/size.ts +0 -31
  146. package/CHANGELOG.md +0 -38
@@ -1,5 +1,5 @@
1
1
  import { DisplayObject, IElement, Line } from '@antv/g';
2
- import { sort, group, mean, bisector } from 'd3-array';
2
+ import { sort, group, mean, bisector, minIndex } from 'd3-array';
3
3
  import { deepMix, lowerFirst, throttle } from '@antv/util';
4
4
  import { Tooltip as TooltipComponent } from '@antv/gui';
5
5
  import { Constant, Identity } from '@antv/scale';
@@ -102,6 +102,11 @@ function destroyTooltip(root) {
102
102
  }
103
103
  }
104
104
 
105
+ function showUndefined(item) {
106
+ const { value } = item;
107
+ return { ...item, value: value === undefined ? 'undefined' : value };
108
+ }
109
+
105
110
  function singleItem(element) {
106
111
  const { __data__: datum } = element;
107
112
  const { title, items = [] } = datum;
@@ -110,7 +115,8 @@ function singleItem(element) {
110
115
  .map(({ color = itemColorOf(element), ...item }) => ({
111
116
  ...item,
112
117
  color,
113
- }));
118
+ }))
119
+ .map(showUndefined);
114
120
  return {
115
121
  ...(title && { title }),
116
122
  items: newItems,
@@ -156,7 +162,7 @@ function unique(items, key = (d) => d) {
156
162
  function groupItems(
157
163
  elements,
158
164
  scale,
159
- groupName = true,
165
+ groupName,
160
166
  data = elements.map((d) => d['__data__']),
161
167
  ) {
162
168
  const key = (d) => (d instanceof Date ? +d : d);
@@ -164,25 +170,36 @@ function groupItems(
164
170
  data.map((d) => d.title),
165
171
  key,
166
172
  ).filter(defined);
167
- const newItems = data.flatMap((datum, i) => {
168
- const element = elements[i];
169
- const { items = [], title } = datum;
170
- return items
171
- .filter(defined)
172
- .map(({ color = itemColorOf(element), name, ...item }) => {
173
- const name1 = groupName
174
- ? groupNameOf(scale, datum) || name
175
- : name || groupNameOf(scale, datum);
176
- return {
177
- ...item,
178
- color,
179
- name: name1 || title,
180
- };
181
- });
182
- });
173
+ const newItems = data
174
+ .flatMap((datum, i) => {
175
+ const element = elements[i];
176
+ const { items = [], title } = datum;
177
+ const definedItems = items.filter(defined);
178
+
179
+ // If there is only one item, use groupName as title by default.
180
+ const useGroupName =
181
+ groupName !== undefined ? groupName : items.length <= 1 ? true : false;
182
+
183
+ return definedItems.map(
184
+ ({ color = itemColorOf(element), name, ...item }) => {
185
+ const name1 = useGroupName
186
+ ? groupNameOf(scale, datum) || name
187
+ : name || groupNameOf(scale, datum);
188
+ return {
189
+ ...item,
190
+ color,
191
+ name: name1 || title,
192
+ };
193
+ },
194
+ );
195
+ })
196
+ .map(showUndefined);
183
197
  return {
184
198
  ...(T.length > 0 && { title: T.join(',') }),
185
- items: unique(newItems, (d) => `(${key(d.name)}, ${key(d.value)})`),
199
+ items: unique(
200
+ newItems,
201
+ (d) => `(${key(d.name)}, ${key(d.value)}, ${key(d.color)})`,
202
+ ),
186
203
  };
187
204
  }
188
205
 
@@ -262,6 +279,13 @@ function isEmptyTooltipData(data) {
262
279
  return false;
263
280
  }
264
281
 
282
+ function hasSeries(markState): boolean {
283
+ return Array.from(markState.values()).some(
284
+ // @ts-ignore
285
+ (d) => d.interaction?.seriesTooltip && d.tooltip,
286
+ );
287
+ }
288
+
265
289
  /**
266
290
  * Show tooltip for series item.
267
291
  */
@@ -275,7 +299,7 @@ export function seriesTooltip(
275
299
  coordinate,
276
300
  crosshairs,
277
301
  render,
278
- groupName = true,
302
+ groupName,
279
303
  wait = 50,
280
304
  leading = true,
281
305
  trailing = false,
@@ -330,9 +354,13 @@ export function seriesTooltip(
330
354
  // Apply offset for band scale x.
331
355
  const offsetX = scaleX?.getBandWidth ? scaleX.getBandWidth() / 2 : 0;
332
356
 
333
- const indexByFocus = (focus, I, X) => {
357
+ const abstractX = (focus) => {
334
358
  const [normalizedX] = coordinate.invert(focus);
335
- const finalX = normalizedX - offsetX;
359
+ return normalizedX - offsetX;
360
+ };
361
+
362
+ const indexByFocus = (focus, I, X) => {
363
+ const finalX = abstractX(focus);
336
364
  const [minX, maxX] = sort([X[0], X[X.length - 1]]);
337
365
  // Skip x out of range.
338
366
  if (finalX < minX || finalX > maxX) return null;
@@ -392,8 +420,17 @@ export function seriesTooltip(
392
420
  selectedSeriesData.push([d, p] as const);
393
421
  }
394
422
  }
423
+
424
+ // Filter selectedSeriesData with different x,
425
+ // make sure there is only one x closest to focusX.
426
+ const SX = Array.from(new Set(selectedSeriesData.map((d) => d[0].x)));
427
+ const closestX = SX[minIndex(SX, (x) => Math.abs(x - abstractX(focus)))];
428
+ const filteredSeriesData = selectedSeriesData.filter(
429
+ (d) => d[0].x === closestX,
430
+ );
431
+
395
432
  const selectedData = [
396
- ...selectedSeriesData.map((d) => d[0]),
433
+ ...filteredSeriesData.map((d) => d[0]),
397
434
  ...selectedItems.map((d) => d.__data__),
398
435
  ];
399
436
 
@@ -435,7 +472,7 @@ export function seriesTooltip(
435
472
  }
436
473
 
437
474
  if (crosshairs) {
438
- const points = selectedSeriesData.map((d) => d[1]);
475
+ const points = filteredSeriesData.map((d) => d[1]);
439
476
  updateRuleY(root, points, {
440
477
  ...ruleStyle,
441
478
  width,
@@ -478,7 +515,7 @@ export function tooltip(
478
515
  elements: elementsof,
479
516
  scale,
480
517
  render,
481
- groupName = true,
518
+ groupName,
482
519
  sort: sortFunction,
483
520
  filter: filterFunction,
484
521
  wait = 50,
@@ -569,7 +606,6 @@ export function Tooltip(options) {
569
606
  return (target, viewInstances) => {
570
607
  const { container, view } = target;
571
608
  const { scale, markState, coordinate } = view;
572
-
573
609
  // Get default value from mark states.
574
610
  const defaultSeries = interactionKeyof(markState, 'seriesTooltip');
575
611
  const defaultShowCrosshairs = interactionKeyof(markState, 'crosshairs');
@@ -577,7 +613,7 @@ export function Tooltip(options) {
577
613
  const isSeries = maybeValue(series, defaultSeries);
578
614
 
579
615
  // For non-facet and series tooltip.
580
- if (isSeries && !facet) {
616
+ if (isSeries && hasSeries(markState) && !facet) {
581
617
  return seriesTooltip(plotArea, {
582
618
  ...rest,
583
619
  elements: selectG2Elements,
@@ -12,6 +12,7 @@ import {
12
12
  } from '../coordinate';
13
13
  import { combine } from '../utils/array';
14
14
  import { defined } from '../utils/helper';
15
+ import { LEGEND_INFER_STRATEGIES } from '../component/constant';
15
16
  import {
16
17
  coordOf,
17
18
  isHelix,
@@ -191,103 +192,6 @@ function inferLegendComponentType(
191
192
 
192
193
  if (scalesByField.size === 0) return [];
193
194
 
194
- /**
195
- *
196
- * @param arr
197
- * @param main necessary channel
198
- * @returns
199
- */
200
- const createStrategy = <T>(arr: T[], main: T): T[][] => {
201
- const result = combine(arr);
202
- result.forEach((c) => c.unshift(main));
203
- result.push([main]);
204
- return result.sort((a, b) => b.length - a.length);
205
- };
206
-
207
- const createCategoryStrategy = () => {
208
- // category legend only support constant size
209
- const color = [
210
- ['color', 'discrete'],
211
- ['color', 'constant'],
212
- ];
213
- const shape = [
214
- ['shape', 'discrete'],
215
- ['shape', 'constant'],
216
- ];
217
- const size = [['size', 'constant']];
218
- const opacity = [
219
- ['opacity', 'discrete'],
220
- ['opacity', 'constant'],
221
- ];
222
-
223
- const stg: [string, string][][] = [];
224
- for (const cr of color) {
225
- for (const sp of shape) {
226
- for (const sz of size) {
227
- for (const op of opacity) {
228
- if (![cr, sp, sz, op].every((d) => d[1] === 'constant')) {
229
- stg.push(
230
- ...(createStrategy([sp, sz, op], cr) as [string, string][][]),
231
- );
232
- }
233
- }
234
- }
235
- }
236
- }
237
- // refactor above code
238
-
239
- return stg.sort((a, b) => b.length - a.length);
240
- };
241
-
242
- // [legend type, [[channels, scale types]]][]
243
- const strategy: [string, [string, string][][]][] = [
244
- ['legendCategory', createCategoryStrategy()],
245
- [
246
- 'legendContinuousSize',
247
- [
248
- [
249
- ['color', 'continuous'],
250
- ['opacity', 'continuous'],
251
- ['size', 'continuous'],
252
- ],
253
- [
254
- ['color', 'constant'],
255
- ['opacity', 'continuous'],
256
- ['size', 'continuous'],
257
- ],
258
- [
259
- ['color', 'continuous'],
260
- ['size', 'continuous'],
261
- ],
262
- [
263
- ['color', 'constant'],
264
- ['size', 'continuous'],
265
- ],
266
- ],
267
- ],
268
- [
269
- 'legendContinuousBlockSize',
270
- [
271
- [
272
- ['color', 'distribution'],
273
- ['opacity', 'distribution'],
274
- ['size', 'distribution'],
275
- ],
276
- [
277
- ['color', 'distribution'],
278
- ['size', 'distribution'],
279
- ],
280
- ],
281
- ],
282
- [
283
- 'legendContinuousBlock',
284
- createStrategy([['opacity', 'continuous']], ['color', 'distribution']),
285
- ],
286
- [
287
- 'legendContinuous',
288
- createStrategy([['opacity', 'continuous']], ['color', 'continuous']),
289
- ],
290
- ];
291
195
  function getScaleType(scale: G2ScaleOptions): string {
292
196
  const { type } = scale;
293
197
  if (typeof type !== 'string') return null;
@@ -308,7 +212,7 @@ function inferLegendComponentType(
308
212
 
309
213
  const sort = (arr: string[][]) =>
310
214
  arr.sort((a, b) => a[0].localeCompare(b[0]));
311
- for (const [componentType, accords] of strategy) {
215
+ for (const [componentType, accords] of LEGEND_INFER_STRATEGIES) {
312
216
  for (const { option, combination } of options) {
313
217
  if (accords.some((accord) => isEqual(sort(accord), sort(option)))) {
314
218
  return [componentType, combination] as [string, G2ScaleOptions[]];
@@ -250,7 +250,7 @@ export async function plot<T extends G2ViewTree>(
250
250
  // Apply new interaction.
251
251
  const interaction = useInteraction(option);
252
252
  const destroy = interaction(target, updateViewInstances, context.emitter);
253
- nameInteraction.set(options.type, { destroy });
253
+ nameInteraction.set(option.type, { destroy });
254
254
  }
255
255
  }
256
256
 
@@ -272,6 +272,9 @@ export async function plot<T extends G2ViewTree>(
272
272
  }
273
273
 
274
274
  context.views = views;
275
+
276
+ // Clear and update animation.
277
+ context.animations?.forEach((animation) => animation?.cancel());
275
278
  context.animations = transitions;
276
279
 
277
280
  context.emitter.emit(ChartEvent.AFTER_PAINT);
@@ -38,7 +38,9 @@ function inferKeys<T extends G2ViewTree = G2ViewTree>(options: T): T {
38
38
  const { children = [] } = node;
39
39
  if (Array.isArray(children)) {
40
40
  for (let i = 0; i < children.length; i++) {
41
- const child = children[i];
41
+ // Clone node as well.
42
+ const child = deepMix({}, children[i]);
43
+ children[i] = child;
42
44
  nodeParent.set(child, node);
43
45
  nodeIndex.set(child, i);
44
46
  discovered.push(child);
@@ -170,13 +170,11 @@ export function extractTooltip(
170
170
  : definedChannel
171
171
  ? encode[channel].value[i]
172
172
  : null;
173
- if (value1) {
174
- values[i] = {
175
- name: name1,
176
- color,
177
- value: normalizedValueFormatter(value1),
178
- };
179
- }
173
+ values[i] = {
174
+ name: name1,
175
+ color,
176
+ value: normalizedValueFormatter(value1),
177
+ };
180
178
  }
181
179
  return values;
182
180
  }
@@ -46,7 +46,14 @@ function getDefaultStyle(
46
46
  */
47
47
  export const Label: SC<LabelOptions> = (options) => {
48
48
  return (points, value, coordinate, theme) => {
49
- const { text, x, y, ...overrideStyle } = value;
49
+ const {
50
+ text,
51
+ x,
52
+ y,
53
+ transform: specifiedTS = '',
54
+ transformOrigin,
55
+ ...overrideStyle
56
+ } = value;
50
57
  const {
51
58
  rotate = 0,
52
59
  transform = '',
@@ -56,7 +63,11 @@ export const Label: SC<LabelOptions> = (options) => {
56
63
  return select(new Advance())
57
64
  .call(applyStyle, defaultStyle)
58
65
  .style('text', `${text}`)
59
- .style('transform', `${transform}rotate(${+rotate})`)
66
+ .style(
67
+ 'labelTransform',
68
+ `${transform} rotate(${+rotate}) ${specifiedTS}`.trim(),
69
+ )
70
+ .style('labelTransformOrigin', transformOrigin)
60
71
  .style('coordCenter', coordinate.getCenter())
61
72
  .call(applyStyle, overrideStyle)
62
73
  .node();
@@ -4,10 +4,44 @@ import { arc, line } from 'd3-shape';
4
4
  import { isPolar } from '../../utils/coordinate';
5
5
  import { select } from '../../utils/selection';
6
6
  import { dist } from '../../utils/vector';
7
+ import { subObject } from '../../utils/helper';
7
8
  import { Primitive, ShapeComponent as SC, Vector2 } from '../../runtime';
8
9
  import { applyStyle, getShapeTheme } from '../utils';
9
10
 
10
- export type LineOptions = Record<string, any>;
11
+ export type LineOptions = {
12
+ /**
13
+ * Whether show arrow.
14
+ */
15
+ arrow?: boolean;
16
+ /**
17
+ * Arrow size(px), default is 4px.
18
+ */
19
+ arrowSize?: number;
20
+ /**
21
+ * Fill color of arrow.
22
+ */
23
+ arrowFill?: string;
24
+ /**
25
+ * Stroke color of arrow.
26
+ */
27
+ arrowStroke?: string;
28
+ /**
29
+ * Others.
30
+ */
31
+ [key: string]: any;
32
+ };
33
+
34
+ function getArrowMarker(arrowSize: number, arrowStyle: any) {
35
+ const arrowMarker = new Path({
36
+ style: {
37
+ path: `M ${arrowSize},${arrowSize} L -${arrowSize},0 L ${arrowSize},-${arrowSize} L 0,0 Z`,
38
+ anchor: '0.5 0.5',
39
+ transformOrigin: 'center',
40
+ ...arrowStyle,
41
+ },
42
+ });
43
+ return arrowMarker;
44
+ }
11
45
 
12
46
  function getPath(points: Vector2[], coordinate: Coordinate) {
13
47
  if (!isPolar(coordinate))
@@ -32,7 +66,7 @@ function getTransform(coordinate: Coordinate, transform?: Primitive) {
32
66
  }
33
67
 
34
68
  export const Line: SC<LineOptions> = (options) => {
35
- const { ...style } = options;
69
+ const { arrow, arrowSize = 4, ...style } = options;
36
70
  return (points, value, coordinate, theme) => {
37
71
  const { mark, shape, defaultShape } = value;
38
72
  const { defaultColor, lineWidth, ...shapeTheme } = getShapeTheme(
@@ -43,6 +77,14 @@ export const Line: SC<LineOptions> = (options) => {
43
77
  );
44
78
  const { color = defaultColor, size = lineWidth } = value;
45
79
 
80
+ const arrowMarker = arrow
81
+ ? getArrowMarker(arrowSize, {
82
+ fill: style.stroke || color,
83
+ stroke: style.stroke || color,
84
+ ...subObject(style, 'arrow'),
85
+ })
86
+ : null;
87
+
46
88
  const path = getPath(points, coordinate);
47
89
  const transform = getTransform(coordinate, value.transform);
48
90
 
@@ -52,6 +94,7 @@ export const Line: SC<LineOptions> = (options) => {
52
94
  .style('stroke', color)
53
95
  .style('lineWidth', size)
54
96
  .style('transform', transform)
97
+ .style('markerEnd', arrowMarker)
55
98
  .call(applyStyle, style)
56
99
  .node();
57
100
  };
@@ -1,49 +1,17 @@
1
- import { Path } from '@antv/g';
2
- import { path as d3path } from 'd3-path';
3
- import { applyStyle, arrowPoints, ArrowOptions, getShapeTheme } from '../utils';
4
- import { select } from '../../utils/selection';
1
+ import { ArrowOptions } from '../utils';
5
2
  import { ShapeComponent as SC } from '../../runtime';
3
+ import { Vector } from '../../shape/vector/vector';
6
4
 
7
5
  export type LinkOptions = ArrowOptions;
8
6
 
9
7
  /**
10
- * Connect 2 points with a single line.
8
+ * Connect 2 points with a single line with arrow.
9
+ * ----->
11
10
  */
12
11
  export const Link: SC<LinkOptions> = (options) => {
13
- const { arrowSize, ...style } = options;
12
+ const { arrow = false } = options;
14
13
  return (points, value, coordinate, theme) => {
15
- const { mark, shape, defaultShape } = value;
16
- const { defaultColor, ...shapeTheme } = getShapeTheme(
17
- theme,
18
- mark,
19
- shape,
20
- defaultShape,
21
- );
22
- const { color = defaultColor, transform } = value;
23
- const [from, to] = points;
24
-
25
- // Draw line
26
- const path = d3path();
27
- path.moveTo(...from);
28
- path.lineTo(...to);
29
-
30
- // Draw 2 arrows.
31
- if (arrowSize) {
32
- // Calculate arrow end point.
33
- const [arrow1, arrow2] = arrowPoints(from, to, { arrowSize });
34
- path.moveTo(...to);
35
- path.lineTo(...arrow1);
36
- path.moveTo(...to);
37
- path.lineTo(...arrow2);
38
- }
39
-
40
- return select(new Path())
41
- .call(applyStyle, shapeTheme)
42
- .style('d', path.toString())
43
- .style('stroke', color)
44
- .style('transform', transform)
45
- .call(applyStyle, style)
46
- .node();
14
+ return Vector({ ...options, arrow })(points, value, coordinate, theme);
47
15
  };
48
16
  };
49
17
 
@@ -40,6 +40,9 @@ type TextShapeStyleProps = Omit<TextStyleProps, 'text'> &
40
40
  connector?: boolean;
41
41
  startMarker?: boolean;
42
42
  endMarker?: boolean;
43
+ labelTransform?: string;
44
+ labelTransformOrigin?: string;
45
+ rotate?: number;
43
46
  };
44
47
 
45
48
  function getConnectorPoint(shape: GText | Rect) {
@@ -125,8 +128,9 @@ export const Advance = createElement((g) => {
125
128
  // Do not pass className
126
129
  class: className,
127
130
  transform,
128
- // @ts-ignore
129
131
  rotate,
132
+ labelTransform,
133
+ labelTransformOrigin,
130
134
  x,
131
135
  y,
132
136
  x0 = x,
@@ -147,7 +151,12 @@ export const Advance = createElement((g) => {
147
151
  const shape1 = select(g)
148
152
  .maybeAppend('text', 'text')
149
153
  .style('zIndex', 0)
150
- .call(applyStyle, { textBaseline: 'middle', ...rest })
154
+ .call(applyStyle, {
155
+ textBaseline: 'middle',
156
+ transform: labelTransform,
157
+ transformOrigin: labelTransformOrigin,
158
+ ...rest,
159
+ })
151
160
  .node();
152
161
 
153
162
  const shape2 = select(g)
@@ -32,6 +32,10 @@ export function appendPolygon(path: D3Path, points: Vector2[]) {
32
32
  }
33
33
 
34
34
  export type ArrowOptions = {
35
+ /**
36
+ * Whether show arrow of line.
37
+ */
38
+ arrow?: boolean;
35
39
  /**
36
40
  * Arrow size, can be a px number, or a percentage string. Default: '40%'
37
41
  */
@@ -7,33 +7,36 @@ import { ShapeComponent as SC } from '../../runtime';
7
7
  export type VectorOptions = ArrowOptions;
8
8
 
9
9
  /**
10
+ * Connect 2 points with a single line with arrow.
10
11
  * ----->
11
12
  */
12
13
  export const Vector: SC<VectorOptions> = (options) => {
13
- const { arrowSize = '40%', ...style } = options;
14
+ const { arrow = true, arrowSize = '40%', ...style } = options;
14
15
  return (points, value, coordinate, theme) => {
15
- const { mark, shape, defaultShape, color, transform } = value;
16
+ const { mark, shape, defaultShape, transform } = value;
16
17
  const { defaultColor, ...shapeTheme } = getShapeTheme(
17
18
  theme,
18
19
  mark,
19
20
  shape,
20
21
  defaultShape,
21
22
  );
23
+ const { color = defaultColor } = value;
22
24
  const [from, to] = points;
23
25
 
24
- // Calculate arrow end point.
25
- const [arrow1, arrow2] = arrowPoints(from, to, { arrowSize });
26
-
27
26
  // Draw line
28
27
  const path = d3path();
29
28
  path.moveTo(...from);
30
29
  path.lineTo(...to);
31
30
 
32
31
  // Draw 2 arrows.
33
- path.moveTo(...to);
34
- path.lineTo(...arrow1);
35
- path.moveTo(...to);
36
- path.lineTo(...arrow2);
32
+ if (arrow) {
33
+ // Calculate arrow end point.
34
+ const [arrow1, arrow2] = arrowPoints(from, to, { arrowSize });
35
+ path.moveTo(...to);
36
+ path.lineTo(...arrow1);
37
+ path.moveTo(...to);
38
+ path.lineTo(...arrow2);
39
+ }
37
40
 
38
41
  return select(new Path())
39
42
  .call(applyStyle, shapeTheme)
@@ -44,7 +44,7 @@ export type SortOptions = {
44
44
  };
45
45
 
46
46
  function sortQuantitative(I, mark, options): [number[], G2Mark] {
47
- const { reverse, slice, channel } = options;
47
+ const { reverse, channel } = options;
48
48
  const { encode } = mark;
49
49
  const [V] = columnOf(encode, channel);
50
50
  const sortedI = sort(I, (i: number) => V[i]);
@@ -53,12 +53,21 @@ function sortQuantitative(I, mark, options): [number[], G2Mark] {
53
53
  return [sortedI, mark];
54
54
  }
55
55
 
56
+ // If domain is specified, only sort data in the domain.
57
+ function filterIndex(I, values, specifiedDomain): number[] {
58
+ if (!Array.isArray(specifiedDomain)) return I;
59
+ const domain = new Set(specifiedDomain);
60
+ return I.filter((i) => domain.has(values[i]));
61
+ }
62
+
56
63
  function sortOrdinal(I, mark, options): [number[], G2Mark] {
57
64
  const { reverse, slice, channel, ...rest } = options;
58
- const { encode } = mark;
65
+ const { encode, scale = {} } = mark;
66
+ const domain = scale[channel]?.domain;
59
67
  const [T] = columnOf(encode, channel);
60
68
  const normalizeReducer = createReducer(channel, rest, encode);
61
- const sortedDomain = groupSort(I, normalizeReducer, (i: number) => T[i]);
69
+ const SI = filterIndex(I, T, domain);
70
+ const sortedDomain = groupSort(SI, normalizeReducer, (i: number) => T[i]);
62
71
  if (reverse) sortedDomain.reverse();
63
72
  const s = typeof slice === 'number' ? [0, slice] : slice;
64
73
  const slicedDomain = slice ? sortedDomain.slice(...s) : sortedDomain;
package/src/utils/size.ts CHANGED
@@ -31,37 +31,6 @@ export function getContainerSize(container: HTMLElement): Size {
31
31
  };
32
32
  }
33
33
 
34
- /**
35
- *
36
- * @description Calculate the chart size.
37
- * @param container DOM container element.
38
- * @param autoFit Should auto fit.
39
- * @param width Chart width which is set by user.
40
- * @param height Chart height which is set by user.
41
- * @returns chart width and height.
42
- */
43
- export function getChartSize(
44
- container: HTMLElement,
45
- autoFit: boolean,
46
- width: number,
47
- height: number,
48
- ): Size {
49
- if (!autoFit) {
50
- return {
51
- width,
52
- height,
53
- };
54
- }
55
-
56
- // Get size from wrapper container.
57
- const size = getContainerSize(container);
58
-
59
- return {
60
- width: size.width,
61
- height: size.height,
62
- };
63
- }
64
-
65
34
  /**
66
35
  * @description Calculate the real canvas size by view options.
67
36
  */