circularLayoutHelper.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. var vec2 = require("zrender/lib/core/vector");
  20. var _graphHelper = require("./graphHelper");
  21. var getSymbolSize = _graphHelper.getSymbolSize;
  22. var getNodeGlobalScale = _graphHelper.getNodeGlobalScale;
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. var PI = Math.PI;
  42. var _symbolRadiansHalf = [];
  43. /**
  44. * `basedOn` can be:
  45. * 'value':
  46. * This layout is not accurate and have same bad case. For example,
  47. * if the min value is very smaller than the max value, the nodes
  48. * with the min value probably overlap even though there is enough
  49. * space to layout them. So we only use this approach in the as the
  50. * init layout of the force layout.
  51. * FIXME
  52. * Probably we do not need this method any more but use
  53. * `basedOn: 'symbolSize'` in force layout if
  54. * delay its init operations to GraphView.
  55. * 'symbolSize':
  56. * This approach work only if all of the symbol size calculated.
  57. * That is, the progressive rendering is not applied to graph.
  58. * FIXME
  59. * If progressive rendering is applied to graph some day,
  60. * probably we have to use `basedOn: 'value'`.
  61. *
  62. * @param {module:echarts/src/model/Series} seriesModel
  63. * @param {string} basedOn 'value' or 'symbolSize'
  64. */
  65. 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, coordSys, graph, nodeData, r, cx, cy, count);
  85. graph.eachEdge(function (edge) {
  86. var curveness = edge.getModel().get('lineStyle.curveness') || 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, coordSys, 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, coordSys, 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. };
  137. exports.circularLayout = circularLayout;