@antv/g2 5.1.20 → 5.1.22

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 (40) hide show
  1. package/dist/g2.lite.min.js +1 -1
  2. package/dist/g2.min.js +1 -1
  3. package/esm/interaction/tooltip.d.ts +2 -2
  4. package/esm/interaction/tooltip.js +152 -30
  5. package/esm/interaction/tooltip.js.map +1 -1
  6. package/esm/runtime/component.js +2 -0
  7. package/esm/runtime/component.js.map +1 -1
  8. package/esm/runtime/layout.js +3 -3
  9. package/esm/runtime/layout.js.map +1 -1
  10. package/esm/runtime/plot.js +8 -6
  11. package/esm/runtime/plot.js.map +1 -1
  12. package/esm/runtime/transform.js +2 -2
  13. package/esm/runtime/transform.js.map +1 -1
  14. package/esm/shape/vector/vector.js +2 -3
  15. package/esm/shape/vector/vector.js.map +1 -1
  16. package/esm/theme/dark.js +1 -1
  17. package/esm/theme/dark.js.map +1 -1
  18. package/lib/interaction/tooltip.d.ts +2 -2
  19. package/lib/interaction/tooltip.js +151 -29
  20. package/lib/interaction/tooltip.js.map +1 -1
  21. package/lib/runtime/component.js +2 -0
  22. package/lib/runtime/component.js.map +1 -1
  23. package/lib/runtime/layout.js +3 -3
  24. package/lib/runtime/layout.js.map +1 -1
  25. package/lib/runtime/plot.js +8 -6
  26. package/lib/runtime/plot.js.map +1 -1
  27. package/lib/runtime/transform.js +2 -2
  28. package/lib/runtime/transform.js.map +1 -1
  29. package/lib/shape/vector/vector.js +2 -3
  30. package/lib/shape/vector/vector.js.map +1 -1
  31. package/lib/theme/dark.js +1 -1
  32. package/lib/theme/dark.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/interaction/tooltip.ts +209 -28
  35. package/src/runtime/component.ts +1 -0
  36. package/src/runtime/layout.ts +3 -3
  37. package/src/runtime/plot.ts +9 -6
  38. package/src/runtime/transform.ts +2 -2
  39. package/src/shape/vector/vector.ts +2 -3
  40. package/src/theme/dark.ts +1 -1
@@ -5,7 +5,7 @@ import { Tooltip as TooltipComponent } from '@antv/component';
5
5
  import { Constant, Band } from '@antv/scale';
6
6
  import { defined, subObject } from '../utils/helper';
7
7
  import { isTranspose, isPolar } from '../utils/coordinate';
8
- import { angle, sub } from '../utils/vector';
8
+ import { angle, sub, dist } from '../utils/vector';
9
9
  import { invert } from '../utils/scale';
10
10
  import { BBox } from '../runtime';
11
11
  import {
@@ -288,6 +288,106 @@ function groupItems(
288
288
  };
289
289
  }
290
290
 
