circularLayoutHelper.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.js';
  41. import { getSymbolSize, getNodeGlobalScale } from './graphHelper.js';
  42. import * as zrUtil from 'zrender/lib/core/util.js';
  43. import { getCurvenessForEdge } from '../helper/multipleGraphEdgeHelper.js';
  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, draggingNode, pointer) {
  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. if (draggingNode) {
  85. var _a = coordSys.pointToData(pointer),
  86. tempX = _a[0],
  87. tempY = _a[1];
  88. var v = [tempX - cx, tempY - cy];
  89. vec2.normalize(v, v);
  90. vec2.scale(v, v, r);
  91. draggingNode.setLayout([cx + v[0], cy + v[1]], true);
  92. var circularRotateLabel = seriesModel.get(['circular', 'rotateLabel']);
  93. rotateNodeLabel(draggingNode, circularRotateLabel, cx, cy);
  94. }
  95. _layoutNodesBasedOn[basedOn](seriesModel, graph, nodeData, r, cx, cy, count);
  96. graph.eachEdge(function (edge, index) {
  97. var curveness = zrUtil.retrieve3(edge.getModel().get(['lineStyle', 'curveness']), getCurvenessForEdge(edge, seriesModel, index), 0);
  98. var p1 = vec2.clone(edge.node1.getLayout());
  99. var p2 = vec2.clone(edge.node2.getLayout());
  100. var cp1;
  101. var x12 = (p1[0] + p2[0]) / 2;
  102. var y12 = (p1[1] + p2[1]) / 2;
  103. if (+curveness) {
  104. curveness *= 3;
  105. cp1 = [cx * curveness + x12 * (1 - curveness), cy * curveness + y12 * (1 - curveness)];
  106. }
  107. edge.setLayout([p1, p2, cp1]);
  108. });
  109. }
  110. var _layoutNodesBasedOn = {
  111. value: function (seriesModel, graph, nodeData, r, cx, cy, count) {
  112. var angle = 0;
  113. var sum = nodeData.getSum('value');
  114. var unitAngle = Math.PI * 2 / (sum || count);
  115. graph.eachNode(function (node) {
  116. var value = node.getValue('value');
  117. var radianHalf = unitAngle * (sum ? value : 1) / 2;
  118. angle += radianHalf;
  119. node.setLayout([r * Math.cos(angle) + cx, r * Math.sin(angle) + cy]);
  120. angle += radianHalf;
  121. });
  122. },
  123. symbolSize: function (seriesModel, graph, nodeData, r, cx, cy, count) {
  124. var sumRadian = 0;
  125. _symbolRadiansHalf.length = count;
  126. var nodeScale = getNodeGlobalScale(seriesModel);
  127. graph.eachNode(function (node) {
  128. var symbolSize = getSymbolSize(node);
  129. // Normally this case will not happen, but we still add
  130. // some the defensive code (2px is an arbitrary value).
  131. isNaN(symbolSize) && (symbolSize = 2);
  132. symbolSize < 0 && (symbolSize = 0);
  133. symbolSize *= nodeScale;
  134. var symbolRadianHalf = Math.asin(symbolSize / 2 / r);
  135. // when `symbolSize / 2` is bigger than `r`.
  136. isNaN(symbolRadianHalf) && (symbolRadianHalf = PI / 2);
  137. _symbolRadiansHalf[node.dataIndex] = symbolRadianHalf;
  138. sumRadian += symbolRadianHalf * 2;
  139. });
  140. var halfRemainRadian = (2 * PI - sumRadian) / count / 2;
  141. var angle = 0;
  142. graph.eachNode(function (node) {
  143. var radianHalf = halfRemainRadian + _symbolRadiansHalf[node.dataIndex];
  144. angle += radianHalf;
  145. // init circular layout for
  146. // 1. layout undefined node
  147. // 2. not fixed node
  148. (!node.getLayout() || !node.getLayout().fixed) && node.setLayout([r * Math.cos(angle) + cx, r * Math.sin(angle) + cy]);
  149. angle += radianHalf;
  150. });
  151. }
  152. };
  153. export function rotateNodeLabel(node, circularRotateLabel, cx, cy) {
  154. var el = node.getGraphicEl();
  155. // need to check if el exists. '-' value may not create node element.
  156. if (!el) {
  157. return;
  158. }
  159. var nodeModel = node.getModel();
  160. var labelRotate = nodeModel.get(['label', 'rotate']) || 0;
  161. var symbolPath = el.getSymbolPath();
  162. if (circularRotateLabel) {
  163. var pos = node.getLayout();
  164. var rad = Math.atan2(pos[1] - cy, pos[0] - cx);
  165. if (rad < 0) {
  166. rad = Math.PI * 2 + rad;
  167. }
  168. var isLeft = pos[0] < cx;
  169. if (isLeft) {
  170. rad = rad - Math.PI;
  171. }
  172. var textPosition = isLeft ? 'left' : 'right';
  173. symbolPath.setTextConfig({
  174. rotation: -rad,
  175. position: textPosition,
  176. origin: 'center'
  177. });
  178. var emphasisState = symbolPath.ensureState('emphasis');
  179. zrUtil.extend(emphasisState.textConfig || (emphasisState.textConfig = {}), {
  180. position: textPosition
  181. });
  182. } else {
  183. symbolPath.setTextConfig({
  184. rotation: labelRotate *= Math.PI / 180
  185. });
  186. }
  187. }