@antv/g2 5.3.3-beta.0 → 5.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/g2.lite.min.js +30 -22
- package/dist/g2.min.js +30 -22
- package/esm/api/runtime.d.ts +10 -0
- package/esm/api/runtime.js +63 -3
- package/esm/api/runtime.js.map +1 -1
- package/esm/data/utils/venn/fmin/bisect.d.ts +3 -0
- package/esm/data/utils/venn/fmin/bisect.js +30 -0
- package/esm/data/utils/venn/fmin/bisect.js.map +1 -0
- package/esm/data/utils/venn/fmin/blas1.d.ts +7 -0
- package/esm/data/utils/venn/fmin/blas1.js +38 -0
- package/esm/data/utils/venn/fmin/blas1.js.map +1 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.d.ts +6 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.js +91 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.js.map +1 -0
- package/esm/data/utils/venn/fmin/gradientDescent.d.ts +10 -0
- package/esm/data/utils/venn/fmin/gradientDescent.js +66 -0
- package/esm/data/utils/venn/fmin/gradientDescent.js.map +1 -0
- package/esm/data/utils/venn/fmin/index.d.ts +34 -0
- package/esm/data/utils/venn/fmin/index.js +35 -0
- package/esm/data/utils/venn/fmin/index.js.map +1 -0
- package/esm/data/utils/venn/fmin/linesearch.d.ts +1 -0
- package/esm/data/utils/venn/fmin/linesearch.js +60 -0
- package/esm/data/utils/venn/fmin/linesearch.js.map +1 -0
- package/esm/data/utils/venn/fmin/nelderMead.d.ts +5 -0
- package/esm/data/utils/venn/fmin/nelderMead.js +133 -0
- package/esm/data/utils/venn/fmin/nelderMead.js.map +1 -0
- package/esm/data/utils/venn/layout.d.ts +2 -2
- package/esm/data/utils/venn/layout.js +1 -1
- package/esm/data/utils/venn/layout.js.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.js +2 -2
- package/esm/index.js.map +1 -1
- package/esm/interaction/elementSelect.js +32 -2
- package/esm/interaction/elementSelect.js.map +1 -1
- package/esm/interaction/tooltip.d.ts +57 -0
- package/esm/interaction/tooltip.js +142 -82
- package/esm/interaction/tooltip.js.map +1 -1
- package/esm/interaction/utils.d.ts +2 -2
- package/esm/interaction/utils.js +10 -2
- package/esm/interaction/utils.js.map +1 -1
- package/esm/mark/line.js +4 -1
- package/esm/mark/line.js.map +1 -1
- package/esm/runtime/component.js +5 -2
- package/esm/runtime/component.js.map +1 -1
- package/esm/runtime/plot.js +6 -3
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/types/common.d.ts +1 -0
- package/esm/shape/connector/connector.js +19 -15
- package/esm/shape/connector/connector.js.map +1 -1
- package/esm/spec/transform.d.ts +1 -0
- package/esm/theme/create.d.ts +0 -1
- package/esm/theme/create.js +0 -1
- package/esm/theme/create.js.map +1 -1
- package/esm/transform/group.d.ts +0 -1
- package/esm/transform/group.js.map +1 -1
- package/esm/transform/groupN.d.ts +1 -1
- package/esm/utils/style.js +11 -2
- package/esm/utils/style.js.map +1 -1
- package/esm/utils/traverse-elements.d.ts +2 -0
- package/esm/utils/traverse-elements.js +13 -0
- package/esm/utils/traverse-elements.js.map +1 -0
- package/lib/api/runtime.d.ts +10 -0
- package/lib/api/runtime.js +70 -10
- package/lib/api/runtime.js.map +1 -1
- package/lib/data/utils/venn/fmin/bisect.d.ts +3 -0
- package/lib/data/utils/venn/fmin/bisect.js +34 -0
- package/lib/data/utils/venn/fmin/bisect.js.map +1 -0
- package/lib/data/utils/venn/fmin/blas1.d.ts +7 -0
- package/lib/data/utils/venn/fmin/blas1.js +48 -0
- package/lib/data/utils/venn/fmin/blas1.js.map +1 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.d.ts +6 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.js +96 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.js.map +1 -0
- package/lib/data/utils/venn/fmin/gradientDescent.d.ts +10 -0
- package/lib/data/utils/venn/fmin/gradientDescent.js +71 -0
- package/lib/data/utils/venn/fmin/gradientDescent.js.map +1 -0
- package/lib/data/utils/venn/fmin/index.d.ts +34 -0
- package/lib/data/utils/venn/fmin/index.js +49 -0
- package/lib/data/utils/venn/fmin/index.js.map +1 -0
- package/lib/data/utils/venn/fmin/linesearch.d.ts +1 -0
- package/lib/data/utils/venn/fmin/linesearch.js +64 -0
- package/lib/data/utils/venn/fmin/linesearch.js.map +1 -0
- package/lib/data/utils/venn/fmin/nelderMead.d.ts +5 -0
- package/lib/data/utils/venn/fmin/nelderMead.js +137 -0
- package/lib/data/utils/venn/fmin/nelderMead.js.map +1 -0
- package/lib/data/utils/venn/layout.d.ts +2 -2
- package/lib/data/utils/venn/layout.js +1 -1
- package/lib/data/utils/venn/layout.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/interaction/elementSelect.js +32 -2
- package/lib/interaction/elementSelect.js.map +1 -1
- package/lib/interaction/tooltip.d.ts +57 -0
- package/lib/interaction/tooltip.js +145 -82
- package/lib/interaction/tooltip.js.map +1 -1
- package/lib/interaction/utils.d.ts +2 -2
- package/lib/interaction/utils.js +10 -2
- package/lib/interaction/utils.js.map +1 -1
- package/lib/mark/line.js +3 -0
- package/lib/mark/line.js.map +1 -1
- package/lib/runtime/component.js +5 -2
- package/lib/runtime/component.js.map +1 -1
- package/lib/runtime/plot.js +6 -3
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/types/common.d.ts +1 -0
- package/lib/shape/connector/connector.js +19 -15
- package/lib/shape/connector/connector.js.map +1 -1
- package/lib/spec/transform.d.ts +1 -0
- package/lib/theme/create.d.ts +0 -1
- package/lib/theme/create.js +0 -1
- package/lib/theme/create.js.map +1 -1
- package/lib/transform/group.d.ts +0 -1
- package/lib/transform/group.js.map +1 -1
- package/lib/transform/groupN.d.ts +1 -1
- package/lib/utils/style.js +11 -2
- package/lib/utils/style.js.map +1 -1
- package/lib/utils/traverse-elements.d.ts +2 -0
- package/lib/utils/traverse-elements.js +17 -0
- package/lib/utils/traverse-elements.js.map +1 -0
- package/package.json +3 -3
- package/src/api/runtime.ts +107 -3
- package/src/data/utils/venn/fmin/bisect.ts +32 -0
- package/src/data/utils/venn/fmin/blas1.ts +42 -0
- package/src/data/utils/venn/fmin/conjugateGradient.ts +106 -0
- package/src/data/utils/venn/fmin/gradientDescent.ts +75 -0
- package/src/data/utils/venn/fmin/index.ts +35 -0
- package/src/data/utils/venn/fmin/linesearch.ts +70 -0
- package/src/data/utils/venn/fmin/nelderMead.ts +147 -0
- package/src/data/utils/venn/layout.ts +1 -1
- package/src/index.ts +2 -2
- package/src/interaction/elementSelect.ts +32 -3
- package/src/interaction/tooltip.ts +213 -132
- package/src/interaction/utils.ts +12 -5
- package/src/mark/line.ts +3 -1
- package/src/runtime/component.ts +5 -3
- package/src/runtime/plot.ts +6 -1
- package/src/runtime/types/common.ts +1 -0
- package/src/shape/connector/connector.ts +30 -19
- package/src/spec/transform.ts +1 -0
- package/src/theme/create.ts +0 -1
- package/src/transform/group.ts +0 -1
- package/src/transform/groupN.ts +1 -1
- package/src/utils/style.ts +11 -1
- package/src/utils/traverse-elements.ts +16 -0
|
@@ -2,6 +2,7 @@ import { Circle, DisplayObject, IElement, Line } from '@antv/g';
|
|
|
2
2
|
import { sort, group, mean, bisector, minIndex } from '@antv/vendor/d3-array';
|
|
3
3
|
import { deepMix, lowerFirst, throttle } from '@antv/util';
|
|
4
4
|
import { Tooltip as TooltipComponent } from '@antv/component';
|
|
5
|
+
import { G2Element } from 'utils/selection';
|
|
5
6
|
import { defined, groupNameOf, subObject, dataOf } from '../utils/helper';
|
|
6
7
|
import { isTranspose, isPolar } from '../utils/coordinate';
|
|
7
8
|
import { angle, sub, dist } from '../utils/vector';
|
|
@@ -504,7 +505,7 @@ function interactionKeyof(markState, key) {
|
|
|
504
505
|
);
|
|
505
506
|
}
|
|
506
507
|
|
|
507
|
-
function maybeValue(specified, defaults) {
|
|
508
|
+
export function maybeValue(specified, defaults) {
|
|
508
509
|
return specified === undefined ? defaults : specified;
|
|
509
510
|
}
|
|
510
511
|
|
|
@@ -522,56 +523,111 @@ function hasSeries(markState): boolean {
|
|
|
522
523
|
}
|
|
523
524
|
|
|
524
525
|
/**
|
|
525
|
-
*
|
|
526
|
+
* Finds a single element based on the mouse event in a non-series context (e.g., single item tooltip).
|
|
527
|
+
* @param root - The root display object of the chart.
|
|
528
|
+
* @param event - The mouse event object (e.g., pointermove, pointerdown).
|
|
529
|
+
* @param elements - Array of chart elements to search within.
|
|
530
|
+
* @param coordinate - The coordinate system of the chart (e.g., Cartesian, polar).
|
|
531
|
+
* @param scale - The scale configurations (e.g., x, series scales).
|
|
532
|
+
* @param shared - Whether the tooltip is shared among multiple elements (e.g., grouped bars).
|
|
533
|
+
* @returns The matched display object or `undefined` if no element is found.
|
|
534
|
+
* @description
|
|
535
|
+
* - Handles bar charts by sorting elements and using bisector search for efficient lookup.
|
|
536
|
+
* - For non-bar charts, directly finds the target element from the event's target.
|
|
537
|
+
* - Adjusts for bar spacing in grouped charts when `shared` is false.
|
|
526
538
|
*/
|
|
527
|
-
export function
|
|
528
|
-
root
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
mount,
|
|
551
|
-
bounding,
|
|
552
|
-
theme,
|
|
553
|
-
offset,
|
|
554
|
-
disableNative = false,
|
|
555
|
-
marker = true,
|
|
556
|
-
preserve = false,
|
|
557
|
-
style: _style = {},
|
|
558
|
-
css = {},
|
|
559
|
-
...rest
|
|
560
|
-
}: Record<string, any>,
|
|
561
|
-
) {
|
|
562
|
-
const elements = elementsof(root);
|
|
563
|
-
const transposed = isTranspose(coordinate);
|
|
564
|
-
const polar = isPolar(coordinate);
|
|
565
|
-
const style = deepMix(_style, rest);
|
|
539
|
+
export function findSingleElement({
|
|
540
|
+
root,
|
|
541
|
+
event,
|
|
542
|
+
elements,
|
|
543
|
+
coordinate,
|
|
544
|
+
scale,
|
|
545
|
+
shared,
|
|
546
|
+
}): DisplayObject | undefined {
|
|
547
|
+
const inInterval = (d) => d.markType === 'interval';
|
|
548
|
+
const isBar = elements.every(inInterval) && !isPolar(coordinate);
|
|
549
|
+
const scaleX = scale.x;
|
|
550
|
+
const scaleSeries = scale.series;
|
|
551
|
+
const bandWidth = scaleX?.getBandWidth?.() ?? 0;
|
|
552
|
+
const xof = scaleSeries
|
|
553
|
+
? (d) => {
|
|
554
|
+
const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
|
|
555
|
+
return (
|
|
556
|
+
d.__data__.x +
|
|
557
|
+
d.__data__.series * bandWidth +
|
|
558
|
+
bandWidth / (seriesCount * 2)
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
: (d) => d.__data__.x + bandWidth / 2;
|
|
566
562
|
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
563
|
+
// Sort for bisector search.
|
|
564
|
+
if (isBar) elements.sort((a, b) => xof(a) - xof(b));
|
|
565
|
+
const findElementByTarget = (event) => {
|
|
566
|
+
const { target } = event;
|
|
567
|
+
return maybeRoot(target, (node) => {
|
|
568
|
+
if (!node.classList) return false;
|
|
569
|
+
return node.classList.includes('element');
|
|
570
|
+
});
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const element = isBar
|
|
574
|
+
? (event) => {
|
|
575
|
+
const mouse = mousePosition(root, event);
|
|
576
|
+
if (!mouse) return;
|
|
577
|
+
const [abstractX] = coordinate.invert(mouse);
|
|
578
|
+
const search = bisector(xof).center;
|
|
579
|
+
const i = search(elements, abstractX);
|
|
580
|
+
const target = elements[i];
|
|
581
|
+
|
|
582
|
+
if (!shared) {
|
|
583
|
+
// For grouped bar chart without shared options.
|
|
584
|
+
const isGrouped = elements.find(
|
|
585
|
+
(d) => d !== target && xof(d) === xof(target),
|
|
586
|
+
);
|
|
587
|
+
if (isGrouped) return findElementByTarget(event);
|
|
588
|
+
}
|
|
589
|
+
return target;
|
|
590
|
+
}
|
|
591
|
+
: findElementByTarget;
|
|
592
|
+
|
|
593
|
+
return element(event);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Finds series-related elements and data based on the mouse event for series tooltips.
|
|
598
|
+
* @param root - The root display object of the chart.
|
|
599
|
+
* @param event - The mouse event object (e.g., pointermove, pointerdown).
|
|
600
|
+
* @param elements - Array of chart elements to search within.
|
|
601
|
+
* @param coordinate - The coordinate system of the chart (e.g., Cartesian, polar).
|
|
602
|
+
* @param scale - The scale configurations (e.g., x, series scales).
|
|
603
|
+
* @param startX - The starting X position of the plot area.
|
|
604
|
+
* @param startY - The starting Y position of the plot area.
|
|
605
|
+
* @returns An object containing:
|
|
606
|
+
* - `selectedElements`: Matched display objects (series and item elements).
|
|
607
|
+
* - `selectedData`: Corresponding data records of the selected elements.
|
|
608
|
+
* - `filteredSeriesData`: Filtered series data closest to the mouse focus.
|
|
609
|
+
* - `abstractX`: A function to convert mouse coordinates to abstract X values.
|
|
610
|
+
* @description
|
|
611
|
+
* - Splits elements into series and item elements for targeted searching.
|
|
612
|
+
* - Handles bar charts and band scales using bisector search and coordinate inversion.
|
|
613
|
+
* - Sorts elements to ensure correct visual ordering (top-to-bottom or right-to-left in transposed mode).
|
|
614
|
+
* - Filters and groups data to provide accurate tooltip information for series.
|
|
615
|
+
*/
|
|
616
|
+
export function findSeriesElement({
|
|
617
|
+
root,
|
|
618
|
+
event,
|
|
619
|
+
elements,
|
|
620
|
+
coordinate,
|
|
621
|
+
scale,
|
|
622
|
+
startX,
|
|
623
|
+
startY,
|
|
624
|
+
}): {
|
|
625
|
+
selectedElements: DisplayObject[];
|
|
626
|
+
selectedData: Record<string, any>[];
|
|
627
|
+
filteredSeriesData: any[];
|
|
628
|
+
abstractX: (number) => number;
|
|
629
|
+
} {
|
|
630
|
+
const transposed = isTranspose(coordinate);
|
|
575
631
|
|
|
576
632
|
// Split elements into series elements and item elements.
|
|
577
633
|
const seriesElements = [];
|
|
@@ -698,7 +754,97 @@ export function seriesTooltip(
|
|
|
698
754
|
}),
|
|
699
755
|
);
|
|
700
756
|
};
|
|
757
|
+
const mouse = mousePosition(root, event);
|
|
758
|
+
if (!mouse) return;
|
|
759
|
+
const focus = [mouse[0] - startX, mouse[1] - startY];
|
|
760
|
+
if (!focus) return;
|
|
761
|
+
// Get selected item element.
|
|
762
|
+
const selectedItems = elementsByFocus(focus, itemElements);
|
|
763
|
+
|
|
764
|
+
// Get selected data item from both series element and item element.
|
|
765
|
+
const selectedSeriesElements = [];
|
|
766
|
+
const selectedSeriesData = [];
|
|
767
|
+
for (const element of seriesElements) {
|
|
768
|
+
const [sortedIndex, X] = elementSortedX.get(element);
|
|
769
|
+
const index = indexByFocus(event, focus, sortedIndex, X);
|
|
770
|
+
if (index !== null) {
|
|
771
|
+
selectedSeriesElements.push(element);
|
|
772
|
+
const d = seriesData(element, index);
|
|
773
|
+
const { x, y } = d;
|
|
774
|
+
const p = coordinate.map([(x || 0) + offsetX, y || 0]);
|
|
775
|
+
selectedSeriesData.push([{ ...d, element }, p] as const);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// Filter selectedSeriesData with different x,
|
|
780
|
+
// make sure there is only one x closest to focusX.
|
|
781
|
+
const SX = Array.from(new Set(selectedSeriesData.map((d) => d[0].x)));
|
|
782
|
+
const closestX = SX[minIndex(SX, (x) => Math.abs(x - abstractX(focus)))];
|
|
783
|
+
const filteredSeriesData = selectedSeriesData.filter(
|
|
784
|
+
(d) => d[0].x === closestX,
|
|
785
|
+
);
|
|
786
|
+
|
|
787
|
+
const selectedData = [
|
|
788
|
+
...filteredSeriesData.map((d) => d[0]),
|
|
789
|
+
...selectedItems.map((d) => d.__data__),
|
|
790
|
+
];
|
|
791
|
+
|
|
792
|
+
// Get the displayed tooltip data.
|
|
793
|
+
const selectedElements = [...selectedSeriesElements, ...selectedItems];
|
|
701
794
|
|
|
795
|
+
return { selectedElements, selectedData, filteredSeriesData, abstractX };
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Show tooltip for series item.
|
|
800
|
+
*/
|
|
801
|
+
export function seriesTooltip(
|
|
802
|
+
root: DisplayObject,
|
|
803
|
+
{
|
|
804
|
+
elements: elementsof,
|
|
805
|
+
sort: sortFunction,
|
|
806
|
+
filter: filterFunction,
|
|
807
|
+
scale,
|
|
808
|
+
coordinate,
|
|
809
|
+
crosshairs,
|
|
810
|
+
crosshairsX,
|
|
811
|
+
crosshairsY,
|
|
812
|
+
render,
|
|
813
|
+
groupName,
|
|
814
|
+
emitter,
|
|
815
|
+
wait = 50,
|
|
816
|
+
leading = true,
|
|
817
|
+
trailing = false,
|
|
818
|
+
startX = 0,
|
|
819
|
+
startY = 0,
|
|
820
|
+
body = true,
|
|
821
|
+
single = true,
|
|
822
|
+
position,
|
|
823
|
+
enterable,
|
|
824
|
+
mount,
|
|
825
|
+
bounding,
|
|
826
|
+
theme,
|
|
827
|
+
offset,
|
|
828
|
+
disableNative = false,
|
|
829
|
+
marker = true,
|
|
830
|
+
preserve = false,
|
|
831
|
+
style: _style = {},
|
|
832
|
+
css = {},
|
|
833
|
+
...rest
|
|
834
|
+
}: Record<string, any>,
|
|
835
|
+
) {
|
|
836
|
+
const elements = elementsof(root);
|
|
837
|
+
const style = deepMix(_style, rest);
|
|
838
|
+
const polar = isPolar(coordinate);
|
|
839
|
+
const transposed = isTranspose(coordinate);
|
|
840
|
+
const {
|
|
841
|
+
innerWidth: plotWidth,
|
|
842
|
+
innerHeight: plotHeight,
|
|
843
|
+
width: mainWidth,
|
|
844
|
+
height: mainHeight,
|
|
845
|
+
insetLeft,
|
|
846
|
+
insetTop,
|
|
847
|
+
} = coordinate.getOptions();
|
|
702
848
|
const update = throttle(
|
|
703
849
|
(event) => {
|
|
704
850
|
const mouse = mousePosition(root, event);
|
|
@@ -706,41 +852,16 @@ export function seriesTooltip(
|
|
|
706
852
|
const bbox = bboxOf(root);
|
|
707
853
|
const x = bbox.min[0];
|
|
708
854
|
const y = bbox.min[1];
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
const index = indexByFocus(event, focus, sortedIndex, X);
|
|
720
|
-
if (index !== null) {
|
|
721
|
-
selectedSeriesElements.push(element);
|
|
722
|
-
const d = seriesData(element, index);
|
|
723
|
-
const { x, y } = d;
|
|
724
|
-
const p = coordinate.map([(x || 0) + offsetX, y || 0]);
|
|
725
|
-
selectedSeriesData.push([{ ...d, element }, p] as const);
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
// Filter selectedSeriesData with different x,
|
|
730
|
-
// make sure there is only one x closest to focusX.
|
|
731
|
-
const SX = Array.from(new Set(selectedSeriesData.map((d) => d[0].x)));
|
|
732
|
-
const closestX = SX[minIndex(SX, (x) => Math.abs(x - abstractX(focus)))];
|
|
733
|
-
const filteredSeriesData = selectedSeriesData.filter(
|
|
734
|
-
(d) => d[0].x === closestX,
|
|
735
|
-
);
|
|
736
|
-
|
|
737
|
-
const selectedData = [
|
|
738
|
-
...filteredSeriesData.map((d) => d[0]),
|
|
739
|
-
...selectedItems.map((d) => d.__data__),
|
|
740
|
-
];
|
|
741
|
-
|
|
742
|
-
// Get the displayed tooltip data.
|
|
743
|
-
const selectedElements = [...selectedSeriesElements, ...selectedItems];
|
|
855
|
+
const { selectedElements, selectedData, filteredSeriesData, abstractX } =
|
|
856
|
+
findSeriesElement({
|
|
857
|
+
root,
|
|
858
|
+
event,
|
|
859
|
+
elements,
|
|
860
|
+
coordinate,
|
|
861
|
+
scale,
|
|
862
|
+
startX,
|
|
863
|
+
startY,
|
|
864
|
+
});
|
|
744
865
|
const tooltipData = groupItems(
|
|
745
866
|
selectedElements,
|
|
746
867
|
scale,
|
|
@@ -973,56 +1094,16 @@ export function tooltip(
|
|
|
973
1094
|
) {
|
|
974
1095
|
const elements = elementsof(root);
|
|
975
1096
|
const keyGroup = group(elements, groupKey);
|
|
976
|
-
const inInterval = (d) => d.markType === 'interval';
|
|
977
|
-
const isBar = elements.every(inInterval) && !isPolar(coordinate);
|
|
978
|
-
const scaleX = scale.x;
|
|
979
|
-
const scaleSeries = scale.series;
|
|
980
|
-
const bandWidth = scaleX?.getBandWidth?.() ?? 0;
|
|
981
|
-
const xof = scaleSeries
|
|
982
|
-
? (d) => {
|
|
983
|
-
const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
|
|
984
|
-
return (
|
|
985
|
-
d.__data__.x +
|
|
986
|
-
d.__data__.series * bandWidth +
|
|
987
|
-
bandWidth / (seriesCount * 2)
|
|
988
|
-
);
|
|
989
|
-
}
|
|
990
|
-
: (d) => d.__data__.x + bandWidth / 2;
|
|
991
|
-
|
|
992
|
-
// Sort for bisector search.
|
|
993
|
-
if (isBar) elements.sort((a, b) => xof(a) - xof(b));
|
|
994
|
-
|
|
995
|
-
const findElementByTarget = (event) => {
|
|
996
|
-
const { target } = event;
|
|
997
|
-
return maybeRoot(target, (node) => {
|
|
998
|
-
if (!node.classList) return false;
|
|
999
|
-
return node.classList.includes('element');
|
|
1000
|
-
});
|
|
1001
|
-
};
|
|
1002
|
-
|
|
1003
|
-
const findElement = isBar
|
|
1004
|
-
? (event) => {
|
|
1005
|
-
const mouse = mousePosition(root, event);
|
|
1006
|
-
if (!mouse) return;
|
|
1007
|
-
const [abstractX] = coordinate.invert(mouse);
|
|
1008
|
-
const search = bisector(xof).center;
|
|
1009
|
-
const i = search(elements, abstractX);
|
|
1010
|
-
const target = elements[i];
|
|
1011
|
-
|
|
1012
|
-
if (!shared) {
|
|
1013
|
-
// For grouped bar chart without shared options.
|
|
1014
|
-
const isGrouped = elements.find(
|
|
1015
|
-
(d) => d !== target && xof(d) === xof(target),
|
|
1016
|
-
);
|
|
1017
|
-
if (isGrouped) return findElementByTarget(event);
|
|
1018
|
-
}
|
|
1019
|
-
return target;
|
|
1020
|
-
}
|
|
1021
|
-
: findElementByTarget;
|
|
1022
|
-
|
|
1023
1097
|
const pointermove = throttle(
|
|
1024
1098
|
(event) => {
|
|
1025
|
-
const element =
|
|
1099
|
+
const element = findSingleElement({
|
|
1100
|
+
root,
|
|
1101
|
+
event,
|
|
1102
|
+
elements,
|
|
1103
|
+
coordinate,
|
|
1104
|
+
scale,
|
|
1105
|
+
shared,
|
|
1106
|
+
});
|
|
1026
1107
|
if (!element) {
|
|
1027
1108
|
hideTooltip({ root, single, emitter, event });
|
|
1028
1109
|
return;
|
package/src/interaction/utils.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { isPolar, isTranspose } from '../utils/coordinate';
|
|
|
17
17
|
import { getStyle } from '../utils/style';
|
|
18
18
|
import { reorder } from '../shape/utils';
|
|
19
19
|
import { angle, angleBetween, sub } from '../utils/vector';
|
|
20
|
+
import { traverseElements } from '../utils/traverse-elements';
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Given root of chart returns elements to be manipulated
|
|
@@ -139,6 +140,14 @@ const STATE_GROUPS = {
|
|
|
139
140
|
highlight: ['active', 'inactive'],
|
|
140
141
|
};
|
|
141
142
|
|
|
143
|
+
const setElementAttribute = (element: DisplayObject, k: string, v: string) => {
|
|
144
|
+
traverseElements(element, (el) => {
|
|
145
|
+
if ('setAttribute' in el && typeof el.setAttribute === 'function') {
|
|
146
|
+
(el as DisplayObject).setAttribute(k, v);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
142
151
|
export function createUseState(
|
|
143
152
|
style: Record<string, any>,
|
|
144
153
|
elements: Element[],
|
|
@@ -157,16 +166,14 @@ export function createUseState(
|
|
|
157
166
|
}
|
|
158
167
|
});
|
|
159
168
|
|
|
160
|
-
return (
|
|
161
|
-
|
|
162
|
-
setAttribute = (element, key, v) => element.setAttribute(key, v),
|
|
163
|
-
) => useState(undefined, valueof, setAttribute);
|
|
169
|
+
return (valueof = (d, element) => d, setAttribute = setElementAttribute) =>
|
|
170
|
+
useState(undefined, valueof, setAttribute);
|
|
164
171
|
}
|
|
165
172
|
|
|
166
173
|
export function useState(
|
|
167
174
|
style: Record<string, any> | undefined,
|
|
168
175
|
valueof = (d, element) => d,
|
|
169
|
-
setAttribute =
|
|
176
|
+
setAttribute = setElementAttribute,
|
|
170
177
|
) {
|
|
171
178
|
const STATES = '__states__';
|
|
172
179
|
const ORIGINAL = '__ordinal__';
|
package/src/mark/line.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Vector } from '@antv/coord';
|
|
2
2
|
import { group } from '@antv/vendor/d3-array';
|
|
3
|
-
import { isParallel } from '../utils/coordinate';
|
|
3
|
+
import { isParallel, isPolar } from '../utils/coordinate';
|
|
4
4
|
import { Mark, MarkComponent as MC, SingleMark, Vector2 } from '../runtime';
|
|
5
5
|
import { LineMark } from '../spec';
|
|
6
6
|
import {
|
|
@@ -68,6 +68,8 @@ const parallel: Mark = (index, scale, value, coordinate) => {
|
|
|
68
68
|
if (PV.length === 0) {
|
|
69
69
|
throw new Error('Missing encode for position channel.');
|
|
70
70
|
}
|
|
71
|
+
// Close the loop for radar(= parallel + polar) coordinate.
|
|
72
|
+
if (isPolar(coordinate)) PV.push(PV[0]);
|
|
71
73
|
|
|
72
74
|
// One data corresponds to one line.
|
|
73
75
|
const P = Array.from(index, (i) => {
|
package/src/runtime/component.ts
CHANGED
|
@@ -439,12 +439,14 @@ function inferAxisPositionAndOrientation(
|
|
|
439
439
|
scale.name.startsWith('position'),
|
|
440
440
|
);
|
|
441
441
|
const index = matchPosition(name);
|
|
442
|
-
if (
|
|
443
|
-
return [null, null];
|
|
442
|
+
if (index === null) return [null, null];
|
|
444
443
|
// infer radar axis orientation
|
|
445
444
|
const [startAngle, endAngle] = angleOf(coordinates);
|
|
445
|
+
const positionLength = isRadar(coordinates)
|
|
446
|
+
? positions.length
|
|
447
|
+
: positions.length - 1;
|
|
446
448
|
const angle =
|
|
447
|
-
((endAngle - startAngle) /
|
|
449
|
+
((endAngle - startAngle) / positionLength) * index + startAngle;
|
|
448
450
|
return ['center', angle];
|
|
449
451
|
}
|
|
450
452
|
|
package/src/runtime/plot.ts
CHANGED
|
@@ -789,6 +789,7 @@ function initializeState(
|
|
|
789
789
|
// the same style with JS standard and lodash APIs.
|
|
790
790
|
// @todo More proper way to index scale for different marks.
|
|
791
791
|
const children = [];
|
|
792
|
+
const dataMap = new Map();
|
|
792
793
|
for (const [mark, state] of markState.entries()) {
|
|
793
794
|
const {
|
|
794
795
|
// scale,
|
|
@@ -799,7 +800,9 @@ function initializeState(
|
|
|
799
800
|
dataDomain,
|
|
800
801
|
modifier,
|
|
801
802
|
key: markKey,
|
|
803
|
+
data,
|
|
802
804
|
} = mark;
|
|
805
|
+
dataMap.set(markKey, data);
|
|
803
806
|
const { index, channels, tooltip } = state;
|
|
804
807
|
const scale = Object.fromEntries(
|
|
805
808
|
channels.map(({ name, scale }) => [name, scale]),
|
|
@@ -828,6 +831,7 @@ function initializeState(
|
|
|
828
831
|
index: d,
|
|
829
832
|
markKey,
|
|
830
833
|
viewKey: key,
|
|
834
|
+
data: data[d],
|
|
831
835
|
...(tooltip && {
|
|
832
836
|
title: titleOf(d),
|
|
833
837
|
items: itemsOf(d),
|
|
@@ -867,6 +871,7 @@ function initializeState(
|
|
|
867
871
|
scale: scaleInstance,
|
|
868
872
|
style: framedStyle,
|
|
869
873
|
components,
|
|
874
|
+
data: dataMap,
|
|
870
875
|
labelTransform: compose(labelTransform.map(useLabelTransform)),
|
|
871
876
|
};
|
|
872
877
|
|
|
@@ -1318,7 +1323,7 @@ function normalizeLabelSelector(
|
|
|
1318
1323
|
* transitions together.
|
|
1319
1324
|
*/
|
|
1320
1325
|
function getLocalBounds(element: DisplayObject) {
|
|
1321
|
-
const cloneElement = element.cloneNode();
|
|
1326
|
+
const cloneElement = element.cloneNode(true);
|
|
1322
1327
|
const animations = element.getAnimations();
|
|
1323
1328
|
cloneElement.style.visibility = 'hidden';
|
|
1324
1329
|
animations.forEach((animation) => {
|
|
@@ -35,40 +35,49 @@ function inferConnectorPath(points: Vector2[]) {
|
|
|
35
35
|
function getPoints(
|
|
36
36
|
coordinate: Coordinate,
|
|
37
37
|
points: Vector2[],
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
sourceOffsetY: number,
|
|
39
|
+
targetOffsetY: number,
|
|
40
|
+
sourceOffsetX: number,
|
|
41
|
+
targetOffsetX: number,
|
|
40
42
|
length1 = 0,
|
|
41
43
|
): Vector2[] {
|
|
42
44
|
const [[x0, y0], [x1, y1]] = points;
|
|
43
45
|
|
|
44
46
|
if (isTranspose(coordinate)) {
|
|
45
|
-
const X0 = x0 +
|
|
46
|
-
const X1 = x1 +
|
|
47
|
+
const X0 = x0 + sourceOffsetY;
|
|
48
|
+
const X1 = x1 + targetOffsetY;
|
|
47
49
|
const X = X0 + length1;
|
|
50
|
+
const Y0 = y0 + sourceOffsetX;
|
|
51
|
+
const Y1 = y1 + targetOffsetX;
|
|
48
52
|
return [
|
|
49
|
-
[X0,
|
|
50
|
-
[X,
|
|
51
|
-
[X,
|
|
52
|
-
[X1,
|
|
53
|
+
[X0, Y0],
|
|
54
|
+
[X, Y0],
|
|
55
|
+
[X, Y1],
|
|
56
|
+
[X1, Y1],
|
|
53
57
|
];
|
|
54
58
|
}
|
|
55
59
|
|
|
56
|
-
const Y0 = y0 -
|
|
57
|
-
const Y1 = y1 -
|
|
60
|
+
const Y0 = y0 - sourceOffsetY;
|
|
61
|
+
const Y1 = y1 - targetOffsetY;
|
|
58
62
|
const Y = Y0 - length1;
|
|
63
|
+
const X0 = x0 - sourceOffsetX;
|
|
64
|
+
const X1 = x1 - targetOffsetX;
|
|
59
65
|
return [
|
|
60
|
-
[
|
|
61
|
-
[
|
|
62
|
-
[
|
|
63
|
-
[
|
|
66
|
+
[X0, Y0],
|
|
67
|
+
[X0, Y],
|
|
68
|
+
[X1, Y],
|
|
69
|
+
[X1, Y1],
|
|
64
70
|
];
|
|
65
71
|
}
|
|
66
72
|
|
|
67
73
|
export const Connector: SC<ConnectorOptions> = (options, context) => {
|
|
68
74
|
const {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
75
|
+
offsetX = 0,
|
|
76
|
+
sourceOffsetX = offsetX,
|
|
77
|
+
targetOffsetX = offsetX,
|
|
78
|
+
offsetY = 0,
|
|
79
|
+
sourceOffsetY = offsetY,
|
|
80
|
+
targetOffsetY = offsetY,
|
|
72
81
|
connectLength1: length1,
|
|
73
82
|
endMarker = true,
|
|
74
83
|
...style
|
|
@@ -81,8 +90,10 @@ export const Connector: SC<ConnectorOptions> = (options, context) => {
|
|
|
81
90
|
const P = getPoints(
|
|
82
91
|
coordinate,
|
|
83
92
|
points,
|
|
84
|
-
|
|
85
|
-
|
|
93
|
+
sourceOffsetY,
|
|
94
|
+
targetOffsetY,
|
|
95
|
+
sourceOffsetX,
|
|
96
|
+
targetOffsetX,
|
|
86
97
|
length1 ?? connectLength1,
|
|
87
98
|
);
|
|
88
99
|
const makerStyle = subObject({ ...style, ...defaults }, 'endMarker');
|
package/src/spec/transform.ts
CHANGED
package/src/theme/create.ts
CHANGED
package/src/transform/group.ts
CHANGED
package/src/transform/groupN.ts
CHANGED
package/src/utils/style.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { DisplayObject, BaseStyleProps } from '@antv/g';
|
|
2
|
+
import { traverseElements } from './traverse-elements';
|
|
2
3
|
|
|
3
4
|
const defaultStyle: BaseStyleProps = {
|
|
4
5
|
visibility: 'visible',
|
|
@@ -8,7 +9,16 @@ const defaultStyle: BaseStyleProps = {
|
|
|
8
9
|
};
|
|
9
10
|
|
|
10
11
|
export function getStyle(element: DisplayObject, key: string) {
|
|
11
|
-
|
|
12
|
+
let value: any;
|
|
13
|
+
traverseElements(element, (el) => {
|
|
14
|
+
if (el.tagName !== 'g' && el.style?.[key] !== undefined) {
|
|
15
|
+
value = el.style[key];
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return value ?? defaultStyle[key];
|
|
12
22
|
}
|
|
13
23
|
|
|
14
24
|
export function setStyle(
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DisplayObject } from '@antv/g';
|
|
2
|
+
|
|
3
|
+
export function traverseElements(
|
|
4
|
+
element: DisplayObject,
|
|
5
|
+
visitor: (el: DisplayObject) => boolean | void,
|
|
6
|
+
): boolean {
|
|
7
|
+
if (visitor(element)) return true;
|
|
8
|
+
if (element.tagName === 'g') {
|
|
9
|
+
const { childNodes = [] } = element;
|
|
10
|
+
for (const child of childNodes) {
|
|
11
|
+
if (traverseElements(child as DisplayObject, visitor)) return true;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return false;
|
|
16
|
+
}
|