AxisBuilder.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 { retrieve, defaults, extend, each, isObject, isString, isNumber, isFunction, retrieve2, assert, map, retrieve3, filter } from 'zrender/lib/core/util.js';
  41. import * as graphic from '../../util/graphic.js';
  42. import { getECData } from '../../util/innerStore.js';
  43. import { createTextStyle } from '../../label/labelStyle.js';
  44. import Model from '../../model/Model.js';
  45. import { isRadianAroundZero, remRadian } from '../../util/number.js';
  46. import { createSymbol, normalizeSymbolOffset } from '../../util/symbol.js';
  47. import * as matrixUtil from 'zrender/lib/core/matrix.js';
  48. import { applyTransform as v2ApplyTransform } from 'zrender/lib/core/vector.js';
  49. import { getTickValueOutermost, isNameLocationCenter, shouldShowAllLabels } from '../../coord/axisHelper.js';
  50. import { hideOverlap, labelIntersect, computeLabelGeometry2, ensureLabelLayoutWithGeometry, labelLayoutApplyTranslation, setLabelLayoutDirty, newLabelLayoutWithGeometry } from '../../label/labelLayoutHelper.js';
  51. import { makeInner } from '../../util/model.js';
  52. import { getAxisBreakHelper } from './axisBreakHelper.js';
  53. import { AXIS_BREAK_EXPAND_ACTION_TYPE } from './axisAction.js';
  54. import { getScaleBreakHelper, hasBreaks } from '../../scale/break.js';
  55. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  56. import Point from 'zrender/lib/core/Point.js';
  57. import { copyTransform } from 'zrender/lib/core/Transformable.js';
  58. import { AxisTickLabelComputingKind, createAxisLabelsComputingContext } from '../../coord/axisTickLabelBuilder.js';
  59. import { isOrdinalScale, isTimeScale } from '../../scale/helper.js';
  60. var PI = Math.PI;
  61. var DEFAULT_CENTER_NAME_MARGIN_LEVELS = [[1, 2, 1, 2], [5, 3, 5, 3], [8, 3, 8, 3]];
  62. var DEFAULT_ENDS_NAME_MARGIN_LEVELS = [[0, 1, 0, 1], [0, 3, 0, 3], [0, 3, 0, 3]];
  63. export var getLabelInner = makeInner();
  64. var getTickInner = makeInner();
  65. /**
  66. * A context shared by difference axisBuilder instances.
  67. * For cross-axes overlap resolving.
  68. *
  69. * Lifecycle constraint: should not over a pass of ec main process.
  70. * If model is changed, the context must be disposed.
  71. *
  72. * @see AxisBuilderLocalContext
  73. */
  74. var AxisBuilderSharedContext = /** @class */function () {
  75. function AxisBuilderSharedContext(resolveAxisNameOverlap) {
  76. /**
  77. * [CAUTION] Do not modify this data structure outside this class.
  78. */
  79. this.recordMap = {};
  80. this.resolveAxisNameOverlap = resolveAxisNameOverlap;
  81. }
  82. AxisBuilderSharedContext.prototype.ensureRecord = function (axisModel) {
  83. var dim = axisModel.axis.dim;
  84. var idx = axisModel.componentIndex;
  85. var recordMap = this.recordMap;
  86. var records = recordMap[dim] || (recordMap[dim] = []);
  87. return records[idx] || (records[idx] = {
  88. ready: {}
  89. });
  90. };
  91. return AxisBuilderSharedContext;
  92. }();
  93. export { AxisBuilderSharedContext };
  94. ;
  95. /**
  96. * [CAUTION]
  97. * 1. The call of this function must be after axisLabel overlap handlings
  98. * (such as `hideOverlap`, `fixMinMaxLabelShow`) and after transform calculating.
  99. * 2. Can be called multiple times and should be idempotent.
  100. */
  101. function resetOverlapRecordToShared(cfg, shared, axisModel, labelLayoutList) {
  102. var axis = axisModel.axis;
  103. var record = shared.ensureRecord(axisModel);
  104. var labelInfoList = [];
  105. var stOccupiedRect;
  106. var useStOccupiedRect = hasAxisName(cfg.axisName) && isNameLocationCenter(cfg.nameLocation);
  107. each(labelLayoutList, function (layout) {
  108. var layoutInfo = ensureLabelLayoutWithGeometry(layout);
  109. if (!layoutInfo || layoutInfo.label.ignore) {
  110. return;
  111. }
  112. labelInfoList.push(layoutInfo);
  113. var transGroup = record.transGroup;
  114. if (useStOccupiedRect) {
  115. // Transform to "standard axis" for creating stOccupiedRect (the label rects union).
  116. transGroup.transform ? matrixUtil.invert(_stTransTmp, transGroup.transform) : matrixUtil.identity(_stTransTmp);
  117. if (layoutInfo.transform) {
  118. matrixUtil.mul(_stTransTmp, _stTransTmp, layoutInfo.transform);
  119. }
  120. BoundingRect.copy(_stLabelRectTmp, layoutInfo.localRect);
  121. _stLabelRectTmp.applyTransform(_stTransTmp);
  122. stOccupiedRect ? stOccupiedRect.union(_stLabelRectTmp) : BoundingRect.copy(stOccupiedRect = new BoundingRect(0, 0, 0, 0), _stLabelRectTmp);
  123. }
  124. });
  125. var sortByDim = Math.abs(record.dirVec.x) > 0.1 ? 'x' : 'y';
  126. var sortByValue = record.transGroup[sortByDim];
  127. labelInfoList.sort(function (info1, info2) {
  128. return Math.abs(info1.label[sortByDim] - sortByValue) - Math.abs(info2.label[sortByDim] - sortByValue);
  129. });
  130. if (useStOccupiedRect && stOccupiedRect) {
  131. var extent = axis.getExtent();
  132. var axisLineX = Math.min(extent[0], extent[1]);
  133. var axisLineWidth = Math.max(extent[0], extent[1]) - axisLineX;
  134. // If `nameLocation` is 'middle', enlarge axis labels boundingRect to axisLine to avoid bad
  135. // case like that axis name is placed in the gap between axis labels and axis line.
  136. // If only one label exists, the entire band should be occupied for
  137. // visual consistency, so extent it to [0, canvas width].
  138. stOccupiedRect.union(new BoundingRect(axisLineX, 0, axisLineWidth, 1));
  139. }
  140. record.stOccupiedRect = stOccupiedRect;
  141. record.labelInfoList = labelInfoList;
  142. }
  143. var _stTransTmp = matrixUtil.create();
  144. var _stLabelRectTmp = new BoundingRect(0, 0, 0, 0);
  145. /**
  146. * The default resolver does not involve other axes within the same coordinate system.
  147. */
  148. export var resolveAxisNameOverlapDefault = function (cfg, ctx, axisModel, nameLayoutInfo, nameMoveDirVec, thisRecord) {
  149. if (isNameLocationCenter(cfg.nameLocation)) {
  150. var stOccupiedRect = thisRecord.stOccupiedRect;
  151. if (stOccupiedRect) {
  152. moveIfOverlap(computeLabelGeometry2({}, stOccupiedRect, thisRecord.transGroup.transform), nameLayoutInfo, nameMoveDirVec);
  153. }
  154. } else {
  155. moveIfOverlapByLinearLabels(thisRecord.labelInfoList, thisRecord.dirVec, nameLayoutInfo, nameMoveDirVec);
  156. }
  157. };
  158. // [NOTICE] not consider ignore.
  159. function moveIfOverlap(basedLayoutInfo, movableLayoutInfo, moveDirVec) {
  160. var mtv = new Point();
  161. if (labelIntersect(basedLayoutInfo, movableLayoutInfo, mtv, {
  162. direction: Math.atan2(moveDirVec.y, moveDirVec.x),
  163. bidirectional: false,
  164. touchThreshold: 0.05
  165. })) {
  166. labelLayoutApplyTranslation(movableLayoutInfo, mtv);
  167. }
  168. }
  169. export function moveIfOverlapByLinearLabels(baseLayoutInfoList, baseDirVec, movableLayoutInfo, moveDirVec) {
  170. // Detect and move from far to close.
  171. var sameDir = Point.dot(moveDirVec, baseDirVec) >= 0;
  172. for (var idx = 0, len = baseLayoutInfoList.length; idx < len; idx++) {
  173. var labelInfo = baseLayoutInfoList[sameDir ? idx : len - 1 - idx];
  174. if (!labelInfo.label.ignore) {
  175. moveIfOverlap(labelInfo, movableLayoutInfo, moveDirVec);
  176. }
  177. }
  178. }
  179. /**
  180. * @caution
  181. * - Ensure it is called after the data processing stage finished.
  182. * - It might be called before `CahrtView#render`, sush as called at `CoordinateSystem#update`,
  183. * thus ensure the result the same whenever it is called.
  184. *
  185. * A builder for a straight-line axis.
  186. *
  187. * A final axis is translated and rotated from a "standard axis".
  188. * So opt.position and opt.rotation is required.
  189. *
  190. * A "standard axis" is the axis [0,0]-->[abs(axisExtent[1]-axisExtent[0]),0]
  191. * for example: [0,0]-->[50,0]
  192. */
  193. var AxisBuilder = /** @class */function () {
  194. /**
  195. * [CAUTION]: axisModel.axis.extent/scale must be ready to use.
  196. */
  197. function AxisBuilder(axisModel, api, opt, shared) {
  198. this.group = new graphic.Group();
  199. this._axisModel = axisModel;
  200. this._api = api;
  201. this._local = {};
  202. this._shared = shared || new AxisBuilderSharedContext(resolveAxisNameOverlapDefault);
  203. this._resetCfgDetermined(opt);
  204. }
  205. /**
  206. * Regarding axis label related configurations, only the change of label.x/y is supported; other
  207. * changes are not necessary and not performant. To be specific, only `axis.position`
  208. * (and consequently `labelOffset`) and `axis.extent` can be changed, and assume everything in
  209. * `axisModel` are not changed.
  210. * Axis line related configurations can be changed since this method can only be called
  211. * before they are created.
  212. */
  213. AxisBuilder.prototype.updateCfg = function (opt) {
  214. if (process.env.NODE_ENV !== 'production') {
  215. var ready = this._shared.ensureRecord(this._axisModel).ready;
  216. // After that, changing cfg is not supported; avoid unnecessary complexity.
  217. assert(!ready.axisLine && !ready.axisTickLabelDetermine);
  218. // Have to be called again if cfg changed.
  219. ready.axisName = ready.axisTickLabelEstimate = false;
  220. }
  221. var raw = this._cfg.raw;
  222. raw.position = opt.position;
  223. raw.labelOffset = opt.labelOffset;
  224. this._resetCfgDetermined(raw);
  225. };
  226. /**
  227. * [CAUTION] For debug usage. Never change it outside!
  228. */
  229. AxisBuilder.prototype.__getRawCfg = function () {
  230. return this._cfg.raw;
  231. };
  232. AxisBuilder.prototype._resetCfgDetermined = function (raw) {
  233. var axisModel = this._axisModel;
  234. // FIXME:
  235. // Currently there is no uniformed way to set default values if an option
  236. // is specified null/undefined by user (intentionally or unintentionally),
  237. // e.g. null/undefined is not a illegal value for `nameLocation`.
  238. // Try to use `getDefaultOption` to address it. But radar has no `getDefaultOption`.
  239. var axisModelDefaultOption = axisModel.getDefaultOption ? axisModel.getDefaultOption() : {};
  240. // Default value
  241. var axisName = retrieve2(raw.axisName, axisModel.get('name'));
  242. var nameMoveOverlapOption = axisModel.get('nameMoveOverlap');
  243. if (nameMoveOverlapOption == null || nameMoveOverlapOption === 'auto') {
  244. nameMoveOverlapOption = retrieve2(raw.defaultNameMoveOverlap, true);
  245. }
  246. var cfg = {
  247. raw: raw,
  248. position: raw.position,
  249. rotation: raw.rotation,
  250. nameDirection: retrieve2(raw.nameDirection, 1),
  251. tickDirection: retrieve2(raw.tickDirection, 1),
  252. labelDirection: retrieve2(raw.labelDirection, 1),
  253. labelOffset: retrieve2(raw.labelOffset, 0),
  254. silent: retrieve2(raw.silent, true),
  255. axisName: axisName,
  256. nameLocation: retrieve3(axisModel.get('nameLocation'), axisModelDefaultOption.nameLocation, 'end'),
  257. shouldNameMoveOverlap: hasAxisName(axisName) && nameMoveOverlapOption,
  258. optionHideOverlap: axisModel.get(['axisLabel', 'hideOverlap']),
  259. showMinorTicks: axisModel.get(['minorTick', 'show'])
  260. };
  261. if (process.env.NODE_ENV !== 'production') {
  262. assert(cfg.position != null);
  263. assert(cfg.rotation != null);
  264. }
  265. this._cfg = cfg;
  266. // FIXME Not use a separate text group?
  267. var transformGroup = new graphic.Group({
  268. x: cfg.position[0],
  269. y: cfg.position[1],
  270. rotation: cfg.rotation
  271. });
  272. transformGroup.updateTransform();
  273. this._transformGroup = transformGroup;
  274. var record = this._shared.ensureRecord(axisModel);
  275. record.transGroup = this._transformGroup;
  276. record.dirVec = new Point(Math.cos(-cfg.rotation), Math.sin(-cfg.rotation));
  277. };
  278. AxisBuilder.prototype.build = function (axisPartNameMap, extraParams) {
  279. var _this = this;
  280. if (!axisPartNameMap) {
  281. axisPartNameMap = {
  282. axisLine: true,
  283. axisTickLabelEstimate: false,
  284. axisTickLabelDetermine: true,
  285. axisName: true
  286. };
  287. }
  288. each(AXIS_BUILDER_AXIS_PART_NAMES, function (partName) {
  289. if (axisPartNameMap[partName]) {
  290. builders[partName](_this._cfg, _this._local, _this._shared, _this._axisModel, _this.group, _this._transformGroup, _this._api, extraParams || {});
  291. }
  292. });
  293. return this;
  294. };
  295. /**
  296. * Currently only get text align/verticalAlign by rotation.
  297. * NO `position` is involved, otherwise it have to be performed for each `updateAxisLabelChangableProps`.
  298. */
  299. AxisBuilder.innerTextLayout = function (axisRotation, textRotation, direction) {
  300. var rotationDiff = remRadian(textRotation - axisRotation);
  301. var textAlign;
  302. var textVerticalAlign;
  303. if (isRadianAroundZero(rotationDiff)) {
  304. // Label is parallel with axis line.
  305. textVerticalAlign = direction > 0 ? 'top' : 'bottom';
  306. textAlign = 'center';
  307. } else if (isRadianAroundZero(rotationDiff - PI)) {
  308. // Label is inverse parallel with axis line.
  309. textVerticalAlign = direction > 0 ? 'bottom' : 'top';
  310. textAlign = 'center';
  311. } else {
  312. textVerticalAlign = 'middle';
  313. if (rotationDiff > 0 && rotationDiff < PI) {
  314. textAlign = direction > 0 ? 'right' : 'left';
  315. } else {
  316. textAlign = direction > 0 ? 'left' : 'right';
  317. }
  318. }
  319. return {
  320. rotation: rotationDiff,
  321. textAlign: textAlign,
  322. textVerticalAlign: textVerticalAlign
  323. };
  324. };
  325. AxisBuilder.makeAxisEventDataBase = function (axisModel) {
  326. var eventData = {
  327. componentType: axisModel.mainType,
  328. componentIndex: axisModel.componentIndex
  329. };
  330. eventData[axisModel.mainType + 'Index'] = axisModel.componentIndex;
  331. return eventData;
  332. };
  333. AxisBuilder.isLabelSilent = function (axisModel) {
  334. var tooltipOpt = axisModel.get('tooltip');
  335. return axisModel.get('silent')
  336. // Consider mouse cursor, add these restrictions.
  337. || !(axisModel.get('triggerEvent') || tooltipOpt && tooltipOpt.show);
  338. };
  339. return AxisBuilder;
  340. }();
  341. ;
  342. // Sorted by dependency order.
  343. var AXIS_BUILDER_AXIS_PART_NAMES = ['axisLine', 'axisTickLabelEstimate', 'axisTickLabelDetermine', 'axisName'];
  344. var builders = {
  345. axisLine: function (cfg, local, shared, axisModel, group, transformGroup, api) {
  346. if (process.env.NODE_ENV !== 'production') {
  347. var ready = shared.ensureRecord(axisModel).ready;
  348. assert(!ready.axisLine);
  349. ready.axisLine = true;
  350. }
  351. var shown = axisModel.get(['axisLine', 'show']);
  352. if (shown === 'auto') {
  353. shown = true;
  354. if (cfg.raw.axisLineAutoShow != null) {
  355. shown = !!cfg.raw.axisLineAutoShow;
  356. }
  357. }
  358. if (!shown) {
  359. return;
  360. }
  361. var extent = axisModel.axis.getExtent();
  362. var matrix = transformGroup.transform;
  363. var pt1 = [extent[0], 0];
  364. var pt2 = [extent[1], 0];
  365. var inverse = pt1[0] > pt2[0];
  366. if (matrix) {
  367. v2ApplyTransform(pt1, pt1, matrix);
  368. v2ApplyTransform(pt2, pt2, matrix);
  369. }
  370. var lineStyle = extend({
  371. lineCap: 'round'
  372. }, axisModel.getModel(['axisLine', 'lineStyle']).getLineStyle());
  373. var pathBaseProp = {
  374. strokeContainThreshold: cfg.raw.strokeContainThreshold || 5,
  375. silent: true,
  376. z2: 1,
  377. style: lineStyle
  378. };
  379. if (axisModel.get(['axisLine', 'breakLine']) && hasBreaks(axisModel.axis.scale)) {
  380. getAxisBreakHelper().buildAxisBreakLine(axisModel, group, transformGroup, pathBaseProp);
  381. } else {
  382. var line = new graphic.Line(extend({
  383. shape: {
  384. x1: pt1[0],
  385. y1: pt1[1],
  386. x2: pt2[0],
  387. y2: pt2[1]
  388. }
  389. }, pathBaseProp));
  390. graphic.subPixelOptimizeLine(line.shape, line.style.lineWidth);
  391. line.anid = 'line';
  392. group.add(line);
  393. }
  394. var arrows = axisModel.get(['axisLine', 'symbol']);
  395. if (arrows != null) {
  396. var arrowSize = axisModel.get(['axisLine', 'symbolSize']);
  397. if (isString(arrows)) {
  398. // Use the same arrow for start and end point
  399. arrows = [arrows, arrows];
  400. }
  401. if (isString(arrowSize) || isNumber(arrowSize)) {
  402. // Use the same size for width and height
  403. arrowSize = [arrowSize, arrowSize];
  404. }
  405. var arrowOffset = normalizeSymbolOffset(axisModel.get(['axisLine', 'symbolOffset']) || 0, arrowSize);
  406. var symbolWidth_1 = arrowSize[0];
  407. var symbolHeight_1 = arrowSize[1];
  408. each([{
  409. rotate: cfg.rotation + Math.PI / 2,
  410. offset: arrowOffset[0],
  411. r: 0
  412. }, {
  413. rotate: cfg.rotation - Math.PI / 2,
  414. offset: arrowOffset[1],
  415. r: Math.sqrt((pt1[0] - pt2[0]) * (pt1[0] - pt2[0]) + (pt1[1] - pt2[1]) * (pt1[1] - pt2[1]))
  416. }], function (point, index) {
  417. if (arrows[index] !== 'none' && arrows[index] != null) {
  418. var symbol = createSymbol(arrows[index], -symbolWidth_1 / 2, -symbolHeight_1 / 2, symbolWidth_1, symbolHeight_1, lineStyle.stroke, true);
  419. // Calculate arrow position with offset
  420. var r = point.r + point.offset;
  421. var pt = inverse ? pt2 : pt1;
  422. symbol.attr({
  423. rotation: point.rotate,
  424. x: pt[0] + r * Math.cos(cfg.rotation),
  425. y: pt[1] - r * Math.sin(cfg.rotation),
  426. silent: true,
  427. z2: 11
  428. });
  429. group.add(symbol);
  430. }
  431. });
  432. }
  433. },
  434. /**
  435. * [CAUTION] This method can be called multiple times, following the change due to `resetCfg` called
  436. * in size measurement. Thus this method should be idempotent, and should be performant.
  437. */
  438. axisTickLabelEstimate: function (cfg, local, shared, axisModel, group, transformGroup, api, extraParams) {
  439. if (process.env.NODE_ENV !== 'production') {
  440. var ready = shared.ensureRecord(axisModel).ready;
  441. assert(!ready.axisTickLabelDetermine);
  442. ready.axisTickLabelEstimate = true;
  443. }
  444. var needCallLayout = dealLastTickLabelResultReusable(local, group, extraParams);
  445. if (needCallLayout) {
  446. layOutAxisTickLabel(cfg, local, shared, axisModel, group, transformGroup, api, AxisTickLabelComputingKind.estimate);
  447. }
  448. },
  449. /**
  450. * Finish axis tick label build.
  451. * Can be only called once.
  452. */
  453. axisTickLabelDetermine: function (cfg, local, shared, axisModel, group, transformGroup, api, extraParams) {
  454. if (process.env.NODE_ENV !== 'production') {
  455. var ready = shared.ensureRecord(axisModel).ready;
  456. ready.axisTickLabelDetermine = true;
  457. }
  458. var needCallLayout = dealLastTickLabelResultReusable(local, group, extraParams);
  459. if (needCallLayout) {
  460. layOutAxisTickLabel(cfg, local, shared, axisModel, group, transformGroup, api, AxisTickLabelComputingKind.determine);
  461. }
  462. var ticksEls = buildAxisMajorTicks(cfg, group, transformGroup, axisModel);
  463. syncLabelIgnoreToMajorTicks(cfg, local.labelLayoutList, ticksEls);
  464. buildAxisMinorTicks(cfg, group, transformGroup, axisModel, cfg.tickDirection);
  465. },
  466. /**
  467. * [CAUTION] This method can be called multiple times, following the change due to `resetCfg` called
  468. * in size measurement. Thus this method should be idempotent, and should be performant.
  469. */
  470. axisName: function (cfg, local, shared, axisModel, group, transformGroup, api, extraParams) {
  471. var sharedRecord = shared.ensureRecord(axisModel);
  472. if (process.env.NODE_ENV !== 'production') {
  473. var ready = sharedRecord.ready;
  474. assert(ready.axisTickLabelEstimate || ready.axisTickLabelDetermine);
  475. ready.axisName = true;
  476. }
  477. // Remove the existing name result created in estimation phase.
  478. if (local.nameEl) {
  479. group.remove(local.nameEl);
  480. local.nameEl = sharedRecord.nameLayout = sharedRecord.nameLocation = null;
  481. }
  482. var name = cfg.axisName;
  483. if (!hasAxisName(name)) {
  484. return;
  485. }
  486. var nameLocation = cfg.nameLocation;
  487. var nameDirection = cfg.nameDirection;
  488. var textStyleModel = axisModel.getModel('nameTextStyle');
  489. var gap = axisModel.get('nameGap') || 0;
  490. var extent = axisModel.axis.getExtent();
  491. var gapStartEndSignal = axisModel.axis.inverse ? -1 : 1;
  492. var pos = new Point(0, 0);
  493. var nameMoveDirVec = new Point(0, 0);
  494. if (nameLocation === 'start') {
  495. pos.x = extent[0] - gapStartEndSignal * gap;
  496. nameMoveDirVec.x = -gapStartEndSignal;
  497. } else if (nameLocation === 'end') {
  498. pos.x = extent[1] + gapStartEndSignal * gap;
  499. nameMoveDirVec.x = gapStartEndSignal;
  500. } else {
  501. // 'middle' or 'center'
  502. pos.x = (extent[0] + extent[1]) / 2;
  503. pos.y = cfg.labelOffset + nameDirection * gap;
  504. nameMoveDirVec.y = nameDirection;
  505. }
  506. var mt = matrixUtil.create();
  507. nameMoveDirVec.transform(matrixUtil.rotate(mt, mt, cfg.rotation));
  508. var nameRotation = axisModel.get('nameRotate');
  509. if (nameRotation != null) {
  510. nameRotation = nameRotation * PI / 180; // To radian.
  511. }
  512. var labelLayout;
  513. var axisNameAvailableWidth;
  514. if (isNameLocationCenter(nameLocation)) {
  515. labelLayout = AxisBuilder.innerTextLayout(cfg.rotation, nameRotation != null ? nameRotation : cfg.rotation,
  516. // Adapt to axis.
  517. nameDirection);
  518. } else {
  519. labelLayout = endTextLayout(cfg.rotation, nameLocation, nameRotation || 0, extent);
  520. axisNameAvailableWidth = cfg.raw.axisNameAvailableWidth;
  521. if (axisNameAvailableWidth != null) {
  522. axisNameAvailableWidth = Math.abs(axisNameAvailableWidth / Math.sin(labelLayout.rotation));
  523. !isFinite(axisNameAvailableWidth) && (axisNameAvailableWidth = null);
  524. }
  525. }
  526. var textFont = textStyleModel.getFont();
  527. var truncateOpt = axisModel.get('nameTruncate', true) || {};
  528. var ellipsis = truncateOpt.ellipsis;
  529. var maxWidth = retrieve(cfg.raw.nameTruncateMaxWidth, truncateOpt.maxWidth, axisNameAvailableWidth);
  530. var nameMarginLevel = extraParams.nameMarginLevel || 0;
  531. var textEl = new graphic.Text({
  532. x: pos.x,
  533. y: pos.y,
  534. rotation: labelLayout.rotation,
  535. silent: AxisBuilder.isLabelSilent(axisModel),
  536. style: createTextStyle(textStyleModel, {
  537. text: name,
  538. font: textFont,
  539. overflow: 'truncate',
  540. width: maxWidth,
  541. ellipsis: ellipsis,
  542. fill: textStyleModel.getTextColor() || axisModel.get(['axisLine', 'lineStyle', 'color']),
  543. align: textStyleModel.get('align') || labelLayout.textAlign,
  544. verticalAlign: textStyleModel.get('verticalAlign') || labelLayout.textVerticalAlign
  545. }),
  546. z2: 1
  547. });
  548. graphic.setTooltipConfig({
  549. el: textEl,
  550. componentModel: axisModel,
  551. itemName: name
  552. });
  553. textEl.__fullText = name;
  554. // Id for animation
  555. textEl.anid = 'name';
  556. if (axisModel.get('triggerEvent')) {
  557. var eventData = AxisBuilder.makeAxisEventDataBase(axisModel);
  558. eventData.targetType = 'axisName';
  559. eventData.name = name;
  560. getECData(textEl).eventData = eventData;
  561. }
  562. transformGroup.add(textEl);
  563. textEl.updateTransform();
  564. local.nameEl = textEl;
  565. var nameLayout = sharedRecord.nameLayout = ensureLabelLayoutWithGeometry({
  566. label: textEl,
  567. priority: textEl.z2,
  568. defaultAttr: {
  569. ignore: textEl.ignore
  570. },
  571. marginDefault: isNameLocationCenter(nameLocation)
  572. // Make axis name visually far from axis labels.
  573. // (but not too aggressive, consider multiple small charts)
  574. ? DEFAULT_CENTER_NAME_MARGIN_LEVELS[nameMarginLevel]
  575. // top/button margin is set to `0` to inserted the xAxis name into the indention
  576. // above the axis labels to save space. (see example below.)
  577. : DEFAULT_ENDS_NAME_MARGIN_LEVELS[nameMarginLevel]
  578. });
  579. sharedRecord.nameLocation = nameLocation;
  580. group.add(textEl);
  581. textEl.decomposeTransform();
  582. if (cfg.shouldNameMoveOverlap && nameLayout) {
  583. var record = shared.ensureRecord(axisModel);
  584. if (process.env.NODE_ENV !== 'production') {
  585. assert(record.labelInfoList);
  586. }
  587. shared.resolveAxisNameOverlap(cfg, shared, axisModel, nameLayout, nameMoveDirVec, record);
  588. }
  589. }
  590. };
  591. function layOutAxisTickLabel(cfg, local, shared, axisModel, group, transformGroup, api, kind) {
  592. if (!axisLabelBuildResultExists(local)) {
  593. buildAxisLabel(cfg, local, group, kind, axisModel, api);
  594. }
  595. var labelLayoutList = local.labelLayoutList;
  596. updateAxisLabelChangableProps(cfg, axisModel, labelLayoutList, transformGroup);
  597. adjustBreakLabels(axisModel, cfg.rotation, labelLayoutList);
  598. var optionHideOverlap = cfg.optionHideOverlap;
  599. fixMinMaxLabelShow(axisModel, labelLayoutList, optionHideOverlap);
  600. if (optionHideOverlap) {
  601. // This bit fixes the label overlap issue for the time chart.
  602. // See https://github.com/apache/echarts/issues/14266 for more.
  603. hideOverlap(
  604. // Filter the already ignored labels by the previous overlap resolving methods.
  605. filter(labelLayoutList, function (layout) {
  606. return layout && !layout.label.ignore;
  607. }));
  608. }
  609. // Always call it even this axis has no name, since it serves in overlapping detection
  610. // and grid outerBounds on other axis.
  611. resetOverlapRecordToShared(cfg, shared, axisModel, labelLayoutList);
  612. }
  613. ;
  614. function endTextLayout(rotation, textPosition, textRotate, extent) {
  615. var rotationDiff = remRadian(textRotate - rotation);
  616. var textAlign;
  617. var textVerticalAlign;
  618. var inverse = extent[0] > extent[1];
  619. var onLeft = textPosition === 'start' && !inverse || textPosition !== 'start' && inverse;
  620. if (isRadianAroundZero(rotationDiff - PI / 2)) {
  621. textVerticalAlign = onLeft ? 'bottom' : 'top';
  622. textAlign = 'center';
  623. } else if (isRadianAroundZero(rotationDiff - PI * 1.5)) {
  624. textVerticalAlign = onLeft ? 'top' : 'bottom';
  625. textAlign = 'center';
  626. } else {
  627. textVerticalAlign = 'middle';
  628. if (rotationDiff < PI * 1.5 && rotationDiff > PI / 2) {
  629. textAlign = onLeft ? 'left' : 'right';
  630. } else {
  631. textAlign = onLeft ? 'right' : 'left';
  632. }
  633. }
  634. return {
  635. rotation: rotationDiff,
  636. textAlign: textAlign,
  637. textVerticalAlign: textVerticalAlign
  638. };
  639. }
  640. /**
  641. * Assume `labelLayoutList` has no `label.ignore: true`.
  642. * Assume `labelLayoutList` have been sorted by value ascending order.
  643. */
  644. function fixMinMaxLabelShow(axisModel, labelLayoutList, optionHideOverlap) {
  645. var axis = axisModel.axis;
  646. var customValuesOption = axisModel.get(['axisLabel', 'customValues']);
  647. if (shouldShowAllLabels(axis)) {
  648. return;
  649. }
  650. // FIXME
  651. // Have not consider onBand yet, where tick els is more than label els.
  652. // Assert no ignore in labels.
  653. function deal(showMinMaxLabelOption, outmostLabelIdx, innerLabelIdx) {
  654. var outmostLabelLayout = ensureLabelLayoutWithGeometry(labelLayoutList[outmostLabelIdx]);
  655. var innerLabelLayout = ensureLabelLayoutWithGeometry(labelLayoutList[innerLabelIdx]);
  656. var scale = axis.scale;
  657. if (!outmostLabelLayout || !innerLabelLayout) {
  658. return;
  659. }
  660. if (showMinMaxLabelOption == null) {
  661. if (!optionHideOverlap && customValuesOption) {
  662. // In this case, users are unlikely to expect labels to be hidden.
  663. return;
  664. }
  665. var tick = getLabelInner(outmostLabelLayout.label).labelInfo.tick;
  666. if (
  667. // TimeScale does not expand extent to "nice", so eliminate labels that are not nice.
  668. isTimeScale(scale) && tick.notNice
  669. // Category axis does not expect tick that out of axisLabel.internal to be displayed
  670. // unless required.
  671. || isOrdinalScale(scale) && tick.offInterval) {
  672. ignoreEl(outmostLabelLayout.label);
  673. return;
  674. }
  675. }
  676. if (showMinMaxLabelOption === false || outmostLabelLayout.suggestIgnore) {
  677. ignoreEl(outmostLabelLayout.label);
  678. return;
  679. }
  680. if (innerLabelLayout.suggestIgnore) {
  681. ignoreEl(innerLabelLayout.label);
  682. return;
  683. }
  684. // PENDING: Originally we thought `optionHideOverlap === false` means do not hide anything,
  685. // since currently the bounding rect of text might not accurate enough and might slightly bigger,
  686. // which causes false positive. But `optionHideOverlap: null/undfined` is falsy and likely
  687. // be treated as false.
  688. // In most fonts the glyph does not reach the boundary of the bounding rect.
  689. // This is needed to avoid too aggressive to hide two elements that meet at the edge
  690. // due to compact layout by the same bounding rect or OBB.
  691. var touchThreshold = 0.1;
  692. // This treatment is for backward compatibility. And `!optionHideOverlap` implies that
  693. // the user accepts the visual touch between adjacent labels, thus "hide min/max label"
  694. // should be conservative, since the space might be sufficient in this case.
  695. if (!optionHideOverlap) {
  696. var marginForce = [0, 0, 0, 0];
  697. // Make a copy to apply `ignoreMargin`.
  698. outmostLabelLayout = newLabelLayoutWithGeometry({
  699. marginForce: marginForce
  700. }, outmostLabelLayout);
  701. innerLabelLayout = newLabelLayoutWithGeometry({
  702. marginForce: marginForce
  703. }, innerLabelLayout);
  704. }
  705. if (labelIntersect(outmostLabelLayout, innerLabelLayout, null, {
  706. touchThreshold: touchThreshold
  707. })) {
  708. if (showMinMaxLabelOption) {
  709. ignoreEl(innerLabelLayout.label);
  710. } else {
  711. ignoreEl(outmostLabelLayout.label);
  712. }
  713. }
  714. }
  715. // If min or max are user set, we need to check
  716. // If the tick on min(max) are overlap on their neighbour tick
  717. // If they are overlapped, we need to hide the min(max) tick label
  718. var showMinLabelOption = axisModel.get(['axisLabel', 'showMinLabel']);
  719. var showMaxLabelOption = axisModel.get(['axisLabel', 'showMaxLabel']);
  720. var labelsLen = labelLayoutList.length;
  721. deal(showMinLabelOption, 0, 1);
  722. deal(showMaxLabelOption, labelsLen - 1, labelsLen - 2);
  723. }
  724. // Under default settings, it is visually odd to display a tick without its label,
  725. // since ticks extend from axis line towards labels and serve as visual guide.
  726. // PENDING: Is there opposite cases?
  727. function syncLabelIgnoreToMajorTicks(cfg, labelLayoutList, tickEls) {
  728. if (cfg.showMinorTicks) {
  729. // It probably unreasonable to hide major ticks when show minor ticks.
  730. return;
  731. }
  732. each(labelLayoutList, function (labelLayout) {
  733. if (labelLayout && labelLayout.label.ignore) {
  734. for (var idx = 0; idx < tickEls.length; idx++) {
  735. var tickEl = tickEls[idx];
  736. // Assume small array, linear search is fine for performance.
  737. // PENDING: measure?
  738. var tickInner = getTickInner(tickEl);
  739. var labelInner = getLabelInner(labelLayout.label);
  740. if (tickInner.tickValue != null && !tickInner.onBand && tickInner.tickValue === labelInner.labelInfo.tick.value) {
  741. ignoreEl(tickEl);
  742. return;
  743. }
  744. }
  745. }
  746. });
  747. }
  748. function ignoreEl(el) {
  749. el && (el.ignore = true);
  750. }
  751. function createTicks(ticksCoords, tickTransform, tickEndCoord, tickLineStyle, anidPrefix) {
  752. var tickEls = [];
  753. var pt1 = [];
  754. var pt2 = [];
  755. for (var i = 0; i < ticksCoords.length; i++) {
  756. var tickCoord = ticksCoords[i].coord;
  757. pt1[0] = tickCoord;
  758. pt1[1] = 0;
  759. pt2[0] = tickCoord;
  760. pt2[1] = tickEndCoord;
  761. if (tickTransform) {
  762. v2ApplyTransform(pt1, pt1, tickTransform);
  763. v2ApplyTransform(pt2, pt2, tickTransform);
  764. }
  765. // Tick line, Not use group transform to have better line draw
  766. var tickEl = new graphic.Line({
  767. shape: {
  768. x1: pt1[0],
  769. y1: pt1[1],
  770. x2: pt2[0],
  771. y2: pt2[1]
  772. },
  773. style: tickLineStyle,
  774. z2: 2,
  775. autoBatch: true,
  776. silent: true
  777. });
  778. graphic.subPixelOptimizeLine(tickEl.shape, tickEl.style.lineWidth);
  779. tickEl.anid = anidPrefix + '_' + ticksCoords[i].tickValue;
  780. tickEls.push(tickEl);
  781. var inner = getTickInner(tickEl);
  782. inner.onBand = !!ticksCoords[i].onBand;
  783. inner.tickValue = ticksCoords[i].tickValue;
  784. }
  785. return tickEls;
  786. }
  787. function buildAxisMajorTicks(cfg, group, transformGroup, axisModel) {
  788. var axis = axisModel.axis;
  789. var tickModel = axisModel.getModel('axisTick');
  790. var shown = tickModel.get('show');
  791. if (shown === 'auto') {
  792. shown = true;
  793. if (cfg.raw.axisTickAutoShow != null) {
  794. shown = !!cfg.raw.axisTickAutoShow;
  795. }
  796. }
  797. if (!shown || axis.scale.isBlank()) {
  798. return [];
  799. }
  800. var lineStyleModel = tickModel.getModel('lineStyle');
  801. var tickEndCoord = cfg.tickDirection * tickModel.get('length');
  802. var ticksCoords = axis.getTicksCoords();
  803. var ticksEls = createTicks(ticksCoords, transformGroup.transform, tickEndCoord, defaults(lineStyleModel.getLineStyle(), {
  804. stroke: axisModel.get(['axisLine', 'lineStyle', 'color'])
  805. }), 'ticks');
  806. for (var i = 0; i < ticksEls.length; i++) {
  807. group.add(ticksEls[i]);
  808. }
  809. return ticksEls;
  810. }
  811. function buildAxisMinorTicks(cfg, group, transformGroup, axisModel, tickDirection) {
  812. var axis = axisModel.axis;
  813. var minorTickModel = axisModel.getModel('minorTick');
  814. if (!cfg.showMinorTicks || axis.scale.isBlank()) {
  815. return;
  816. }
  817. var minorTicksCoords = axis.getMinorTicksCoords();
  818. if (!minorTicksCoords.length) {
  819. return;
  820. }
  821. var lineStyleModel = minorTickModel.getModel('lineStyle');
  822. var tickEndCoord = tickDirection * minorTickModel.get('length');
  823. var minorTickLineStyle = defaults(lineStyleModel.getLineStyle(), defaults(axisModel.getModel('axisTick').getLineStyle(), {
  824. stroke: axisModel.get(['axisLine', 'lineStyle', 'color'])
  825. }));
  826. for (var i = 0; i < minorTicksCoords.length; i++) {
  827. var minorTicksEls = createTicks(minorTicksCoords[i], transformGroup.transform, tickEndCoord, minorTickLineStyle, 'minorticks_' + i);
  828. for (var k = 0; k < minorTicksEls.length; k++) {
  829. group.add(minorTicksEls[k]);
  830. }
  831. }
  832. }
  833. // Return whether need to call `layOutAxisTickLabel` again.
  834. function dealLastTickLabelResultReusable(local, group, extraParams) {
  835. if (axisLabelBuildResultExists(local)) {
  836. var axisLabelsCreationContext = local.axisLabelsCreationContext;
  837. if (process.env.NODE_ENV !== 'production') {
  838. assert(local.labelGroup && axisLabelsCreationContext);
  839. }
  840. var noPxChangeTryDetermine = axisLabelsCreationContext.out.noPxChangeTryDetermine;
  841. if (extraParams.noPxChange) {
  842. var canDetermine = true;
  843. for (var idx = 0; idx < noPxChangeTryDetermine.length; idx++) {
  844. canDetermine = canDetermine && noPxChangeTryDetermine[idx]();
  845. }
  846. if (canDetermine) {
  847. return false;
  848. }
  849. }
  850. if (noPxChangeTryDetermine.length) {
  851. // Remove the result of `buildAxisLabel`
  852. group.remove(local.labelGroup);
  853. axisLabelBuildResultSet(local, null, null, null);
  854. }
  855. }
  856. return true;
  857. }
  858. function buildAxisLabel(cfg, local, group, kind, axisModel, api) {
  859. var axis = axisModel.axis;
  860. var show = retrieve(cfg.raw.axisLabelShow, axisModel.get(['axisLabel', 'show']));
  861. var labelGroup = new graphic.Group();
  862. group.add(labelGroup);
  863. var axisLabelCreationCtx = createAxisLabelsComputingContext(kind);
  864. if (!show || axis.scale.isBlank()) {
  865. axisLabelBuildResultSet(local, [], labelGroup, axisLabelCreationCtx);
  866. return;
  867. }
  868. var labelModel = axisModel.getModel('axisLabel');
  869. var labels = axis.getViewLabels(axisLabelCreationCtx);
  870. // Special label rotate.
  871. var labelRotation = (retrieve(cfg.raw.labelRotate, labelModel.get('rotate')) || 0) * PI / 180;
  872. var labelLayout = AxisBuilder.innerTextLayout(cfg.rotation, labelRotation, cfg.labelDirection);
  873. var rawCategoryData = axisModel.getCategories && axisModel.getCategories(true);
  874. var labelEls = [];
  875. var triggerEvent = axisModel.get('triggerEvent');
  876. var z2Min = Infinity;
  877. var z2Max = -Infinity;
  878. each(labels, function (labelItem, index) {
  879. var _a;
  880. var labelItemTick = labelItem.tick;
  881. var formattedLabel = labelItem.formattedLabel;
  882. var rawLabel = labelItem.rawLabel;
  883. var itemLabelModel = labelModel;
  884. var tickValue = getTickValueOutermost(axis.scale, labelItemTick);
  885. if (rawCategoryData && rawCategoryData[tickValue]) {
  886. var rawCategoryItem = rawCategoryData[tickValue];
  887. if (isObject(rawCategoryItem) && rawCategoryItem.textStyle) {
  888. itemLabelModel = new Model(rawCategoryItem.textStyle, labelModel, axisModel.ecModel);
  889. }
  890. }
  891. var textColor = itemLabelModel.getTextColor() || axisModel.get(['axisLine', 'lineStyle', 'color']);
  892. var align = itemLabelModel.getShallow('align', true) || labelLayout.textAlign;
  893. var alignMin = retrieve2(itemLabelModel.getShallow('alignMinLabel', true), align);
  894. var alignMax = retrieve2(itemLabelModel.getShallow('alignMaxLabel', true), align);
  895. var verticalAlign = itemLabelModel.getShallow('verticalAlign', true) || itemLabelModel.getShallow('baseline', true) || labelLayout.textVerticalAlign;
  896. var verticalAlignMin = retrieve2(itemLabelModel.getShallow('verticalAlignMinLabel', true), verticalAlign);
  897. var verticalAlignMax = retrieve2(itemLabelModel.getShallow('verticalAlignMaxLabel', true), verticalAlign);
  898. var z2 = 10 + (((_a = labelItemTick.time) === null || _a === void 0 ? void 0 : _a.level) || 0);
  899. z2Min = Math.min(z2Min, z2);
  900. z2Max = Math.max(z2Max, z2);
  901. var textEl = new graphic.Text({
  902. // --- transform props start ---
  903. // All of the transform props MUST not be set here, but should be set in
  904. // `updateAxisLabelChangableProps`, because they may change in estimation,
  905. // and need to calculate based on global coord sys by `decomposeTransform`.
  906. x: 0,
  907. y: 0,
  908. rotation: 0,
  909. // --- transform props end ---
  910. silent: AxisBuilder.isLabelSilent(axisModel),
  911. z2: z2,
  912. style: createTextStyle(itemLabelModel, {
  913. text: formattedLabel,
  914. align: index === 0 ? alignMin : index === labels.length - 1 ? alignMax : align,
  915. verticalAlign: index === 0 ? verticalAlignMin : index === labels.length - 1 ? verticalAlignMax : verticalAlign,
  916. fill: isFunction(textColor) ? textColor(
  917. // (1) In category axis with data zoom, tick is not the original
  918. // index of axis.data. So tick should not be exposed to user
  919. // in category axis.
  920. // (2) Compatible with previous version, which always use formatted label as
  921. // input. But in interval scale the formatted label is like '223,445', which
  922. // maked user replace ','. So we modify it to return original val but remain
  923. // it as 'string' to avoid error in replacing.
  924. axis.type === 'category' ? rawLabel : axis.type === 'value' ? tickValue + '' : tickValue, index) : textColor
  925. })
  926. });
  927. textEl.anid = 'label_' + tickValue;
  928. var inner = getLabelInner(textEl);
  929. inner.labelInfo = labelItem;
  930. inner.layoutRotation = labelLayout.rotation;
  931. graphic.setTooltipConfig({
  932. el: textEl,
  933. componentModel: axisModel,
  934. itemName: formattedLabel,
  935. formatterParamsExtra: {
  936. isTruncated: function () {
  937. return textEl.isTruncated;
  938. },
  939. value: rawLabel,
  940. tickIndex: index
  941. }
  942. });
  943. // Pack data for mouse event
  944. if (triggerEvent) {
  945. var eventData = AxisBuilder.makeAxisEventDataBase(axisModel);
  946. eventData.targetType = 'axisLabel';
  947. eventData.value = rawLabel;
  948. eventData.tickIndex = index;
  949. var labelItemTickBreak = labelItem.tick["break"];
  950. if (labelItemTickBreak) {
  951. var labelItemTickBreakParsedBreak = labelItemTickBreak.parsedBreak;
  952. eventData["break"] = {
  953. // type: labelItem.break.type,
  954. start: labelItemTickBreakParsedBreak.vmin,
  955. end: labelItemTickBreakParsedBreak.vmax
  956. };
  957. }
  958. if (axis.type === 'category') {
  959. eventData.dataIndex = tickValue;
  960. }
  961. getECData(textEl).eventData = eventData;
  962. if (labelItemTickBreak) {
  963. addBreakEventHandler(axisModel, api, textEl, labelItemTickBreak);
  964. }
  965. }
  966. labelEls.push(textEl);
  967. labelGroup.add(textEl);
  968. });
  969. var labelLayoutList = map(labelEls, function (label) {
  970. return {
  971. label: label,
  972. priority: getLabelInner(label).labelInfo.tick["break"] ? label.z2 + (z2Max - z2Min + 1) // Make break labels be highest priority.
  973. : label.z2,
  974. defaultAttr: {
  975. ignore: label.ignore
  976. }
  977. };
  978. });
  979. axisLabelBuildResultSet(local, labelLayoutList, labelGroup, axisLabelCreationCtx);
  980. }
  981. // Indicate that `layOutAxisTickLabel` has been called.
  982. function axisLabelBuildResultExists(local) {
  983. return !!local.labelLayoutList;
  984. }
  985. function axisLabelBuildResultSet(local, labelLayoutList, labelGroup, axisLabelsCreationContext) {
  986. // Ensure the same lifetime.
  987. local.labelLayoutList = labelLayoutList;
  988. local.labelGroup = labelGroup;
  989. local.axisLabelsCreationContext = axisLabelsCreationContext;
  990. }
  991. function updateAxisLabelChangableProps(cfg, axisModel, labelLayoutList, transformGroup) {
  992. var labelMargin = axisModel.get(['axisLabel', 'margin']);
  993. each(labelLayoutList, function (layout, idx) {
  994. var geometry = ensureLabelLayoutWithGeometry(layout);
  995. if (!geometry) {
  996. return;
  997. }
  998. var labelEl = geometry.label;
  999. var inner = getLabelInner(labelEl);
  1000. // See the comment in `suggestIgnore`.
  1001. geometry.suggestIgnore = labelEl.ignore;
  1002. // Currently no `ignore:true` is set in `buildAxisLabel`
  1003. // But `ignore:true` may be set subsequently for overlap handling, thus reset it here.
  1004. labelEl.ignore = false;
  1005. copyTransform(_tmpLayoutEl, _tmpLayoutElReset);
  1006. var axis = axisModel.axis;
  1007. _tmpLayoutEl.x = axis.dataToCoord(getTickValueOutermost(axis.scale, inner.labelInfo.tick));
  1008. _tmpLayoutEl.y = cfg.labelOffset + cfg.labelDirection * labelMargin;
  1009. _tmpLayoutEl.rotation = inner.layoutRotation;
  1010. transformGroup.add(_tmpLayoutEl);
  1011. _tmpLayoutEl.updateTransform();
  1012. transformGroup.remove(_tmpLayoutEl);
  1013. _tmpLayoutEl.decomposeTransform();
  1014. copyTransform(labelEl, _tmpLayoutEl);
  1015. labelEl.markRedraw();
  1016. setLabelLayoutDirty(geometry, true);
  1017. ensureLabelLayoutWithGeometry(geometry);
  1018. });
  1019. }
  1020. var _tmpLayoutEl = new graphic.Rect();
  1021. var _tmpLayoutElReset = new graphic.Rect();
  1022. function hasAxisName(axisName) {
  1023. return !!axisName;
  1024. }
  1025. function addBreakEventHandler(axisModel, api, textEl, visualBreak) {
  1026. textEl.on('click', function (params) {
  1027. var payload = {
  1028. type: AXIS_BREAK_EXPAND_ACTION_TYPE,
  1029. breaks: [{
  1030. start: visualBreak.parsedBreak.breakOption.start,
  1031. end: visualBreak.parsedBreak.breakOption.end
  1032. }]
  1033. };
  1034. payload[axisModel.axis.dim + "AxisIndex"] = axisModel.componentIndex;
  1035. api.dispatchAction(payload);
  1036. });
  1037. }
  1038. function adjustBreakLabels(axisModel, axisRotation, labelLayoutList) {
  1039. var scaleBreakHelper = getScaleBreakHelper();
  1040. if (!scaleBreakHelper) {
  1041. return;
  1042. }
  1043. var breakLabelIndexPairs = scaleBreakHelper.retrieveAxisBreakPairs(labelLayoutList, function (layoutInfo) {
  1044. return layoutInfo && getLabelInner(layoutInfo.label).labelInfo.tick["break"];
  1045. }, true);
  1046. var moveOverlap = axisModel.get(['breakLabelLayout', 'moveOverlap'], true);
  1047. if (moveOverlap === true || moveOverlap === 'auto') {
  1048. each(breakLabelIndexPairs, function (idxPair) {
  1049. getAxisBreakHelper().adjustBreakLabelPair(axisModel.axis.inverse, axisRotation, [ensureLabelLayoutWithGeometry(labelLayoutList[idxPair[0]]), ensureLabelLayoutWithGeometry(labelLayoutList[idxPair[1]])]);
  1050. });
  1051. }
  1052. }
  1053. export default AxisBuilder;