AxisBuilder.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. import {retrieve, defaults, extend, each} from 'zrender/src/core/util';
  20. import * as formatUtil from '../../util/format';
  21. import * as graphic from '../../util/graphic';
  22. import Model from '../../model/Model';
  23. import {isRadianAroundZero, remRadian} from '../../util/number';
  24. import {createSymbol} from '../../util/symbol';
  25. import * as matrixUtil from 'zrender/src/core/matrix';
  26. import {applyTransform as v2ApplyTransform} from 'zrender/src/core/vector';
  27. import {shouldShowAllLabels} from '../../coord/axisHelper';
  28. var PI = Math.PI;
  29. /**
  30. * A final axis is translated and rotated from a "standard axis".
  31. * So opt.position and opt.rotation is required.
  32. *
  33. * A standard axis is and axis from [0, 0] to [0, axisExtent[1]],
  34. * for example: (0, 0) ------------> (0, 50)
  35. *
  36. * nameDirection or tickDirection or labelDirection is 1 means tick
  37. * or label is below the standard axis, whereas is -1 means above
  38. * the standard axis. labelOffset means offset between label and axis,
  39. * which is useful when 'onZero', where axisLabel is in the grid and
  40. * label in outside grid.
  41. *
  42. * Tips: like always,
  43. * positive rotation represents anticlockwise, and negative rotation
  44. * represents clockwise.
  45. * The direction of position coordinate is the same as the direction
  46. * of screen coordinate.
  47. *
  48. * Do not need to consider axis 'inverse', which is auto processed by
  49. * axis extent.
  50. *
  51. * @param {module:zrender/container/Group} group
  52. * @param {Object} axisModel
  53. * @param {Object} opt Standard axis parameters.
  54. * @param {Array.<number>} opt.position [x, y]
  55. * @param {number} opt.rotation by radian
  56. * @param {number} [opt.nameDirection=1] 1 or -1 Used when nameLocation is 'middle' or 'center'.
  57. * @param {number} [opt.tickDirection=1] 1 or -1
  58. * @param {number} [opt.labelDirection=1] 1 or -1
  59. * @param {number} [opt.labelOffset=0] Usefull when onZero.
  60. * @param {string} [opt.axisLabelShow] default get from axisModel.
  61. * @param {string} [opt.axisName] default get from axisModel.
  62. * @param {number} [opt.axisNameAvailableWidth]
  63. * @param {number} [opt.labelRotate] by degree, default get from axisModel.
  64. * @param {number} [opt.strokeContainThreshold] Default label interval when label
  65. * @param {number} [opt.nameTruncateMaxWidth]
  66. */
  67. var AxisBuilder = function (axisModel, opt) {
  68. /**
  69. * @readOnly
  70. */
  71. this.opt = opt;
  72. /**
  73. * @readOnly
  74. */
  75. this.axisModel = axisModel;
  76. // Default value
  77. defaults(
  78. opt,
  79. {
  80. labelOffset: 0,
  81. nameDirection: 1,
  82. tickDirection: 1,
  83. labelDirection: 1,
  84. silent: true
  85. }
  86. );
  87. /**
  88. * @readOnly
  89. */
  90. this.group = new graphic.Group();
  91. // FIXME Not use a seperate text group?
  92. var dumbGroup = new graphic.Group({
  93. position: opt.position.slice(),
  94. rotation: opt.rotation
  95. });
  96. // this.group.add(dumbGroup);
  97. // this._dumbGroup = dumbGroup;
  98. dumbGroup.updateTransform();
  99. this._transform = dumbGroup.transform;
  100. this._dumbGroup = dumbGroup;
  101. };
  102. AxisBuilder.prototype = {
  103. constructor: AxisBuilder,
  104. hasBuilder: function (name) {
  105. return !!builders[name];
  106. },
  107. add: function (name) {
  108. builders[name].call(this);
  109. },
  110. getGroup: function () {
  111. return this.group;
  112. }
  113. };
  114. var builders = {
  115. /**
  116. * @private
  117. */
  118. axisLine: function () {
  119. var opt = this.opt;
  120. var axisModel = this.axisModel;
  121. if (!axisModel.get('axisLine.show')) {
  122. return;
  123. }
  124. var extent = this.axisModel.axis.getExtent();
  125. var matrix = this._transform;
  126. var pt1 = [extent[0], 0];
  127. var pt2 = [extent[1], 0];
  128. if (matrix) {
  129. v2ApplyTransform(pt1, pt1, matrix);
  130. v2ApplyTransform(pt2, pt2, matrix);
  131. }
  132. var lineStyle = extend(
  133. {
  134. lineCap: 'round'
  135. },
  136. axisModel.getModel('axisLine.lineStyle').getLineStyle()
  137. );
  138. this.group.add(new graphic.Line({
  139. // Id for animation
  140. anid: 'line',
  141. subPixelOptimize: true,
  142. shape: {
  143. x1: pt1[0],
  144. y1: pt1[1],
  145. x2: pt2[0],
  146. y2: pt2[1]
  147. },
  148. style: lineStyle,
  149. strokeContainThreshold: opt.strokeContainThreshold || 5,
  150. silent: true,
  151. z2: 1
  152. }));
  153. var arrows = axisModel.get('axisLine.symbol');
  154. var arrowSize = axisModel.get('axisLine.symbolSize');
  155. var arrowOffset = axisModel.get('axisLine.symbolOffset') || 0;
  156. if (typeof arrowOffset === 'number') {
  157. arrowOffset = [arrowOffset, arrowOffset];
  158. }
  159. if (arrows != null) {
  160. if (typeof arrows === 'string') {
  161. // Use the same arrow for start and end point
  162. arrows = [arrows, arrows];
  163. }
  164. if (typeof arrowSize === 'string'
  165. || typeof arrowSize === 'number'
  166. ) {
  167. // Use the same size for width and height
  168. arrowSize = [arrowSize, arrowSize];
  169. }
  170. var symbolWidth = arrowSize[0];
  171. var symbolHeight = arrowSize[1];
  172. each([{
  173. rotate: opt.rotation + Math.PI / 2,
  174. offset: arrowOffset[0],
  175. r: 0
  176. }, {
  177. rotate: opt.rotation - Math.PI / 2,
  178. offset: arrowOffset[1],
  179. r: Math.sqrt((pt1[0] - pt2[0]) * (pt1[0] - pt2[0])
  180. + (pt1[1] - pt2[1]) * (pt1[1] - pt2[1]))
  181. }], function (point, index) {
  182. if (arrows[index] !== 'none' && arrows[index] != null) {
  183. var symbol = createSymbol(
  184. arrows[index],
  185. -symbolWidth / 2,
  186. -symbolHeight / 2,
  187. symbolWidth,
  188. symbolHeight,
  189. lineStyle.stroke,
  190. true
  191. );
  192. // Calculate arrow position with offset
  193. var r = point.r + point.offset;
  194. var pos = [
  195. pt1[0] + r * Math.cos(opt.rotation),
  196. pt1[1] - r * Math.sin(opt.rotation)
  197. ];
  198. symbol.attr({
  199. rotation: point.rotate,
  200. position: pos,
  201. silent: true,
  202. z2: 11
  203. });
  204. this.group.add(symbol);
  205. }
  206. }, this);
  207. }
  208. },
  209. /**
  210. * @private
  211. */
  212. axisTickLabel: function () {
  213. var axisModel = this.axisModel;
  214. var opt = this.opt;
  215. var ticksEls = buildAxisMajorTicks(this, axisModel, opt);
  216. var labelEls = buildAxisLabel(this, axisModel, opt);
  217. fixMinMaxLabelShow(axisModel, labelEls, ticksEls);
  218. buildAxisMinorTicks(this, axisModel, opt);
  219. },
  220. /**
  221. * @private
  222. */
  223. axisName: function () {
  224. var opt = this.opt;
  225. var axisModel = this.axisModel;
  226. var name = retrieve(opt.axisName, axisModel.get('name'));
  227. if (!name) {
  228. return;
  229. }
  230. var nameLocation = axisModel.get('nameLocation');
  231. var nameDirection = opt.nameDirection;
  232. var textStyleModel = axisModel.getModel('nameTextStyle');
  233. var gap = axisModel.get('nameGap') || 0;
  234. var extent = this.axisModel.axis.getExtent();
  235. var gapSignal = extent[0] > extent[1] ? -1 : 1;
  236. var pos = [
  237. nameLocation === 'start'
  238. ? extent[0] - gapSignal * gap
  239. : nameLocation === 'end'
  240. ? extent[1] + gapSignal * gap
  241. : (extent[0] + extent[1]) / 2, // 'middle'
  242. // Reuse labelOffset.
  243. isNameLocationCenter(nameLocation) ? opt.labelOffset + nameDirection * gap : 0
  244. ];
  245. var labelLayout;
  246. var nameRotation = axisModel.get('nameRotate');
  247. if (nameRotation != null) {
  248. nameRotation = nameRotation * PI / 180; // To radian.
  249. }
  250. var axisNameAvailableWidth;
  251. if (isNameLocationCenter(nameLocation)) {
  252. labelLayout = innerTextLayout(
  253. opt.rotation,
  254. nameRotation != null ? nameRotation : opt.rotation, // Adapt to axis.
  255. nameDirection
  256. );
  257. }
  258. else {
  259. labelLayout = endTextLayout(
  260. opt, nameLocation, nameRotation || 0, extent
  261. );
  262. axisNameAvailableWidth = opt.axisNameAvailableWidth;
  263. if (axisNameAvailableWidth != null) {
  264. axisNameAvailableWidth = Math.abs(
  265. axisNameAvailableWidth / Math.sin(labelLayout.rotation)
  266. );
  267. !isFinite(axisNameAvailableWidth) && (axisNameAvailableWidth = null);
  268. }
  269. }
  270. var textFont = textStyleModel.getFont();
  271. var truncateOpt = axisModel.get('nameTruncate', true) || {};
  272. var ellipsis = truncateOpt.ellipsis;
  273. var maxWidth = retrieve(
  274. opt.nameTruncateMaxWidth, truncateOpt.maxWidth, axisNameAvailableWidth
  275. );
  276. // FIXME
  277. // truncate rich text? (consider performance)
  278. var truncatedText = (ellipsis != null && maxWidth != null)
  279. ? formatUtil.truncateText(
  280. name, maxWidth, textFont, ellipsis,
  281. {minChar: 2, placeholder: truncateOpt.placeholder}
  282. )
  283. : name;
  284. var tooltipOpt = axisModel.get('tooltip', true);
  285. var mainType = axisModel.mainType;
  286. var formatterParams = {
  287. componentType: mainType,
  288. name: name,
  289. $vars: ['name']
  290. };
  291. formatterParams[mainType + 'Index'] = axisModel.componentIndex;
  292. var textEl = new graphic.Text({
  293. // Id for animation
  294. anid: 'name',
  295. __fullText: name,
  296. __truncatedText: truncatedText,
  297. position: pos,
  298. rotation: labelLayout.rotation,
  299. silent: isLabelSilent(axisModel),
  300. z2: 1,
  301. tooltip: (tooltipOpt && tooltipOpt.show)
  302. ? extend({
  303. content: name,
  304. formatter: function () {
  305. return name;
  306. },
  307. formatterParams: formatterParams
  308. }, tooltipOpt)
  309. : null
  310. });
  311. graphic.setTextStyle(textEl.style, textStyleModel, {
  312. text: truncatedText,
  313. textFont: textFont,
  314. textFill: textStyleModel.getTextColor()
  315. || axisModel.get('axisLine.lineStyle.color'),
  316. textAlign: textStyleModel.get('align')
  317. || labelLayout.textAlign,
  318. textVerticalAlign: textStyleModel.get('verticalAlign')
  319. || labelLayout.textVerticalAlign
  320. });
  321. if (axisModel.get('triggerEvent')) {
  322. textEl.eventData = makeAxisEventDataBase(axisModel);
  323. textEl.eventData.targetType = 'axisName';
  324. textEl.eventData.name = name;
  325. }
  326. // FIXME
  327. this._dumbGroup.add(textEl);
  328. textEl.updateTransform();
  329. this.group.add(textEl);
  330. textEl.decomposeTransform();
  331. }
  332. };
  333. var makeAxisEventDataBase = AxisBuilder.makeAxisEventDataBase = function (axisModel) {
  334. var eventData = {
  335. componentType: axisModel.mainType,
  336. componentIndex: axisModel.componentIndex
  337. };
  338. eventData[axisModel.mainType + 'Index'] = axisModel.componentIndex;
  339. return eventData;
  340. };
  341. /**
  342. * @public
  343. * @static
  344. * @param {Object} opt
  345. * @param {number} axisRotation in radian
  346. * @param {number} textRotation in radian
  347. * @param {number} direction
  348. * @return {Object} {
  349. * rotation, // according to axis
  350. * textAlign,
  351. * textVerticalAlign
  352. * }
  353. */
  354. var innerTextLayout = AxisBuilder.innerTextLayout = function (axisRotation, textRotation, direction) {
  355. var rotationDiff = remRadian(textRotation - axisRotation);
  356. var textAlign;
  357. var textVerticalAlign;
  358. if (isRadianAroundZero(rotationDiff)) { // Label is parallel with axis line.
  359. textVerticalAlign = direction > 0 ? 'top' : 'bottom';
  360. textAlign = 'center';
  361. }
  362. else if (isRadianAroundZero(rotationDiff - PI)) { // Label is inverse parallel with axis line.
  363. textVerticalAlign = direction > 0 ? 'bottom' : 'top';
  364. textAlign = 'center';
  365. }
  366. else {
  367. textVerticalAlign = 'middle';
  368. if (rotationDiff > 0 && rotationDiff < PI) {
  369. textAlign = direction > 0 ? 'right' : 'left';
  370. }
  371. else {
  372. textAlign = direction > 0 ? 'left' : 'right';
  373. }
  374. }
  375. return {
  376. rotation: rotationDiff,
  377. textAlign: textAlign,
  378. textVerticalAlign: textVerticalAlign
  379. };
  380. };
  381. function endTextLayout(opt, textPosition, textRotate, extent) {
  382. var rotationDiff = remRadian(textRotate - opt.rotation);
  383. var textAlign;
  384. var textVerticalAlign;
  385. var inverse = extent[0] > extent[1];
  386. var onLeft = (textPosition === 'start' && !inverse)
  387. || (textPosition !== 'start' && inverse);
  388. if (isRadianAroundZero(rotationDiff - PI / 2)) {
  389. textVerticalAlign = onLeft ? 'bottom' : 'top';
  390. textAlign = 'center';
  391. }
  392. else if (isRadianAroundZero(rotationDiff - PI * 1.5)) {
  393. textVerticalAlign = onLeft ? 'top' : 'bottom';
  394. textAlign = 'center';
  395. }
  396. else {
  397. textVerticalAlign = 'middle';
  398. if (rotationDiff < PI * 1.5 && rotationDiff > PI / 2) {
  399. textAlign = onLeft ? 'left' : 'right';
  400. }
  401. else {
  402. textAlign = onLeft ? 'right' : 'left';
  403. }
  404. }
  405. return {
  406. rotation: rotationDiff,
  407. textAlign: textAlign,
  408. textVerticalAlign: textVerticalAlign
  409. };
  410. }
  411. var isLabelSilent = AxisBuilder.isLabelSilent = function (axisModel) {
  412. var tooltipOpt = axisModel.get('tooltip');
  413. return axisModel.get('silent')
  414. // Consider mouse cursor, add these restrictions.
  415. || !(
  416. axisModel.get('triggerEvent') || (tooltipOpt && tooltipOpt.show)
  417. );
  418. };
  419. function fixMinMaxLabelShow(axisModel, labelEls, tickEls) {
  420. if (shouldShowAllLabels(axisModel.axis)) {
  421. return;
  422. }
  423. // If min or max are user set, we need to check
  424. // If the tick on min(max) are overlap on their neighbour tick
  425. // If they are overlapped, we need to hide the min(max) tick label
  426. var showMinLabel = axisModel.get('axisLabel.showMinLabel');
  427. var showMaxLabel = axisModel.get('axisLabel.showMaxLabel');
  428. // FIXME
  429. // Have not consider onBand yet, where tick els is more than label els.
  430. labelEls = labelEls || [];
  431. tickEls = tickEls || [];
  432. var firstLabel = labelEls[0];
  433. var nextLabel = labelEls[1];
  434. var lastLabel = labelEls[labelEls.length - 1];
  435. var prevLabel = labelEls[labelEls.length - 2];
  436. var firstTick = tickEls[0];
  437. var nextTick = tickEls[1];
  438. var lastTick = tickEls[tickEls.length - 1];
  439. var prevTick = tickEls[tickEls.length - 2];
  440. if (showMinLabel === false) {
  441. ignoreEl(firstLabel);
  442. ignoreEl(firstTick);
  443. }
  444. else if (isTwoLabelOverlapped(firstLabel, nextLabel)) {
  445. if (showMinLabel) {
  446. ignoreEl(nextLabel);
  447. ignoreEl(nextTick);
  448. }
  449. else {
  450. ignoreEl(firstLabel);
  451. ignoreEl(firstTick);
  452. }
  453. }
  454. if (showMaxLabel === false) {
  455. ignoreEl(lastLabel);
  456. ignoreEl(lastTick);
  457. }
  458. else if (isTwoLabelOverlapped(prevLabel, lastLabel)) {
  459. if (showMaxLabel) {
  460. ignoreEl(prevLabel);
  461. ignoreEl(prevTick);
  462. }
  463. else {
  464. ignoreEl(lastLabel);
  465. ignoreEl(lastTick);
  466. }
  467. }
  468. }
  469. function ignoreEl(el) {
  470. el && (el.ignore = true);
  471. }
  472. function isTwoLabelOverlapped(current, next, labelLayout) {
  473. // current and next has the same rotation.
  474. var firstRect = current && current.getBoundingRect().clone();
  475. var nextRect = next && next.getBoundingRect().clone();
  476. if (!firstRect || !nextRect) {
  477. return;
  478. }
  479. // When checking intersect of two rotated labels, we use mRotationBack
  480. // to avoid that boundingRect is enlarge when using `boundingRect.applyTransform`.
  481. var mRotationBack = matrixUtil.identity([]);
  482. matrixUtil.rotate(mRotationBack, mRotationBack, -current.rotation);
  483. firstRect.applyTransform(matrixUtil.mul([], mRotationBack, current.getLocalTransform()));
  484. nextRect.applyTransform(matrixUtil.mul([], mRotationBack, next.getLocalTransform()));
  485. return firstRect.intersect(nextRect);
  486. }
  487. function isNameLocationCenter(nameLocation) {
  488. return nameLocation === 'middle' || nameLocation === 'center';
  489. }
  490. function createTicks(ticksCoords, tickTransform, tickEndCoord, tickLineStyle, aniid) {
  491. var tickEls = [];
  492. var pt1 = [];
  493. var pt2 = [];
  494. for (var i = 0; i < ticksCoords.length; i++) {
  495. var tickCoord = ticksCoords[i].coord;
  496. pt1[0] = tickCoord;
  497. pt1[1] = 0;
  498. pt2[0] = tickCoord;
  499. pt2[1] = tickEndCoord;
  500. if (tickTransform) {
  501. v2ApplyTransform(pt1, pt1, tickTransform);
  502. v2ApplyTransform(pt2, pt2, tickTransform);
  503. }
  504. // Tick line, Not use group transform to have better line draw
  505. var tickEl = new graphic.Line({
  506. // Id for animation
  507. anid: aniid + '_' + ticksCoords[i].tickValue,
  508. subPixelOptimize: true,
  509. shape: {
  510. x1: pt1[0],
  511. y1: pt1[1],
  512. x2: pt2[0],
  513. y2: pt2[1]
  514. },
  515. style: tickLineStyle,
  516. z2: 2,
  517. silent: true
  518. });
  519. tickEls.push(tickEl);
  520. }
  521. return tickEls;
  522. }
  523. function buildAxisMajorTicks(axisBuilder, axisModel, opt) {
  524. var axis = axisModel.axis;
  525. var tickModel = axisModel.getModel('axisTick');
  526. if (!tickModel.get('show') || axis.scale.isBlank()) {
  527. return;
  528. }
  529. var lineStyleModel = tickModel.getModel('lineStyle');
  530. var tickEndCoord = opt.tickDirection * tickModel.get('length');
  531. var ticksCoords = axis.getTicksCoords();
  532. var ticksEls = createTicks(ticksCoords, axisBuilder._transform, tickEndCoord, defaults(
  533. lineStyleModel.getLineStyle(),
  534. {
  535. stroke: axisModel.get('axisLine.lineStyle.color')
  536. }
  537. ), 'ticks');
  538. for (var i = 0; i < ticksEls.length; i++) {
  539. axisBuilder.group.add(ticksEls[i]);
  540. }
  541. return ticksEls;
  542. }
  543. function buildAxisMinorTicks(axisBuilder, axisModel, opt) {
  544. var axis = axisModel.axis;
  545. var minorTickModel = axisModel.getModel('minorTick');
  546. if (!minorTickModel.get('show') || axis.scale.isBlank()) {
  547. return;
  548. }
  549. var minorTicksCoords = axis.getMinorTicksCoords();
  550. if (!minorTicksCoords.length) {
  551. return;
  552. }
  553. var lineStyleModel = minorTickModel.getModel('lineStyle');
  554. var tickEndCoord = opt.tickDirection * minorTickModel.get('length');
  555. var minorTickLineStyle = defaults(
  556. lineStyleModel.getLineStyle(),
  557. defaults(
  558. axisModel.getModel('axisTick').getLineStyle(),
  559. {
  560. stroke: axisModel.get('axisLine.lineStyle.color')
  561. }
  562. )
  563. );
  564. for (var i = 0; i < minorTicksCoords.length; i++) {
  565. var minorTicksEls = createTicks(
  566. minorTicksCoords[i], axisBuilder._transform, tickEndCoord, minorTickLineStyle, 'minorticks_' + i
  567. );
  568. for (var k = 0; k < minorTicksEls.length; k++) {
  569. axisBuilder.group.add(minorTicksEls[k]);
  570. }
  571. }
  572. }
  573. function buildAxisLabel(axisBuilder, axisModel, opt) {
  574. var axis = axisModel.axis;
  575. var show = retrieve(opt.axisLabelShow, axisModel.get('axisLabel.show'));
  576. if (!show || axis.scale.isBlank()) {
  577. return;
  578. }
  579. var labelModel = axisModel.getModel('axisLabel');
  580. var labelMargin = labelModel.get('margin');
  581. var labels = axis.getViewLabels();
  582. // Special label rotate.
  583. var labelRotation = (
  584. retrieve(opt.labelRotate, labelModel.get('rotate')) || 0
  585. ) * PI / 180;
  586. var labelLayout = innerTextLayout(opt.rotation, labelRotation, opt.labelDirection);
  587. var rawCategoryData = axisModel.getCategories && axisModel.getCategories(true);
  588. var labelEls = [];
  589. var silent = isLabelSilent(axisModel);
  590. var triggerEvent = axisModel.get('triggerEvent');
  591. each(labels, function (labelItem, index) {
  592. var tickValue = labelItem.tickValue;
  593. var formattedLabel = labelItem.formattedLabel;
  594. var rawLabel = labelItem.rawLabel;
  595. var itemLabelModel = labelModel;
  596. if (rawCategoryData && rawCategoryData[tickValue] && rawCategoryData[tickValue].textStyle) {
  597. itemLabelModel = new Model(
  598. rawCategoryData[tickValue].textStyle, labelModel, axisModel.ecModel
  599. );
  600. }
  601. var textColor = itemLabelModel.getTextColor()
  602. || axisModel.get('axisLine.lineStyle.color');
  603. var tickCoord = axis.dataToCoord(tickValue);
  604. var pos = [
  605. tickCoord,
  606. opt.labelOffset + opt.labelDirection * labelMargin
  607. ];
  608. var textEl = new graphic.Text({
  609. // Id for animation
  610. anid: 'label_' + tickValue,
  611. position: pos,
  612. rotation: labelLayout.rotation,
  613. silent: silent,
  614. z2: 10
  615. });
  616. graphic.setTextStyle(textEl.style, itemLabelModel, {
  617. text: formattedLabel,
  618. textAlign: itemLabelModel.getShallow('align', true)
  619. || labelLayout.textAlign,
  620. textVerticalAlign: itemLabelModel.getShallow('verticalAlign', true)
  621. || itemLabelModel.getShallow('baseline', true)
  622. || labelLayout.textVerticalAlign,
  623. textFill: typeof textColor === 'function'
  624. ? textColor(
  625. // (1) In category axis with data zoom, tick is not the original
  626. // index of axis.data. So tick should not be exposed to user
  627. // in category axis.
  628. // (2) Compatible with previous version, which always use formatted label as
  629. // input. But in interval scale the formatted label is like '223,445', which
  630. // maked user repalce ','. So we modify it to return original val but remain
  631. // it as 'string' to avoid error in replacing.
  632. axis.type === 'category'
  633. ? rawLabel
  634. : axis.type === 'value'
  635. ? tickValue + ''
  636. : tickValue,
  637. index
  638. )
  639. : textColor
  640. });
  641. // Pack data for mouse event
  642. if (triggerEvent) {
  643. textEl.eventData = makeAxisEventDataBase(axisModel);
  644. textEl.eventData.targetType = 'axisLabel';
  645. textEl.eventData.value = rawLabel;
  646. }
  647. // FIXME
  648. axisBuilder._dumbGroup.add(textEl);
  649. textEl.updateTransform();
  650. labelEls.push(textEl);
  651. axisBuilder.group.add(textEl);
  652. textEl.decomposeTransform();
  653. });
  654. return labelEls;
  655. }
  656. export default AxisBuilder;