circularLayoutHelper.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 * as vec2 from 'zrender/lib/core/vector';
  41. import { getSymbolSize, getNodeGlobalScale } from './graphHelper';
  42. import * as zrUtil from 'zrender/lib/core/util';
  43. import { getCurvenessForEdge } from '../helper/multipleGraphEdgeHelper';
  44. var PI = Math.PI;
  45. var _symbolRadiansHalf = [];
  46. /**
  47. * `basedOn` can be:
  48. * 'value':
  49. * This layout is not accurate and have same bad case. For example,
  50. * if the min value is very smaller than the max value, the nodes
  51. * with the min value probably overlap even though there is enough
  52. * space to layout them. So we only use this approach in the as the
  53. * init layout of the force layout.
  54. * FIXME
  55. * Probably we do not need this method any more but use
  56. * `basedOn: 'symbolSize'` in force layout if
  57. * delay its init operations to GraphView.
  58. * 'symbolSize':
  59. * This approach work only if all of the symbol size calculated.
  60. * That is, the progressive rendering is not applied to graph.
  61. * FIXME
  62. * If progressive rendering is applied to graph some day,
  63. * probably we have to use `basedOn: 'value'`.
  64. */
  65. export function circularLayout(seriesModel, basedOn) {
  66. var coordSys = seriesModel.coordinateSystem;
  67. if (coordSys && coordSys.type !== 'view') {
  68. return;
  69. }
  70. var rect = coordSys.getBoundingRect();
  71. var nodeData = seriesModel.getData();
  72. var graph = nodeData.graph;
  73. var cx = rect.width / 2 + rect.x;
  74. var cy = rect.height / 2 + rect.y;
  75. var r = Math.min(rect.width, rect.height) / 2;
  76. var count = nodeData.count();
  77. nodeData.setLayout({
  78. cx: cx,
  79. cy: cy
  80. });
  81. if (!count) {
  82. return;
  83. }
  84. _layoutNodesBasedOn[basedOn](seriesModel, graph, nodeData, r, cx, cy, count);
  85. graph.eachEdge(function (edge, index) {
  86. var curveness = zrUtil.retrieve3(edge.getModel().get(['lineStyle', 'curveness']), getCurvenessForEdge(edge, seriesModel, index), 0);
  87. var p1 = vec2.clone(edge.node1.getLayout());
  88. var p2 = vec2.clone(edge.node2.getLayout());
  89. var cp1;
  90. var x12 = (p1[0] + p2[0]) / 2;
  91. var y12 = (p1[1] + p2[1]) / 2;
  92. if (+curveness) {
  93. curveness *= 3;
  94. cp1 = [cx * curveness + x12 * (1 - curveness), cy * curveness + y12 * (1 - curveness)];
  95. }
  96. edge.setLayout([p1, p2, cp1]);
  97. });
  98. }
  99. var _layoutNodesBasedOn = {
  100. value: function (seriesModel, graph, nodeData, r, cx, cy, count) {
  101. var angle = 0;
  102. var sum = nodeData.getSum('value');
  103. var unitAngle = Math.PI * 2 / (sum || count);
  104. graph.eachNode(function (node) {
  105. var value = node.getValue('value');
  106. var radianHalf = unitAngle * (sum ? value : 1) / 2;
  107. angle += radianHalf;
  108. node.setLayout([r * Math.cos(angle) + cx, r * Math.sin(angle) + cy]);
  109. angle += radianHalf;
  110. });
  111. },
  112. symbolSize: function (seriesModel, graph, nodeData, r, cx, cy, count) {
  113. var sumRadian = 0;
  114. _symbolRadiansHalf.length = count;
  115. var nodeScale = getNodeGlobalScale(seriesModel);
  116. graph.eachNode(function (node) {
  117. var symbolSize = getSymbolSize(node); // Normally this case will not happen, but we still add
  118. // some the defensive code (2px is an arbitrary value).
  119. isNaN(symbolSize) && (symbolSize = 2);
  120. symbolSize < 0 && (symbolSize = 0);
  121. symbolSize *= nodeScale;
  122. var symbolRadianHalf = Math.asin(symbolSize / 2 / r); // when `symbolSize / 2` is bigger than `r`.
  123. isNaN(symbolRadianHalf) && (symbolRadianHalf = PI / 2);
  124. _symbolRadiansHalf[node.dataIndex] = symbolRadianHalf;
  125. sumRadian += symbolRadianHalf * 2;
  126. });
  127. var halfRemainRadian = (2 * PI - sumRadian) / count / 2;
  128. var angle = 0;
  129. graph.eachNode(function (node) {
  130. var radianHalf = halfRemainRadian + _symbolRadiansHalf[node.dataIndex];
  131. angle += radianHalf;
  132. node.setLayout([r * Math.cos(angle) + cx, r * Math.sin(angle) + cy]);
  133. angle += radianHalf;
  134. });
  135. }
  136. };