AxisBuilder.js 22 KB

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