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
/*
* 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.
*/
/**
* AUTO-GENERATED FILE. DO NOT MODIFY.
*/
/*
* 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 { forceLayout } from './forceHelper.js';
import { simpleLayout } from './simpleLayoutHelper.js';
import { circularLayout } from './circularLayoutHelper.js';
import { linearMap } from '../../util/number.js';
import * as vec2 from 'zrender/lib/core/vector.js';
import * as zrUtil from 'zrender/lib/core/util.js';
import { getCurvenessForEdge } from '../helper/multipleGraphEdgeHelper.js';
export default function graphForceLayout(ecModel) {
ecModel.eachSeriesByType('graph', function (graphSeries) {
var coordSys = graphSeries.coordinateSystem;
if (coordSys && coordSys.type !== 'view') {
return;
}
if (graphSeries.get('layout') === 'force') {
var preservedPoints_1 = graphSeries.preservedPoints || {};
var graph_1 = graphSeries.getGraph();
var nodeData_1 = graph_1.data;
var edgeData = graph_1.edgeData;
var forceModel = graphSeries.getModel('force');
var initLayout = forceModel.get('initLayout');
if (graphSeries.preservedPoints) {
nodeData_1.each(function (idx) {
var id = nodeData_1.getId(idx);
nodeData_1.setItemLayout(idx, preservedPoints_1[id] || [NaN, NaN]);
});
} else if (!initLayout || initLayout === 'none') {
simpleLayout(graphSeries);
} else if (initLayout === 'circular') {
circularLayout(graphSeries, 'value');
}
var nodeDataExtent_1 = nodeData_1.getDataExtent('value');
var edgeDataExtent_1 = edgeData.getDataExtent('value');
// let edgeDataExtent = edgeData.getDataExtent('value');
var repulsion = forceModel.get('repulsion');
var edgeLength = forceModel.get('edgeLength');
var repulsionArr_1 = zrUtil.isArray(repulsion) ? repulsion : [repulsion, repulsion];
var edgeLengthArr_1 = zrUtil.isArray(edgeLength) ? edgeLength : [edgeLength, edgeLength];
// Larger value has smaller length
edgeLengthArr_1 = [edgeLengthArr_1[1], edgeLengthArr_1[0]];
var nodes_1 = nodeData_1.mapArray('value', function (value, idx) {
var point = nodeData_1.getItemLayout(idx);
var rep = linearMap(value, nodeDataExtent_1, repulsionArr_1);
if (isNaN(rep)) {
rep = (repulsionArr_1[0] + repulsionArr_1[1]) / 2;
}
return {
w: rep,
rep: rep,
fixed: nodeData_1.getItemModel(idx).get('fixed'),
p: !point || isNaN(point[0]) || isNaN(point[1]) ? null : point
};
});
var edges = edgeData.mapArray('value', function (value, idx) {
var edge = graph_1.getEdgeByIndex(idx);
var d = linearMap(value, edgeDataExtent_1, edgeLengthArr_1);
if (isNaN(d)) {
d = (edgeLengthArr_1[0] + edgeLengthArr_1[1]) / 2;
}
var edgeModel = edge.getModel();
var curveness = zrUtil.retrieve3(edge.getModel().get(['lineStyle', 'curveness']), -getCurvenessForEdge(edge, graphSeries, idx, true), 0);
return {
n1: nodes_1[edge.node1.dataIndex],
n2: nodes_1[edge.node2.dataIndex],
d: d,
curveness: curveness,
ignoreForceLayout: edgeModel.get('ignoreForceLayout')
};
});
// let coordSys = graphSeries.coordinateSystem;
var rect = coordSys.getBoundingRect();
var forceInstance = forceLayout(nodes_1, edges, {
rect: rect,
gravity: forceModel.get('gravity'),
friction: forceModel.get('friction')
});
forceInstance.beforeStep(function (nodes, edges) {
for (var i = 0, l = nodes.length; i < l; i++) {
if (nodes[i].fixed) {
// Write back to layout instance
vec2.copy(nodes[i].p, graph_1.getNodeByIndex(i).getLayout());
}
}
});
forceInstance.afterStep(function (nodes, edges, stopped) {
for (var i = 0, l = nodes.length; i < l; i++) {
if (!nodes[i].fixed) {
graph_1.getNodeByIndex(i).setLayout(nodes[i].p);
}
preservedPoints_1[nodeData_1.getId(i)] = nodes[i].p;
}
for (var i = 0, l = edges.length; i < l; i++) {
var e = edges[i];
var edge = graph_1.getEdgeByIndex(i);
var p1 = e.n1.p;
var p2 = e.n2.p;
var points = edge.getLayout();
points = points ? points.slice() : [];
points[0] = points[0] || [];
points[1] = points[1] || [];
vec2.copy(points[0], p1);
vec2.copy(points[1], p2);
if (+e.curveness) {
points[2] = [(p1[0] + p2[0]) / 2 - (p1[1] - p2[1]) * e.curveness, (p1[1] + p2[1]) / 2 - (p2[0] - p1[0]) * e.curveness];
}
edge.setLayout(points);
}
});
graphSeries.forceLayout = forceInstance;
graphSeries.preservedPoints = preservedPoints_1;
// Step to get the layout
forceInstance.step();
} else {
// Remove prev injected forceLayout instance
graphSeries.forceLayout = null;
}
});
}