-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Expand file tree
/
Copy pathGraphSeries.ts
More file actions
522 lines (423 loc) · 14.6 KB
/
GraphSeries.ts
File metadata and controls
522 lines (423 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import SeriesData from '../../data/SeriesData';
import * as zrUtil from 'zrender/src/core/util';
import {defaultEmphasis} from '../../util/model';
import Model from '../../model/Model';
import createGraphFromNodeEdge from '../helper/createGraphFromNodeEdge';
import LegendVisualProvider from '../../visual/LegendVisualProvider';
import {
SeriesOption,
SeriesOnCartesianOptionMixin,
SeriesOnPolarOptionMixin,
SeriesOnCalendarOptionMixin,
SeriesOnGeoOptionMixin,
SeriesOnSingleOptionMixin,
OptionDataValue,
RoamOptionMixin,
SeriesLabelOption,
ItemStyleOption,
LineStyleOption,
SymbolOptionMixin,
BoxLayoutOptionMixin,
Dictionary,
SeriesLineLabelOption,
StatesOptionMixin,
GraphEdgeItemObject,
OptionDataValueNumeric,
CallbackDataParams,
DefaultEmphasisFocus
} from '../../util/types';
import SeriesModel from '../../model/Series';
import Graph from '../../data/Graph';
import GlobalModel from '../../model/Global';
import { VectorArray } from 'zrender/src/core/vector';
import { ForceLayoutInstance } from './forceLayout';
import { LineDataVisual } from '../../visual/commonVisualTypes';
import { createTooltipMarkup } from '../../component/tooltip/tooltipMarkup';
import { defaultSeriesFormatTooltip } from '../../component/tooltip/seriesFormatTooltip';
import {initCurvenessList, createEdgeMapForCurveness} from '../helper/multipleGraphEdgeHelper';
import Thumbnail, { ThumbnailOption } from './Thumbnail';
import tokens from '../../visual/tokens';
type GraphDataValue = OptionDataValue | OptionDataValue[];
interface GraphEdgeLineStyleOption extends LineStyleOption {
curveness?: number
}
export interface GraphNodeStateOption<TCbParams = never> {
itemStyle?: ItemStyleOption<TCbParams>
label?: SeriesLabelOption
}
interface ExtraEmphasisState {
focus?: DefaultEmphasisFocus | 'adjacency'
}
interface GraphNodeStatesMixin {
emphasis?: ExtraEmphasisState
}
interface GraphEdgeStatesMixin {
emphasis?: ExtraEmphasisState
}
export interface GraphNodeItemOption extends SymbolOptionMixin, GraphNodeStateOption,
StatesOptionMixin<GraphNodeStateOption, GraphNodeStatesMixin> {
id?: string
name?: string
value?: GraphDataValue
/**
* Fixed x position
*/
x?: number
/**
* Fixed y position
*/
y?: number
/**
* If this node is fixed during force layout.
*/
fixed?: boolean
/**
* Index or name of category
*/
category?: number | string
draggable?: boolean
cursor?: string
}
export interface GraphEdgeStateOption {
lineStyle?: GraphEdgeLineStyleOption
label?: SeriesLineLabelOption
}
export interface GraphEdgeItemOption extends
GraphEdgeStateOption,
StatesOptionMixin<GraphEdgeStateOption, GraphEdgeStatesMixin>,
GraphEdgeItemObject<OptionDataValueNumeric> {
value?: number
/**
* Symbol of both line ends
*/
symbol?: string | string[]
symbolSize?: number | number[]
ignoreForceLayout?: boolean
}
export interface GraphCategoryItemOption extends SymbolOptionMixin,
GraphNodeStateOption, StatesOptionMixin<GraphNodeStateOption, GraphNodeStatesMixin> {
name?: string
value?: OptionDataValue
}
export interface GraphSeriesOption
extends SeriesOption<GraphNodeStateOption<CallbackDataParams>, GraphNodeStatesMixin>,
SeriesOnCartesianOptionMixin, SeriesOnPolarOptionMixin, SeriesOnCalendarOptionMixin,
SeriesOnGeoOptionMixin, SeriesOnSingleOptionMixin,
SymbolOptionMixin<CallbackDataParams>,
RoamOptionMixin,
BoxLayoutOptionMixin {
type?: 'graph'
coordinateSystem?: string
legendHoverLink?: boolean
layout?: 'none' | 'force' | 'circular'
data?: (GraphNodeItemOption | GraphDataValue)[]
nodes?: (GraphNodeItemOption | GraphDataValue)[]
edges?: GraphEdgeItemOption[]
links?: GraphEdgeItemOption[]
categories?: GraphCategoryItemOption[]
/**
* @deprecated
*/
focusNodeAdjacency?: boolean
/**
* Symbol size scale ratio in roam
*/
nodeScaleRatio?: 0.6,
draggable?: boolean
edgeSymbol?: string | string[]
edgeSymbolSize?: number | number[]
edgeLabel?: SeriesLineLabelOption
label?: SeriesLabelOption
itemStyle?: ItemStyleOption<CallbackDataParams>
lineStyle?: GraphEdgeLineStyleOption
emphasis?: {
focus?: Exclude<GraphNodeItemOption['emphasis'], undefined>['focus']
scale?: boolean | number
label?: SeriesLabelOption
edgeLabel?: SeriesLabelOption
itemStyle?: ItemStyleOption
lineStyle?: LineStyleOption
}
blur?: {
label?: SeriesLabelOption
edgeLabel?: SeriesLabelOption
itemStyle?: ItemStyleOption
lineStyle?: LineStyleOption
}
select?: {
label?: SeriesLabelOption
edgeLabel?: SeriesLabelOption
itemStyle?: ItemStyleOption
lineStyle?: LineStyleOption
}
// Configuration of circular layout
circular?: {
rotateLabel?: boolean
}
// Configuration of force directed layout
force?: {
initLayout?: 'circular' | 'none'
// Node repulsion. Can be an array to represent range.
repulsion?: number | number[]
gravity?: number
// Initial friction
friction?: number
// Edge length. Can be an array to represent range.
edgeLength?: number | number[]
layoutAnimation?: boolean
}
/**
* auto curveness for multiple edge, invalid when `lineStyle.curveness` is set
*/
autoCurveness?: boolean | number | number[]
thumbnail?: ThumbnailOption
}
class GraphSeriesModel extends SeriesModel<GraphSeriesOption> {
static readonly type = 'series.graph';
readonly type = GraphSeriesModel.type;
static readonly dependencies = ['grid', 'polar', 'geo', 'singleAxis', 'calendar'];
private _categoriesData: SeriesData;
private _categoriesModels: Model<GraphCategoryItemOption>[];
/**
* Preserved points during layouting
*/
preservedPoints?: Dictionary<VectorArray>;
forceLayout?: ForceLayoutInstance;
hasSymbolVisual = true;
init(option: GraphSeriesOption) {
super.init.apply(this, arguments as any);
const self = this;
function getCategoriesData() {
return self._categoriesData;
}
// Provide data for legend select
this.legendVisualProvider = new LegendVisualProvider(
getCategoriesData, getCategoriesData
);
this.fillDataTextStyle(option.edges || option.links);
this._updateCategoriesData();
}
mergeOption(option: GraphSeriesOption) {
super.mergeOption.apply(this, arguments as any);
this.fillDataTextStyle(option.edges || option.links);
this._updateCategoriesData();
}
mergeDefaultAndTheme(option: GraphSeriesOption) {
super.mergeDefaultAndTheme.apply(this, arguments as any);
defaultEmphasis(option, 'edgeLabel', ['show']);
}
getInitialData(option: GraphSeriesOption, ecModel: GlobalModel): SeriesData {
const edges = option.edges || option.links || [];
const nodes = option.data || option.nodes || [];
const self = this;
if (nodes && edges) {
// auto curveness
initCurvenessList(this);
const graph = createGraphFromNodeEdge(nodes as GraphNodeItemOption[], edges, this, true, beforeLink);
zrUtil.each(graph.edges, function (edge) {
createEdgeMapForCurveness(edge.node1, edge.node2, this, edge.dataIndex);
}, this);
return graph.data;
}
function beforeLink(nodeData: SeriesData, edgeData: SeriesData) {
// Overwrite nodeData.getItemModel to
nodeData.wrapMethod('getItemModel', function (model) {
const categoriesModels = self._categoriesModels;
const categoryIdx = model.getShallow('category');
const categoryModel = categoriesModels[categoryIdx];
if (categoryModel) {
categoryModel.parentModel = model.parentModel;
model.parentModel = categoryModel;
}
return model;
});
// TODO Inherit resolveParentPath by default in Model#getModel?
const oldGetModel = Model.prototype.getModel;
function newGetModel(this: Model, path: any, parentModel?: Model) {
const model = oldGetModel.call(this, path, parentModel);
model.resolveParentPath = resolveParentPath;
return model;
}
edgeData.wrapMethod('getItemModel', function (model: Model) {
model.resolveParentPath = resolveParentPath;
model.getModel = newGetModel;
return model;
});
function resolveParentPath(this: Model, pathArr: readonly string[]): string[] {
if (pathArr && (pathArr[0] === 'label' || pathArr[1] === 'label')) {
const newPathArr = pathArr.slice();
if (pathArr[0] === 'label') {
newPathArr[0] = 'edgeLabel';
}
else if (pathArr[1] === 'label') {
newPathArr[1] = 'edgeLabel';
}
return newPathArr;
}
return pathArr as string[];
}
}
}
getGraph(): Graph {
return this.getData().graph;
}
getEdgeData() {
return this.getGraph().edgeData as SeriesData<GraphSeriesModel, LineDataVisual>;
}
getCategoriesData(): SeriesData {
return this._categoriesData;
}
formatTooltip(
dataIndex: number,
multipleSeries: boolean,
dataType: string
) {
if (dataType === 'edge') {
const nodeData = this.getData();
const params = this.getDataParams(dataIndex, dataType);
const edge = nodeData.graph.getEdgeByIndex(dataIndex);
const sourceName = nodeData.getName(edge.node1.dataIndex);
const targetName = nodeData.getName(edge.node2.dataIndex);
const nameArr = [];
sourceName != null && nameArr.push(sourceName);
targetName != null && nameArr.push(targetName);
return createTooltipMarkup('nameValue', {
name: nameArr.join(' > '),
value: params.value,
noValue: params.value == null
});
}
// dataType === 'node' or empty
const nodeMarkup = defaultSeriesFormatTooltip({
series: this,
dataIndex: dataIndex,
multipleSeries: multipleSeries
});
return nodeMarkup;
}
_updateCategoriesData() {
const categories = zrUtil.map(this.option.categories || [], function (category) {
// Data must has value
return category.value != null ? category : zrUtil.extend({
value: 0
}, category);
});
const categoriesData = new SeriesData(['value'], this);
categoriesData.initData(categories);
this._categoriesData = categoriesData;
this._categoriesModels = categoriesData.mapArray(function (idx) {
return categoriesData.getItemModel(idx);
});
}
setZoom(zoom: number) {
this.option.zoom = zoom;
}
setCenter(center: number[]) {
this.option.center = center;
}
isAnimationEnabled() {
return super.isAnimationEnabled()
// Not enable animation when do force layout
&& !(this.get('layout') === 'force' && this.get(['force', 'layoutAnimation']));
}
static defaultOption: GraphSeriesOption = {
// zlevel: 0,
z: 2,
coordinateSystem: 'view',
// Default option for all coordinate systems
// xAxisIndex: 0,
// yAxisIndex: 0,
// polarIndex: 0,
// geoIndex: 0,
legendHoverLink: true,
layout: null,
// Configuration of circular layout
circular: {
rotateLabel: false
},
// Configuration of force directed layout
force: {
initLayout: null,
// Node repulsion. Can be an array to represent range.
repulsion: [0, 50],
gravity: 0.1,
// Initial friction
friction: 0.6,
// Edge length. Can be an array to represent range.
edgeLength: 30,
layoutAnimation: true
},
left: 'center',
top: 'center',
// right: null,
// bottom: null,
// width: '80%',
// height: '80%',
symbol: 'circle',
symbolSize: 10,
edgeSymbol: ['none', 'none'],
edgeSymbolSize: 10,
edgeLabel: {
position: 'middle',
distance: 5
},
draggable: false,
roam: false,
// Default on center of graph
center: null,
zoom: 1,
// Symbol size scale ratio in roam
nodeScaleRatio: 0.6,
// cursor: null,
// categories: [],
// data: []
// Or
// nodes: []
//
// links: []
// Or
// edges: []
label: {
show: false,
formatter: '{b}'
},
itemStyle: {},
lineStyle: {
// Don't use tokens.color.border because of the opacity
color: tokens.color.neutral50,
width: 1,
opacity: 0.5
},
emphasis: {
scale: true,
label: {
show: true
}
},
select: {
itemStyle: {
borderColor: tokens.color.primary
}
},
thumbnail: Thumbnail.defaultOption
};
}
export default GraphSeriesModel;