forceLayout.js 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* 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.
*/

S
sushuang 已提交
20 21 22
import {forceLayout} from './forceHelper';
import {simpleLayout} from './simpleLayoutHelper';
import {circularLayout} from './circularLayoutHelper';
S
sushuang 已提交
23
import {linearMap} from '../../util/number';
S
sushuang 已提交
24 25
import * as vec2 from 'zrender/src/core/vector';
import * as zrUtil from 'zrender/src/core/util';
26
import {getCurvenessForEdge} from '../helper/multipleGraphEdgeHelper';
L
lang 已提交
27

S
sushuang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
export default function (ecModel) {
    ecModel.eachSeriesByType('graph', function (graphSeries) {
        var coordSys = graphSeries.coordinateSystem;
        if (coordSys && coordSys.type !== 'view') {
            return;
        }
        if (graphSeries.get('layout') === 'force') {
            var preservedPoints = graphSeries.preservedPoints || {};
            var graph = graphSeries.getGraph();
            var nodeData = graph.data;
            var edgeData = graph.edgeData;
            var forceModel = graphSeries.getModel('force');
            var initLayout = forceModel.get('initLayout');
            if (graphSeries.preservedPoints) {
                nodeData.each(function (idx) {
                    var id = nodeData.getId(idx);
                    nodeData.setItemLayout(idx, preservedPoints[id] || [NaN, NaN]);
                });
            }
            else if (!initLayout || initLayout === 'none') {
S
sushuang 已提交
48
                simpleLayout(graphSeries);
S
sushuang 已提交
49 50
            }
            else if (initLayout === 'circular') {
51
                circularLayout(graphSeries, 'value');
S
sushuang 已提交
52
            }
L
lang 已提交
53

S
sushuang 已提交
54 55 56 57 58 59 60
            var nodeDataExtent = nodeData.getDataExtent('value');
            var edgeDataExtent = edgeData.getDataExtent('value');
            // var edgeDataExtent = edgeData.getDataExtent('value');
            var repulsion = forceModel.get('repulsion');
            var edgeLength = forceModel.get('edgeLength');
            if (!zrUtil.isArray(repulsion)) {
                repulsion = [repulsion, repulsion];
61
            }
S
sushuang 已提交
62 63 64 65 66
            if (!zrUtil.isArray(edgeLength)) {
                edgeLength = [edgeLength, edgeLength];
            }
            // Larger value has smaller length
            edgeLength = [edgeLength[1], edgeLength[0]];
L
lang 已提交
67

S
sushuang 已提交
68 69 70 71 72
            var nodes = nodeData.mapArray('value', function (value, idx) {
                var point = nodeData.getItemLayout(idx);
                var rep = linearMap(value, nodeDataExtent, repulsion);
                if (isNaN(rep)) {
                    rep = (repulsion[0] + repulsion[1]) / 2;
73
                }
S
sushuang 已提交
74 75 76 77 78 79 80 81 82 83 84 85
                return {
                    w: rep,
                    rep: rep,
                    fixed: nodeData.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.getEdgeByIndex(idx);
                var d = linearMap(value, edgeDataExtent, edgeLength);
                if (isNaN(d)) {
                    d = (edgeLength[0] + edgeLength[1]) / 2;
86
                }
S
SHUANG SU 已提交
87
                var edgeModel = edge.getModel();
88 89 90 91 92
                var curveness = zrUtil.retrieve3(
                    -getCurvenessForEdge(edge, graphSeries, idx, true),
                    edge.getModel().get('lineStyle.curveness'),
                    0
                );
S
sushuang 已提交
93 94 95 96
                return {
                    n1: nodes[edge.node1.dataIndex],
                    n2: nodes[edge.node2.dataIndex],
                    d: d,
97
                    curveness: curveness,
S
SHUANG SU 已提交
98
                    ignoreForceLayout: edgeModel.get('ignoreForceLayout')
S
sushuang 已提交
99 100
                };
            });
L
lang 已提交
101

S
sushuang 已提交
102 103
            var coordSys = graphSeries.coordinateSystem;
            var rect = coordSys.getBoundingRect();
S
sushuang 已提交
104
            var forceInstance = forceLayout(nodes, edges, {
S
sushuang 已提交
105
                rect: rect,
106 107
                gravity: forceModel.get('gravity'),
                friction: forceModel.get('friction')
S
sushuang 已提交
108 109 110 111 112 113 114
            });
            var oldStep = forceInstance.step;
            forceInstance.step = function (cb) {
                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.getNodeByIndex(i).getLayout());
115
                    }
S
sushuang 已提交
116 117
                }
                oldStep(function (nodes, edges, stopped) {
L
lang 已提交
118
                    for (var i = 0, l = nodes.length; i < l; i++) {
S
sushuang 已提交
119 120
                        if (!nodes[i].fixed) {
                            graph.getNodeByIndex(i).setLayout(nodes[i].p);
L
lang 已提交
121
                        }
S
sushuang 已提交
122
                        preservedPoints[nodeData.getId(i)] = nodes[i].p;
L
lang 已提交
123
                    }
S
sushuang 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
                    for (var i = 0, l = edges.length; i < l; i++) {
                        var e = edges[i];
                        var edge = graph.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
                            ];
L
lang 已提交
140
                        }
S
sushuang 已提交
141 142 143
                        edge.setLayout(points);
                    }
                    // Update layout
L
lang 已提交
144

S
sushuang 已提交
145 146 147 148 149
                    cb && cb(stopped);
                });
            };
            graphSeries.forceLayout = forceInstance;
            graphSeries.preservedPoints = preservedPoints;
L
lang 已提交
150

S
sushuang 已提交
151 152 153 154 155 156 157 158
            // Step to get the layout
            forceInstance.step();
        }
        else {
            // Remove prev injected forceLayout instance
            graphSeries.forceLayout = null;
        }
    });
159
}