@antv/g2 5.1.20 → 5.1.21

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 +1 -1
  4. package/esm/interaction/tooltip.js +120 -19
  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 +1 -1
  19. package/lib/interaction/tooltip.js +119 -18
  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 +178 -18
  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
@@ -442,6 +552,8 @@ export function seriesTooltip(
442
552
  scale,
443
553
  coordinate,
444
554
  crosshairs,
555
+ crosshairsX,
556
+ crosshairsY,
445
557
  render,
446
558
  groupName,
447
559
  emitter,
@@ -469,6 +581,7 @@ export function seriesTooltip(
469
581
  const transposed = isTranspose(coordinate);
470
582
  const polar = isPolar(coordinate);
471
583
  const style = deepMix(_style, rest);
584
+
472
585
  const {
473
586
  innerWidth: plotWidth,
474
587
  innerHeight: plotHeight,
@@ -662,22 +775,50 @@ export function seriesTooltip(
662
775
  });
663
776
  }
664
777
 
665
- if (crosshairs) {
666
- const points = filteredSeriesData.map((d) => d[1]);
778
+ if (crosshairs || crosshairsX || crosshairsY) {
667
779
  const ruleStyle = subObject(style, 'crosshairs');
668
- updateRuleY(root, points, {
780
+
781
+ const ruleStyleX = {
669
782
  ...ruleStyle,
670
- plotWidth,
671
- plotHeight,
672
- mainWidth,
673
- mainHeight,
674
- insetLeft,
675
- insetTop,
676
- startX,
677
- startY,
678
- transposed,
679
- polar,
680
- });
783
+ ...subObject(style, 'crosshairsX'),
784
+ };
785
+ const ruleStyleY = {
786
+ ...ruleStyle,
787
+ ...subObject(style, 'crosshairsY'),
788
+ };
789
+
790
+ const points = filteredSeriesData.map((d) => d[1]);
791
+ if (crosshairsX) {
792
+ updateRuleX(root, points, mouse, {
793
+ ...ruleStyleX,
794
+ plotWidth,
795
+ plotHeight,
796
+ mainWidth,
797
+ mainHeight,
798
+ insetLeft,
799
+ insetTop,
800
+ startX,
801
+ startY,
802
+ transposed,
803
+ polar,
804
+ });
805
+ }
806
+
807
+ if (crosshairsY) {
808
+ updateRuleY(root, points, {
809
+ ...ruleStyleY,
810
+ plotWidth,
811
+ plotHeight,
812
+ mainWidth,
813
+ mainHeight,
814
+ insetLeft,
815
+ insetTop,
816
+ startX,
817
+ startY,
818
+ transposed,
819
+ polar,
820
+ });
821
+ }
681
822
  }
682
823
 
683
824
  if (marker) {
@@ -701,13 +842,19 @@ export function seriesTooltip(
701
842
 
702
843
  const hide = (event: MouseEvent) => {
703
844
  hideTooltip({ root, single, emitter, event });
704
- if (crosshairs) hideRuleY(root);
845
+ if (crosshairs) {
846
+ hideRuleY(root);
847
+ hideRuleX(root);
848
+ }
705
849
  if (marker) hideMarker(root);
706
850
  };
707
851
 
708
852
  const destroy = () => {
709
853
  destroyTooltip({ root, single });
710
- if (crosshairs) hideRuleY(root);
854
+ if (crosshairs) {
855
+ hideRuleY(root);
856
+ hideRuleX(root);
857
+ }
711
858
  if (marker) hideMarker(root);
712
859
  };
713
860
 
@@ -939,12 +1086,15 @@ export function Tooltip(options) {
939
1086
  const {
940
1087
  shared,
941
1088
  crosshairs,
1089
+ crosshairsX,
1090
+ crosshairsY,
942
1091
  series,
943
1092
  name,
944
1093
  item = () => ({}),
945
1094
  facet = false,
946
1095
  ...rest
947
1096
  } = options;
1097
+
948
1098
  return (target, viewInstances, emitter) => {
949
1099
  const { container, view } = target;
950
1100
  const { scale, markState, coordinate, theme } = view;
@@ -953,6 +1103,7 @@ export function Tooltip(options) {
953
1103
  const defaultShowCrosshairs = interactionKeyof(markState, 'crosshairs');
954
1104
  const plotArea = selectPlotArea(container);
955
1105
  const isSeries = maybeValue(series, defaultSeries);
1106
+ const crosshairsSetting = maybeValue(crosshairs, defaultShowCrosshairs);
956
1107
 
957
1108
  // For non-facet and series tooltip.
958
1109
  if (isSeries && hasSeries(markState) && !facet) {
@@ -962,7 +1113,12 @@ export function Tooltip(options) {
962
1113
  elements: selectG2Elements,
963
1114
  scale,
964
1115
  coordinate,
965
- crosshairs: maybeValue(crosshairs, defaultShowCrosshairs),
1116
+ crosshairs: crosshairsSetting,
1117
+ // the crosshairsX settings level: crosshairsX > crosshairs > false
1118
+ // it means crosshairsX default is false
1119
+ crosshairsX: maybeValue(maybeValue(crosshairsX, crosshairs), false),
1120
+ // crosshairsY default depend on the crossharisSettings
1121
+ crosshairsY: maybeValue(crosshairsY, crosshairsSetting),
966
1122
  item,
967
1123
  emitter,
968
1124
  });
@@ -992,6 +1148,10 @@ export function Tooltip(options) {
992
1148
  scale,
993
1149
  coordinate,
994
1150
  crosshairs: maybeValue(crosshairs, defaultShowCrosshairs),
1151
+ // the crosshairsX settings level: crosshairsX > crosshairs > false
1152
+ // it means crosshairsX default is false
1153
+ crosshairsX: maybeValue(maybeValue(crosshairsX, crosshairs), false),
1154
+ crosshairsY: maybeValue(crosshairsY, crosshairsSetting),
995
1155
  item,
996
1156
  startX,
997
1157
  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',