circularLayoutHelper.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. var zrUtil = require("zrender/lib/core/util");
  24. var _multipleGraphEdgeHelper = require("../helper/multipleGraphEdgeHelper");
  25. var getCurvenessForEdge = _multipleGraphEdgeHelper.getCurvenessForEdge;
  26. /*
  27. * Licensed to the Apache Software Foundation (ASF) under one
  28. * or more contributor license agreements. See the NOTICE file
  29. * distributed with this work for additional information
  30. * regarding copyright ownership. The ASF licenses this file
  31. * to you under the Apache License, Version 2.0 (the
  32. * "License"); you may not use this file except in compliance
  33. * with the License. You may obtain a copy of the License at
  34. *
  35. * http://www.apache.org/licenses/LICENSE-2.0
  36. *
  37. * Unless required by applicable law or agreed to in writing,
  38. * software distributed under the License is distributed on an
  39. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  40. * KIND, either express or implied. See the License for the
  41. * specific language governing permissions and limitations
  42. * under the License.
  43. */
  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. * @param {module:echarts/src/model/Series} seriesModel
  66. * @param {string} basedOn 'value' or 'symbolSize'
  67. */
  68. function circularLayout(seriesModel, basedOn) {
  69. var coordSys = seriesModel.coordinateSystem;
  70. if (coordSys && coordSys.type !== 'view') {
  71. return;
  72. }
  73. var rect = coordSys.getBoundingRect();
  74. var nodeData = seriesModel.getData();
  75. var graph = nodeData.graph;
  76. var cx = rect.width / 2 + rect.x;
  77. var cy = rect.height / 2 + rect.y;
  78. var r = Math.min(rect.width, rect.height) / 2;
  79. var count = nodeData.count();
  80. nodeData.setLayout({
  81. cx: cx,
  82. cy: cy
  83. });
  84. if (!count) {
  85. return;
  86. }
  87. _layoutNodesBasedOn[basedOn](seriesModel, coordSys, graph, nodeData, r, cx, cy, count);
  88. graph.eachEdge(function (edge, index) {
  89. var curveness = zrUtil.retrieve3(edge.getModel().get('lineStyle.curveness'), getCurvenessForEdge(edge, seriesModel, index), 0);
  90. var p1 = vec2.clone(edge.node1.getLayout());
  91. var p2 = vec2.clone(edge.node2.getLayout());
  92. var cp1;
  93. var x12 = (p1[0] + p2[0]) / 2;
  94. var y12 = (p1[1] + p2[1]) / 2;
  95. if (+curveness) {
  96. curveness *= 3;
  97. cp1 = [cx * curveness + x12 * (1 - curveness), cy * curveness + y12 * (1 - curveness)];
  98. }
  99. edge.setLayout([p1, p2, cp1]);
  100. });
  101. }
  102. var _layoutNodesBasedOn = {
  103. value: function (seriesModel, coordSys, graph, nodeData, r, cx, cy, count) {
  104. var angle = 0;
  105. var sum = nodeData.getSum('value');
  106. var unitAngle = Math.PI * 2 / (sum || count);
  107. graph.eachNode(function (node) {
  108. var value = node.getValue('value');
  109. var radianHalf = unitAngle * (sum ? value : 1) / 2;
  110. angle += radianHalf;
  111. node.setLayout([r * Math.cos(angle) + cx, r * Math.sin(angle) + cy]);
  112. angle += radianHalf;
  113. });
  114. },
  115. symbolSize: function (seriesModel, coordSys, graph, nodeData, r, cx, cy, count) {
  116. var sumRadian = 0;
  117. _symbolRadiansHalf.length = count;
  118. var nodeScale = getNodeGlobalScale(seriesModel);
  119. graph.eachNode(function (node) {
  120. var symbolSize = getSymbolSize(node); // Normally this case will not happen, but we still add
  121. // some the defensive code (2px is an arbitrary value).
  122. isNaN(symbolSize) && (symbolSize = 2);
  123. symbolSize < 0 && (symbolSize = 0);
  124. symbolSize *= nodeScale;
  125. var symbolRadianHalf = Math.asin(symbolSize / 2 / r); // when `symbolSize / 2` is bigger than `r`.
  126. isNaN(symbolRadianHalf) && (symbolRadianHalf = PI / 2);
  127. _symbolRadiansHalf[node.dataIndex] = symbolRadianHalf;
  128. sumRadian += symbolRadianHalf * 2;
  129. });
  130. var halfRemainRadian = (2 * PI - sumRadian) / count / 2;
  131. var angle = 0;
  132. graph.eachNode(function (node) {
  133. var radianHalf = halfRemainRadian + _symbolRadiansHalf[node.dataIndex];
  134. angle += radianHalf;
  135. node.setLayout([r * Math.cos(angle) + cx, r * Math.sin(angle) + cy]);
  136. angle += radianHalf;
  137. });
  138. }
  139. };
  140. exports.circularLayout = circularLayout;