labelLayout.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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.js';
  42. import { Point } from '../../util/graphic.js';
  43. import { each, isNumber } from 'zrender/lib/core/util.js';
  44. import { limitTurnAngle, limitSurfaceAngle } from '../../label/labelGuideHelper.js';
  45. import { shiftLayoutOnY } from '../../label/labelLayoutHelper.js';
  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. var newX = cx + (dx + item.len2) * dir;
  62. var deltaX = newX - item.label.x;
  63. var newTargetWidth = item.targetTextWidth - deltaX * dir; // text x is changed, so need to recalculate width.
  64. constrainTextWidth(item, newTargetWidth, true);
  65. item.label.x = newX;
  66. }
  67. } // Adjust X based on the shifted y. Make tight labels aligned on an ellipse curve.
  68. function recalculateX(items) {
  69. // Extremes of
  70. var topSemi = {
  71. list: [],
  72. maxY: 0
  73. };
  74. var bottomSemi = {
  75. list: [],
  76. maxY: 0
  77. };
  78. for (var i = 0; i < items.length; i++) {
  79. if (items[i].labelAlignTo !== 'none') {
  80. continue;
  81. }
  82. var item = items[i];
  83. var semi = item.label.y > cy ? bottomSemi : topSemi;
  84. var dy = Math.abs(item.label.y - cy);
  85. if (dy >= semi.maxY) {
  86. var dx = item.label.x - cx - item.len2 * dir; // horizontal r is always same with original r because x is not changed.
  87. var rA = r + item.len; // Canculate rB based on the topest / bottemest label.
  88. var rB = Math.abs(dx) < rA ? Math.sqrt(dy * dy / (1 - dx * dx / rA / rA)) : rA;
  89. semi.rB = rB;
  90. semi.maxY = dy;
  91. }
  92. semi.list.push(item);
  93. }
  94. recalculateXOnSemiToAlignOnEllipseCurve(topSemi);
  95. recalculateXOnSemiToAlignOnEllipseCurve(bottomSemi);
  96. }
  97. var len = list.length;
  98. for (var i = 0; i < len; i++) {
  99. if (list[i].position === 'outer' && list[i].labelAlignTo === 'labelLine') {
  100. var dx = list[i].label.x - farthestX;
  101. list[i].linePoints[1][0] += dx;
  102. list[i].label.x = farthestX;
  103. }
  104. }
  105. if (shiftLayoutOnY(list, viewTop, viewTop + viewHeight)) {
  106. recalculateX(list);
  107. }
  108. }
  109. function avoidOverlap(labelLayoutList, cx, cy, r, viewWidth, viewHeight, viewLeft, viewTop) {
  110. var leftList = [];
  111. var rightList = [];
  112. var leftmostX = Number.MAX_VALUE;
  113. var rightmostX = -Number.MAX_VALUE;
  114. for (var i = 0; i < labelLayoutList.length; i++) {
  115. var label = labelLayoutList[i].label;
  116. if (isPositionCenter(labelLayoutList[i])) {
  117. continue;
  118. }
  119. if (label.x < cx) {
  120. leftmostX = Math.min(leftmostX, label.x);
  121. leftList.push(labelLayoutList[i]);
  122. } else {
  123. rightmostX = Math.max(rightmostX, label.x);
  124. rightList.push(labelLayoutList[i]);
  125. }
  126. }
  127. for (var i = 0; i < labelLayoutList.length; i++) {
  128. var layout = labelLayoutList[i];
  129. if (!isPositionCenter(layout) && layout.linePoints) {
  130. if (layout.labelStyleWidth != null) {
  131. continue;
  132. }
  133. var label = layout.label;
  134. var linePoints = layout.linePoints;
  135. var targetTextWidth = void 0;
  136. if (layout.labelAlignTo === 'edge') {
  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 if (layout.labelAlignTo === 'labelLine') {
  143. if (label.x < cx) {
  144. targetTextWidth = leftmostX - viewLeft - layout.bleedMargin;
  145. } else {
  146. targetTextWidth = viewLeft + viewWidth - rightmostX - layout.bleedMargin;
  147. }
  148. } else {
  149. if (label.x < cx) {
  150. targetTextWidth = label.x - viewLeft - layout.bleedMargin;
  151. } else {
  152. targetTextWidth = viewLeft + viewWidth - label.x - layout.bleedMargin;
  153. }
  154. }
  155. layout.targetTextWidth = targetTextWidth;
  156. constrainTextWidth(layout, targetTextWidth);
  157. }
  158. }
  159. adjustSingleSide(rightList, cx, cy, r, 1, viewWidth, viewHeight, viewLeft, viewTop, rightmostX);
  160. adjustSingleSide(leftList, cx, cy, r, -1, viewWidth, viewHeight, viewLeft, viewTop, leftmostX);
  161. for (var i = 0; i < labelLayoutList.length; i++) {
  162. var layout = labelLayoutList[i];
  163. if (!isPositionCenter(layout) && layout.linePoints) {
  164. var label = layout.label;
  165. var linePoints = layout.linePoints;
  166. var isAlignToEdge = layout.labelAlignTo === 'edge';
  167. var padding = label.style.padding;
  168. var paddingH = padding ? padding[1] + padding[3] : 0; // textRect.width already contains paddingH if bgColor is set
  169. var extraPaddingH = label.style.backgroundColor ? 0 : paddingH;
  170. var realTextWidth = layout.rect.width + extraPaddingH;
  171. var dist = linePoints[1][0] - linePoints[2][0];
  172. if (isAlignToEdge) {
  173. if (label.x < cx) {
  174. linePoints[2][0] = viewLeft + layout.edgeDistance + realTextWidth + layout.labelDistance;
  175. } else {
  176. linePoints[2][0] = viewLeft + viewWidth - layout.edgeDistance - realTextWidth - layout.labelDistance;
  177. }
  178. } else {
  179. if (label.x < cx) {
  180. linePoints[2][0] = label.x + layout.labelDistance;
  181. } else {
  182. linePoints[2][0] = label.x - layout.labelDistance;
  183. }
  184. linePoints[1][0] = linePoints[2][0] + dist;
  185. }
  186. linePoints[1][1] = linePoints[2][1] = label.y;
  187. }
  188. }
  189. }
  190. /**
  191. * Set max width of each label, and then wrap each label to the max width.
  192. *
  193. * @param layout label layout
  194. * @param availableWidth max width for the label to display
  195. * @param forceRecalculate recaculate the text layout even if the current width
  196. * is smaller than `availableWidth`. This is useful when the text was previously
  197. * wrapped by calling `constrainTextWidth` but now `availableWidth` changed, in
  198. * which case, previous wrapping should be redo.
  199. */
  200. function constrainTextWidth(layout, availableWidth, forceRecalculate) {
  201. if (forceRecalculate === void 0) {
  202. forceRecalculate = false;
  203. }
  204. if (layout.labelStyleWidth != null) {
  205. // User-defined style.width has the highest priority.
  206. return;
  207. }
  208. var label = layout.label;
  209. var style = label.style;
  210. var textRect = layout.rect;
  211. var bgColor = style.backgroundColor;
  212. var padding = style.padding;
  213. var paddingH = padding ? padding[1] + padding[3] : 0;
  214. var overflow = style.overflow; // textRect.width already contains paddingH if bgColor is set
  215. var oldOuterWidth = textRect.width + (bgColor ? 0 : paddingH);
  216. if (availableWidth < oldOuterWidth || forceRecalculate) {
  217. var oldHeight = textRect.height;
  218. if (overflow && overflow.match('break')) {
  219. // Temporarily set background to be null to calculate
  220. // the bounding box without background.
  221. label.setStyle('backgroundColor', null); // Set constraining width
  222. label.setStyle('width', availableWidth - paddingH); // This is the real bounding box of the text without padding.
  223. var innerRect = label.getBoundingRect();
  224. label.setStyle('width', Math.ceil(innerRect.width));
  225. label.setStyle('backgroundColor', bgColor);
  226. } else {
  227. var availableInnerWidth = availableWidth - paddingH;
  228. var newWidth = availableWidth < oldOuterWidth // Current text is too wide, use `availableWidth` as max width.
  229. ? availableInnerWidth : // Current available width is enough, but the text may have
  230. // already been wrapped with a smaller available width.
  231. forceRecalculate ? availableInnerWidth > layout.unconstrainedWidth // Current available is larger than text width,
  232. // so don't constrain width (otherwise it may have
  233. // empty space in the background).
  234. ? null // Current available is smaller than text width, so
  235. // use the current available width as constraining
  236. // width.
  237. : availableInnerWidth : // Current available width is enough, so no need to
  238. // constrain.
  239. null;
  240. label.setStyle('width', newWidth);
  241. }
  242. var newRect = label.getBoundingRect();
  243. textRect.width = newRect.width;
  244. var margin = (label.style.margin || 0) + 2.1;
  245. textRect.height = newRect.height + margin;
  246. textRect.y -= (textRect.height - oldHeight) / 2;
  247. }
  248. }
  249. function isPositionCenter(sectorShape) {
  250. // Not change x for center label
  251. return sectorShape.position === 'center';
  252. }
  253. export default function pieLabelLayout(seriesModel) {
  254. var data = seriesModel.getData();
  255. var labelLayoutList = [];
  256. var cx;
  257. var cy;
  258. var hasLabelRotate = false;
  259. var minShowLabelRadian = (seriesModel.get('minShowLabelAngle') || 0) * RADIAN;
  260. var viewRect = data.getLayout('viewRect');
  261. var r = data.getLayout('r');
  262. var viewWidth = viewRect.width;
  263. var viewLeft = viewRect.x;
  264. var viewTop = viewRect.y;
  265. var viewHeight = viewRect.height;
  266. function setNotShow(el) {
  267. el.ignore = true;
  268. }
  269. function isLabelShown(label) {
  270. if (!label.ignore) {
  271. return true;
  272. }
  273. for (var key in label.states) {
  274. if (label.states[key].ignore === false) {
  275. return true;
  276. }
  277. }
  278. return false;
  279. }
  280. data.each(function (idx) {
  281. var sector = data.getItemGraphicEl(idx);
  282. var sectorShape = sector.shape;
  283. var label = sector.getTextContent();
  284. var labelLine = sector.getTextGuideLine();
  285. var itemModel = data.getItemModel(idx);
  286. var labelModel = itemModel.getModel('label'); // Use position in normal or emphasis
  287. var labelPosition = labelModel.get('position') || itemModel.get(['emphasis', 'label', 'position']);
  288. var labelDistance = labelModel.get('distanceToLabelLine');
  289. var labelAlignTo = labelModel.get('alignTo');
  290. var edgeDistance = parsePercent(labelModel.get('edgeDistance'), viewWidth);
  291. var bleedMargin = labelModel.get('bleedMargin');
  292. var labelLineModel = itemModel.getModel('labelLine');
  293. var labelLineLen = labelLineModel.get('length');
  294. labelLineLen = parsePercent(labelLineLen, viewWidth);
  295. var labelLineLen2 = labelLineModel.get('length2');
  296. labelLineLen2 = parsePercent(labelLineLen2, viewWidth);
  297. if (Math.abs(sectorShape.endAngle - sectorShape.startAngle) < minShowLabelRadian) {
  298. each(label.states, setNotShow);
  299. label.ignore = true;
  300. if (labelLine) {
  301. each(labelLine.states, setNotShow);
  302. labelLine.ignore = true;
  303. }
  304. return;
  305. }
  306. if (!isLabelShown(label)) {
  307. return;
  308. }
  309. var midAngle = (sectorShape.startAngle + sectorShape.endAngle) / 2;
  310. var nx = Math.cos(midAngle);
  311. var ny = Math.sin(midAngle);
  312. var textX;
  313. var textY;
  314. var linePoints;
  315. var textAlign;
  316. cx = sectorShape.cx;
  317. cy = sectorShape.cy;
  318. var isLabelInside = labelPosition === 'inside' || labelPosition === 'inner';
  319. if (labelPosition === 'center') {
  320. textX = sectorShape.cx;
  321. textY = sectorShape.cy;
  322. textAlign = 'center';
  323. } else {
  324. var x1 = (isLabelInside ? (sectorShape.r + sectorShape.r0) / 2 * nx : sectorShape.r * nx) + cx;
  325. var y1 = (isLabelInside ? (sectorShape.r + sectorShape.r0) / 2 * ny : sectorShape.r * ny) + cy;
  326. textX = x1 + nx * 3;
  327. textY = y1 + ny * 3;
  328. if (!isLabelInside) {
  329. // For roseType
  330. var x2 = x1 + nx * (labelLineLen + r - sectorShape.r);
  331. var y2 = y1 + ny * (labelLineLen + r - sectorShape.r);
  332. var x3 = x2 + (nx < 0 ? -1 : 1) * labelLineLen2;
  333. var y3 = y2;
  334. if (labelAlignTo === 'edge') {
  335. // Adjust textX because text align of edge is opposite
  336. textX = nx < 0 ? viewLeft + edgeDistance : viewLeft + viewWidth - edgeDistance;
  337. } else {
  338. textX = x3 + (nx < 0 ? -labelDistance : labelDistance);
  339. }
  340. textY = y3;
  341. linePoints = [[x1, y1], [x2, y2], [x3, y3]];
  342. }
  343. textAlign = isLabelInside ? 'center' : labelAlignTo === 'edge' ? nx > 0 ? 'right' : 'left' : nx > 0 ? 'left' : 'right';
  344. }
  345. var PI = Math.PI;
  346. var labelRotate = 0;
  347. var rotate = labelModel.get('rotate');
  348. if (isNumber(rotate)) {
  349. labelRotate = rotate * (PI / 180);
  350. } else if (labelPosition === 'center') {
  351. labelRotate = 0;
  352. } else if (rotate === 'radial' || rotate === true) {
  353. var radialAngle = nx < 0 ? -midAngle + PI : -midAngle;
  354. labelRotate = radialAngle;
  355. } else if (rotate === 'tangential' && labelPosition !== 'outside' && labelPosition !== 'outer') {
  356. var rad = Math.atan2(nx, ny);
  357. if (rad < 0) {
  358. rad = PI * 2 + rad;
  359. }
  360. var isDown = ny > 0;
  361. if (isDown) {
  362. rad = PI + rad;
  363. }
  364. labelRotate = rad - PI;
  365. }
  366. hasLabelRotate = !!labelRotate;
  367. label.x = textX;
  368. label.y = textY;
  369. label.rotation = labelRotate;
  370. label.setStyle({
  371. verticalAlign: 'middle'
  372. }); // Not sectorShape the inside label
  373. if (!isLabelInside) {
  374. var textRect = label.getBoundingRect().clone();
  375. textRect.applyTransform(label.getComputedTransform()); // Text has a default 1px stroke. Exclude this.
  376. var margin = (label.style.margin || 0) + 2.1;
  377. textRect.y -= margin / 2;
  378. textRect.height += margin;
  379. labelLayoutList.push({
  380. label: label,
  381. labelLine: labelLine,
  382. position: labelPosition,
  383. len: labelLineLen,
  384. len2: labelLineLen2,
  385. minTurnAngle: labelLineModel.get('minTurnAngle'),
  386. maxSurfaceAngle: labelLineModel.get('maxSurfaceAngle'),
  387. surfaceNormal: new Point(nx, ny),
  388. linePoints: linePoints,
  389. textAlign: textAlign,
  390. labelDistance: labelDistance,
  391. labelAlignTo: labelAlignTo,
  392. edgeDistance: edgeDistance,
  393. bleedMargin: bleedMargin,
  394. rect: textRect,
  395. unconstrainedWidth: textRect.width,
  396. labelStyleWidth: label.style.width
  397. });
  398. } else {
  399. label.setStyle({
  400. align: textAlign
  401. });
  402. var selectState = label.states.select;
  403. if (selectState) {
  404. selectState.x += label.x;
  405. selectState.y += label.y;
  406. }
  407. }
  408. sector.setTextConfig({
  409. inside: isLabelInside
  410. });
  411. });
  412. if (!hasLabelRotate && seriesModel.get('avoidLabelOverlap')) {
  413. avoidOverlap(labelLayoutList, cx, cy, r, viewWidth, viewHeight, viewLeft, viewTop);
  414. }
  415. for (var i = 0; i < labelLayoutList.length; i++) {
  416. var layout = labelLayoutList[i];
  417. var label = layout.label;
  418. var labelLine = layout.labelLine;
  419. var notShowLabel = isNaN(label.x) || isNaN(label.y);
  420. if (label) {
  421. label.setStyle({
  422. align: layout.textAlign
  423. });
  424. if (notShowLabel) {
  425. each(label.states, setNotShow);
  426. label.ignore = true;
  427. }
  428. var selectState = label.states.select;
  429. if (selectState) {
  430. selectState.x += label.x;
  431. selectState.y += label.y;
  432. }
  433. }
  434. if (labelLine) {
  435. var linePoints = layout.linePoints;
  436. if (notShowLabel || !linePoints) {
  437. each(labelLine.states, setNotShow);
  438. labelLine.ignore = true;
  439. } else {
  440. limitTurnAngle(linePoints, layout.minTurnAngle);
  441. limitSurfaceAngle(linePoints, layout.surfaceNormal, layout.maxSurfaceAngle);
  442. labelLine.setShape({
  443. points: linePoints
  444. }); // Set the anchor to the midpoint of sector
  445. label.__hostTarget.textGuideLineConfig = {
  446. anchor: new Point(linePoints[0][0], linePoints[0][1])
  447. };
  448. }
  449. }
  450. }
  451. }