circularLayoutHelper.js 5.1 KB

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