sectorLabel.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 { calculateTextPosition } from 'zrender/lib/contain/text.js';
  41. import { isArray, isNumber } from 'zrender/lib/core/util.js';
  42. export function createSectorCalculateTextPosition(positionMapping, opts) {
  43. opts = opts || {};
  44. var isRoundCap = opts.isRoundCap;
  45. return function (out, opts, boundingRect) {
  46. var textPosition = opts.position;
  47. if (!textPosition || textPosition instanceof Array) {
  48. return calculateTextPosition(out, opts, boundingRect);
  49. }
  50. var mappedSectorPosition = positionMapping(textPosition);
  51. var distance = opts.distance != null ? opts.distance : 5;
  52. var sector = this.shape;
  53. var cx = sector.cx;
  54. var cy = sector.cy;
  55. var r = sector.r;
  56. var r0 = sector.r0;
  57. var middleR = (r + r0) / 2;
  58. var startAngle = sector.startAngle;
  59. var endAngle = sector.endAngle;
  60. var middleAngle = (startAngle + endAngle) / 2;
  61. var extraDist = isRoundCap ? Math.abs(r - r0) / 2 : 0;
  62. var mathCos = Math.cos;
  63. var mathSin = Math.sin;
  64. // base position: top-left
  65. var x = cx + r * mathCos(startAngle);
  66. var y = cy + r * mathSin(startAngle);
  67. var textAlign = 'left';
  68. var textVerticalAlign = 'top';
  69. switch (mappedSectorPosition) {
  70. case 'startArc':
  71. x = cx + (r0 - distance) * mathCos(middleAngle);
  72. y = cy + (r0 - distance) * mathSin(middleAngle);
  73. textAlign = 'center';
  74. textVerticalAlign = 'top';
  75. break;
  76. case 'insideStartArc':
  77. x = cx + (r0 + distance) * mathCos(middleAngle);
  78. y = cy + (r0 + distance) * mathSin(middleAngle);
  79. textAlign = 'center';
  80. textVerticalAlign = 'bottom';
  81. break;
  82. case 'startAngle':
  83. x = cx + middleR * mathCos(startAngle) + adjustAngleDistanceX(startAngle, distance + extraDist, false);
  84. y = cy + middleR * mathSin(startAngle) + adjustAngleDistanceY(startAngle, distance + extraDist, false);
  85. textAlign = 'right';
  86. textVerticalAlign = 'middle';
  87. break;
  88. case 'insideStartAngle':
  89. x = cx + middleR * mathCos(startAngle) + adjustAngleDistanceX(startAngle, -distance + extraDist, false);
  90. y = cy + middleR * mathSin(startAngle) + adjustAngleDistanceY(startAngle, -distance + extraDist, false);
  91. textAlign = 'left';
  92. textVerticalAlign = 'middle';
  93. break;
  94. case 'middle':
  95. x = cx + middleR * mathCos(middleAngle);
  96. y = cy + middleR * mathSin(middleAngle);
  97. textAlign = 'center';
  98. textVerticalAlign = 'middle';
  99. break;
  100. case 'endArc':
  101. x = cx + (r + distance) * mathCos(middleAngle);
  102. y = cy + (r + distance) * mathSin(middleAngle);
  103. textAlign = 'center';
  104. textVerticalAlign = 'bottom';
  105. break;
  106. case 'insideEndArc':
  107. x = cx + (r - distance) * mathCos(middleAngle);
  108. y = cy + (r - distance) * mathSin(middleAngle);
  109. textAlign = 'center';
  110. textVerticalAlign = 'top';
  111. break;
  112. case 'endAngle':
  113. x = cx + middleR * mathCos(endAngle) + adjustAngleDistanceX(endAngle, distance + extraDist, true);
  114. y = cy + middleR * mathSin(endAngle) + adjustAngleDistanceY(endAngle, distance + extraDist, true);
  115. textAlign = 'left';
  116. textVerticalAlign = 'middle';
  117. break;
  118. case 'insideEndAngle':
  119. x = cx + middleR * mathCos(endAngle) + adjustAngleDistanceX(endAngle, -distance + extraDist, true);
  120. y = cy + middleR * mathSin(endAngle) + adjustAngleDistanceY(endAngle, -distance + extraDist, true);
  121. textAlign = 'right';
  122. textVerticalAlign = 'middle';
  123. break;
  124. default:
  125. return calculateTextPosition(out, opts, boundingRect);
  126. }
  127. out = out || {};
  128. out.x = x;
  129. out.y = y;
  130. out.align = textAlign;
  131. out.verticalAlign = textVerticalAlign;
  132. return out;
  133. };
  134. }
  135. export function setSectorTextRotation(sector, textPosition, positionMapping, rotateType) {
  136. if (isNumber(rotateType)) {
  137. // user-set rotation
  138. sector.setTextConfig({
  139. rotation: rotateType
  140. });
  141. return;
  142. } else if (isArray(textPosition)) {
  143. // user-set position, use 0 as auto rotation
  144. sector.setTextConfig({
  145. rotation: 0
  146. });
  147. return;
  148. }
  149. var shape = sector.shape;
  150. var startAngle = shape.clockwise ? shape.startAngle : shape.endAngle;
  151. var endAngle = shape.clockwise ? shape.endAngle : shape.startAngle;
  152. var middleAngle = (startAngle + endAngle) / 2;
  153. var anchorAngle;
  154. var mappedSectorPosition = positionMapping(textPosition);
  155. switch (mappedSectorPosition) {
  156. case 'startArc':
  157. case 'insideStartArc':
  158. case 'middle':
  159. case 'insideEndArc':
  160. case 'endArc':
  161. anchorAngle = middleAngle;
  162. break;
  163. case 'startAngle':
  164. case 'insideStartAngle':
  165. anchorAngle = startAngle;
  166. break;
  167. case 'endAngle':
  168. case 'insideEndAngle':
  169. anchorAngle = endAngle;
  170. break;
  171. default:
  172. sector.setTextConfig({
  173. rotation: 0
  174. });
  175. return;
  176. }
  177. var rotate = Math.PI * 1.5 - anchorAngle;
  178. /**
  179. * TODO: labels with rotate > Math.PI / 2 should be rotate another
  180. * half round flipped to increase readability. However, only middle
  181. * position supports this for now, because in other positions, the
  182. * anchor point is not at the center of the text, so the positions
  183. * after rotating is not as expected.
  184. */
  185. if (mappedSectorPosition === 'middle' && rotate > Math.PI / 2 && rotate < Math.PI * 1.5) {
  186. rotate -= Math.PI;
  187. }
  188. sector.setTextConfig({
  189. rotation: rotate
  190. });
  191. }
  192. function adjustAngleDistanceX(angle, distance, isEnd) {
  193. return distance * Math.sin(angle) * (isEnd ? -1 : 1);
  194. }
  195. function adjustAngleDistanceY(angle, distance, isEnd) {
  196. return distance * Math.cos(angle) * (isEnd ? 1 : -1);
  197. }