AxisBuilder.js 22 KB

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