forceLayout.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { forceLayout } from './forceHelper.js';
  41. import { simpleLayout } from './simpleLayoutHelper.js';
  42. import { circularLayout } from './circularLayoutHelper.js';
  43. import { linearMap } from '../../util/number.js';
  44. import * as vec2 from 'zrender/lib/core/vector.js';
  45. import * as zrUtil from 'zrender/lib/core/util.js';
  46. import { getCurvenessForEdge } from '../helper/multipleGraphEdgeHelper.js';
  47. export default function graphForceLayout(ecModel) {
  48. ecModel.eachSeriesByType('graph', function (graphSeries) {
  49. var coordSys = graphSeries.coordinateSystem;
  50. if (coordSys && coordSys.type !== 'view') {
  51. return;
  52. }
  53. if (graphSeries.get('layout') === 'force') {
  54. var preservedPoints_1 = graphSeries.preservedPoints || {};
  55. var graph_1 = graphSeries.getGraph();
  56. var nodeData_1 = graph_1.data;
  57. var edgeData = graph_1.edgeData;
  58. var forceModel = graphSeries.getModel('force');
  59. var initLayout = forceModel.get('initLayout');
  60. if (graphSeries.preservedPoints) {
  61. nodeData_1.each(function (idx) {
  62. var id = nodeData_1.getId(idx);
  63. nodeData_1.setItemLayout(idx, preservedPoints_1[id] || [NaN, NaN]);
  64. });
  65. } else if (!initLayout || initLayout === 'none') {
  66. simpleLayout(graphSeries);
  67. } else if (initLayout === 'circular') {
  68. circularLayout(graphSeries, 'value');
  69. }
  70. var nodeDataExtent_1 = nodeData_1.getDataExtent('value');
  71. var edgeDataExtent_1 = edgeData.getDataExtent('value');
  72. // let edgeDataExtent = edgeData.getDataExtent('value');
  73. var repulsion = forceModel.get('repulsion');
  74. var edgeLength = forceModel.get('edgeLength');
  75. var repulsionArr_1 = zrUtil.isArray(repulsion) ? repulsion : [repulsion, repulsion];
  76. var edgeLengthArr_1 = zrUtil.isArray(edgeLength) ? edgeLength : [edgeLength, edgeLength];
  77. // Larger value has smaller length
  78. edgeLengthArr_1 = [edgeLengthArr_1[1], edgeLengthArr_1[0]];
  79. var nodes_1 = nodeData_1.mapArray('value', function (value, idx) {
  80. var point = nodeData_1.getItemLayout(idx);
  81. var rep = linearMap(value, nodeDataExtent_1, repulsionArr_1);
  82. if (isNaN(rep)) {
  83. rep = (repulsionArr_1[0] + repulsionArr_1[1]) / 2;
  84. }
  85. return {
  86. w: rep,
  87. rep: rep,
  88. fixed: nodeData_1.getItemModel(idx).get('fixed'),
  89. p: !point || isNaN(point[0]) || isNaN(point[1]) ? null : point
  90. };
  91. });
  92. var edges = edgeData.mapArray('value', function (value, idx) {
  93. var edge = graph_1.getEdgeByIndex(idx);
  94. var d = linearMap(value, edgeDataExtent_1, edgeLengthArr_1);
  95. if (isNaN(d)) {
  96. d = (edgeLengthArr_1[0] + edgeLengthArr_1[1]) / 2;
  97. }
  98. var edgeModel = edge.getModel();
  99. var curveness = zrUtil.retrieve3(edge.getModel().get(['lineStyle', 'curveness']), -getCurvenessForEdge(edge, graphSeries, idx, true), 0);
  100. return {
  101. n1: nodes_1[edge.node1.dataIndex],
  102. n2: nodes_1[edge.node2.dataIndex],
  103. d: d,
  104. curveness: curveness,
  105. ignoreForceLayout: edgeModel.get('ignoreForceLayout')
  106. };
  107. });
  108. // let coordSys = graphSeries.coordinateSystem;
  109. var rect = coordSys.getBoundingRect();
  110. var forceInstance = forceLayout(nodes_1, edges, {
  111. rect: rect,
  112. gravity: forceModel.get('gravity'),
  113. friction: forceModel.get('friction')
  114. });
  115. forceInstance.beforeStep(function (nodes, edges) {
  116. for (var i = 0, l = nodes.length; i < l; i++) {
  117. if (nodes[i].fixed) {
  118. // Write back to layout instance
  119. vec2.copy(nodes[i].p, graph_1.getNodeByIndex(i).getLayout());
  120. }
  121. }
  122. });
  123. forceInstance.afterStep(function (nodes, edges, stopped) {
  124. for (var i = 0, l = nodes.length; i < l; i++) {
  125. if (!nodes[i].fixed) {
  126. graph_1.getNodeByIndex(i).setLayout(nodes[i].p);
  127. }
  128. preservedPoints_1[nodeData_1.getId(i)] = nodes[i].p;
  129. }
  130. for (var i = 0, l = edges.length; i < l; i++) {
  131. var e = edges[i];
  132. var edge = graph_1.getEdgeByIndex(i);
  133. var p1 = e.n1.p;
  134. var p2 = e.n2.p;
  135. var points = edge.getLayout();
  136. points = points ? points.slice() : [];
  137. points[0] = points[0] || [];
  138. points[1] = points[1] || [];
  139. vec2.copy(points[0], p1);
  140. vec2.copy(points[1], p2);
  141. if (+e.curveness) {
  142. points[2] = [(p1[0] + p2[0]) / 2 - (p1[1] - p2[1]) * e.curveness, (p1[1] + p2[1]) / 2 - (p2[0] - p1[0]) * e.curveness];
  143. }
  144. edge.setLayout(points);
  145. }
  146. });
  147. graphSeries.forceLayout = forceInstance;
  148. graphSeries.preservedPoints = preservedPoints_1;
  149. // Step to get the layout
  150. forceInstance.step();
  151. } else {
  152. // Remove prev injected forceLayout instance
  153. graphSeries.forceLayout = null;
  154. }
  155. });
  156. }