labelLayout.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. // FIXME emphasis label position is not same with normal label position
  41. import { parsePercent } from '../../util/number';
  42. import { Point } from '../../util/graphic';
  43. import { each } from 'zrender/lib/core/util';
  44. import { limitTurnAngle, limitSurfaceAngle } from '../../label/labelGuideHelper';
  45. import { shiftLayoutOnY } from '../../label/labelLayoutHelper';
  46. var RADIAN = Math.PI / 180;
  47. function adjustSingleSide(list, cx, cy, r, dir, viewWidth, viewHeight, viewLeft, viewTop, farthestX) {
  48. if (list.length < 2) {
  49. return;
  50. }
  51. ;
  52. function recalculateXOnSemiToAlignOnEllipseCurve(semi) {
  53. var rB = semi.rB;
  54. var rB2 = rB * rB;
  55. for (var i = 0; i < semi.list.length; i++) {
  56. var item = semi.list[i];
  57. var dy = Math.abs(item.label.y - cy); // horizontal r is always same with original r because x is not changed.
  58. var rA = r + item.len;
  59. var rA2 = rA * rA; // Use ellipse implicit function to calculate x
  60. var dx = Math.sqrt((1 - Math.abs(dy * dy / rB2)) * rA2);
  61. item.label.x = cx + (dx + item.len2) * dir;
  62. }
  63. } // Adjust X based on the shifted y. Make tight labels aligned on an ellipse curve.
  64. function recalculateX(items) {
  65. // Extremes of
  66. var topSemi = {
  67. list: [],
  68. maxY: 0
  69. };
  70. var bottomSemi = {
  71. list: [],
  72. maxY: 0
  73. };
  74. for (var i = 0; i < items.length; i++) {
  75. if (items[i].labelAlignTo !== 'none') {
  76. continue;
  77. }
  78. var item = items[i];
  79. var semi = item.label.y > cy ? bottomSemi : topSemi;
  80. var dy = Math.abs(item.label.y - cy);
  81. if (dy > semi.maxY) {
  82. var dx = item.label.x - cx - item.len2 * dir; // horizontal r is always same with original r because x is not changed.
  83. var rA = r + item.len; // Canculate rB based on the topest / bottemest label.
  84. var rB = Math.abs(dx) < rA ? Math.sqrt(dy * dy / (1 - dx * dx / rA / rA)) : rA;
  85. semi.rB = rB;
  86. semi.maxY = dy;
  87. }
  88. semi.list.push(item);
  89. }
  90. recalculateXOnSemiToAlignOnEllipseCurve(topSemi);
  91. recalculateXOnSemiToAlignOnEllipseCurve(bottomSemi);
  92. }
  93. var len = list.length;
  94. for (var i = 0; i < len; i++) {
  95. if (list[i].position === 'outer' && list[i].labelAlignTo === 'labelLine') {
  96. var dx = list[i].label.x - farthestX;
  97. list[i].linePoints[1][0] += dx;
  98. list[i].label.x = farthestX;
  99. }
  100. }
  101. if (shiftLayoutOnY(list, viewTop, viewTop + viewHeight)) {
  102. recalculateX(list);
  103. }
  104. }
  105. function avoidOverlap(labelLayoutList, cx, cy, r, viewWidth, viewHeight, viewLeft, viewTop) {
  106. var leftList = [];
  107. var rightList = [];
  108. var leftmostX = Number.MAX_VALUE;
  109. var rightmostX = -Number.MAX_VALUE;
  110. for (var i = 0; i < labelLayoutList.length; i++) {
  111. var label = labelLayoutList[i].label;
  112. if (isPositionCenter(labelLayoutList[i])) {
  113. continue;
  114. }
  115. if (label.x < cx) {
  116. leftmostX = Math.min(leftmostX, label.x);
  117. leftList.push(labelLayoutList[i]);
  118. } else {
  119. rightmostX = Math.max(rightmostX, label.x);
  120. rightList.push(labelLayoutList[i]);
  121. }
  122. }
  123. adjustSingleSide(rightList, cx, cy, r, 1, viewWidth, viewHeight, viewLeft, viewTop, rightmostX);
  124. adjustSingleSide(leftList, cx, cy, r, -1, viewWidth, viewHeight, viewLeft, viewTop, leftmostX);
  125. for (var i = 0; i < labelLayoutList.length; i++) {
  126. var layout = labelLayoutList[i];
  127. var label = layout.label;
  128. if (isPositionCenter(layout)) {
  129. continue;
  130. }
  131. var linePoints = layout.linePoints;
  132. if (linePoints) {
  133. var isAlignToEdge = layout.labelAlignTo === 'edge';
  134. var realTextWidth = layout.rect.width;
  135. var targetTextWidth = void 0;
  136. if (isAlignToEdge) {
  137. if (label.x < cx) {
  138. targetTextWidth = linePoints[2][0] - layout.labelDistance - viewLeft - layout.edgeDistance;
  139. } else {
  140. targetTextWidth = viewLeft + viewWidth - layout.edgeDistance - linePoints[2][0] - layout.labelDistance;
  141. }
  142. } else {
  143. if (label.x < cx) {
  144. targetTextWidth = label.x - viewLeft - layout.bleedMargin;
  145. } else {
  146. targetTextWidth = viewLeft + viewWidth - label.x - layout.bleedMargin;
  147. }
  148. }
  149. if (targetTextWidth < layout.rect.width) {
  150. // TODOTODO
  151. // layout.text = textContain.truncateText(layout.text, targetTextWidth, layout.font);
  152. layout.label.style.width = targetTextWidth;
  153. if (layout.labelAlignTo === 'edge') {
  154. realTextWidth = targetTextWidth; // realTextWidth = textContain.getWidth(layout.text, layout.font);
  155. }
  156. }
  157. var dist = linePoints[1][0] - linePoints[2][0];
  158. if (isAlignToEdge) {
  159. if (label.x < cx) {
  160. linePoints[2][0] = viewLeft + layout.edgeDistance + realTextWidth + layout.labelDistance;
  161. } else {
  162. linePoints[2][0] = viewLeft + viewWidth - layout.edgeDistance - realTextWidth - layout.labelDistance;
  163. }
  164. } else {
  165. if (label.x < cx) {
  166. linePoints[2][0] = label.x + layout.labelDistance;
  167. } else {
  168. linePoints[2][0] = label.x - layout.labelDistance;
  169. }
  170. linePoints[1][0] = linePoints[2][0] + dist;
  171. }
  172. linePoints[1][1] = linePoints[2][1] = label.y;
  173. }
  174. }
  175. }
  176. function isPositionCenter(sectorShape) {
  177. // Not change x for center label
  178. return sectorShape.position === 'center';
  179. }
  180. export default function pieLabelLayout(seriesModel) {
  181. var data = seriesModel.getData();
  182. var labelLayoutList = [];
  183. var cx;
  184. var cy;
  185. var hasLabelRotate = false;
  186. var minShowLabelRadian = (seriesModel.get('minShowLabelAngle') || 0) * RADIAN;
  187. var viewRect = data.getLayout('viewRect');
  188. var r = data.getLayout('r');
  189. var viewWidth = viewRect.width;
  190. var viewLeft = viewRect.x;
  191. var viewTop = viewRect.y;
  192. var viewHeight = viewRect.height;
  193. function setNotShow(el) {
  194. el.ignore = true;
  195. }
  196. function isLabelShown(label) {
  197. if (!label.ignore) {
  198. return true;
  199. }
  200. for (var key in label.states) {
  201. if (label.states[key].ignore === false) {
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. data.each(function (idx) {
  208. var sector = data.getItemGraphicEl(idx);
  209. var sectorShape = sector.shape;
  210. var label = sector.getTextContent();
  211. var labelLine = sector.getTextGuideLine();
  212. var itemModel = data.getItemModel(idx);
  213. var labelModel = itemModel.getModel('label'); // Use position in normal or emphasis
  214. var labelPosition = labelModel.get('position') || itemModel.get(['emphasis', 'label', 'position']);
  215. var labelDistance = labelModel.get('distanceToLabelLine');
  216. var labelAlignTo = labelModel.get('alignTo');
  217. var edgeDistance = parsePercent(labelModel.get('edgeDistance'), viewWidth);
  218. var bleedMargin = labelModel.get('bleedMargin');
  219. var labelLineModel = itemModel.getModel('labelLine');
  220. var labelLineLen = labelLineModel.get('length');
  221. labelLineLen = parsePercent(labelLineLen, viewWidth);
  222. var labelLineLen2 = labelLineModel.get('length2');
  223. labelLineLen2 = parsePercent(labelLineLen2, viewWidth);
  224. if (Math.abs(sectorShape.endAngle - sectorShape.startAngle) < minShowLabelRadian) {
  225. each(label.states, setNotShow);
  226. label.ignore = true;
  227. return;
  228. }
  229. if (!isLabelShown(label)) {
  230. return;
  231. }
  232. var midAngle = (sectorShape.startAngle + sectorShape.endAngle) / 2;
  233. var nx = Math.cos(midAngle);
  234. var ny = Math.sin(midAngle);
  235. var textX;
  236. var textY;
  237. var linePoints;
  238. var textAlign;
  239. cx = sectorShape.cx;
  240. cy = sectorShape.cy;
  241. var isLabelInside = labelPosition === 'inside' || labelPosition === 'inner';
  242. if (labelPosition === 'center') {
  243. textX = sectorShape.cx;
  244. textY = sectorShape.cy;
  245. textAlign = 'center';
  246. } else {
  247. var x1 = (isLabelInside ? (sectorShape.r + sectorShape.r0) / 2 * nx : sectorShape.r * nx) + cx;
  248. var y1 = (isLabelInside ? (sectorShape.r + sectorShape.r0) / 2 * ny : sectorShape.r * ny) + cy;
  249. textX = x1 + nx * 3;
  250. textY = y1 + ny * 3;
  251. if (!isLabelInside) {
  252. // For roseType
  253. var x2 = x1 + nx * (labelLineLen + r - sectorShape.r);
  254. var y2 = y1 + ny * (labelLineLen + r - sectorShape.r);
  255. var x3 = x2 + (nx < 0 ? -1 : 1) * labelLineLen2;
  256. var y3 = y2;
  257. if (labelAlignTo === 'edge') {
  258. // Adjust textX because text align of edge is opposite
  259. textX = nx < 0 ? viewLeft + edgeDistance : viewLeft + viewWidth - edgeDistance;
  260. } else {
  261. textX = x3 + (nx < 0 ? -labelDistance : labelDistance);
  262. }
  263. textY = y3;
  264. linePoints = [[x1, y1], [x2, y2], [x3, y3]];
  265. }
  266. textAlign = isLabelInside ? 'center' : labelAlignTo === 'edge' ? nx > 0 ? 'right' : 'left' : nx > 0 ? 'left' : 'right';
  267. }
  268. var labelRotate;
  269. var rotate = labelModel.get('rotate');
  270. if (typeof rotate === 'number') {
  271. labelRotate = rotate * (Math.PI / 180);
  272. } else if (labelPosition === 'center') {
  273. labelRotate = 0;
  274. } else {
  275. var radialAngle = nx < 0 ? -midAngle + Math.PI : -midAngle;
  276. if (rotate === 'radial' || rotate === true) {
  277. labelRotate = radialAngle;
  278. } else if (rotate === 'tangential' && labelPosition !== 'outside' && labelPosition !== 'outer') {
  279. labelRotate = radialAngle + Math.PI / 2;
  280. if (labelRotate > Math.PI / 2) {
  281. labelRotate -= Math.PI;
  282. }
  283. } else {
  284. labelRotate = 0;
  285. }
  286. }
  287. hasLabelRotate = !!labelRotate;
  288. label.x = textX;
  289. label.y = textY;
  290. label.rotation = labelRotate;
  291. label.setStyle({
  292. verticalAlign: 'middle'
  293. }); // Not sectorShape the inside label
  294. if (!isLabelInside) {
  295. var textRect = label.getBoundingRect().clone();
  296. textRect.applyTransform(label.getComputedTransform()); // Text has a default 1px stroke. Exclude this.
  297. var margin = (label.style.margin || 0) + 2.1;
  298. textRect.y -= margin / 2;
  299. textRect.height += margin;
  300. labelLayoutList.push({
  301. label: label,
  302. labelLine: labelLine,
  303. position: labelPosition,
  304. len: labelLineLen,
  305. len2: labelLineLen2,
  306. minTurnAngle: labelLineModel.get('minTurnAngle'),
  307. maxSurfaceAngle: labelLineModel.get('maxSurfaceAngle'),
  308. surfaceNormal: new Point(nx, ny),
  309. linePoints: linePoints,
  310. textAlign: textAlign,
  311. labelDistance: labelDistance,
  312. labelAlignTo: labelAlignTo,
  313. edgeDistance: edgeDistance,
  314. bleedMargin: bleedMargin,
  315. rect: textRect
  316. });
  317. } else {
  318. label.setStyle({
  319. align: textAlign
  320. });
  321. var selectState = label.states.select;
  322. if (selectState) {
  323. selectState.x += label.x;
  324. selectState.y += label.y;
  325. }
  326. }
  327. sector.setTextConfig({
  328. inside: isLabelInside
  329. });
  330. });
  331. if (!hasLabelRotate && seriesModel.get('avoidLabelOverlap')) {
  332. avoidOverlap(labelLayoutList, cx, cy, r, viewWidth, viewHeight, viewLeft, viewTop);
  333. }
  334. for (var i = 0; i < labelLayoutList.length; i++) {
  335. var layout = labelLayoutList[i];
  336. var label = layout.label;
  337. var labelLine = layout.labelLine;
  338. var notShowLabel = isNaN(label.x) || isNaN(label.y);
  339. if (label) {
  340. label.setStyle({
  341. align: layout.textAlign
  342. });
  343. if (notShowLabel) {
  344. each(label.states, setNotShow);
  345. label.ignore = true;
  346. }
  347. var selectState = label.states.select;
  348. if (selectState) {
  349. selectState.x += label.x;
  350. selectState.y += label.y;
  351. }
  352. }
  353. if (labelLine) {
  354. var linePoints = layout.linePoints;
  355. if (notShowLabel || !linePoints) {
  356. each(labelLine.states, setNotShow);
  357. labelLine.ignore = true;
  358. } else {
  359. limitTurnAngle(linePoints, layout.minTurnAngle);
  360. limitSurfaceAngle(linePoints, layout.surfaceNormal, layout.maxSurfaceAngle);
  361. labelLine.setShape({
  362. points: linePoints
  363. }); // Set the anchor to the midpoint of sector
  364. label.__hostTarget.textGuideLineConfig = {
  365. anchor: new Point(linePoints[0][0], linePoints[0][1])
  366. };
  367. }
  368. }
  369. }
  370. }