forceLayout.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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'); // let edgeDataExtent = edgeData.getDataExtent('value');
  72. var repulsion = forceModel.get('repulsion');
  73. var edgeLength = forceModel.get('edgeLength');
  74. var repulsionArr_1 = zrUtil.isArray(repulsion) ? repulsion : [repulsion, repulsion];
  75. var edgeLengthArr_1 = zrUtil.isArray(edgeLength) ? edgeLength : [edgeLength, edgeLength]; // Larger value has smaller length
  76. edgeLengthArr_1 = [edgeLengthArr_1[1], edgeLengthArr_1[0]];
  77. var nodes_1 = nodeData_1.mapArray('value', function (value, idx) {
  78. var point = nodeData_1.getItemLayout(idx);
  79. var rep = linearMap(value, nodeDataExtent_1, repulsionArr_1);
  80. if (isNaN(rep)) {
  81. rep = (repulsionArr_1[0] + repulsionArr_1[1]) / 2;
  82. }
  83. return {
  84. w: rep,
  85. rep: rep,
  86. fixed: nodeData_1.getItemModel(idx).get('fixed'),
  87. p: !point || isNaN(point[0]) || isNaN(point[1]) ? null : point
  88. };
  89. });
  90. var edges = edgeData.mapArray('value', function (value, idx) {
  91. var edge = graph_1.getEdgeByIndex(idx);
  92. var d = linearMap(value, edgeDataExtent_1, edgeLengthArr_1);
  93. if (isNaN(d)) {
  94. d = (edgeLengthArr_1[0] + edgeLengthArr_1[1]) / 2;
  95. }
  96. var edgeModel = edge.getModel();
  97. var curveness = zrUtil.retrieve3(edge.getModel().get(['lineStyle', 'curveness']), -getCurvenessForEdge(edge, graphSeries, idx, true), 0);
  98. return {
  99. n1: nodes_1[edge.node1.dataIndex],
  100. n2: nodes_1[edge.node2.dataIndex],
  101. d: d,
  102. curveness: curveness,
  103. ignoreForceLayout: edgeModel.get('ignoreForceLayout')
  104. };
  105. }); // let coordSys = graphSeries.coordinateSystem;
  106. var rect = coordSys.getBoundingRect();
  107. var forceInstance = forceLayout(nodes_1, edges, {
  108. rect: rect,
  109. gravity: forceModel.get('gravity'),
  110. friction: forceModel.get('friction')
  111. });
  112. forceInstance.beforeStep(function (nodes, edges) {
  113. for (var i = 0, l = nodes.length; i < l; i++) {
  114. if (nodes[i].fixed) {
  115. // Write back to layout instance
  116. vec2.copy(nodes[i].p, graph_1.getNodeByIndex(i).getLayout());
  117. }
  118. }
  119. });
  120. forceInstance.afterStep(function (nodes, edges, stopped) {
  121. for (var i = 0, l = nodes.length; i < l; i++) {
  122. if (!nodes[i].fixed) {
  123. graph_1.getNodeByIndex(i).setLayout(nodes[i].p);
  124. }
  125. preservedPoints_1[nodeData_1.getId(i)] = nodes[i].p;
  126. }
  127. for (var i = 0, l = edges.length; i < l; i++) {
  128. var e = edges[i];
  129. var edge = graph_1.getEdgeByIndex(i);
  130. var p1 = e.n1.p;
  131. var p2 = e.n2.p;
  132. var points = edge.getLayout();
  133. points = points ? points.slice() : [];
  134. points[0] = points[0] || [];
  135. points[1] = points[1] || [];
  136. vec2.copy(points[0], p1);
  137. vec2.copy(points[1], p2);
  138. if (+e.curveness) {
  139. points[2] = [(p1[0] + p2[0]) / 2 - (p1[1] - p2[1]) * e.curveness, (p1[1] + p2[1]) / 2 - (p2[0] - p1[0]) * e.curveness];
  140. }
  141. edge.setLayout(points);
  142. }
  143. });
  144. graphSeries.forceLayout = forceInstance;
  145. graphSeries.preservedPoints = preservedPoints_1; // Step to get the layout
  146. forceInstance.step();
  147. } else {
  148. // Remove prev injected forceLayout instance
  149. graphSeries.forceLayout = null;
  150. }
  151. });
  152. }