@antv/g6 5.0.0-beta.1 → 5.0.0-beta.2
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/g6.min.js +2 -2
- package/esm/constant/shape.d.ts +2 -1
- package/esm/constant/shape.js.map +1 -1
- package/esm/index.d.ts +5 -1
- package/esm/index.js +3 -2
- package/esm/index.js.map +1 -1
- package/esm/item/edge.d.ts +1 -1
- package/esm/item/edge.js.map +1 -1
- package/esm/runtime/controller/data.js +1 -1
- package/esm/runtime/controller/data.js.map +1 -1
- package/esm/stdlib/behavior/activate-relations.d.ts +9 -9
- package/esm/stdlib/behavior/activate-relations.js +6 -6
- package/esm/stdlib/behavior/activate-relations.js.map +1 -1
- package/esm/stdlib/index.d.ts +3 -0
- package/esm/stdlib/index.js +4 -1
- package/esm/stdlib/index.js.map +1 -1
- package/esm/stdlib/item/edge/base.d.ts +9 -0
- package/esm/stdlib/item/edge/base.js +3 -0
- package/esm/stdlib/item/edge/base.js.map +1 -1
- package/esm/stdlib/item/edge/line.js +5 -2
- package/esm/stdlib/item/edge/line.js.map +1 -1
- package/esm/stdlib/item/edge/polyline.d.ts +3 -2
- package/esm/stdlib/item/edge/polyline.js +3 -3
- package/esm/stdlib/item/edge/polyline.js.map +1 -1
- package/esm/stdlib/item/node/hexagon.js +6 -3
- package/esm/stdlib/item/node/hexagon.js.map +1 -1
- package/esm/stdlib/plugin/index.d.ts +1 -0
- package/esm/stdlib/plugin/index.js +1 -0
- package/esm/stdlib/plugin/index.js.map +1 -1
- package/esm/stdlib/plugin/legend/index.js +1 -1
- package/esm/stdlib/plugin/legend/index.js.map +1 -1
- package/esm/stdlib/plugin/timebar/index.d.ts +98 -0
- package/esm/stdlib/plugin/timebar/index.js +303 -0
- package/esm/stdlib/plugin/timebar/index.js.map +1 -0
- package/esm/stdlib/plugin/toolbar/index.js +1 -1
- package/esm/stdlib/plugin/toolbar/index.js.map +1 -1
- package/esm/stdlib/plugin/types.d.ts +1 -0
- package/esm/types/common.d.ts +1 -0
- package/esm/util/canvas.d.ts +2 -2
- package/esm/util/canvas.js +5 -11
- package/esm/util/canvas.js.map +1 -1
- package/esm/util/router.d.ts +4 -3
- package/esm/util/router.js +8 -5
- package/esm/util/router.js.map +1 -1
- package/esm/util/shape.d.ts +2 -2
- package/esm/util/shape.js +3 -3
- package/esm/util/shape.js.map +1 -1
- package/lib/constant/shape.d.ts +2 -1
- package/lib/constant/shape.js.map +1 -1
- package/lib/index.d.ts +5 -1
- package/lib/index.js +4 -2
- package/lib/index.js.map +1 -1
- package/lib/item/edge.d.ts +1 -1
- package/lib/item/edge.js.map +1 -1
- package/lib/runtime/controller/data.js +1 -1
- package/lib/runtime/controller/data.js.map +1 -1
- package/lib/stdlib/behavior/activate-relations.d.ts +9 -9
- package/lib/stdlib/behavior/activate-relations.js +6 -6
- package/lib/stdlib/behavior/activate-relations.js.map +1 -1
- package/lib/stdlib/index.d.ts +3 -0
- package/lib/stdlib/index.js +4 -1
- package/lib/stdlib/index.js.map +1 -1
- package/lib/stdlib/item/edge/base.d.ts +9 -0
- package/lib/stdlib/item/edge/base.js +3 -0
- package/lib/stdlib/item/edge/base.js.map +1 -1
- package/lib/stdlib/item/edge/line.js +5 -2
- package/lib/stdlib/item/edge/line.js.map +1 -1
- package/lib/stdlib/item/edge/polyline.d.ts +3 -2
- package/lib/stdlib/item/edge/polyline.js +3 -3
- package/lib/stdlib/item/edge/polyline.js.map +1 -1
- package/lib/stdlib/item/node/hexagon.js +6 -3
- package/lib/stdlib/item/node/hexagon.js.map +1 -1
- package/lib/stdlib/plugin/index.d.ts +1 -0
- package/lib/stdlib/plugin/index.js +1 -0
- package/lib/stdlib/plugin/index.js.map +1 -1
- package/lib/stdlib/plugin/legend/index.js +1 -1
- package/lib/stdlib/plugin/legend/index.js.map +1 -1
- package/lib/stdlib/plugin/timebar/index.d.ts +98 -0
- package/lib/stdlib/plugin/timebar/index.js +307 -0
- package/lib/stdlib/plugin/timebar/index.js.map +1 -0
- package/lib/stdlib/plugin/toolbar/index.js +1 -1
- package/lib/stdlib/plugin/toolbar/index.js.map +1 -1
- package/lib/stdlib/plugin/types.d.ts +1 -0
- package/lib/types/common.d.ts +1 -0
- package/lib/util/canvas.d.ts +2 -2
- package/lib/util/canvas.js +5 -11
- package/lib/util/canvas.js.map +1 -1
- package/lib/util/router.d.ts +4 -3
- package/lib/util/router.js +8 -5
- package/lib/util/router.js.map +1 -1
- package/lib/util/shape.d.ts +2 -2
- package/lib/util/shape.js +3 -3
- package/lib/util/shape.js.map +1 -1
- package/package.json +9 -7
- package/src/constant/shape.ts +2 -1
- package/src/index.ts +4 -2
- package/src/item/edge.ts +1 -1
- package/src/runtime/controller/data.ts +1 -1
- package/src/stdlib/behavior/activate-relations.ts +6 -6
- package/src/stdlib/index.ts +14 -2
- package/src/stdlib/item/edge/base.ts +9 -0
- package/src/stdlib/item/edge/line.ts +8 -2
- package/src/stdlib/item/edge/polyline.ts +7 -5
- package/src/stdlib/item/node/hexagon.ts +8 -4
- package/src/stdlib/plugin/index.ts +1 -0
- package/src/stdlib/plugin/legend/index.ts +1 -1
- package/src/stdlib/plugin/timebar/index.ts +473 -0
- package/src/stdlib/plugin/toolbar/index.ts +1 -1
- package/src/stdlib/plugin/types.ts +1 -0
- package/src/types/common.ts +1 -0
- package/src/util/canvas.ts +5 -5
- package/src/util/router.ts +15 -6
- package/src/util/shape.ts +8 -5
- package/src/api-doc/readme.md +0 -59
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import { createDom } from '@antv/dom-util';
|
|
2
|
+
import { Canvas } from '@antv/g';
|
|
3
|
+
import type { TimebarStyleProps } from '@antv/gui';
|
|
4
|
+
import { Timebar as GUITimebar } from '@antv/gui';
|
|
5
|
+
import { deepMix } from '@antv/util';
|
|
6
|
+
import type { EdgeModel, NodeModel } from '../../../types';
|
|
7
|
+
import { GraphData, IGraph } from '../../../types';
|
|
8
|
+
import type { Padding } from '../../../types/common';
|
|
9
|
+
import type { ITEM_TYPE } from '../../../types/item';
|
|
10
|
+
import { Plugin as Base, IPluginBaseConfig } from '../../../types/plugin';
|
|
11
|
+
import { createCanvas } from '../../../util/canvas';
|
|
12
|
+
import { formatPadding } from '../../../util/shape';
|
|
13
|
+
|
|
14
|
+
// -- start -- Adapt to GUI -- start --
|
|
15
|
+
type SubStyleProps<T, Prefix extends string> = {
|
|
16
|
+
[K in keyof T as K extends `${Prefix}${infer Rest}`
|
|
17
|
+
? Uncapitalize<Rest>
|
|
18
|
+
: never]: T[K];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const prefixStyleProps = (style: Record<string, any>, prefix: string) => {
|
|
22
|
+
const result: Record<string, any> = {};
|
|
23
|
+
Object.entries(style).forEach(([key, val]) => {
|
|
24
|
+
const newKey = `${prefix}${key[0].toUpperCase()}${key.slice(1)}`;
|
|
25
|
+
result[newKey] = val;
|
|
26
|
+
});
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
// -- end -- Adapt to GUI -- end --
|
|
30
|
+
|
|
31
|
+
type Model = NodeModel | EdgeModel;
|
|
32
|
+
type ItemType = Extract<ITEM_TYPE, 'node' | 'edge'>;
|
|
33
|
+
|
|
34
|
+
type STATE = {
|
|
35
|
+
nodes: { enter: Model[]; exit: Model[] };
|
|
36
|
+
edges: { enter: Model[]; exit: Model[] };
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type Datum = Record<string, any>;
|
|
40
|
+
|
|
41
|
+
export interface TimebarConfig
|
|
42
|
+
extends IPluginBaseConfig,
|
|
43
|
+
Pick<
|
|
44
|
+
TimebarStyleProps,
|
|
45
|
+
| 'playMode'
|
|
46
|
+
| 'values'
|
|
47
|
+
| 'labelFormatter'
|
|
48
|
+
| 'interval'
|
|
49
|
+
| 'loop'
|
|
50
|
+
| 'onChange'
|
|
51
|
+
| 'onBackward'
|
|
52
|
+
| 'onPause'
|
|
53
|
+
| 'onPlay'
|
|
54
|
+
| 'onForward'
|
|
55
|
+
| 'onSelectionTypeChange'
|
|
56
|
+
| 'onChartTypeChange'
|
|
57
|
+
> {
|
|
58
|
+
/** specific the x pos of timebar manually */
|
|
59
|
+
x?: number;
|
|
60
|
+
/** specific the y pos of timebar manually */
|
|
61
|
+
y?: number;
|
|
62
|
+
/** width of timebar */
|
|
63
|
+
width?: number;
|
|
64
|
+
/** height of timebar */
|
|
65
|
+
height?: number;
|
|
66
|
+
/** padding of timebar */
|
|
67
|
+
padding?: Padding;
|
|
68
|
+
/** data of timebar */
|
|
69
|
+
data: Datum[];
|
|
70
|
+
/**
|
|
71
|
+
* position of the timebar relative to graph
|
|
72
|
+
* @description it will be failure if x or y is specified
|
|
73
|
+
*/
|
|
74
|
+
position?: 'bottom';
|
|
75
|
+
timebarType: TimebarStyleProps['type'];
|
|
76
|
+
/**
|
|
77
|
+
* how to filter the graph data, default is modify
|
|
78
|
+
* @description modify: the graph data will be changed by addItem or removeItem
|
|
79
|
+
* @description visibility: the graph item will be hidden or shown
|
|
80
|
+
*/
|
|
81
|
+
filterType?: 'modify' | 'visibility';
|
|
82
|
+
/** filter item by specific types */
|
|
83
|
+
filterItemTypes?: ItemType[];
|
|
84
|
+
/** describe how to get time from item */
|
|
85
|
+
getTimeFromItem?: (model: Model, itemType: ItemType) => number | Date;
|
|
86
|
+
/** describe how to get time from timebar data */
|
|
87
|
+
getTimeFromData?: (datum: Datum) => number | Date;
|
|
88
|
+
/** describe how to get value from timebar data */
|
|
89
|
+
getValueFromData?: (datum: Datum) => number;
|
|
90
|
+
/** determines whether an item should be ignored during the filtering process */
|
|
91
|
+
shouldIgnore?: (
|
|
92
|
+
model: Model,
|
|
93
|
+
itemType: ItemType,
|
|
94
|
+
dateRange: number | Date | [number, number] | [Date, Date],
|
|
95
|
+
) => boolean;
|
|
96
|
+
/**
|
|
97
|
+
* manage graph manually when timebar range/value change
|
|
98
|
+
* @description if it is not specified, the graph will be filtered by default
|
|
99
|
+
*/
|
|
100
|
+
filter?: (
|
|
101
|
+
graph: IGraph,
|
|
102
|
+
values: Parameters<TimebarStyleProps['onChange']>,
|
|
103
|
+
) => void;
|
|
104
|
+
axisStyle?: SubStyleProps<TimebarStyleProps, 'axis'>;
|
|
105
|
+
chartStyle?: SubStyleProps<TimebarStyleProps, 'chart'>;
|
|
106
|
+
controllerStyle?: SubStyleProps<TimebarStyleProps, 'controller'>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const prospectiveTimeKeys = ['timestamp', 'time', 'date', 'datetime'];
|
|
110
|
+
const prospectiveValueKeys = ['value', 'date'];
|
|
111
|
+
|
|
112
|
+
const tryToGet = <T = any>(
|
|
113
|
+
datum: Datum,
|
|
114
|
+
optionsKeys: string[],
|
|
115
|
+
defaultValue?: T,
|
|
116
|
+
) => {
|
|
117
|
+
for (let i = 0; i < optionsKeys.length; i++) {
|
|
118
|
+
const key = optionsKeys[i];
|
|
119
|
+
const val = datum?.[key] as T;
|
|
120
|
+
if (val) return val;
|
|
121
|
+
}
|
|
122
|
+
return defaultValue;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export class Timebar extends Base {
|
|
126
|
+
private wrapper: HTMLDivElement;
|
|
127
|
+
private canvas: Canvas;
|
|
128
|
+
private timebar: GUITimebar;
|
|
129
|
+
private graphDataCache: GraphData;
|
|
130
|
+
private itemTypes: ItemType[] = ['node', 'edge'];
|
|
131
|
+
|
|
132
|
+
private get padding() {
|
|
133
|
+
return formatPadding(this.options.padding);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private get containerShape() {
|
|
137
|
+
const { width, height } = this.options;
|
|
138
|
+
const [top, right, bottom, left] = this.padding;
|
|
139
|
+
/** extra height for speed select to avoid being cut */
|
|
140
|
+
const extraHeightForSpeedSelect = 30;
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
x: left,
|
|
144
|
+
y: top,
|
|
145
|
+
width: width + left + right,
|
|
146
|
+
height: height + top + bottom + extraHeightForSpeedSelect,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private get data() {
|
|
151
|
+
const { graph } = this;
|
|
152
|
+
return { nodes: graph.getAllNodesData(), edges: graph.getAllEdgesData() };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
constructor(options?: TimebarConfig) {
|
|
156
|
+
super(options);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public getDefaultCfgs(): TimebarConfig {
|
|
160
|
+
// @ts-ignore
|
|
161
|
+
const configuration: TimebarConfig = {
|
|
162
|
+
axisStyle: {},
|
|
163
|
+
className: 'g6-component-timebar',
|
|
164
|
+
controllerStyle: {},
|
|
165
|
+
data: [],
|
|
166
|
+
filterItemTypes: ['node'],
|
|
167
|
+
filterType: 'modify',
|
|
168
|
+
getTimeFromItem: (model) =>
|
|
169
|
+
tryToGet<number | Date>(model.data, prospectiveTimeKeys, undefined),
|
|
170
|
+
getTimeFromData: (datum) =>
|
|
171
|
+
tryToGet<number | Date>(datum, prospectiveTimeKeys, undefined),
|
|
172
|
+
getValueFromItem: (datum) => tryToGet(datum, prospectiveValueKeys, 0),
|
|
173
|
+
height: 60,
|
|
174
|
+
padding: 10,
|
|
175
|
+
position: 'bottom',
|
|
176
|
+
timebarType: 'time',
|
|
177
|
+
width: 200,
|
|
178
|
+
};
|
|
179
|
+
return configuration;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public getEvents() {
|
|
183
|
+
return {
|
|
184
|
+
afterrender: this.updateTimebar,
|
|
185
|
+
afterchangedata: this.handleDataChange,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public init(graph: IGraph) {
|
|
190
|
+
super.init(graph);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** reset */
|
|
194
|
+
public reset() {
|
|
195
|
+
this.timebar.reset();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** backward */
|
|
199
|
+
public backward() {
|
|
200
|
+
this.timebar.backward();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** play */
|
|
204
|
+
public play() {
|
|
205
|
+
this.timebar.play();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** pause */
|
|
209
|
+
public pause() {
|
|
210
|
+
this.timebar.pause();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** forward */
|
|
214
|
+
public forward() {
|
|
215
|
+
this.timebar.forward();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
public destroy() {
|
|
219
|
+
this.canvas.destroy();
|
|
220
|
+
super.destroy();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Get the container of timebar */
|
|
224
|
+
private get container(): HTMLElement {
|
|
225
|
+
const { container } = this.options;
|
|
226
|
+
if (container instanceof HTMLElement) return container;
|
|
227
|
+
const element = document.getElementById(container);
|
|
228
|
+
if (element) return element;
|
|
229
|
+
return this.graph.container;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Create a wrapper div for timebar */
|
|
233
|
+
private createWrapper() {
|
|
234
|
+
const { options } = this;
|
|
235
|
+
const { className } = options;
|
|
236
|
+
const { width, height } = this.containerShape;
|
|
237
|
+
|
|
238
|
+
const wrapper = (HTMLDivElement = createDom(
|
|
239
|
+
`<div class="${className}" style="width: ${width}px;height: ${height}"></div>`,
|
|
240
|
+
));
|
|
241
|
+
this.container.appendChild(wrapper);
|
|
242
|
+
return wrapper;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private createCanvas() {
|
|
246
|
+
const { width, height } = this.containerShape;
|
|
247
|
+
const canvas = createCanvas(
|
|
248
|
+
'canvas',
|
|
249
|
+
this.wrapper,
|
|
250
|
+
width,
|
|
251
|
+
height,
|
|
252
|
+
undefined,
|
|
253
|
+
{ background: '#fff' },
|
|
254
|
+
);
|
|
255
|
+
return canvas;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private updateTimebar() {
|
|
259
|
+
if (!this.wrapper) this.wrapper = this.createWrapper();
|
|
260
|
+
if (!this.canvas) this.canvas = this.createCanvas();
|
|
261
|
+
|
|
262
|
+
this.canvas.ready.then(() => {
|
|
263
|
+
this.renderTimebar();
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private updatePosition() {
|
|
268
|
+
const { graph } = this;
|
|
269
|
+
const { x, y } = this.options;
|
|
270
|
+
const { width } = this.containerShape;
|
|
271
|
+
const { position } = this.options;
|
|
272
|
+
const [graphWidth, graphHeight] = graph.getSize();
|
|
273
|
+
|
|
274
|
+
// if x or y is not specified, calculate the position
|
|
275
|
+
if (!x && !y) {
|
|
276
|
+
switch (position) {
|
|
277
|
+
case 'bottom':
|
|
278
|
+
this.wrapper.style.top = graphHeight + 'px';
|
|
279
|
+
this.wrapper.style.left = (graphWidth - width) / 2 + 'px';
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private renderTimebar() {
|
|
286
|
+
const { x, y } = this.containerShape;
|
|
287
|
+
const {
|
|
288
|
+
x: ignoreX,
|
|
289
|
+
y: ignoreY,
|
|
290
|
+
axisStyle = {},
|
|
291
|
+
chartStyle = {},
|
|
292
|
+
className,
|
|
293
|
+
container,
|
|
294
|
+
controllerStyle = {},
|
|
295
|
+
data: userDefinedData,
|
|
296
|
+
filter,
|
|
297
|
+
filterItemTypes,
|
|
298
|
+
filterType,
|
|
299
|
+
getTimeFromData,
|
|
300
|
+
getTimeFromItem,
|
|
301
|
+
getValueFromData,
|
|
302
|
+
onChange,
|
|
303
|
+
padding,
|
|
304
|
+
position,
|
|
305
|
+
shouldIgnore,
|
|
306
|
+
timebarType,
|
|
307
|
+
type,
|
|
308
|
+
...userDefinedOptions
|
|
309
|
+
} = this.options;
|
|
310
|
+
|
|
311
|
+
const data = userDefinedData.map((datum) => ({
|
|
312
|
+
time: getTimeFromData?.(datum) || datum?.time,
|
|
313
|
+
value: getValueFromData?.(datum) || datum?.value,
|
|
314
|
+
}));
|
|
315
|
+
|
|
316
|
+
const style: TimebarStyleProps = {
|
|
317
|
+
x,
|
|
318
|
+
y,
|
|
319
|
+
data,
|
|
320
|
+
type: timebarType,
|
|
321
|
+
onChange: (values) => {
|
|
322
|
+
this.handleValuesChange(values);
|
|
323
|
+
onChange?.(values);
|
|
324
|
+
},
|
|
325
|
+
...prefixStyleProps(axisStyle, 'axis'),
|
|
326
|
+
...prefixStyleProps(controllerStyle, 'controller'),
|
|
327
|
+
...prefixStyleProps(chartStyle, 'chart'),
|
|
328
|
+
...userDefinedOptions,
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
if (!this.timebar)
|
|
332
|
+
this.timebar = this.canvas.appendChild(new GUITimebar({ style }));
|
|
333
|
+
else this.timebar.update(style);
|
|
334
|
+
this.updatePosition();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private handleDataChange() {
|
|
338
|
+
this.graphDataCache = deepMix({}, this.data);
|
|
339
|
+
this.updateTimebar();
|
|
340
|
+
this.processValuesChange();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
private handleValuesChange(
|
|
344
|
+
values: Parameters<TimebarStyleProps['onChange']>[0],
|
|
345
|
+
) {
|
|
346
|
+
this.processValuesChange(values);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private processValuesChange(
|
|
350
|
+
values: Parameters<TimebarStyleProps['onChange']>[0] = this.timebar.values,
|
|
351
|
+
) {
|
|
352
|
+
const { filter } = this.options;
|
|
353
|
+
if (filter) filter(values);
|
|
354
|
+
else this.filter(values);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private filter(values: Parameters<TimebarStyleProps['onChange']>[0]) {
|
|
358
|
+
// cache data
|
|
359
|
+
if (
|
|
360
|
+
!this.graphDataCache ||
|
|
361
|
+
(this.graphDataCache.nodes.length === 0 &&
|
|
362
|
+
this.graphDataCache.edges.length === 0)
|
|
363
|
+
) {
|
|
364
|
+
this.graphDataCache = deepMix({}, this.data);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const { filterType, filterItemTypes, shouldIgnore, getTimeFromItem } =
|
|
368
|
+
this.options;
|
|
369
|
+
|
|
370
|
+
const isTimeInValues = (time: number | Date) => {
|
|
371
|
+
if (Array.isArray(values)) {
|
|
372
|
+
return time >= values[0] && time <= values[1];
|
|
373
|
+
}
|
|
374
|
+
return time === values;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* store which items to be existed and exited
|
|
379
|
+
* if modify, enter item will be added, exit item will be removed
|
|
380
|
+
* if visibility, enter item will be shown, exit item will be hidden
|
|
381
|
+
*/
|
|
382
|
+
const state: STATE = {
|
|
383
|
+
nodes: { enter: [], exit: [] },
|
|
384
|
+
edges: { enter: [], exit: [] },
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
const filterByType = (type: ItemType) => {
|
|
388
|
+
const key = `${type}s`;
|
|
389
|
+
if (!(key in this.data)) return;
|
|
390
|
+
|
|
391
|
+
const items = this.graphDataCache[key];
|
|
392
|
+
items.forEach((item) => {
|
|
393
|
+
const queryItem =
|
|
394
|
+
type === 'node'
|
|
395
|
+
? this.graph.getNodeData(item.id)
|
|
396
|
+
: this.graph.getEdgeData(item.id);
|
|
397
|
+
const time = getTimeFromItem(item, type);
|
|
398
|
+
const isItemExists = queryItem !== undefined;
|
|
399
|
+
const isItemVisible = queryItem && this.graph.getItemVisible(item.id);
|
|
400
|
+
|
|
401
|
+
const isEdgeDangling = () => {
|
|
402
|
+
if (type !== 'edge') return false;
|
|
403
|
+
const { source, target } = item as EdgeModel;
|
|
404
|
+
// enter nodes and current nodes
|
|
405
|
+
const nodes = [...state.nodes.enter, ...this.data.nodes];
|
|
406
|
+
const isSourceExists = nodes.find((n) => n.id === source);
|
|
407
|
+
const isTargetExists = nodes.find((n) => n.id === target);
|
|
408
|
+
return !isSourceExists || !isTargetExists;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
const addItem = () => {
|
|
412
|
+
if (filterType === 'modify' && !isItemExists) {
|
|
413
|
+
state[key].enter.push(item);
|
|
414
|
+
} else if (filterType === 'visibility' && !isItemVisible) {
|
|
415
|
+
state[key].enter.push(item);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
const removeItem = () => {
|
|
420
|
+
if (filterType === 'modify' && isItemExists) {
|
|
421
|
+
state[key].exit.push(item);
|
|
422
|
+
} else if (filterType === 'visibility' && isItemVisible) {
|
|
423
|
+
state[key].exit.push(item);
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
if (isEdgeDangling()) return;
|
|
428
|
+
if (!filterItemTypes.includes(type)) addItem();
|
|
429
|
+
// if time is undefined, always enter
|
|
430
|
+
else if (time === undefined) addItem();
|
|
431
|
+
// ignore, push to enter
|
|
432
|
+
else if (shouldIgnore?.(item, type, values)) addItem();
|
|
433
|
+
// time in range, push to enter
|
|
434
|
+
else if (isTimeInValues(time)) addItem();
|
|
435
|
+
// push to exit
|
|
436
|
+
else if (this.data[key].find(({ id }) => id === item.id)) removeItem();
|
|
437
|
+
});
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
this.itemTypes.forEach((type) => filterByType(type));
|
|
441
|
+
|
|
442
|
+
this.mutateGraph(state);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
private mutateGraph(state: STATE) {
|
|
446
|
+
const { filterType } = this.options;
|
|
447
|
+
const mutateByType = (type: ItemType) => {
|
|
448
|
+
const { enter, exit } = state[`${type}s`];
|
|
449
|
+
|
|
450
|
+
if (filterType === 'modify') {
|
|
451
|
+
this.graph.addData(type, enter);
|
|
452
|
+
this.graph.removeData(
|
|
453
|
+
type,
|
|
454
|
+
exit.map((n) => n.id),
|
|
455
|
+
);
|
|
456
|
+
} else {
|
|
457
|
+
this.graph.showItem(enter.map((e) => e.id));
|
|
458
|
+
this.graph.hideItem(exit.map((e) => e.id));
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
const preventConsoleError = (callback: () => void) => {
|
|
463
|
+
const originError = console.error;
|
|
464
|
+
console.error = () => {};
|
|
465
|
+
callback?.();
|
|
466
|
+
console.error = originError;
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
preventConsoleError(() => {
|
|
470
|
+
this.itemTypes.forEach((type) => mutateByType(type));
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
}
|
package/src/types/common.ts
CHANGED
package/src/util/canvas.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Canvas } from '@antv/g';
|
|
1
|
+
import { Canvas, CanvasConfig } from '@antv/g';
|
|
2
2
|
import { Renderer as CanvasRenderer } from '@antv/g-canvas';
|
|
3
|
-
import { Renderer as SVGRenderer } from '@antv/g-svg';
|
|
4
|
-
import { Renderer as WebGLRenderer } from '@antv/g-webgl';
|
|
5
3
|
import { Plugin as Plugin3D } from '@antv/g-plugin-3d';
|
|
6
4
|
import { Plugin as DragNDropPlugin } from '@antv/g-plugin-dragndrop';
|
|
5
|
+
import { Renderer as SVGRenderer } from '@antv/g-svg';
|
|
6
|
+
import { Renderer as WebGLRenderer } from '@antv/g-webgl';
|
|
7
7
|
import { RendererName } from '../types/render';
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -21,7 +21,7 @@ export const createCanvas = (
|
|
|
21
21
|
width: number,
|
|
22
22
|
height: number,
|
|
23
23
|
pixelRatio?: number,
|
|
24
|
-
|
|
24
|
+
canvasConfig: Partial<CanvasConfig> = {},
|
|
25
25
|
): Canvas => {
|
|
26
26
|
let renderer: any;
|
|
27
27
|
switch (rendererType.toLowerCase()) {
|
|
@@ -53,7 +53,7 @@ export const createCanvas = (
|
|
|
53
53
|
devicePixelRatio: pixelRatio,
|
|
54
54
|
renderer,
|
|
55
55
|
supportsMutipleCanvasesInOneContainer: true,
|
|
56
|
-
|
|
56
|
+
...canvasConfig,
|
|
57
57
|
});
|
|
58
58
|
};
|
|
59
59
|
|
package/src/util/router.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { deepMix, map } from '@antv/util';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { ID } from '../types';
|
|
3
|
+
import { Point, PolyPoint } from '../types/common';
|
|
4
4
|
import Node from '../item/node';
|
|
5
5
|
import { simplePathFinder, SortedArray } from './polyline';
|
|
6
6
|
import {
|
|
@@ -14,6 +14,7 @@ import { getLineIntersect } from './shape';
|
|
|
14
14
|
import { eulerDist, manhattanDist } from './math';
|
|
15
15
|
|
|
16
16
|
export interface RouterCfg {
|
|
17
|
+
name: 'orth' | 'er';
|
|
17
18
|
/** Spacing between lines and points */
|
|
18
19
|
offset?: number;
|
|
19
20
|
/** Grid size */
|
|
@@ -46,6 +47,7 @@ export interface RouterCfg {
|
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
const defaultCfg: RouterCfg = {
|
|
50
|
+
name: 'orth',
|
|
49
51
|
obstacleAvoidance: false,
|
|
50
52
|
offset: 20,
|
|
51
53
|
maxAllowedDirectionChange: Math.PI / 2,
|
|
@@ -73,6 +75,7 @@ const straightPath = (start: PolyPoint, end: PolyPoint): PolyPoint[] => {
|
|
|
73
75
|
};
|
|
74
76
|
|
|
75
77
|
export const octolinearCfg: RouterCfg = {
|
|
78
|
+
name: 'er',
|
|
76
79
|
maxAllowedDirectionChange: Math.PI / 4,
|
|
77
80
|
directions: [
|
|
78
81
|
{ stepX: 1, stepY: 0 },
|
|
@@ -85,7 +88,7 @@ export const octolinearCfg: RouterCfg = {
|
|
|
85
88
|
{ stepX: 1, stepY: -1 },
|
|
86
89
|
],
|
|
87
90
|
distFunc: eulerDist,
|
|
88
|
-
fallbackRoute:
|
|
91
|
+
fallbackRoute: simplePathFinder,
|
|
89
92
|
};
|
|
90
93
|
|
|
91
94
|
/**
|
|
@@ -368,6 +371,7 @@ const getControlPoints = (
|
|
|
368
371
|
|
|
369
372
|
/** Find the shortest path computed by A* routing algorithm */
|
|
370
373
|
export const pathFinder = (
|
|
374
|
+
points: Point[],
|
|
371
375
|
sourceNodeId: ID,
|
|
372
376
|
targetNodeId: ID,
|
|
373
377
|
nodeMap: Map<ID, Node>,
|
|
@@ -376,11 +380,16 @@ export const pathFinder = (
|
|
|
376
380
|
const startNode = nodeMap.get(sourceNodeId);
|
|
377
381
|
const endNode = nodeMap.get(targetNodeId);
|
|
378
382
|
|
|
379
|
-
const startPoint: PolyPoint = startNode
|
|
380
|
-
const endPoint: PolyPoint =
|
|
383
|
+
const startPoint: PolyPoint = startNode?.getPosition() || points[0];
|
|
384
|
+
const endPoint: PolyPoint =
|
|
385
|
+
endNode?.getPosition() || points[points.length - 1];
|
|
381
386
|
|
|
382
387
|
if (isNaN(startPoint.x) || isNaN(endPoint.x)) return [];
|
|
383
|
-
|
|
388
|
+
|
|
389
|
+
const defaultCfgs =
|
|
390
|
+
routerCfg.name === 'orth' ? defaultCfg : deepMix(defaultCfg, octolinearCfg);
|
|
391
|
+
|
|
392
|
+
const cfg: RouterCfg = deepMix(defaultCfgs, routerCfg);
|
|
384
393
|
|
|
385
394
|
if (!cfg.obstacleAvoidance) {
|
|
386
395
|
return cfg.fallbackRoute(startPoint, endPoint, startNode, endNode, cfg);
|
package/src/util/shape.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
} from '@antv/g';
|
|
17
17
|
import { clone, isArray, isNumber } from '@antv/util';
|
|
18
18
|
import { DEFAULT_LABEL_BG_PADDING } from '../constant';
|
|
19
|
-
import { Point } from '../types/common';
|
|
19
|
+
import { Padding, Point, StandardPadding } from '../types/common';
|
|
20
20
|
import { EdgeDisplayModel, EdgeShapeMap } from '../types/edge';
|
|
21
21
|
import {
|
|
22
22
|
GShapeStyle,
|
|
@@ -242,7 +242,10 @@ export const updateShapes = (
|
|
|
242
242
|
* @param defaultArr default value
|
|
243
243
|
* @returns [padding-top, padding-right, padding-bottom, padding-left]
|
|
244
244
|
*/
|
|
245
|
-
export const formatPadding = (
|
|
245
|
+
export const formatPadding = (
|
|
246
|
+
value: Padding,
|
|
247
|
+
defaultArr: StandardPadding = DEFAULT_LABEL_BG_PADDING,
|
|
248
|
+
): [number, number, number, number] => {
|
|
246
249
|
if (isArray(value)) {
|
|
247
250
|
switch (value.length) {
|
|
248
251
|
case 0:
|
|
@@ -250,11 +253,11 @@ export const formatPadding = (value, defaultArr = DEFAULT_LABEL_BG_PADDING) => {
|
|
|
250
253
|
case 1:
|
|
251
254
|
return [value[0], value[0], value[0], value[0]];
|
|
252
255
|
case 2:
|
|
253
|
-
return value
|
|
256
|
+
return [value[0], value[1], value[0], value[1]];
|
|
254
257
|
case 3:
|
|
255
|
-
return
|
|
258
|
+
return [value[0], value[1], value[2], value[1]];
|
|
256
259
|
default:
|
|
257
|
-
return value;
|
|
260
|
+
return [value[0], value[1], value[2], value[3] || value[4]];
|
|
258
261
|
}
|
|
259
262
|
}
|
|
260
263
|
if (isNumber(value)) return [value, value, value, value];
|
package/src/api-doc/readme.md
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
# G6 API Core Modules
|
|
2
|
-
|
|
3
|
-
- [behaviors](modules/behaviors.md)
|
|
4
|
-
- [graph](modules/graph.md)
|
|
5
|
-
- [item](modules/item.md)
|
|
6
|
-
- [layout](modules/layout.md)
|
|
7
|
-
- [plugins](modules/plugins.md)
|
|
8
|
-
- [types](modules/types.md)
|
|
9
|
-
- [util](modules/util.md)
|
|
10
|
-
|
|
11
|
-
```jsx
|
|
12
|
-
import { Graph, Util } from '@antv/g6';
|
|
13
|
-
const data = Util.mock(6).circle();
|
|
14
|
-
|
|
15
|
-
const graph = new Graph({
|
|
16
|
-
container: 'container',
|
|
17
|
-
width: 500,
|
|
18
|
-
height: 500,
|
|
19
|
-
data,
|
|
20
|
-
layout: {
|
|
21
|
-
type: 'grid',
|
|
22
|
-
},
|
|
23
|
-
plugins: [
|
|
24
|
-
{
|
|
25
|
-
key: 'minimap',
|
|
26
|
-
type: 'minimap',
|
|
27
|
-
size: [300, 200],
|
|
28
|
-
mode: 'delegate',
|
|
29
|
-
delegateStyle: {
|
|
30
|
-
fill: 'red',
|
|
31
|
-
},
|
|
32
|
-
className: 'g6-minimap-2',
|
|
33
|
-
viewportClassName: 'g6-minimap-viewport-2',
|
|
34
|
-
},
|
|
35
|
-
],
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const nodes = graph.getAllNodesData();
|
|
39
|
-
const nodes = graph.getAllNodesData();
|
|
40
|
-
|
|
41
|
-
graph.on('node:click', (e) => {});
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
# Introduce
|
|
45
|
-
|
|
46
|
-
In the above code snippet, we may want to know more about what G6 has to offer, in which case we will need the G6 API documentation
|
|
47
|
-
|
|
48
|
-
#### What types, methods and classes are exported in the '@antv/g6' package?
|
|
49
|
-
|
|
50
|
-
- [Graph](./classes/graph.Graph.md)
|
|
51
|
-
- [Util](./module/utils.md)
|
|
52
|
-
|
|
53
|
-
#### What is Specification which in `new Graph(Specification)`?
|
|
54
|
-
|
|
55
|
-
- [Specification](./interfaces/types.Specification.md)
|
|
56
|
-
|
|
57
|
-
#### What are the detailed parameters of the plugins ?
|
|
58
|
-
|
|
59
|
-
- [Plugins](./modules/plugins.md)
|