291
+ function updateRuleX(
292
+ root,
293
+ points,
294
+ mouse,
295
+ {
296
+ plotWidth,
297
+ plotHeight,
298
+ mainWidth,
299
+ mainHeight,
300
+ startX,
301
+ startY,
302
+ transposed,
303
+ polar,
304
+ insetLeft,
305
+ insetTop,
306
+ ...rest
307
+ },
308
+ ) {
309
+ const defaults = {
310
+ lineWidth: 1,
311
+ stroke: '#1b1e23',
312
+ strokeOpacity: 0.5,
313
+ ...rest,
314
+ };
315
+
316
+ const createCircle = (cx, cy, r) => {
317
+ const circle = new Circle({
318
+ style: {
319
+ cx,
320
+ cy,
321
+ r,
322
+ ...defaults,
323
+ },
324
+ });
325
+ root.appendChild(circle);
326
+ return circle;
327
+ };
328
+
329
+ const createLine = (x1, x2, y1, y2) => {
330
+ const line = new Line({
331
+ style: {
332
+ x1,
333
+ x2,
334
+ y1,
335
+ y2,
336
+ ...defaults,
337
+ },
338
+ });
339
+ root.appendChild(line);
340
+ return line;
341
+ };
342
+
343
+ const minDistPoint = (mouse, points) => {
344
+ // only one point do not need compute
345
+ if (points.length === 1) {
346
+ return points[0];
347
+ }
348
+ const dists = points.map((p) => dist(p, mouse));
349
+ const minDistIndex = minIndex(dists, (d) => d);
350
+ return points[minDistIndex];
351
+ };
352
+
353
+ const target = minDistPoint(mouse, points);
354
+
355
+ const pointsOf = () => {
356
+ if (transposed)
357
+ return [
358
+ startX + target[0],
359
+ startX + target[0],
360
+ startY,
361
+ startY + plotHeight,
362
+ ];
363
+ return [startX, startX + plotWidth, target[1] + startY, target[1] + startY];
364
+ };
365
+
366
+ const pointsOfPolar = () => {
367
+ const cx = startX + insetLeft + mainWidth / 2;
368
+ const cy = startY + insetTop + mainHeight / 2;
369
+ const cdist = dist([cx, cy], target);
370
+ return [cx, cy, cdist];
371
+ };
372
+
373
+ if (polar) {
374
+ const [cx, cy, r] = pointsOfPolar();
375
+ const ruleX = root.ruleX || createCircle(cx, cy, r);
376
+ ruleX.style.cx = cx;
377
+ ruleX.style.cy = cy;
378
+ ruleX.style.r = r;
379
+ root.ruleX = ruleX;
380
+ } else {
381
+ const [x1, x2, y1, y2] = pointsOf();
382
+ const ruleX = root.ruleX || createLine(x1, x2, y1, y2);
383
+ ruleX.style.x1 = x1;
384
+ ruleX.style.x2 = x2;
385
+ ruleX.style.y1 = y1;
386
+ ruleX.style.y2 = y2;
387
+ root.ruleX = ruleX;
388
+ }
389
+ }
390
+
291
391
  function updateRuleY(
292
392
  root,
293
393
  points,
@@ -311,10 +411,12 @@ function updateRuleY(
311
411
  strokeOpacity: 0.5,
312
412
  ...rest,
313
413
  };
414
+
314
415
  const Y = points.map((p) => p[1]);
315
416
  const X = points.map((p) => p[0]);
316
417
  const y = mean(Y);
317
418
  const x = mean(X);
419
+
318
420
  const pointsOf = () => {
319
421
  if (polar) {
320
422
  const r = Math.min(mainWidth, mainHeight) / 2;
@@ -328,6 +430,7 @@ function updateRuleY(
328
430
  if (transposed) return [startX, startX + plotWidth, y + startY, y + startY];
329
431
  return [x + startX, x + startX, startY, startY + plotHeight];
330
432
  };
433
+
331
434
  const [x1, x2, y1, y2] = pointsOf();
332
435
  const createLine = () => {
333
436
  const line = new Line({
@@ -360,6 +463,13 @@ function hideRuleY(root) {
360
463
  }
361
464
  }
362
465
 
466
+ function hideRuleX(root) {
467
+ if (root.ruleX) {
468
+ root.ruleX.remove();
469
+ root.ruleX = undefined;
470
+ }
471
+ }
472
+
363
473
  function updateMarker(root, { data, style, theme }) {
364
474
  if (root.markers) root.markers.forEach((d) => d.remove());
365
475
  const markers = data
@@ -423,13 +533,6 @@ function hasSeries(markState): boolean {
423
533
  );
424
534
  }
425
535
 
426
- function findElement(node: DisplayObject) {
427
- return maybeRoot(node, (node) => {
428
- if (!node.classList) return false;
429
- return node.classList.includes('element');
430
- });
431
- }
432
-
433
536
  /**
434
537
  * Show tooltip for series item.
435
538
  */
@@ -442,6 +545,8 @@ export function seriesTooltip(
442
545
  scale,
443
546
  coordinate,
444
547
  crosshairs,
548
+ crosshairsX,
549
+ crosshairsY,
445
550
  render,
446
551
  groupName,
447
552
  emitter,
@@ -469,6 +574,7 @@ export function seriesTooltip(
469
574
  const transposed = isTranspose(coordinate);
470
575
  const polar = isPolar(coordinate);
471
576
  const style = deepMix(_style, rest);
577
+
472
578
  const {
473
579
  innerWidth: plotWidth,
474
580
  innerHeight: plotHeight,
@@ -488,7 +594,9 @@ export function seriesTooltip(
488
594
  else if (title || items) itemElements.push(element);
489
595
  }
490
596
 
491
- const isBandScale = !!(transposed ? scale.y : scale.x).getBandWidth;
597
+ // For band scale x, find the closest series element to focus,
598
+ // useful for interval + line mark.
599
+ const isBandScale = !!scale.x.getBandWidth;
492
600
  const closest = isBandScale && itemElements.length > 0;
493
601
 
494
602
  // Sorted elements from top to bottom visually,
@@ -662,22 +770,50 @@ export function seriesTooltip(
662
770
  });
663
771
  }
664
772
 
665
- if (crosshairs) {
666
- const points = filteredSeriesData.map((d) => d[1]);
773
+ if (crosshairs || crosshairsX || crosshairsY) {
667
774
  const ruleStyle = subObject(style, 'crosshairs');
668
- updateRuleY(root, points, {
775
+
776
+ const ruleStyleX = {
669
777
  ...ruleStyle,
670
- plotWidth,
671
- plotHeight,
672
- mainWidth,
673
- mainHeight,
674
- insetLeft,
675
- insetTop,
676
- startX,
677
- startY,
678
- transposed,
679
- polar,
680
- });
778
+ ...subObject(style, 'crosshairsX'),
779
+ };
780
+ const ruleStyleY = {
781
+ ...ruleStyle,
782
+ ...subObject(style, 'crosshairsY'),
783
+ };
784
+
785
+ const points = filteredSeriesData.map((d) => d[1]);
786
+ if (crosshairsX) {
787
+ updateRuleX(root, points, mouse, {
788
+ ...ruleStyleX,
789
+ plotWidth,
790
+ plotHeight,
791
+ mainWidth,
792
+ mainHeight,
793
+ insetLeft,
794
+ insetTop,
795
+ startX,
796
+ startY,
797
+ transposed,
798
+ polar,
799
+ });
800
+ }
801
+
802
+ if (crosshairsY) {
803
+ updateRuleY(root, points, {
804
+ ...ruleStyleY,
805
+ plotWidth,
806
+ plotHeight,
807
+ mainWidth,
808
+ mainHeight,
809
+ insetLeft,
810
+ insetTop,
811
+ startX,
812
+ startY,
813
+ transposed,
814
+ polar,
815
+ });
816
+ }
681
817
  }
682
818
 
683
819
  if (marker) {
@@ -701,13 +837,19 @@ export function seriesTooltip(
701
837
 
702
838
  const hide = (event: MouseEvent) => {
703
839
  hideTooltip({ root, single, emitter, event });
704
- if (crosshairs) hideRuleY(root);
840
+ if (crosshairs) {
841
+ hideRuleY(root);
842
+ hideRuleX(root);
843
+ }
705
844
  if (marker) hideMarker(root);
706
845
  };
707
846
 
708
847
  const destroy = () => {
709
848
  destroyTooltip({ root, single });
710
- if (crosshairs) hideRuleY(root);
849
+ if (crosshairs) {
850
+ hideRuleY(root);
851
+ hideRuleX(root);
852
+ }
711
853
  if (marker) hideMarker(root);
712
854
  };
713
855
 
@@ -780,6 +922,7 @@ export function tooltip(
780
922
  root: DisplayObject,
781
923
  {
782
924
  elements: elementsof,
925
+ coordinate,
783
926
  scale,
784
927
  render,
785
928
  groupName,
@@ -807,11 +950,36 @@ export function tooltip(
807
950
  ) {
808
951
  const elements = elementsof(root);
809
952
  const keyGroup = group(elements, groupKey);
953
+ const inInterval = (d) => d.markType === 'interval';
954
+ const isBar = elements.every(inInterval) && !isPolar(coordinate);
955
+ const xof = (d) => d.__data__.x;
956
+ const scaleX = scale.x;
957
+
958
+ // Sort for bisector search.
959
+ if (isBar) elements.sort((a, b) => xof(a) - xof(b));
960
+
961
+ const findElement = isBar
962
+ ? (event) => {
963
+ const mouse = mousePosition(root, event);
964
+ if (!mouse) return;
965
+ const offsetX = scaleX?.getBandWidth ? scaleX.getBandWidth() / 2 : 0;
966
+ const [normalizedX] = coordinate.invert(mouse);
967
+ const abstractX = normalizedX - offsetX;
968
+ const search = bisector(xof).center;
969
+ const i = search(elements, abstractX);
970
+ return elements[i];
971
+ }
972
+ : (event) => {
973
+ const { target } = event;
974
+ return maybeRoot(target, (node) => {
975
+ if (!node.classList) return false;
976
+ return node.classList.includes('element');
977
+ });
978
+ };
810
979
 
811
980
  const pointermove = throttle(
812
981
  (event) => {
813
- const { target } = event;
814
- const element = findElement(target);
982
+ const element = findElement(event);
815
983
  if (!element) {
816
984
  hideTooltip({ root, single, emitter, event });
817
985
  return;
@@ -939,12 +1107,15 @@ export function Tooltip(options) {
939
1107
  const {
940
1108
  shared,
941
1109
  crosshairs,
1110
+ crosshairsX,
1111
+ crosshairsY,
942
1112
  series,
943
1113
  name,
944
1114
  item = () => ({}),
945
1115
  facet = false,
946
1116
  ...rest
947
1117
  } = options;
1118
+
948
1119
  return (target, viewInstances, emitter) => {
949
1120
  const { container, view } = target;
950
1121
  const { scale, markState, coordinate, theme } = view;
@@ -953,6 +1124,7 @@ export function Tooltip(options) {
953
1124
  const defaultShowCrosshairs = interactionKeyof(markState, 'crosshairs');
954
1125
  const plotArea = selectPlotArea(container);
955
1126
  const isSeries = maybeValue(series, defaultSeries);
1127
+ const crosshairsSetting = maybeValue(crosshairs, defaultShowCrosshairs);
956
1128
 
957
1129
  // For non-facet and series tooltip.
958
1130
  if (isSeries && hasSeries(markState) && !facet) {
@@ -962,7 +1134,12 @@ export function Tooltip(options) {
962
1134
  elements: selectG2Elements,
963
1135
  scale,
964
1136
  coordinate,
965
- crosshairs: maybeValue(crosshairs, defaultShowCrosshairs),
1137
+ crosshairs: crosshairsSetting,
1138
+ // the crosshairsX settings level: crosshairsX > crosshairs > false
1139
+ // it means crosshairsX default is false
1140
+ crosshairsX: maybeValue(maybeValue(crosshairsX, crosshairs), false),
1141
+ // crosshairsY default depend on the crossharisSettings
1142
+ crosshairsY: maybeValue(crosshairsY, crosshairsSetting),
966
1143
  item,
967
1144
  emitter,
968
1145
  });
@@ -992,6 +1169,10 @@ export function Tooltip(options) {
992
1169
  scale,
993
1170
  coordinate,
994
1171
  crosshairs: maybeValue(crosshairs, defaultShowCrosshairs),
1172
+ // the crosshairsX settings level: crosshairsX > crosshairs > false
1173
+ // it means crosshairsX default is false
1174
+ crosshairsX: maybeValue(maybeValue(crosshairsX, crosshairs), false),
1175
+ crosshairsY: maybeValue(crosshairsY, crosshairsSetting),
995
1176
  item,
996
1177
  startX,
997
1178
  startY,
@@ -741,6 +741,7 @@ function computeAxisSize(
741
741
  component.labelTransform = 'rotate(90)';
742
742
  component.size = maxLabelWidth + paddingTick;
743
743
  } else {
744
+ component.labelTransform = component.labelTransform ?? 'rotate(0)';
744
745
  component.size = maxLabelHeight + paddingTick;
745
746
  }
746
747
  }
@@ -29,11 +29,11 @@ import {
29
29
  } from './coordinate';
30
30
 
31
31
  export function processAxisZ(components: G2GuideComponentOptions[]) {
32
+ const axisX = components.find(({ type }) => type === 'axisX');
33
+ const axisY = components.find(({ type }) => type === 'axisY');
32
34
  const axisZ = components.find(({ type }) => type === 'axisZ');
33
- if (axisZ) {
34
- const axisX = components.find(({ type }) => type === 'axisX');
35
+ if (axisX && axisY && axisZ) {
35
36
  axisX.plane = 'xy';
36
- const axisY = components.find(({ type }) => type === 'axisY');
37
37
  axisY.plane = 'xy';
38
38
  axisZ.plane = 'yz';
39
39
  axisZ.origin = [axisX.bbox.x, axisX.bbox.y, 0];
@@ -1331,9 +1331,10 @@ function createLabelShapeFunction(
1331
1331
  'shape',
1332
1332
  library,
1333
1333
  );
1334
- const { data: abstractData } = mark;
1334
+ const { data: abstractData, encode } = mark;
1335
1335
  const { data: visualData, defaultLabelShape } = state;
1336
1336
  const point2d = visualData.map((d) => d.points);
1337
+ const channel = mapObject(encode, (d) => d.value);
1337
1338
 
1338
1339
  // Assemble Context.
1339
1340
  const { theme, coordinate } = view;
@@ -1357,7 +1358,7 @@ function createLabelShapeFunction(
1357
1358
  } = options;
1358
1359
  const visualOptions = mapObject(
1359
1360
  { ...abstractOptions, ...abstractStyle } as Record<string, any>,
1360
- (d) => valueOf(d, datum, index, abstractData),
1361
+ (d) => valueOf(d, datum, index, abstractData, { channel }),
1361
1362
  );
1362
1363
  const { shape = defaultLabelShape, text, ...style } = visualOptions;
1363
1364
  const f = typeof formatter === 'string' ? format(formatter) : formatter;
@@ -1377,12 +1378,13 @@ function createLabelShapeFunction(
1377
1378
  }
1378
1379
 
1379
1380
  function valueOf(
1380
- value: Primitive | ((d: any, i: number, array: any) => any),
1381
+ value: Primitive | ((d: any, i: number, array: any, channel: any) => any),
1381
1382
  datum: Record<string, any>,
1382
1383
  i: number,
1383
1384
  data: Record<string, any>,
1385
+ options: { channel: Record<string, any> },
1384
1386
  ) {
1385
- if (typeof value === 'function') return value(datum, i, data);
1387
+ if (typeof value === 'function') return value(datum, i, data, options);
1386
1388
  if (typeof value !== 'string') return value;
1387
1389
  if (isStrictObject(datum) && datum[value] !== undefined) return datum[value];
1388
1390
  return value;
@@ -1497,8 +1499,9 @@ function createMarkShapeFunction(
1497
1499
  'shape',
1498
1500
  library,
1499
1501
  );
1500
- const { data: abstractData } = mark;
1502
+ const { data: abstractData, encode } = mark;
1501
1503
  const { defaultShape, data, shape: shapeLibrary } = state;
1504
+ const channel = mapObject(encode, (d) => d.value);
1502
1505
  const point2d = data.map((d) => d.points);
1503
1506
  const { theme, coordinate } = view;
1504
1507
  const { type: markType, style = {} } = mark;
@@ -1524,7 +1527,7 @@ function createMarkShapeFunction(
1524
1527
 
1525
1528
  const I = seriesIndex ? seriesIndex : i;
1526
1529
  const visualStyle = mapObject(style, (d) =>
1527
- valueOf(d, abstractDatum, I, abstractData),
1530
+ valueOf(d, abstractDatum, I, abstractData, { channel }),
1528
1531
  );
1529
1532
 
1530
1533
  // Try get shape from mark first, then from library.
@@ -260,8 +260,8 @@ export function addGuideToScale(
260
260
  };
261
261
  const axisChannels =
262
262
  typeof axis === 'object'
263
- ? Array.from(new Set(['x', 'y', ...Object.keys(axis)]))
264
- : ['x', 'y'];
263
+ ? Array.from(new Set(['x', 'y', 'z', ...Object.keys(axis)]))
264
+ : ['x', 'y', 'z'];
265
265
 
266
266
  deepMix(mark, {
267
267
  scale: {
@@ -26,9 +26,8 @@ export const Vector: SC<VectorOptions> = (options, context) => {
26
26
  if (arrow) {
27
27
  // Calculate arrow end point.
28
28
  const [arrow1, arrow2] = arrowPoints(from, to, { arrowSize });
29
- path.moveTo(...to);
30
- path.lineTo(...arrow1);
31
- path.moveTo(...to);
29
+ path.moveTo(...arrow1);
30
+ path.lineTo(...to);
32
31
  path.lineTo(...arrow2);
33
32
  }
34
33
 
package/src/theme/dark.ts CHANGED
@@ -9,7 +9,7 @@ export const tokens = {
9
9
  colorWhite: '#000',
10
10
  colorStroke: '#416180',
11
11
  colorDefault: '#1783FF',
12
- colorBackground: '#141414',
12
+ colorBackground: 'transparent',
13
13
  category10: [
14
14
  '#1783FF',
15
15
  '#00C9C9',