@antv/g2 5.0.0-beta.3 → 5.0.0-beta.4
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 +2 -2
- package/dist/g2.min.js +2 -2
- package/esm/api/mark/mark.js +1 -0
- package/esm/api/mark/mark.js.map +1 -1
- package/esm/api/mark/types.d.ts +1 -0
- package/esm/component/axis.js +45 -24
- package/esm/component/axis.js.map +1 -1
- package/esm/component/index.d.ts +4 -0
- package/esm/component/index.js +2 -0
- package/esm/component/index.js.map +1 -1
- package/esm/component/legendCategory.js +1 -0
- package/esm/component/legendCategory.js.map +1 -1
- package/esm/component/slider.d.ts +9 -0
- package/esm/component/slider.js +59 -0
- package/esm/component/slider.js.map +1 -0
- package/esm/component/sliderX.d.ts +7 -0
- package/esm/component/sliderX.js +9 -0
- package/esm/component/sliderX.js.map +1 -0
- package/esm/component/sliderY.d.ts +7 -0
- package/esm/component/sliderY.js +9 -0
- package/esm/component/sliderY.js.map +1 -0
- package/esm/composition/facetRect.js +4 -4
- package/esm/composition/facetRect.js.map +1 -1
- package/esm/composition/mark.js +2 -1
- package/esm/composition/mark.js.map +1 -1
- package/esm/composition/repeatMatrix.js +2 -2
- package/esm/composition/repeatMatrix.js.map +1 -1
- package/esm/data/filter.d.ts +3 -3
- package/esm/data/filter.js.map +1 -1
- package/esm/data/index.d.ts +3 -1
- package/esm/data/index.js +1 -0
- package/esm/data/index.js.map +1 -1
- package/esm/data/sort.d.ts +8 -0
- package/esm/data/sort.js +12 -0
- package/esm/data/sort.js.map +1 -0
- package/esm/interaction/builtin/tooltip.d.ts +6 -4
- package/esm/interaction/index.d.ts +2 -0
- package/esm/interaction/index.js +2 -0
- package/esm/interaction/index.js.map +1 -1
- package/esm/interaction/native/legendFilter.d.ts +8 -0
- package/esm/interaction/native/legendFilter.js +189 -0
- package/esm/interaction/native/legendFilter.js.map +1 -0
- package/esm/interaction/native/tooltip.d.ts +2 -2
- package/esm/interaction/native/tooltip.js +39 -17
- package/esm/interaction/native/tooltip.js.map +1 -1
- package/esm/interaction/native/utils.d.ts +1 -1
- package/esm/interaction/native/utils.js +2 -2
- package/esm/interaction/native/utils.js.map +1 -1
- package/esm/mark/area.js +1 -1
- package/esm/mark/area.js.map +1 -1
- package/esm/mark/line.js +1 -1
- package/esm/mark/line.js.map +1 -1
- package/esm/runtime/component.js +29 -1
- package/esm/runtime/component.js.map +1 -1
- package/esm/runtime/plot.js +14 -7
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/types/mark.d.ts +1 -1
- package/esm/runtime/types/options.d.ts +1 -0
- package/esm/runtime/types/theme.d.ts +5 -0
- package/esm/spec/data.d.ts +10 -3
- package/esm/spec/geometry.d.ts +1 -0
- package/esm/spec/interaction.d.ts +9 -2
- package/esm/spec/transform.d.ts +7 -2
- package/esm/stdlib/index.js +6 -6
- package/esm/stdlib/index.js.map +1 -1
- package/esm/theme/dark.js +78 -28
- package/esm/theme/dark.js.map +1 -1
- package/esm/theme/light.js +68 -17
- package/esm/theme/light.js.map +1 -1
- package/esm/transform/filter.d.ts +7 -0
- package/esm/transform/filter.js +42 -0
- package/esm/transform/filter.js.map +1 -0
- package/esm/transform/index.d.ts +2 -0
- package/esm/transform/index.js +1 -0
- package/esm/transform/index.js.map +1 -1
- package/esm/utils/selection.d.ts +1 -0
- package/esm/utils/selection.js.map +1 -1
- package/esm/utils/size.js +1 -1
- package/esm/utils/size.js.map +1 -1
- package/lib/api/mark/mark.js +1 -0
- package/lib/api/mark/mark.js.map +1 -1
- package/lib/api/mark/types.d.ts +1 -0
- package/lib/component/axis.js +45 -24
- package/lib/component/axis.js.map +1 -1
- package/lib/component/index.d.ts +4 -0
- package/lib/component/index.js +5 -1
- package/lib/component/index.js.map +1 -1
- package/lib/component/legendCategory.js +1 -0
- package/lib/component/legendCategory.js.map +1 -1
- package/lib/component/slider.d.ts +9 -0
- package/lib/component/slider.js +63 -0
- package/lib/component/slider.js.map +1 -0
- package/lib/component/sliderX.d.ts +7 -0
- package/lib/component/sliderX.js +13 -0
- package/lib/component/sliderX.js.map +1 -0
- package/lib/component/sliderY.d.ts +7 -0
- package/lib/component/sliderY.js +13 -0
- package/lib/component/sliderY.js.map +1 -0
- package/lib/composition/facetRect.js +4 -4
- package/lib/composition/facetRect.js.map +1 -1
- package/lib/composition/mark.js +2 -1
- package/lib/composition/mark.js.map +1 -1
- package/lib/composition/repeatMatrix.js +2 -2
- package/lib/composition/repeatMatrix.js.map +1 -1
- package/lib/data/filter.d.ts +3 -3
- package/lib/data/filter.js.map +1 -1
- package/lib/data/index.d.ts +3 -1
- package/lib/data/index.js +3 -1
- package/lib/data/index.js.map +1 -1
- package/lib/data/sort.d.ts +8 -0
- package/lib/data/sort.js +17 -0
- package/lib/data/sort.js.map +1 -0
- package/lib/interaction/builtin/tooltip.d.ts +6 -4
- package/lib/interaction/index.d.ts +2 -0
- package/lib/interaction/index.js +5 -1
- package/lib/interaction/index.js.map +1 -1
- package/lib/interaction/native/legendFilter.d.ts +8 -0
- package/lib/interaction/native/legendFilter.js +193 -0
- package/lib/interaction/native/legendFilter.js.map +1 -0
- package/lib/interaction/native/tooltip.d.ts +2 -2
- package/lib/interaction/native/tooltip.js +39 -17
- package/lib/interaction/native/tooltip.js.map +1 -1
- package/lib/interaction/native/utils.d.ts +1 -1
- package/lib/interaction/native/utils.js +2 -2
- package/lib/interaction/native/utils.js.map +1 -1
- package/lib/mark/area.js +1 -1
- package/lib/mark/area.js.map +1 -1
- package/lib/mark/line.js +1 -1
- package/lib/mark/line.js.map +1 -1
- package/lib/runtime/component.js +29 -1
- package/lib/runtime/component.js.map +1 -1
- package/lib/runtime/plot.js +14 -7
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/types/mark.d.ts +1 -1
- package/lib/runtime/types/options.d.ts +1 -0
- package/lib/runtime/types/theme.d.ts +5 -0
- package/lib/spec/data.d.ts +10 -3
- package/lib/spec/geometry.d.ts +1 -0
- package/lib/spec/interaction.d.ts +9 -2
- package/lib/spec/transform.d.ts +7 -2
- package/lib/stdlib/index.js +2 -2
- package/lib/stdlib/index.js.map +1 -1
- package/lib/theme/dark.js +78 -28
- package/lib/theme/dark.js.map +1 -1
- package/lib/theme/light.js +68 -17
- package/lib/theme/light.js.map +1 -1
- package/lib/transform/filter.d.ts +7 -0
- package/lib/transform/filter.js +46 -0
- package/lib/transform/filter.js.map +1 -0
- package/lib/transform/index.d.ts +2 -0
- package/lib/transform/index.js +3 -1
- package/lib/transform/index.js.map +1 -1
- package/lib/utils/selection.d.ts +1 -0
- package/lib/utils/selection.js.map +1 -1
- package/lib/utils/size.js +1 -1
- package/lib/utils/size.js.map +1 -1
- package/package.json +5 -4
- package/src/api/mark/mark.ts +1 -0
- package/src/api/mark/types.ts +1 -0
- package/src/component/axis.ts +46 -91
- package/src/component/index.ts +4 -0
- package/src/component/legendCategory.ts +1 -0
- package/src/component/slider.ts +63 -0
- package/src/component/sliderX.ts +16 -0
- package/src/component/sliderY.ts +16 -0
- package/src/composition/facetRect.ts +4 -2
- package/src/composition/mark.ts +2 -0
- package/src/composition/repeatMatrix.ts +8 -1
- package/src/data/filter.ts +3 -3
- package/src/data/index.ts +3 -1
- package/src/data/sort.ts +18 -0
- package/src/interaction/index.ts +3 -0
- package/src/interaction/native/legendFilter.ts +206 -0
- package/src/interaction/native/tooltip.ts +57 -16
- package/src/interaction/native/utils.ts +2 -1
- package/src/mark/area.ts +1 -1
- package/src/mark/line.ts +1 -1
- package/src/runtime/component.ts +49 -2
- package/src/runtime/plot.ts +11 -5
- package/src/runtime/types/mark.ts +1 -1
- package/src/runtime/types/options.ts +1 -0
- package/src/runtime/types/theme.ts +5 -0
- package/src/spec/data.ts +12 -2
- package/src/spec/geometry.ts +1 -0
- package/src/spec/interaction.ts +11 -1
- package/src/spec/transform.ts +9 -1
- package/src/stdlib/index.ts +12 -2
- package/src/theme/dark.ts +79 -28
- package/src/theme/light.ts +68 -17
- package/src/transform/filter.ts +48 -0
- package/src/transform/index.ts +2 -0
- package/src/utils/selection.ts +2 -0
- package/src/utils/size.ts +1 -1
- package/src/.DS_Store +0 -0
- package/src/data/.DS_Store +0 -0
- package/src/shape/.DS_Store +0 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { DisplayObject } from '@antv/g';
|
|
2
|
+
import { deepMix } from '@antv/util';
|
|
3
|
+
import { subObject } from '../../utils/helper';
|
|
4
|
+
import { createValueof, useState } from './utils';
|
|
5
|
+
|
|
6
|
+
export const CATEGORY_LEGEND_CLASS_NAME = 'legend-category';
|
|
7
|
+
|
|
8
|
+
export const LEGEND_ITEMS_CLASS_NAME = 'items-item';
|
|
9
|
+
|
|
10
|
+
export const LEGEND_MAKER_CLASS_NAME = 'legend-category-item-marker';
|
|
11
|
+
|
|
12
|
+
export const LEGEND_LABEL_CLASS_NAME = 'legend-category-item-label';
|
|
13
|
+
|
|
14
|
+
function setCursor(root, cursor) {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
const canvas = root.getRootNode().defaultView;
|
|
17
|
+
const dom = canvas.getContextService().getDomElement();
|
|
18
|
+
dom.style.cursor = cursor;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function restoreCursor(root) {
|
|
22
|
+
setCursor(root, 'default');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function markerOf(item) {
|
|
26
|
+
return item.getElementsByClassName(LEGEND_MAKER_CLASS_NAME)[0];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function labelOf(item) {
|
|
30
|
+
return item.getElementsByClassName(LEGEND_LABEL_CLASS_NAME)[0];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function legendsOf(root) {
|
|
34
|
+
return root.getElementsByClassName(LEGEND_ITEMS_CLASS_NAME);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function builtInAccessors(selectedLegend) {
|
|
38
|
+
// Get the value and scale type from legend.
|
|
39
|
+
const { data } = selectedLegend.attributes;
|
|
40
|
+
const { __data__ } = selectedLegend.parentNode;
|
|
41
|
+
const { name: channel } = __data__.scale;
|
|
42
|
+
return [
|
|
43
|
+
channel,
|
|
44
|
+
{
|
|
45
|
+
legends: legendsOf,
|
|
46
|
+
marker: labelOf,
|
|
47
|
+
label: markerOf,
|
|
48
|
+
datum: (d) => {
|
|
49
|
+
const { __data__: datum } = d;
|
|
50
|
+
const { index } = datum;
|
|
51
|
+
return data[index].label;
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
] as any;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function legendFilter(
|
|
58
|
+
root: DisplayObject,
|
|
59
|
+
{
|
|
60
|
+
legends, // given the root of chart returns legends to be manipulated
|
|
61
|
+
marker: markerOf, // given the legend returns the marker
|
|
62
|
+
label: labelOf, // given the legend returns the label
|
|
63
|
+
datum, // given the legend returns the value
|
|
64
|
+
filter, // invoke when dispatch filter event,
|
|
65
|
+
setAttribute, // setter for set element style,
|
|
66
|
+
...options // style options
|
|
67
|
+
},
|
|
68
|
+
) {
|
|
69
|
+
// Index handler by item.
|
|
70
|
+
const itemClick = new Map();
|
|
71
|
+
const itemPointerenter = new Map();
|
|
72
|
+
const itemPointerout = new Map();
|
|
73
|
+
|
|
74
|
+
// State and styles.
|
|
75
|
+
const style =
|
|
76
|
+
Object.keys(options).length === 0
|
|
77
|
+
? {
|
|
78
|
+
labelUnselectedFill: '#aaa',
|
|
79
|
+
markerUnselectedFill: '#aaa',
|
|
80
|
+
}
|
|
81
|
+
: options;
|
|
82
|
+
const markerStyle = subObject(style, 'marker');
|
|
83
|
+
const labelStyle = subObject(style, 'label');
|
|
84
|
+
const { setState: setM, removeState: removeM } = useState(
|
|
85
|
+
markerStyle,
|
|
86
|
+
undefined,
|
|
87
|
+
setAttribute,
|
|
88
|
+
);
|
|
89
|
+
const { setState: setL, removeState: removeL } = useState(
|
|
90
|
+
labelStyle,
|
|
91
|
+
undefined,
|
|
92
|
+
setAttribute,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const items: DisplayObject[] = Array.from(legends(root));
|
|
96
|
+
const selectedValues = items.map(datum);
|
|
97
|
+
const updateLegendState = () => {
|
|
98
|
+
for (const item of items) {
|
|
99
|
+
const value = datum(item);
|
|
100
|
+
const marker = markerOf(item);
|
|
101
|
+
const label = labelOf(item);
|
|
102
|
+
if (!selectedValues.includes(value)) {
|
|
103
|
+
setM(marker, 'unselected');
|
|
104
|
+
setL(label, 'unselected');
|
|
105
|
+
} else {
|
|
106
|
+
removeM(marker, 'unselected');
|
|
107
|
+
removeL(label, 'unselected');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
for (const item of items) {
|
|
113
|
+
// Defined handlers.
|
|
114
|
+
const pointerenter = () => {
|
|
115
|
+
setCursor(root, 'pointer');
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const pointerout = () => {
|
|
119
|
+
restoreCursor(root);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const click = async () => {
|
|
123
|
+
const value = datum(item);
|
|
124
|
+
const index = selectedValues.indexOf(value);
|
|
125
|
+
if (index === -1) selectedValues.push(value);
|
|
126
|
+
else selectedValues.splice(index, 1);
|
|
127
|
+
if (selectedValues.length === 0) selectedValues.push(...items.map(datum));
|
|
128
|
+
await filter(selectedValues);
|
|
129
|
+
updateLegendState();
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// Bind and store handlers.
|
|
133
|
+
item.addEventListener('click', click);
|
|
134
|
+
item.addEventListener('pointerenter', pointerenter);
|
|
135
|
+
item.addEventListener('pointerout', pointerout);
|
|
136
|
+
itemClick.set(item, click);
|
|
137
|
+
itemPointerenter.set(item, pointerenter);
|
|
138
|
+
itemPointerout.set(item, pointerout);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return () => {
|
|
142
|
+
for (const item of items) {
|
|
143
|
+
item.removeEventListener('click', itemClick.get(item));
|
|
144
|
+
item.removeEventListener('pointerenter', itemPointerenter.get(item));
|
|
145
|
+
item.removeEventListener('pointerout', itemPointerout.get(item));
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function LegendFilter({ channel, ...rest }) {
|
|
151
|
+
return (context) => {
|
|
152
|
+
const { container, view, options: viewOptions, update } = context;
|
|
153
|
+
|
|
154
|
+
// Use the first legend if channel is not specified,
|
|
155
|
+
// or use the first legend bind to specified channel.
|
|
156
|
+
const legends = container.getElementsByClassName(
|
|
157
|
+
CATEGORY_LEGEND_CLASS_NAME,
|
|
158
|
+
);
|
|
159
|
+
const selectedLegend = !channel
|
|
160
|
+
? legends[0]
|
|
161
|
+
: legends.find((d) => {
|
|
162
|
+
const { __data__ } = d.parentNode;
|
|
163
|
+
return __data__.scale.name === channel;
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Skip if there is no legend.
|
|
167
|
+
const [selectedChannel, accessors] = selectedLegend
|
|
168
|
+
? builtInAccessors(selectedLegend)
|
|
169
|
+
: [null, {}];
|
|
170
|
+
if (selectedChannel) channel = selectedChannel;
|
|
171
|
+
|
|
172
|
+
// Apply common legend filter.
|
|
173
|
+
return legendFilter(container, {
|
|
174
|
+
...accessors,
|
|
175
|
+
filter: async (value) => {
|
|
176
|
+
const { scale } = view;
|
|
177
|
+
const { [channel]: scaleOrdinal } = scale;
|
|
178
|
+
const { marks } = viewOptions;
|
|
179
|
+
// Add filter transform for every marks,
|
|
180
|
+
// which will skip for mark without color channel.
|
|
181
|
+
const newMarks = marks.map((mark) => {
|
|
182
|
+
const { transform = [] } = mark;
|
|
183
|
+
const newTransform = [
|
|
184
|
+
{ type: 'filter', [channel]: value },
|
|
185
|
+
...transform,
|
|
186
|
+
];
|
|
187
|
+
return deepMix({}, mark, {
|
|
188
|
+
transform: newTransform,
|
|
189
|
+
// Set domain of scale to preserve legends.
|
|
190
|
+
scale: {
|
|
191
|
+
[channel]: {
|
|
192
|
+
domain: scaleOrdinal.getOptions().domain,
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
const newOptions = {
|
|
198
|
+
...viewOptions,
|
|
199
|
+
marks: newMarks,
|
|
200
|
+
};
|
|
201
|
+
return update(newOptions);
|
|
202
|
+
},
|
|
203
|
+
...rest,
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -147,13 +147,13 @@ function groupItems(
|
|
|
147
147
|
};
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
function updateRuleY(root, points, { height, ...rest }) {
|
|
150
|
+
function updateRuleY(root, points, { height, startX, startY, ...rest }) {
|
|
151
151
|
const X = points.map((p) => p[0]);
|
|
152
152
|
const x = mean(X);
|
|
153
|
-
const x1 = x;
|
|
154
|
-
const x2 = x;
|
|
155
|
-
const y1 =
|
|
156
|
-
const y2 = height;
|
|
153
|
+
const x1 = x + startX;
|
|
154
|
+
const x2 = x + startX;
|
|
155
|
+
const y1 = startY;
|
|
156
|
+
const y2 = height - startY;
|
|
157
157
|
const createLine = () => {
|
|
158
158
|
const line = new Line({
|
|
159
159
|
style: {
|
|
@@ -217,8 +217,10 @@ export function seriesTooltip(
|
|
|
217
217
|
trailing = false,
|
|
218
218
|
scale,
|
|
219
219
|
coordinate,
|
|
220
|
-
|
|
220
|
+
crosshairs,
|
|
221
221
|
item,
|
|
222
|
+
startX = 0,
|
|
223
|
+
startY = 0,
|
|
222
224
|
...rest
|
|
223
225
|
}: Record<string, any>,
|
|
224
226
|
) {
|
|
@@ -252,10 +254,11 @@ export function seriesTooltip(
|
|
|
252
254
|
|
|
253
255
|
const update = throttle(
|
|
254
256
|
(event) => {
|
|
255
|
-
const
|
|
257
|
+
const mouse = mousePosition(root, event);
|
|
256
258
|
const bbox = root.getBounds();
|
|
257
259
|
const x = bbox.min[0];
|
|
258
260
|
const y = bbox.min[1];
|
|
261
|
+
const focus = [mouse[0] - startX, mouse[1] - startY];
|
|
259
262
|
if (!focus) return;
|
|
260
263
|
const data = elements.map((element) => {
|
|
261
264
|
const sortedX = elementSortedX.get(element);
|
|
@@ -267,8 +270,10 @@ export function seriesTooltip(
|
|
|
267
270
|
const dataItems = data.map((d) => d[0]);
|
|
268
271
|
const points = data.map((d) => d[1]);
|
|
269
272
|
const tooltipData = groupItems(elements, item, scale, dataItems);
|
|
270
|
-
showTooltip(root, tooltipData,
|
|
271
|
-
if (
|
|
273
|
+
showTooltip(root, tooltipData, mouse[0] + x, mouse[1] + y);
|
|
274
|
+
if (crosshairs) {
|
|
275
|
+
updateRuleY(root, points, { ...ruleStyle, height, startX, startY });
|
|
276
|
+
}
|
|
272
277
|
},
|
|
273
278
|
wait,
|
|
274
279
|
{ leading, trailing },
|
|
@@ -276,7 +281,7 @@ export function seriesTooltip(
|
|
|
276
281
|
|
|
277
282
|
const hide = () => {
|
|
278
283
|
hideTooltip(root);
|
|
279
|
-
if (
|
|
284
|
+
if (crosshairs) hideRuleY(root);
|
|
280
285
|
};
|
|
281
286
|
|
|
282
287
|
root.addEventListener('pointerenter', update);
|
|
@@ -346,29 +351,65 @@ export function tooltip(
|
|
|
346
351
|
export function Tooltip(options) {
|
|
347
352
|
const {
|
|
348
353
|
shared,
|
|
349
|
-
|
|
354
|
+
crosshairs,
|
|
350
355
|
series,
|
|
351
356
|
name,
|
|
352
357
|
item = () => ({}),
|
|
358
|
+
facet = false,
|
|
353
359
|
...rest
|
|
354
360
|
} = options;
|
|
355
|
-
return (
|
|
356
|
-
const { container, view } =
|
|
361
|
+
return (target, viewInstances) => {
|
|
362
|
+
const { container, view } = target;
|
|
357
363
|
const { scale, markState, coordinate } = view;
|
|
364
|
+
|
|
358
365
|
// Get default value from mark states.
|
|
359
366
|
const defaultSeries = interactionKeyof(markState, 'seriesTooltip');
|
|
360
|
-
const defaultShowCrosshairs = interactionKeyof(markState, '
|
|
367
|
+
const defaultShowCrosshairs = interactionKeyof(markState, 'crosshairs');
|
|
361
368
|
const plotArea = selectPlotArea(container);
|
|
362
|
-
|
|
369
|
+
const isSeries = maybeValue(series, defaultSeries);
|
|
370
|
+
|
|
371
|
+
// For non-facet and series tooltip.
|
|
372
|
+
if (isSeries && !facet) {
|
|
363
373
|
return seriesTooltip(plotArea, {
|
|
364
374
|
...rest,
|
|
365
375
|
elements: selectG2Elements,
|
|
366
376
|
scale,
|
|
367
377
|
coordinate,
|
|
368
|
-
|
|
378
|
+
crosshairs: maybeValue(crosshairs, defaultShowCrosshairs),
|
|
369
379
|
item,
|
|
370
380
|
});
|
|
371
381
|
}
|
|
382
|
+
|
|
383
|
+
// For facet and series tooltip.
|
|
384
|
+
if (isSeries && facet) {
|
|
385
|
+
// Get sub view instances for this view.
|
|
386
|
+
const facetInstances = viewInstances.filter(
|
|
387
|
+
(d) => d !== target && d.options.parentKey === target.options.key,
|
|
388
|
+
);
|
|
389
|
+
const elements = facetInstances.flatMap(({ container }) =>
|
|
390
|
+
selectG2Elements(container),
|
|
391
|
+
);
|
|
392
|
+
// Use the scale of the first view.
|
|
393
|
+
const scale = facetInstances[0].view.scale;
|
|
394
|
+
const bbox = plotArea.getBounds();
|
|
395
|
+
const startX = bbox.min[0];
|
|
396
|
+
const startY = bbox.min[1];
|
|
397
|
+
|
|
398
|
+
// @todo Nested structure rather than flat structure for facet?
|
|
399
|
+
// Add listener to the root area.
|
|
400
|
+
// @ts-ignore
|
|
401
|
+
return seriesTooltip(plotArea.parentNode.parentNode, {
|
|
402
|
+
...rest,
|
|
403
|
+
elements: () => elements,
|
|
404
|
+
scale,
|
|
405
|
+
coordinate,
|
|
406
|
+
crosshairs: maybeValue(crosshairs, defaultShowCrosshairs),
|
|
407
|
+
item,
|
|
408
|
+
startX,
|
|
409
|
+
startY,
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
|
|
372
413
|
return tooltip(plotArea, {
|
|
373
414
|
...rest,
|
|
374
415
|
elements: selectG2Elements,
|
|
@@ -73,6 +73,7 @@ export function createDatumof(view: G2ViewDescriptor) {
|
|
|
73
73
|
export function useState(
|
|
74
74
|
style: Record<string, any>,
|
|
75
75
|
valueof = (d, element) => d,
|
|
76
|
+
setAttribute = (element, key, v) => element.setAttribute(key, v),
|
|
76
77
|
) {
|
|
77
78
|
const STATES = '__states__';
|
|
78
79
|
const ORIGINAL = '__ordinal__';
|
|
@@ -91,7 +92,7 @@ export function useState(
|
|
|
91
92
|
for (const [key, value] of Object.entries(stateStyle)) {
|
|
92
93
|
const currentValue = element.getAttribute(key);
|
|
93
94
|
const v = valueof(value, element);
|
|
94
|
-
|
|
95
|
+
setAttribute(element, key, v);
|
|
95
96
|
// Store the attribute if it does not exist in original.
|
|
96
97
|
if (!(key in original)) original[key] = currentValue;
|
|
97
98
|
}
|
package/src/mark/area.ts
CHANGED
package/src/mark/line.ts
CHANGED
package/src/runtime/component.ts
CHANGED
|
@@ -26,7 +26,6 @@ import {
|
|
|
26
26
|
isRadial,
|
|
27
27
|
} from './coordinate';
|
|
28
28
|
import { useLibrary } from './library';
|
|
29
|
-
import { isPosition } from './scale';
|
|
30
29
|
|
|
31
30
|
export function inferComponent(
|
|
32
31
|
scales: G2ScaleOptions[],
|
|
@@ -49,7 +48,8 @@ export function inferComponent(
|
|
|
49
48
|
if (guide === null) return false;
|
|
50
49
|
return true;
|
|
51
50
|
});
|
|
52
|
-
const
|
|
51
|
+
const sliders = createSliders(partialOptions, scales, library);
|
|
52
|
+
const components = [...partialComponents, ...sliders];
|
|
53
53
|
if (title) {
|
|
54
54
|
const { props } = createGuideComponent('title');
|
|
55
55
|
const { defaultPosition, defaultOrder, defaultSize } = props;
|
|
@@ -223,3 +223,50 @@ function inferTitleComponentSize(options: G2TitleOptions) {
|
|
|
223
223
|
// Extra 4px spacing.
|
|
224
224
|
return (text ? titleSize + 4 : 0) + (subtitle ? subtitleSize + spacing : 0);
|
|
225
225
|
}
|
|
226
|
+
|
|
227
|
+
function inferSliderType(
|
|
228
|
+
name: string,
|
|
229
|
+
coordinates: G2CoordinateOptions[] = [],
|
|
230
|
+
) {
|
|
231
|
+
if (name === 'x') return isTranspose(coordinates) ? 'sliderY' : 'sliderX';
|
|
232
|
+
if (name === 'y') return isTranspose(coordinates) ? 'sliderX' : 'sliderY';
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function createSliders(
|
|
237
|
+
partialOptions: G2View,
|
|
238
|
+
scales: G2ScaleOptions[],
|
|
239
|
+
library,
|
|
240
|
+
) {
|
|
241
|
+
const [, createGuideComponent] = useLibrary<
|
|
242
|
+
G2GuideComponentOptions,
|
|
243
|
+
GuideComponentComponent,
|
|
244
|
+
GuideComponent
|
|
245
|
+
>('component', library);
|
|
246
|
+
|
|
247
|
+
const { marks, coordinates } = partialOptions;
|
|
248
|
+
const nameScale = new Map(scales.map((scale) => [scale.name, scale]));
|
|
249
|
+
|
|
250
|
+
return marks
|
|
251
|
+
.filter((d) => d.slider)
|
|
252
|
+
.flatMap((mark) => {
|
|
253
|
+
const { slider } = mark;
|
|
254
|
+
return Object.entries(slider).map(([channelName, options]) => {
|
|
255
|
+
const scale = nameScale.get(channelName);
|
|
256
|
+
const componentType = inferSliderType(channelName, coordinates);
|
|
257
|
+
if (!scale || !componentType) return;
|
|
258
|
+
|
|
259
|
+
const { props } = createGuideComponent(componentType);
|
|
260
|
+
const { defaultPosition, defaultSize, defaultOrder } = props;
|
|
261
|
+
return {
|
|
262
|
+
position: defaultPosition,
|
|
263
|
+
size: defaultSize,
|
|
264
|
+
order: defaultOrder,
|
|
265
|
+
type: componentType,
|
|
266
|
+
...options,
|
|
267
|
+
scale,
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
})
|
|
271
|
+
.filter((d) => !!d);
|
|
272
|
+
}
|
package/src/runtime/plot.ts
CHANGED
|
@@ -396,7 +396,7 @@ function initializeState(
|
|
|
396
396
|
const layout = computeLayout(components, options);
|
|
397
397
|
const coordinate = createCoordinate(layout, options, library);
|
|
398
398
|
const framedStyle = frame
|
|
399
|
-
? deepMix({
|
|
399
|
+
? deepMix({ mainLineWidth: 1, mainStroke: '#000' }, style)
|
|
400
400
|
: style;
|
|
401
401
|
|
|
402
402
|
// Place components and mutate their bbox.
|
|
@@ -643,12 +643,16 @@ async function plotView(
|
|
|
643
643
|
return animation;
|
|
644
644
|
});
|
|
645
645
|
}),
|
|
646
|
-
(exit) =>
|
|
647
|
-
exit
|
|
646
|
+
(exit) => {
|
|
647
|
+
return exit
|
|
648
|
+
.each(function () {
|
|
649
|
+
this.__removed__ = true;
|
|
650
|
+
})
|
|
648
651
|
.transition(function (data) {
|
|
649
652
|
return exitFunction(data, [this]);
|
|
650
653
|
})
|
|
651
|
-
.remove()
|
|
654
|
+
.remove();
|
|
655
|
+
},
|
|
652
656
|
(merge) =>
|
|
653
657
|
merge
|
|
654
658
|
// Append elements to be merged.
|
|
@@ -714,7 +718,9 @@ function plotLabel(
|
|
|
714
718
|
const elements = selection
|
|
715
719
|
.select(`#${key}`)
|
|
716
720
|
.selectAll(className(ELEMENT_CLASS_NAME))
|
|
717
|
-
.nodes()
|
|
721
|
+
.nodes()
|
|
722
|
+
// Only select the valid element.
|
|
723
|
+
.filter((n) => !n.__removed__);
|
|
718
724
|
return labelOptions.flatMap((labelOption, i) => {
|
|
719
725
|
const { transform = [], ...options } = labelOption;
|
|
720
726
|
return elements.flatMap((e) => {
|
|
@@ -100,6 +100,7 @@ export type G2Mark = {
|
|
|
100
100
|
facet?: boolean;
|
|
101
101
|
axis?: boolean | Record<string, any>;
|
|
102
102
|
legend?: boolean | Record<string, any>;
|
|
103
|
+
slider?: Record<string, any>;
|
|
103
104
|
filter?: (i: number) => boolean;
|
|
104
105
|
children?: G2MarkChildrenCallback;
|
|
105
106
|
dataDomain?: number;
|
package/src/spec/data.ts
CHANGED
|
@@ -20,17 +20,19 @@ export type InlineConnector = {
|
|
|
20
20
|
|
|
21
21
|
export type DataTransform =
|
|
22
22
|
| SortByTransform
|
|
23
|
+
| SortTransform
|
|
23
24
|
| PickTransform
|
|
24
25
|
| RenameTransform
|
|
25
26
|
| SubsetTransform
|
|
26
27
|
| FoldTransform
|
|
27
28
|
| JoinTransform
|
|
28
|
-
|
|
|
29
|
+
| FilterDataTransform
|
|
29
30
|
| MapTransform
|
|
30
31
|
| CustomTransform;
|
|
31
32
|
|
|
32
33
|
export type DataTransformTypes =
|
|
33
34
|
| 'sortBy'
|
|
35
|
+
| 'sort'
|
|
34
36
|
| 'pick'
|
|
35
37
|
| 'rename'
|
|
36
38
|
| 'subset'
|
|
@@ -64,7 +66,7 @@ export type SubsetTransform = {
|
|
|
64
66
|
fields?: string[];
|
|
65
67
|
};
|
|
66
68
|
|
|
67
|
-
export type
|
|
69
|
+
export type FilterDataTransform = {
|
|
68
70
|
type?: 'filter';
|
|
69
71
|
/**
|
|
70
72
|
* The filter condition, same with [Array.prototype.filter](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter).
|
|
@@ -72,6 +74,14 @@ export type FilterTransform = {
|
|
|
72
74
|
callback?: (v: any, idx: number, arr: any[]) => boolean;
|
|
73
75
|
};
|
|
74
76
|
|
|
77
|
+
export type SortTransform = {
|
|
78
|
+
type?: 'sort';
|
|
79
|
+
/**
|
|
80
|
+
* The sort comparator, same with [Array.prototype.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
|
|
81
|
+
*/
|
|
82
|
+
callback?: (a: any, b: any) => number;
|
|
83
|
+
};
|
|
84
|
+
|
|
75
85
|
export type FoldTransform = {
|
|
76
86
|
type?: 'fold';
|
|
77
87
|
fields?: string[];
|
package/src/spec/geometry.ts
CHANGED
|
@@ -126,6 +126,7 @@ export type BaseGeometry<
|
|
|
126
126
|
>
|
|
127
127
|
| boolean;
|
|
128
128
|
legend?: Partial<Record<'size' | 'color', Legend | boolean>> | boolean;
|
|
129
|
+
slider?: Partial<Record<'x' | 'y', any>>;
|
|
129
130
|
coordinates?: Coordinate[];
|
|
130
131
|
style?: Record<string, any>;
|
|
131
132
|
interactions?: Interaction[];
|
package/src/spec/interaction.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ChannelTypes } from 'spec';
|
|
1
2
|
import { InteractionComponent } from '../runtime';
|
|
2
3
|
import { ActiveRegionAction, PoptipAction, TooltipAction } from './action';
|
|
3
4
|
import { FisheyeCoordinate } from './coordinate';
|
|
@@ -12,7 +13,8 @@ export type Interaction =
|
|
|
12
13
|
| TooltipInteraction
|
|
13
14
|
| FisheyeInteraction
|
|
14
15
|
| ChartIndex
|
|
15
|
-
| CustomInteraction
|
|
16
|
+
| CustomInteraction
|
|
17
|
+
| LegendFilterInteraction;
|
|
16
18
|
// | ElementListHighlightInteraction
|
|
17
19
|
// | LegendActiveInteraction
|
|
18
20
|
// | LegendHighlightInteraction
|
|
@@ -34,6 +36,7 @@ export type InteractionTypes =
|
|
|
34
36
|
| 'elementSelectByColor'
|
|
35
37
|
| 'fisheye'
|
|
36
38
|
| 'tooltip'
|
|
39
|
+
| 'legendFilter'
|
|
37
40
|
| InteractionComponent;
|
|
38
41
|
// | 'elementListHighlight'
|
|
39
42
|
// | 'legendActive'
|
|
@@ -94,6 +97,12 @@ export type ElementHighlightByColorInteraction = {
|
|
|
94
97
|
color?: string;
|
|
95
98
|
} & Record<`${'link' | 'highlighted' | 'unhighlighted'}${any}`, any>;
|
|
96
99
|
|
|
100
|
+
export type LegendFilterInteraction = {
|
|
101
|
+
type?: 'legendFilter';
|
|
102
|
+
channel?: ChannelTypes;
|
|
103
|
+
[key: string]: any; // @todo
|
|
104
|
+
} & Record<`${'marker' | 'label'}Unselected${any}`, any>;
|
|
105
|
+
|
|
97
106
|
export type ChartIndex = {
|
|
98
107
|
type?: 'chartIndex';
|
|
99
108
|
wait?: number;
|
|
@@ -117,6 +126,7 @@ export type TooltipInteraction = Omit<TooltipAction, 'type'> & {
|
|
|
117
126
|
type?: 'tooltip';
|
|
118
127
|
shared?: boolean;
|
|
119
128
|
series?: boolean;
|
|
129
|
+
facet?: boolean;
|
|
120
130
|
// @todo
|
|
121
131
|
item?: any;
|
|
122
132
|
};
|
package/src/spec/transform.ts
CHANGED
|
@@ -24,7 +24,8 @@ export type Transform =
|
|
|
24
24
|
| BinXTransform
|
|
25
25
|
| BinTransform
|
|
26
26
|
| SampleTransform
|
|
27
|
-
| FlexXTransform
|
|
27
|
+
| FlexXTransform
|
|
28
|
+
| FilterTransform;
|
|
28
29
|
|
|
29
30
|
export type TransformTypes =
|
|
30
31
|
| 'dodgeX'
|
|
@@ -48,6 +49,7 @@ export type TransformTypes =
|
|
|
48
49
|
| 'flexX'
|
|
49
50
|
| 'pack'
|
|
50
51
|
| 'sample'
|
|
52
|
+
| 'filter'
|
|
51
53
|
| TransformComponent;
|
|
52
54
|
|
|
53
55
|
export type TransformOrder =
|
|
@@ -255,3 +257,9 @@ export type SampleTransform = {
|
|
|
255
257
|
groupBy?: string | string[];
|
|
256
258
|
thresholds?: number;
|
|
257
259
|
} & { [key in ChannelTypes]?: Reducer };
|
|
260
|
+
|
|
261
|
+
export type FilterTransform = {
|
|
262
|
+
type?: 'filter';
|
|
263
|
+
} & {
|
|
264
|
+
[key in ChannelTypes]?: any[] | ((v: Primitive) => boolean);
|
|
265
|
+
};
|