@antv/g2 5.3.5 → 5.4.0
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 +24 -24
- package/dist/g2.min.js +24 -24
- package/esm/animation/morphing.js +11 -6
- package/esm/animation/morphing.js.map +1 -1
- package/esm/component/axis.d.ts +1 -0
- package/esm/component/axis.js +5 -5
- package/esm/component/axis.js.map +1 -1
- package/esm/composition/geoView.js +6 -1
- package/esm/composition/geoView.js.map +1 -1
- package/esm/interaction/legendFilter.d.ts +2 -0
- package/esm/interaction/legendFilter.js +56 -0
- package/esm/interaction/legendFilter.js.map +1 -1
- package/esm/interaction/scrollbarFilter.js +4 -1
- package/esm/interaction/scrollbarFilter.js.map +1 -1
- package/esm/interaction/sliderFilter.js +9 -19
- package/esm/interaction/sliderFilter.js.map +1 -1
- package/esm/interaction/tooltip.js +58 -11
- package/esm/interaction/tooltip.js.map +1 -1
- package/esm/label-transform/exceedAdjust.js +7 -1
- package/esm/label-transform/exceedAdjust.js.map +1 -1
- package/esm/lib/builtinlib.d.ts +4 -0
- package/esm/lib/builtinlib.js +2 -1
- package/esm/lib/builtinlib.js.map +1 -1
- package/esm/mark/interval.js +3 -1
- package/esm/mark/interval.js.map +1 -1
- package/esm/mark/sankey.js +29 -0
- package/esm/mark/sankey.js.map +1 -1
- package/esm/mark/utils.js +31 -1
- package/esm/mark/utils.js.map +1 -1
- package/esm/runtime/component.js +17 -6
- package/esm/runtime/component.js.map +1 -1
- package/esm/runtime/constant.d.ts +2 -0
- package/esm/runtime/constant.js +2 -0
- package/esm/runtime/constant.js.map +1 -1
- package/esm/runtime/mark.js +2 -1
- package/esm/runtime/mark.js.map +1 -1
- package/esm/runtime/option-preprocess/axis-breaks.d.ts +6 -0
- package/esm/runtime/option-preprocess/axis-breaks.js +25 -0
- package/esm/runtime/option-preprocess/axis-breaks.js.map +1 -0
- package/esm/runtime/option-preprocess/index.js +2 -1
- package/esm/runtime/option-preprocess/index.js.map +1 -1
- package/esm/runtime/plot.js +59 -8
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/render.js +1 -0
- package/esm/runtime/render.js.map +1 -1
- package/esm/runtime/transform.d.ts +1 -0
- package/esm/runtime/transform.js +9 -0
- package/esm/runtime/transform.js.map +1 -1
- package/esm/runtime/types/options.d.ts +1 -0
- package/esm/shape/axis-breaks/axis-breaks.d.ts +19 -0
- package/esm/shape/axis-breaks/axis-breaks.js +153 -0
- package/esm/shape/axis-breaks/axis-breaks.js.map +1 -0
- package/esm/shape/index.d.ts +2 -0
- package/esm/shape/index.js +1 -0
- package/esm/shape/index.js.map +1 -1
- package/esm/spec/labelTransform.d.ts +1 -1
- package/esm/theme/create.d.ts +1 -0
- package/esm/theme/create.js +2 -1
- package/esm/theme/create.js.map +1 -1
- package/esm/utils/scale.d.ts +1 -0
- package/esm/utils/scale.js +20 -0
- package/esm/utils/scale.js.map +1 -1
- package/esm/utils/selection.js +7 -2
- package/esm/utils/selection.js.map +1 -1
- package/lib/animation/morphing.js +10 -5
- package/lib/animation/morphing.js.map +1 -1
- package/lib/component/axis.d.ts +1 -0
- package/lib/component/axis.js +5 -5
- package/lib/component/axis.js.map +1 -1
- package/lib/composition/geoView.js +6 -1
- package/lib/composition/geoView.js.map +1 -1
- package/lib/interaction/legendFilter.d.ts +2 -0
- package/lib/interaction/legendFilter.js +58 -1
- package/lib/interaction/legendFilter.js.map +1 -1
- package/lib/interaction/scrollbarFilter.js +4 -1
- package/lib/interaction/scrollbarFilter.js.map +1 -1
- package/lib/interaction/sliderFilter.js +7 -17
- package/lib/interaction/sliderFilter.js.map +1 -1
- package/lib/interaction/tooltip.js +57 -10
- package/lib/interaction/tooltip.js.map +1 -1
- package/lib/label-transform/exceedAdjust.js +7 -1
- package/lib/label-transform/exceedAdjust.js.map +1 -1
- package/lib/lib/builtinlib.d.ts +4 -0
- package/lib/lib/builtinlib.js +1 -0
- package/lib/lib/builtinlib.js.map +1 -1
- package/lib/mark/interval.js +3 -1
- package/lib/mark/interval.js.map +1 -1
- package/lib/mark/sankey.js +29 -0
- package/lib/mark/sankey.js.map +1 -1
- package/lib/mark/utils.js +31 -1
- package/lib/mark/utils.js.map +1 -1
- package/lib/runtime/component.js +17 -6
- package/lib/runtime/component.js.map +1 -1
- package/lib/runtime/constant.d.ts +2 -0
- package/lib/runtime/constant.js +3 -1
- package/lib/runtime/constant.js.map +1 -1
- package/lib/runtime/mark.js +1 -0
- package/lib/runtime/mark.js.map +1 -1
- package/lib/runtime/option-preprocess/axis-breaks.d.ts +6 -0
- package/lib/runtime/option-preprocess/axis-breaks.js +30 -0
- package/lib/runtime/option-preprocess/axis-breaks.js.map +1 -0
- package/lib/runtime/option-preprocess/index.js +2 -1
- package/lib/runtime/option-preprocess/index.js.map +1 -1
- package/lib/runtime/plot.js +57 -6
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/render.js +1 -0
- package/lib/runtime/render.js.map +1 -1
- package/lib/runtime/transform.d.ts +1 -0
- package/lib/runtime/transform.js +11 -1
- package/lib/runtime/transform.js.map +1 -1
- package/lib/runtime/types/options.d.ts +1 -0
- package/lib/shape/axis-breaks/axis-breaks.d.ts +19 -0
- package/lib/shape/axis-breaks/axis-breaks.js +157 -0
- package/lib/shape/axis-breaks/axis-breaks.js.map +1 -0
- package/lib/shape/index.d.ts +2 -0
- package/lib/shape/index.js +3 -1
- package/lib/shape/index.js.map +1 -1
- package/lib/spec/labelTransform.d.ts +1 -1
- package/lib/theme/create.d.ts +1 -0
- package/lib/theme/create.js +2 -1
- package/lib/theme/create.js.map +1 -1
- package/lib/utils/scale.d.ts +1 -0
- package/lib/utils/scale.js +22 -1
- package/lib/utils/scale.js.map +1 -1
- package/lib/utils/selection.js +7 -2
- package/lib/utils/selection.js.map +1 -1
- package/package.json +5 -5
- package/src/animation/morphing.ts +33 -10
- package/src/component/axis.ts +5 -0
- package/src/composition/geoView.ts +6 -1
- package/src/interaction/legendFilter.ts +66 -0
- package/src/interaction/scrollbarFilter.ts +5 -1
- package/src/interaction/sliderFilter.ts +8 -24
- package/src/interaction/tooltip.ts +75 -20
- package/src/label-transform/exceedAdjust.ts +9 -2
- package/src/lib/builtinlib.ts +2 -1
- package/src/mark/interval.ts +3 -1
- package/src/mark/sankey.ts +30 -0
- package/src/mark/utils.ts +38 -1
- package/src/runtime/component.ts +20 -3
- package/src/runtime/constant.ts +2 -0
- package/src/runtime/mark.ts +2 -0
- package/src/runtime/option-preprocess/axis-breaks.ts +36 -0
- package/src/runtime/option-preprocess/index.ts +2 -1
- package/src/runtime/plot.ts +82 -7
- package/src/runtime/render.ts +1 -0
- package/src/runtime/transform.ts +17 -0
- package/src/runtime/types/options.ts +1 -0
- package/src/shape/axis-breaks/axis-breaks.ts +203 -0
- package/src/shape/index.ts +2 -0
- package/src/spec/labelTransform.ts +1 -1
- package/src/theme/create.ts +2 -1
- package/src/utils/scale.ts +24 -0
- package/src/utils/selection.ts +7 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Circle, DisplayObject, IElement, Line } from '@antv/g';
|
|
2
2
|
import { sort, group, mean, bisector, minIndex } from '@antv/vendor/d3-array';
|
|
3
|
-
import { deepMix, lowerFirst, set, throttle, last } from '@antv/util';
|
|
3
|
+
import { deepMix, lowerFirst, set, throttle, last, isNumber } from '@antv/util';
|
|
4
4
|
import { Tooltip as TooltipComponent } from '@antv/component';
|
|
5
5
|
import {
|
|
6
6
|
defined,
|
|
@@ -198,9 +198,29 @@ function showUndefined(item) {
|
|
|
198
198
|
}
|
|
199
199
|
|
|
200
200
|
function heatmapItem(element) {
|
|
201
|
-
const datum = element
|
|
202
|
-
const
|
|
201
|
+
const { __data__: datum } = element;
|
|
202
|
+
const { title, items = [] } = datum;
|
|
203
203
|
|
|
204
|
+
// Check if there are user-defined items using the existing CALLBACK_ITEM_SYMBOL
|
|
205
|
+
const hasCustomItems = items.some((item) => CALLBACK_ITEM_SYMBOL in item);
|
|
206
|
+
|
|
207
|
+
if (hasCustomItems) {
|
|
208
|
+
// User has defined custom items, pass them through as is
|
|
209
|
+
const newItems = items
|
|
210
|
+
.filter(defined)
|
|
211
|
+
.map(({ color = itemColorOf(element), ...item }) => ({
|
|
212
|
+
...item,
|
|
213
|
+
color,
|
|
214
|
+
}))
|
|
215
|
+
.map(showUndefined);
|
|
216
|
+
return {
|
|
217
|
+
...(title && { title }),
|
|
218
|
+
items: newItems,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// No user-defined items, use special logic for heatmap
|
|
223
|
+
const normalizedX = datum?.normalized?.x ?? 0;
|
|
204
224
|
const originalDatum = element.parentNode?.__data__;
|
|
205
225
|
const encode = originalDatum?.encode ?? {};
|
|
206
226
|
const { x = {}, y = {}, color = {} } = encode;
|
|
@@ -571,6 +591,35 @@ function normalizedPosition(coordinate, position) {
|
|
|
571
591
|
};
|
|
572
592
|
}
|
|
573
593
|
|
|
594
|
+
/**
|
|
595
|
+
* Determine whether the band widths occupied by different categories are the same.
|
|
596
|
+
*/
|
|
597
|
+
function equalBandWidth(scale) {
|
|
598
|
+
const { x } = scale;
|
|
599
|
+
if (!x || !x.valueBandWidth) return true;
|
|
600
|
+
const { valueBandWidth } = x;
|
|
601
|
+
if (isNumber(valueBandWidth)) return true;
|
|
602
|
+
return new Set(valueBandWidth.values()).size === 1;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* Get the index of the element closest to the abstractX
|
|
607
|
+
*/
|
|
608
|
+
function findNearestElementIndex(scale, abstractX): number {
|
|
609
|
+
const { adjustedRange, valueBandWidth, valueStep } = scale;
|
|
610
|
+
const values: number[] = Array.from(valueBandWidth.values());
|
|
611
|
+
const steps: number[] = Array.from(valueStep.values());
|
|
612
|
+
const ranges = adjustedRange.map((v, i) => {
|
|
613
|
+
const halfStep = (steps[i] - values[i]) / 2;
|
|
614
|
+
return [v - halfStep, v + values[i] + halfStep];
|
|
615
|
+
});
|
|
616
|
+
const index = ranges.findIndex(
|
|
617
|
+
([start, end]) => start <= abstractX && abstractX <= end,
|
|
618
|
+
);
|
|
619
|
+
if (index !== -1) return index;
|
|
620
|
+
return abstractX > 0.5 ? adjustedRange.length - 1 : 0;
|
|
621
|
+
}
|
|
622
|
+
|
|
574
623
|
/**
|
|
575
624
|
* Finds a single element based on the mouse event in a non-series context (e.g., single item tooltip).
|
|
576
625
|
* @param root - The root display object of the chart.
|
|
@@ -596,18 +645,20 @@ export function findSingleElement({
|
|
|
596
645
|
const inInterval = (d) => d.markType === 'interval';
|
|
597
646
|
const isBar = elements.every(inInterval) && !isPolar(coordinate);
|
|
598
647
|
const scaleX = scale.x;
|
|
648
|
+
const isEqualWidth = equalBandWidth(scale);
|
|
599
649
|
const scaleSeries = scale.series;
|
|
600
650
|
const bandWidth = scaleX?.getBandWidth?.() ?? 0;
|
|
601
|
-
const xof =
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
651
|
+
const xof =
|
|
652
|
+
scaleSeries && scaleSeries.valueBandWidth
|
|
653
|
+
? (d) => {
|
|
654
|
+
const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
|
|
655
|
+
return (
|
|
656
|
+
d.__data__.x +
|
|
657
|
+
d.__data__.series * bandWidth +
|
|
658
|
+
bandWidth / (seriesCount * 2)
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
: (d) => d.__data__.x + bandWidth / 2;
|
|
611
662
|
|
|
612
663
|
// Sort for bisector search.
|
|
613
664
|
if (isBar) elements.sort((a, b) => xof(a) - xof(b));
|
|
@@ -635,7 +686,9 @@ export function findSingleElement({
|
|
|
635
686
|
if (!mouse) return;
|
|
636
687
|
const [abstractX] = coordinate.invert(mouse);
|
|
637
688
|
const search = bisector(xof).center;
|
|
638
|
-
const i =
|
|
689
|
+
const i = isEqualWidth
|
|
690
|
+
? search(elements, abstractX)
|
|
691
|
+
: findNearestElementIndex(scaleX, abstractX);
|
|
639
692
|
const target = elements[i];
|
|
640
693
|
|
|
641
694
|
if (!shared) {
|
|
@@ -643,7 +696,7 @@ export function findSingleElement({
|
|
|
643
696
|
const isGrouped = elements.find(
|
|
644
697
|
(d) => d !== target && xof(d) === xof(target),
|
|
645
698
|
);
|
|
646
|
-
if (isGrouped) return findElementByTarget(event);
|
|
699
|
+
if (isGrouped) return findElementByTarget(event) || isGrouped;
|
|
647
700
|
}
|
|
648
701
|
return target;
|
|
649
702
|
}
|
|
@@ -1068,6 +1121,11 @@ export function seriesTooltip(
|
|
|
1068
1121
|
destroy();
|
|
1069
1122
|
};
|
|
1070
1123
|
|
|
1124
|
+
const pointerleave = (e) => {
|
|
1125
|
+
if (mousePosition(root, e)) return;
|
|
1126
|
+
hide(e);
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1071
1129
|
const onTooltipEnable = () => {
|
|
1072
1130
|
addEventListeners();
|
|
1073
1131
|
};
|
|
@@ -1078,10 +1136,7 @@ export function seriesTooltip(
|
|
|
1078
1136
|
root.addEventListener('pointerenter', update);
|
|
1079
1137
|
root.addEventListener('pointermove', update);
|
|
1080
1138
|
// Only emit pointerleave event when the pointer is not in the root area.
|
|
1081
|
-
root.addEventListener('pointerleave',
|
|
1082
|
-
if (mousePosition(root, e)) return;
|
|
1083
|
-
hide(e);
|
|
1084
|
-
});
|
|
1139
|
+
root.addEventListener('pointerleave', pointerleave);
|
|
1085
1140
|
root.addEventListener('pointerup', hide);
|
|
1086
1141
|
}
|
|
1087
1142
|
};
|
|
@@ -1091,7 +1146,7 @@ export function seriesTooltip(
|
|
|
1091
1146
|
root.removeEventListener('pointerdown', update);
|
|
1092
1147
|
root.removeEventListener('pointerenter', update);
|
|
1093
1148
|
root.removeEventListener('pointermove', update);
|
|
1094
|
-
root.removeEventListener('pointerleave',
|
|
1149
|
+
root.removeEventListener('pointerleave', pointerleave);
|
|
1095
1150
|
root.removeEventListener('pointerup', hide);
|
|
1096
1151
|
}
|
|
1097
1152
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DisplayObject } from '@antv/g';
|
|
1
|
+
import { AABB, DisplayObject } from '@antv/g';
|
|
2
2
|
import { LabelTransformComponent as LLC } from '../runtime';
|
|
3
3
|
import { ExceedAdjustLabel } from '../spec';
|
|
4
4
|
import { Bounds } from '../utils/bounds';
|
|
@@ -28,6 +28,13 @@ const adjustPosition = (target: Bounds, edge: Bounds) => {
|
|
|
28
28
|
return [changeX, changeY];
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
const union = (a: AABB, b: AABB) => {
|
|
32
|
+
return {
|
|
33
|
+
min: [Math.min(a.min[0], b.min[0]), Math.min(a.min[1], b.min[1])],
|
|
34
|
+
max: [Math.max(a.max[0], b.max[0]), Math.max(a.max[1], b.max[1])],
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
31
38
|
export type ExceedAdjustOptions = Omit<ExceedAdjustLabel, 'type'> & {
|
|
32
39
|
/** X-axis offset default is 0 */
|
|
33
40
|
offsetX?: number;
|
|
@@ -85,7 +92,7 @@ export const ExceedAdjust: LLC<ExceedAdjustOptions> = (options = {}) => {
|
|
|
85
92
|
|
|
86
93
|
labels.forEach((l) => {
|
|
87
94
|
show(l);
|
|
88
|
-
const { max, min } = l.getRenderBounds();
|
|
95
|
+
const { max, min } = union(l.getRenderBounds(), l.getBounds());
|
|
89
96
|
const [xMax, yMax] = max,
|
|
90
97
|
[xMin, yMin] = min;
|
|
91
98
|
const changeValue = adjustPosition(
|
package/src/lib/builtinlib.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
LegendContinuousSize,
|
|
8
8
|
} from '../component';
|
|
9
9
|
import { Mark, View } from '../composition';
|
|
10
|
-
import { LabelShape } from '../shape';
|
|
10
|
+
import { LabelShape, BreakShape } from '../shape';
|
|
11
11
|
import { Event } from '../interaction/event';
|
|
12
12
|
|
|
13
13
|
// Some private visual components, they are not public to
|
|
@@ -24,5 +24,6 @@ export function builtinlib() {
|
|
|
24
24
|
'composition.mark': Mark,
|
|
25
25
|
'composition.view': View,
|
|
26
26
|
'shape.label.label': LabelShape,
|
|
27
|
+
'shape.break': BreakShape,
|
|
27
28
|
} as const;
|
|
28
29
|
}
|
package/src/mark/interval.ts
CHANGED
|
@@ -38,7 +38,9 @@ export type IntervalOptions = Omit<IntervalMark, 'type'>;
|
|
|
38
38
|
*/
|
|
39
39
|
export const Interval: MC<IntervalOptions> = () => {
|
|
40
40
|
return (index, scale, value, coordinate) => {
|
|
41
|
-
const { x: X,
|
|
41
|
+
const { x: X, y1: Y1, series: S, size: SZ } = value;
|
|
42
|
+
let { y: Y } = value;
|
|
43
|
+
Y = Y.map((d) => (d !== undefined ? d : 1));
|
|
42
44
|
|
|
43
45
|
// Calc width for each interval.
|
|
44
46
|
// The scales for x and series channels must be band scale.
|
package/src/mark/sankey.ts
CHANGED
|
@@ -75,6 +75,36 @@ export const Sankey: CC<SankeyOptions> = (options) => {
|
|
|
75
75
|
// Initialize data, generating nodes by link if is not specified.
|
|
76
76
|
const { links, nodes } = initializeData(data, encode);
|
|
77
77
|
|
|
78
|
+
// Early check for empty data - return empty marks instead of processing
|
|
79
|
+
if (!links || links.length === 0) {
|
|
80
|
+
return [
|
|
81
|
+
// Empty node mark
|
|
82
|
+
deepMix({}, DEFAULT_NODE_OPTIONS, {
|
|
83
|
+
data: [],
|
|
84
|
+
encode: {},
|
|
85
|
+
scale,
|
|
86
|
+
style: subObject(style, 'node'),
|
|
87
|
+
labels: [],
|
|
88
|
+
tooltip: false,
|
|
89
|
+
animate: false,
|
|
90
|
+
axis: false,
|
|
91
|
+
interaction,
|
|
92
|
+
state: {},
|
|
93
|
+
}),
|
|
94
|
+
// Empty link mark
|
|
95
|
+
deepMix({}, DEFAULT_LINK_OPTIONS, {
|
|
96
|
+
data: [],
|
|
97
|
+
encode: {},
|
|
98
|
+
labels: [],
|
|
99
|
+
style: subObject(style, 'link'),
|
|
100
|
+
tooltip: false,
|
|
101
|
+
animate: false,
|
|
102
|
+
interaction,
|
|
103
|
+
state: {},
|
|
104
|
+
}),
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
|
|
78
108
|
// Extract encode for node and link.
|
|
79
109
|
const nodeEncode = subObject(encode, 'node');
|
|
80
110
|
const linkEncode = subObject(encode, 'link');
|
package/src/mark/utils.ts
CHANGED
|
@@ -130,6 +130,31 @@ export function valueof(data: Record<string, any>[], encode: Encode): any[] {
|
|
|
130
130
|
return Array.from(data, field(encode));
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Normalizes data input for graph charts (sankey, chord, forceGraph) to ensure consistent format
|
|
135
|
+
* Supports both array input (like other charts) and object input (original format)
|
|
136
|
+
*
|
|
137
|
+
* @param data - Input data, can be array or object with links/nodes properties
|
|
138
|
+
* @returns Normalized data object with links and optional nodes properties
|
|
139
|
+
*/
|
|
140
|
+
function normalizeGraphData(data: any): { links: any[]; nodes?: any[] } {
|
|
141
|
+
// Handle array input - convert to { links: data } format
|
|
142
|
+
if (Array.isArray(data)) {
|
|
143
|
+
return { links: data };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Handle object input - return as is (original format)
|
|
147
|
+
if (data && typeof data === 'object') {
|
|
148
|
+
return {
|
|
149
|
+
links: data.links || [],
|
|
150
|
+
nodes: data.nodes,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Handle null/undefined and unexpected input types
|
|
155
|
+
return { links: [] };
|
|
156
|
+
}
|
|
157
|
+
|
|
133
158
|
export function initializeData(
|
|
134
159
|
data: { nodes?: any[]; links: any[] },
|
|
135
160
|
encode: Record<string, Encode>,
|
|
@@ -137,15 +162,27 @@ export function initializeData(
|
|
|
137
162
|
links: { target: string; source: string; value: any }[];
|
|
138
163
|
nodes: { key: string }[];
|
|
139
164
|
} {
|
|
165
|
+
const normalizedData = normalizeGraphData(data);
|
|
140
166
|
const {
|
|
141
167
|
source = (d) => d.source,
|
|
142
168
|
target = (d) => d.target,
|
|
143
169
|
value = (d) => d.value,
|
|
144
170
|
} = encode;
|
|
145
|
-
|
|
171
|
+
|
|
172
|
+
const { links, nodes } = normalizedData;
|
|
173
|
+
|
|
174
|
+
// Early return for empty links
|
|
175
|
+
if (!links.length) {
|
|
176
|
+
return {
|
|
177
|
+
links: [],
|
|
178
|
+
nodes: nodes || [],
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
146
182
|
const LS = valueof(links, source);
|
|
147
183
|
const LT = valueof(links, target);
|
|
148
184
|
const LV = valueof(links, value);
|
|
185
|
+
|
|
149
186
|
return {
|
|
150
187
|
links: links.map((_, i) => ({
|
|
151
188
|
target: LT[i],
|
package/src/runtime/component.ts
CHANGED
|
@@ -724,7 +724,18 @@ function computeAxisSize(
|
|
|
724
724
|
// Compute Labels.
|
|
725
725
|
const scale = createScale(component, library);
|
|
726
726
|
const labelBBoxes = computeLabelsBBox(rest, scale);
|
|
727
|
-
|
|
727
|
+
|
|
728
|
+
// Compute dynamic tickLength if it's a function
|
|
729
|
+
let maxTickLength = tickLength;
|
|
730
|
+
if (typeof component.tickLength === 'function') {
|
|
731
|
+
const ticks = scale.getTicks?.() || scale.getOptions().domain;
|
|
732
|
+
const tickLengths = ticks.map((d, i, array) =>
|
|
733
|
+
component.tickLength(d, i, array),
|
|
734
|
+
);
|
|
735
|
+
maxTickLength = Math.max(...tickLengths, 0);
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
const paddingTick = maxTickLength + labelSpacing;
|
|
728
739
|
if (labelBBoxes && labelBBoxes.length) {
|
|
729
740
|
const maxLabelWidth = max(labelBBoxes, (d) => d.width);
|
|
730
741
|
const maxLabelHeight = max(labelBBoxes, (d) => d.height);
|
|
@@ -748,7 +759,7 @@ function computeAxisSize(
|
|
|
748
759
|
}
|
|
749
760
|
}
|
|
750
761
|
} else {
|
|
751
|
-
component.size =
|
|
762
|
+
component.size = maxTickLength;
|
|
752
763
|
}
|
|
753
764
|
|
|
754
765
|
// Compute title.
|
|
@@ -835,7 +846,9 @@ function computeCategoryLegendSize(
|
|
|
835
846
|
};
|
|
836
847
|
|
|
837
848
|
const {
|
|
849
|
+
focus,
|
|
838
850
|
itemSpacing,
|
|
851
|
+
focusMarkerSize,
|
|
839
852
|
itemMarkerSize,
|
|
840
853
|
titleSpacing,
|
|
841
854
|
rowPadding,
|
|
@@ -866,7 +879,11 @@ function computeCategoryLegendSize(
|
|
|
866
879
|
|
|
867
880
|
const height = Math.max(labelBBoxes[0].height, itemMarkerSize) + rowPadding;
|
|
868
881
|
const widthOf = (w, padding = 0) =>
|
|
869
|
-
itemMarkerSize +
|
|
882
|
+
itemMarkerSize +
|
|
883
|
+
w +
|
|
884
|
+
itemSpacing[0] +
|
|
885
|
+
padding +
|
|
886
|
+
(focus ? focusMarkerSize + itemSpacing[2] : 0);
|
|
870
887
|
|
|
871
888
|
// Only support grid layout for vertical area.
|
|
872
889
|
const computeVerticalSize = () => {
|
package/src/runtime/constant.ts
CHANGED
|
@@ -7,3 +7,5 @@ export const COMPONENT_CLASS_NAME = 'component';
|
|
|
7
7
|
export const LABEL_CLASS_NAME = 'label';
|
|
8
8
|
export const AREA_CLASS_NAME = 'area';
|
|
9
9
|
export const MASK_CLASS_NAME = 'mask';
|
|
10
|
+
export const BREAK_CLASS_NAME = 'axis-breaks';
|
|
11
|
+
export const BREAK_GROUP_CLASS_NAME = 'axis-breaks-group';
|
package/src/runtime/mark.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
maybeNonAnimate,
|
|
25
25
|
normalizeTooltip,
|
|
26
26
|
extractTooltip,
|
|
27
|
+
appendMarkScaleKey,
|
|
27
28
|
} from './transform';
|
|
28
29
|
|
|
29
30
|
export async function initializeMark(
|
|
@@ -159,6 +160,7 @@ async function applyMarkTransform(
|
|
|
159
160
|
maybeNonAnimate,
|
|
160
161
|
addGuideToScale,
|
|
161
162
|
normalizeTooltip,
|
|
163
|
+
appendMarkScaleKey,
|
|
162
164
|
...preInference.map(useTransform),
|
|
163
165
|
...transform.map(useTransform),
|
|
164
166
|
...postInference.map(useTransform),
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { get, set } from '@antv/util';
|
|
2
|
+
import { G2ViewTree } from '../types/options';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convert a percentage string to a ratio.
|
|
6
|
+
*/
|
|
7
|
+
export const percentToRatio = (gap: string) => {
|
|
8
|
+
if (!gap || typeof gap !== 'string') {
|
|
9
|
+
return gap;
|
|
10
|
+
}
|
|
11
|
+
const value = gap.endsWith('%')
|
|
12
|
+
? parseFloat(gap.slice(0, -1)) / 100
|
|
13
|
+
: parseFloat(gap);
|
|
14
|
+
if (isNaN(value) || value < 0 || value > 1) {
|
|
15
|
+
throw new Error(`Invalid gap value: ${gap}. It should be between 0 and 1.`);
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function axisBreaks<T extends G2ViewTree = G2ViewTree>(options: T): T {
|
|
21
|
+
const { axis } = options;
|
|
22
|
+
const breaks = get(axis, 'y.breaks');
|
|
23
|
+
|
|
24
|
+
if (breaks) {
|
|
25
|
+
set(
|
|
26
|
+
options,
|
|
27
|
+
'scale.y.breaks',
|
|
28
|
+
breaks.map((item) => ({
|
|
29
|
+
key: `break-${item.start}-${item.end}`,
|
|
30
|
+
...item,
|
|
31
|
+
gap: percentToRatio(item.gap),
|
|
32
|
+
})),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return options;
|
|
36
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { G2ViewTree } from '../types/options';
|
|
2
2
|
import { flow } from '../../utils/flow';
|
|
3
3
|
import { columnWidthRatio } from './style';
|
|
4
|
+
import { axisBreaks } from './axis-breaks';
|
|
4
5
|
|
|
5
6
|
export function preprocessOption<T extends G2ViewTree = G2ViewTree>(
|
|
6
7
|
options: T,
|
|
@@ -19,5 +20,5 @@ export function preprocessOption<T extends G2ViewTree = G2ViewTree>(
|
|
|
19
20
|
// Entry point for all syntactic sugar functions.
|
|
20
21
|
function adapter<T extends G2ViewTree = G2ViewTree>(options: T): T {
|
|
21
22
|
//@todo define a type for params of flow
|
|
22
|
-
return flow(columnWidthRatio)(options);
|
|
23
|
+
return flow(columnWidthRatio, axisBreaks)(options);
|
|
23
24
|
}
|
package/src/runtime/plot.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Vector2 } from '@antv/coord';
|
|
2
2
|
import { DisplayObject, IAnimation as GAnimation, Rect } from '@antv/g';
|
|
3
|
-
import { deepMix, upperFirst, isArray } from '@antv/util';
|
|
3
|
+
import { deepMix, upperFirst, isArray, get } from '@antv/util';
|
|
4
4
|
import { group, groups } from '@antv/vendor/d3-array';
|
|
5
5
|
import { format } from '@antv/vendor/d3-format';
|
|
6
6
|
import { mapObject } from '../utils/array';
|
|
@@ -32,6 +32,8 @@ import {
|
|
|
32
32
|
MAIN_LAYER_CLASS_NAME,
|
|
33
33
|
PLOT_CLASS_NAME,
|
|
34
34
|
VIEW_CLASS_NAME,
|
|
35
|
+
BREAK_CLASS_NAME,
|
|
36
|
+
BREAK_GROUP_CLASS_NAME,
|
|
35
37
|
} from './constant';
|
|
36
38
|
import { coordinate2Transform, createCoordinate } from './coordinate';
|
|
37
39
|
import {
|
|
@@ -263,7 +265,7 @@ export async function plot<T extends G2ViewTree>(
|
|
|
263
265
|
const setState = (key, reducer = (x) => x) => store.set(key, reducer);
|
|
264
266
|
const options = viewNode.get(view);
|
|
265
267
|
const update = createUpdateView(select(container), options, context);
|
|
266
|
-
|
|
268
|
+
const target = {
|
|
267
269
|
view,
|
|
268
270
|
container,
|
|
269
271
|
options,
|
|
@@ -279,6 +281,9 @@ export async function plot<T extends G2ViewTree>(
|
|
|
279
281
|
});
|
|
280
282
|
},
|
|
281
283
|
};
|
|
284
|
+
context.externals.update = target.update;
|
|
285
|
+
context.externals.setState = setState;
|
|
286
|
+
return target;
|
|
282
287
|
});
|
|
283
288
|
};
|
|
284
289
|
|
|
@@ -872,6 +877,7 @@ function initializeState(
|
|
|
872
877
|
style: framedStyle,
|
|
873
878
|
components,
|
|
874
879
|
data: dataMap,
|
|
880
|
+
options: options,
|
|
875
881
|
labelTransform: compose(labelTransform.map(useLabelTransform)),
|
|
876
882
|
};
|
|
877
883
|
|
|
@@ -1027,11 +1033,12 @@ async function plotView(
|
|
|
1027
1033
|
(update) =>
|
|
1028
1034
|
update
|
|
1029
1035
|
.call(updateLayers, Array.from(markState.keys()))
|
|
1030
|
-
.call(
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1036
|
+
.call(updateBBox)
|
|
1037
|
+
// .call((selection) => {
|
|
1038
|
+
// return animationExtent
|
|
1039
|
+
// ? animateBBox(selection, animationExtent)
|
|
1040
|
+
// : updateBBox(selection);
|
|
1041
|
+
// })
|
|
1035
1042
|
.call(applyClip, clip),
|
|
1036
1043
|
)
|
|
1037
1044
|
.transitions();
|
|
@@ -1099,6 +1106,11 @@ async function plotView(
|
|
|
1099
1106
|
}
|
|
1100
1107
|
return animation;
|
|
1101
1108
|
})
|
|
1109
|
+
.each(function (d, i, element) {
|
|
1110
|
+
if (element.__removed__) {
|
|
1111
|
+
element.__removed__ = false;
|
|
1112
|
+
}
|
|
1113
|
+
})
|
|
1102
1114
|
.attr('markType', type)
|
|
1103
1115
|
.attr('className', ELEMENT_CLASS_NAME);
|
|
1104
1116
|
}),
|
|
@@ -1155,6 +1167,7 @@ async function plotView(
|
|
|
1155
1167
|
|
|
1156
1168
|
// Plot label for this view.
|
|
1157
1169
|
plotLabel(view, selection, transitions, library, context);
|
|
1170
|
+
plotBreak(view, selection, library, context);
|
|
1158
1171
|
}
|
|
1159
1172
|
|
|
1160
1173
|
/**
|
|
@@ -1286,6 +1299,68 @@ function getLabels(
|
|
|
1286
1299
|
return selector ? selector(F) : F;
|
|
1287
1300
|
}
|
|
1288
1301
|
|
|
1302
|
+
/**
|
|
1303
|
+
* Plot break shapes.
|
|
1304
|
+
*/
|
|
1305
|
+
function plotBreak(
|
|
1306
|
+
view: G2ViewDescriptor,
|
|
1307
|
+
selection: Selection,
|
|
1308
|
+
library: G2Library,
|
|
1309
|
+
context: G2Context,
|
|
1310
|
+
) {
|
|
1311
|
+
const scale = view.scale;
|
|
1312
|
+
const breaks = get(scale, 'y.options.breaks', []);
|
|
1313
|
+
const { document } = context.canvas;
|
|
1314
|
+
[BREAK_CLASS_NAME, BREAK_GROUP_CLASS_NAME].forEach((d) => {
|
|
1315
|
+
document.getElementsByClassName(d).forEach((e) => {
|
|
1316
|
+
e.remove();
|
|
1317
|
+
});
|
|
1318
|
+
});
|
|
1319
|
+
if (!breaks.length) {
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1322
|
+
const breakLayer = selection.select(className(PLOT_CLASS_NAME)).node();
|
|
1323
|
+
const [useShape] = useLibrary<G2ShapeOptions, ShapeComponent, Shape>(
|
|
1324
|
+
'shape',
|
|
1325
|
+
library,
|
|
1326
|
+
);
|
|
1327
|
+
const breaksShapeFunction = new Map();
|
|
1328
|
+
breaks.forEach((breakConfig, index) => {
|
|
1329
|
+
breaksShapeFunction.set(
|
|
1330
|
+
breakConfig,
|
|
1331
|
+
useShape(
|
|
1332
|
+
{
|
|
1333
|
+
type: 'break',
|
|
1334
|
+
},
|
|
1335
|
+
{
|
|
1336
|
+
view,
|
|
1337
|
+
selection,
|
|
1338
|
+
context,
|
|
1339
|
+
},
|
|
1340
|
+
),
|
|
1341
|
+
);
|
|
1342
|
+
});
|
|
1343
|
+
|
|
1344
|
+
// Render all breaks.
|
|
1345
|
+
select(breakLayer)
|
|
1346
|
+
.selectAll(className(BREAK_CLASS_NAME))
|
|
1347
|
+
.data(breaks, (d) => d.key)
|
|
1348
|
+
.join(
|
|
1349
|
+
(enter) =>
|
|
1350
|
+
enter
|
|
1351
|
+
.append((d, index) => breaksShapeFunction.get(d)(d, index))
|
|
1352
|
+
.attr('className', BREAK_CLASS_NAME),
|
|
1353
|
+
(update) =>
|
|
1354
|
+
update.each(function (d, i, element) {
|
|
1355
|
+
const shapeFunction = breaksShapeFunction.get(d);
|
|
1356
|
+
const node = shapeFunction(d, i);
|
|
1357
|
+
copyAttributes(element, node);
|
|
1358
|
+
}),
|
|
1359
|
+
(exit) => exit.remove(),
|
|
1360
|
+
)
|
|
1361
|
+
.nodes();
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1289
1364
|
function filterValid([I, P, S]: [number[], Vector2[][], number[][]?]): [
|
|
1290
1365
|
number[],
|
|
1291
1366
|
Vector2[][],
|
package/src/runtime/render.ts
CHANGED
|
@@ -84,6 +84,7 @@ export function render<T extends G2ViewTree = G2ViewTree>(
|
|
|
84
84
|
|
|
85
85
|
context.canvas = canvas;
|
|
86
86
|
context.emitter = emitter;
|
|
87
|
+
context.externals = {};
|
|
87
88
|
|
|
88
89
|
const { width: prevWidth, height: prevHeight } = canvas.getConfig();
|
|
89
90
|
if (prevWidth !== width || prevHeight !== height) {
|
package/src/runtime/transform.ts
CHANGED
|
@@ -314,6 +314,23 @@ export function maybeNonAnimate(
|
|
|
314
314
|
return [I, mark];
|
|
315
315
|
}
|
|
316
316
|
|
|
317
|
+
export function appendMarkScaleKey(
|
|
318
|
+
I: number[],
|
|
319
|
+
mark: G2Mark,
|
|
320
|
+
context: G2Context,
|
|
321
|
+
): [number[], G2Mark] {
|
|
322
|
+
deepMix(mark, {
|
|
323
|
+
scale: {
|
|
324
|
+
series: {
|
|
325
|
+
key: `DEFAULT_${mark.type}_SERIES_KEY`,
|
|
326
|
+
...(mark?.scale?.series ?? {}),
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
return [I, mark];
|
|
332
|
+
}
|
|
333
|
+
|
|
317
334
|
function isTypedChannel(channel): boolean {
|
|
318
335
|
if (
|
|
319
336
|
typeof channel !== 'object' ||
|
|
@@ -59,6 +59,7 @@ export type G2Context = {
|
|
|
59
59
|
group?: DisplayObject;
|
|
60
60
|
animations?: GAnimation[];
|
|
61
61
|
views?: G2ViewDescriptor[];
|
|
62
|
+
externals?: Record<string, any>;
|
|
62
63
|
/**
|
|
63
64
|
* Tell G2 how to create a canvas-like element, some marks will use it later such as wordcloud & heatmap.
|
|
64
65
|
* Use `document.createElement('canvas')` instead if not provided.
|