sectorLabel.js 7.2 KB

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