Tick.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import H from '../Globals.js';
  12. /**
  13. * Optional parameters for the tick.
  14. * @private
  15. * @interface Highcharts.TickParametersObject
  16. */ /**
  17. * Set category for the tick.
  18. * @name Highcharts.TickParametersObject#category
  19. * @type {string|undefined}
  20. */ /**
  21. * @name Highcharts.TickParametersObject#options
  22. * @type {Highcharts.Dictionary<any>|undefined}
  23. */ /**
  24. * Set tickmarkOffset for the tick.
  25. * @name Highcharts.TickParametersObject#tickmarkOffset
  26. * @type {number|undefined}
  27. */
  28. import U from '../Utilities.js';
  29. var clamp = U.clamp, correctFloat = U.correctFloat, defined = U.defined, destroyObjectProperties = U.destroyObjectProperties, extend = U.extend, fireEvent = U.fireEvent, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, pick = U.pick;
  30. var deg2rad = H.deg2rad;
  31. /* eslint-disable no-invalid-this, valid-jsdoc */
  32. /**
  33. * The Tick class.
  34. *
  35. * @class
  36. * @name Highcharts.Tick
  37. *
  38. * @param {Highcharts.Axis} axis
  39. * The axis of the tick.
  40. *
  41. * @param {number} pos
  42. * The position of the tick on the axis in terms of axis values.
  43. *
  44. * @param {string} [type]
  45. * The type of tick, either 'minor' or an empty string
  46. *
  47. * @param {boolean} [noLabel=false]
  48. * Whether to disable the label or not. Defaults to false.
  49. *
  50. * @param {object} [parameters]
  51. * Optional parameters for the tick.
  52. */
  53. var Tick = /** @class */ (function () {
  54. /* *
  55. *
  56. * Constructors
  57. *
  58. * */
  59. function Tick(axis, pos, type, noLabel, parameters) {
  60. this.isNew = true;
  61. this.isNewLabel = true;
  62. /**
  63. * The related axis of the tick.
  64. * @name Highcharts.Tick#axis
  65. * @type {Highcharts.Axis}
  66. */
  67. this.axis = axis;
  68. /**
  69. * The logical position of the tick on the axis in terms of axis values.
  70. * @name Highcharts.Tick#pos
  71. * @type {number}
  72. */
  73. this.pos = pos;
  74. /**
  75. * The tick type, which can be `"minor"`, or an empty string.
  76. * @name Highcharts.Tick#type
  77. * @type {string}
  78. */
  79. this.type = type || '';
  80. this.parameters = parameters || {};
  81. /**
  82. * The mark offset of the tick on the axis. Usually `undefined`, numeric
  83. * for grid axes.
  84. * @name Highcharts.Tick#tickmarkOffset
  85. * @type {number|undefined}
  86. */
  87. this.tickmarkOffset = this.parameters.tickmarkOffset;
  88. this.options = this.parameters.options;
  89. fireEvent(this, 'init');
  90. if (!type && !noLabel) {
  91. this.addLabel();
  92. }
  93. }
  94. /* *
  95. *
  96. * Functions
  97. *
  98. * */
  99. /**
  100. * Write the tick label.
  101. *
  102. * @private
  103. * @function Highcharts.Tick#addLabel
  104. * @return {void}
  105. */
  106. Tick.prototype.addLabel = function () {
  107. var tick = this, axis = tick.axis, options = axis.options, chart = axis.chart, categories = axis.categories, log = axis.logarithmic, names = axis.names, pos = tick.pos, labelOptions = pick(tick.options && tick.options.labels, options.labels), str, tickPositions = axis.tickPositions, isFirst = pos === tickPositions[0], isLast = pos === tickPositions[tickPositions.length - 1], value = this.parameters.category || (categories ?
  108. pick(categories[pos], names[pos], pos) :
  109. pos), label = tick.label, animateLabels = (!labelOptions.step || labelOptions.step === 1) &&
  110. axis.tickInterval === 1, tickPositionInfo = tickPositions.info, dateTimeLabelFormat, dateTimeLabelFormats, i, list;
  111. // Set the datetime label format. If a higher rank is set for this
  112. // position, use that. If not, use the general format.
  113. if (axis.dateTime && tickPositionInfo) {
  114. dateTimeLabelFormats = chart.time.resolveDTLFormat(options.dateTimeLabelFormats[(!options.grid &&
  115. tickPositionInfo.higherRanks[pos]) ||
  116. tickPositionInfo.unitName]);
  117. dateTimeLabelFormat = dateTimeLabelFormats.main;
  118. }
  119. // set properties for access in render method
  120. /**
  121. * True if the tick is the first one on the axis.
  122. * @name Highcharts.Tick#isFirst
  123. * @readonly
  124. * @type {boolean|undefined}
  125. */
  126. tick.isFirst = isFirst;
  127. /**
  128. * True if the tick is the last one on the axis.
  129. * @name Highcharts.Tick#isLast
  130. * @readonly
  131. * @type {boolean|undefined}
  132. */
  133. tick.isLast = isLast;
  134. // Get the string
  135. tick.formatCtx = {
  136. axis: axis,
  137. chart: chart,
  138. isFirst: isFirst,
  139. isLast: isLast,
  140. dateTimeLabelFormat: dateTimeLabelFormat,
  141. tickPositionInfo: tickPositionInfo,
  142. value: log ? correctFloat(log.lin2log(value)) : value,
  143. pos: pos
  144. };
  145. str = axis.labelFormatter.call(tick.formatCtx, this.formatCtx);
  146. // Set up conditional formatting based on the format list if existing.
  147. list = dateTimeLabelFormats && dateTimeLabelFormats.list;
  148. if (list) {
  149. tick.shortenLabel = function () {
  150. for (i = 0; i < list.length; i++) {
  151. label.attr({
  152. text: axis.labelFormatter.call(extend(tick.formatCtx, { dateTimeLabelFormat: list[i] }))
  153. });
  154. if (label.getBBox().width <
  155. axis.getSlotWidth(tick) - 2 *
  156. pick(labelOptions.padding, 5)) {
  157. return;
  158. }
  159. }
  160. label.attr({
  161. text: ''
  162. });
  163. };
  164. }
  165. // Call only after first render
  166. if (animateLabels && axis._addedPlotLB) {
  167. tick.moveLabel(str, labelOptions);
  168. }
  169. // First call
  170. if (!defined(label) && !tick.movedLabel) {
  171. /**
  172. * The rendered text label of the tick.
  173. * @name Highcharts.Tick#label
  174. * @type {Highcharts.SVGElement|undefined}
  175. */
  176. tick.label = label = tick.createLabel({ x: 0, y: 0 }, str, labelOptions);
  177. // Base value to detect change for new calls to getBBox
  178. tick.rotation = 0;
  179. // update
  180. }
  181. else if (label && label.textStr !== str && !animateLabels) {
  182. // When resetting text, also reset the width if dynamically set
  183. // (#8809)
  184. if (label.textWidth &&
  185. !(labelOptions.style && labelOptions.style.width) &&
  186. !label.styles.width) {
  187. label.css({ width: null });
  188. }
  189. label.attr({ text: str });
  190. label.textPxLength = label.getBBox().width;
  191. }
  192. };
  193. /**
  194. * Render and return the label of the tick.
  195. *
  196. * @private
  197. * @function Highcharts.Tick#createLabel
  198. * @param {Highcharts.PositionObject} xy
  199. * @param {string} str
  200. * @param {Highcharts.XAxisLabelsOptions} labelOptions
  201. * @return {Highcharts.SVGElement|undefined}
  202. */
  203. Tick.prototype.createLabel = function (xy, str, labelOptions) {
  204. var axis = this.axis, chart = axis.chart, label = defined(str) && labelOptions.enabled ?
  205. chart.renderer
  206. .text(str, xy.x, xy.y, labelOptions.useHTML)
  207. .add(axis.labelGroup) :
  208. null;
  209. // Un-rotated length
  210. if (label) {
  211. // Without position absolute, IE export sometimes is wrong
  212. if (!chart.styledMode) {
  213. label.css(merge(labelOptions.style));
  214. }
  215. label.textPxLength = label.getBBox().width;
  216. }
  217. return label;
  218. };
  219. /**
  220. * Destructor for the tick prototype
  221. *
  222. * @private
  223. * @function Highcharts.Tick#destroy
  224. * @return {void}
  225. */
  226. Tick.prototype.destroy = function () {
  227. destroyObjectProperties(this, this.axis);
  228. };
  229. /**
  230. * Gets the x and y positions for ticks in terms of pixels.
  231. *
  232. * @private
  233. * @function Highcharts.Tick#getPosition
  234. *
  235. * @param {boolean} horiz
  236. * Whether the tick is on an horizontal axis or not.
  237. *
  238. * @param {number} tickPos
  239. * Position of the tick.
  240. *
  241. * @param {number} tickmarkOffset
  242. * Tickmark offset for all ticks.
  243. *
  244. * @param {boolean} [old]
  245. * Whether the axis has changed or not.
  246. *
  247. * @return {Highcharts.PositionObject}
  248. * The tick position.
  249. *
  250. * @fires Highcharts.Tick#event:afterGetPosition
  251. */
  252. Tick.prototype.getPosition = function (horiz, tickPos, tickmarkOffset, old) {
  253. var axis = this.axis, chart = axis.chart, cHeight = (old && chart.oldChartHeight) || chart.chartHeight, pos;
  254. pos = {
  255. x: horiz ?
  256. correctFloat(axis.translate(tickPos + tickmarkOffset, null, null, old) +
  257. axis.transB) :
  258. (axis.left +
  259. axis.offset +
  260. (axis.opposite ?
  261. (((old && chart.oldChartWidth) ||
  262. chart.chartWidth) -
  263. axis.right -
  264. axis.left) :
  265. 0)),
  266. y: horiz ?
  267. (cHeight -
  268. axis.bottom +
  269. axis.offset -
  270. (axis.opposite ? axis.height : 0)) :
  271. correctFloat(cHeight -
  272. axis.translate(tickPos + tickmarkOffset, null, null, old) -
  273. axis.transB)
  274. };
  275. // Chrome workaround for #10516
  276. pos.y = clamp(pos.y, -1e5, 1e5);
  277. fireEvent(this, 'afterGetPosition', { pos: pos });
  278. return pos;
  279. };
  280. /**
  281. * Get the x, y position of the tick label
  282. *
  283. * @private
  284. * @return {Highcharts.PositionObject}
  285. */
  286. Tick.prototype.getLabelPosition = function (x, y, label, horiz, labelOptions, tickmarkOffset, index, step) {
  287. var axis = this.axis, transA = axis.transA, reversed = ( // #7911
  288. axis.isLinked && axis.linkedParent ?
  289. axis.linkedParent.reversed :
  290. axis.reversed), staggerLines = axis.staggerLines, rotCorr = axis.tickRotCorr || { x: 0, y: 0 }, yOffset = labelOptions.y,
  291. // Adjust for label alignment if we use reserveSpace: true (#5286)
  292. labelOffsetCorrection = (!horiz && !axis.reserveSpaceDefault ?
  293. -axis.labelOffset * (axis.labelAlign === 'center' ? 0.5 : 1) :
  294. 0), line, pos = {};
  295. if (!defined(yOffset)) {
  296. if (axis.side === 0) {
  297. yOffset = label.rotation ? -8 : -label.getBBox().height;
  298. }
  299. else if (axis.side === 2) {
  300. yOffset = rotCorr.y + 8;
  301. }
  302. else {
  303. // #3140, #3140
  304. yOffset = Math.cos(label.rotation * deg2rad) *
  305. (rotCorr.y - label.getBBox(false, 0).height / 2);
  306. }
  307. }
  308. x = x +
  309. labelOptions.x +
  310. labelOffsetCorrection +
  311. rotCorr.x -
  312. (tickmarkOffset && horiz ?
  313. tickmarkOffset * transA * (reversed ? -1 : 1) :
  314. 0);
  315. y = y + yOffset - (tickmarkOffset && !horiz ?
  316. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  317. // Correct for staggered labels
  318. if (staggerLines) {
  319. line = (index / (step || 1) % staggerLines);
  320. if (axis.opposite) {
  321. line = staggerLines - line - 1;
  322. }
  323. y += line * (axis.labelOffset / staggerLines);
  324. }
  325. pos.x = x;
  326. pos.y = Math.round(y);
  327. fireEvent(this, 'afterGetLabelPosition', { pos: pos, tickmarkOffset: tickmarkOffset, index: index });
  328. return pos;
  329. };
  330. /**
  331. * Get the offset height or width of the label
  332. *
  333. * @private
  334. * @function Highcharts.Tick#getLabelSize
  335. * @return {number}
  336. */
  337. Tick.prototype.getLabelSize = function () {
  338. return this.label ?
  339. this.label.getBBox()[this.axis.horiz ? 'height' : 'width'] :
  340. 0;
  341. };
  342. /**
  343. * Extendible method to return the path of the marker
  344. *
  345. * @private
  346. *
  347. */
  348. Tick.prototype.getMarkPath = function (x, y, tickLength, tickWidth, horiz, renderer) {
  349. return renderer.crispLine([[
  350. 'M',
  351. x,
  352. y
  353. ], [
  354. 'L',
  355. x + (horiz ? 0 : -tickLength),
  356. y + (horiz ? tickLength : 0)
  357. ]], tickWidth);
  358. };
  359. /**
  360. * Handle the label overflow by adjusting the labels to the left and right
  361. * edge, or hide them if they collide into the neighbour label.
  362. *
  363. * @private
  364. * @function Highcharts.Tick#handleOverflow
  365. * @param {Highcharts.PositionObject} xy
  366. * @return {void}
  367. */
  368. Tick.prototype.handleOverflow = function (xy) {
  369. var tick = this, axis = this.axis, labelOptions = axis.options.labels, pxPos = xy.x, chartWidth = axis.chart.chartWidth, spacing = axis.chart.spacing, leftBound = pick(axis.labelLeft, Math.min(axis.pos, spacing[3])), rightBound = pick(axis.labelRight, Math.max(!axis.isRadial ? axis.pos + axis.len : 0, chartWidth - spacing[1])), label = this.label, rotation = this.rotation, factor = {
  370. left: 0,
  371. center: 0.5,
  372. right: 1
  373. }[axis.labelAlign || label.attr('align')], labelWidth = label.getBBox().width, slotWidth = axis.getSlotWidth(tick), modifiedSlotWidth = slotWidth, xCorrection = factor, goRight = 1, leftPos, rightPos, textWidth, css = {};
  374. // Check if the label overshoots the chart spacing box. If it does, move
  375. // it. If it now overshoots the slotWidth, add ellipsis.
  376. if (!rotation &&
  377. pick(labelOptions.overflow, 'justify') === 'justify') {
  378. leftPos = pxPos - factor * labelWidth;
  379. rightPos = pxPos + (1 - factor) * labelWidth;
  380. if (leftPos < leftBound) {
  381. modifiedSlotWidth =
  382. xy.x + modifiedSlotWidth * (1 - factor) - leftBound;
  383. }
  384. else if (rightPos > rightBound) {
  385. modifiedSlotWidth =
  386. rightBound - xy.x + modifiedSlotWidth * factor;
  387. goRight = -1;
  388. }
  389. modifiedSlotWidth = Math.min(slotWidth, modifiedSlotWidth); // #4177
  390. if (modifiedSlotWidth < slotWidth && axis.labelAlign === 'center') {
  391. xy.x += (goRight *
  392. (slotWidth -
  393. modifiedSlotWidth -
  394. xCorrection * (slotWidth - Math.min(labelWidth, modifiedSlotWidth))));
  395. }
  396. // If the label width exceeds the available space, set a text width
  397. // to be picked up below. Also, if a width has been set before, we
  398. // need to set a new one because the reported labelWidth will be
  399. // limited by the box (#3938).
  400. if (labelWidth > modifiedSlotWidth ||
  401. (axis.autoRotation && (label.styles || {}).width)) {
  402. textWidth = modifiedSlotWidth;
  403. }
  404. // Add ellipsis to prevent rotated labels to be clipped against the edge
  405. // of the chart
  406. }
  407. else if (rotation < 0 &&
  408. pxPos - factor * labelWidth < leftBound) {
  409. textWidth = Math.round(pxPos / Math.cos(rotation * deg2rad) - leftBound);
  410. }
  411. else if (rotation > 0 &&
  412. pxPos + factor * labelWidth > rightBound) {
  413. textWidth = Math.round((chartWidth - pxPos) /
  414. Math.cos(rotation * deg2rad));
  415. }
  416. if (textWidth) {
  417. if (tick.shortenLabel) {
  418. tick.shortenLabel();
  419. }
  420. else {
  421. css.width = Math.floor(textWidth) + 'px';
  422. if (!(labelOptions.style || {}).textOverflow) {
  423. css.textOverflow = 'ellipsis';
  424. }
  425. label.css(css);
  426. }
  427. }
  428. };
  429. /**
  430. * Try to replace the label if the same one already exists.
  431. *
  432. * @private
  433. * @function Highcharts.Tick#moveLabel
  434. * @param {string} str
  435. * @param {Highcharts.XAxisLabelsOptions} labelOptions
  436. *
  437. * @return {void}
  438. */
  439. Tick.prototype.moveLabel = function (str, labelOptions) {
  440. var tick = this, label = tick.label, moved = false, axis = tick.axis, labelPos, reversed = axis.reversed, xPos, yPos;
  441. if (label && label.textStr === str) {
  442. tick.movedLabel = label;
  443. moved = true;
  444. delete tick.label;
  445. }
  446. else { // Find a label with the same string
  447. objectEach(axis.ticks, function (currentTick) {
  448. if (!moved &&
  449. !currentTick.isNew &&
  450. currentTick !== tick &&
  451. currentTick.label &&
  452. currentTick.label.textStr === str) {
  453. tick.movedLabel = currentTick.label;
  454. moved = true;
  455. currentTick.labelPos = tick.movedLabel.xy;
  456. delete currentTick.label;
  457. }
  458. });
  459. }
  460. // Create new label if the actual one is moved
  461. if (!moved && (tick.labelPos || label)) {
  462. labelPos = tick.labelPos || label.xy;
  463. xPos = axis.horiz ?
  464. (reversed ? 0 : axis.width + axis.left) : labelPos.x;
  465. yPos = axis.horiz ?
  466. labelPos.y : (reversed ? (axis.width + axis.left) : 0);
  467. tick.movedLabel = tick.createLabel({ x: xPos, y: yPos }, str, labelOptions);
  468. if (tick.movedLabel) {
  469. tick.movedLabel.attr({ opacity: 0 });
  470. }
  471. }
  472. };
  473. /**
  474. * Put everything in place
  475. *
  476. * @private
  477. * @param {number} index
  478. * @param {boolean} [old]
  479. * Use old coordinates to prepare an animation into new position
  480. * @param {number} [opacity]
  481. * @return {voids}
  482. */
  483. Tick.prototype.render = function (index, old, opacity) {
  484. var tick = this, axis = tick.axis, horiz = axis.horiz, pos = tick.pos, tickmarkOffset = pick(tick.tickmarkOffset, axis.tickmarkOffset), xy = tick.getPosition(horiz, pos, tickmarkOffset, old), x = xy.x, y = xy.y, reverseCrisp = ((horiz && x === axis.pos + axis.len) ||
  485. (!horiz && y === axis.pos)) ? -1 : 1; // #1480, #1687
  486. opacity = pick(opacity, 1);
  487. this.isActive = true;
  488. // Create the grid line
  489. this.renderGridLine(old, opacity, reverseCrisp);
  490. // create the tick mark
  491. this.renderMark(xy, opacity, reverseCrisp);
  492. // the label is created on init - now move it into place
  493. this.renderLabel(xy, old, opacity, index);
  494. tick.isNew = false;
  495. fireEvent(this, 'afterRender');
  496. };
  497. /**
  498. * Renders the gridLine.
  499. *
  500. * @private
  501. * @param {boolean} old Whether or not the tick is old
  502. * @param {number} opacity The opacity of the grid line
  503. * @param {number} reverseCrisp Modifier for avoiding overlapping 1 or -1
  504. * @return {void}
  505. */
  506. Tick.prototype.renderGridLine = function (old, opacity, reverseCrisp) {
  507. var tick = this, axis = tick.axis, options = axis.options, gridLine = tick.gridLine, gridLinePath, attribs = {}, pos = tick.pos, type = tick.type, tickmarkOffset = pick(tick.tickmarkOffset, axis.tickmarkOffset), renderer = axis.chart.renderer, gridPrefix = type ? type + 'Grid' : 'grid', gridLineWidth = options[gridPrefix + 'LineWidth'], gridLineColor = options[gridPrefix + 'LineColor'], dashStyle = options[gridPrefix + 'LineDashStyle'];
  508. if (!gridLine) {
  509. if (!axis.chart.styledMode) {
  510. attribs.stroke = gridLineColor;
  511. attribs['stroke-width'] = gridLineWidth;
  512. if (dashStyle) {
  513. attribs.dashstyle = dashStyle;
  514. }
  515. }
  516. if (!type) {
  517. attribs.zIndex = 1;
  518. }
  519. if (old) {
  520. opacity = 0;
  521. }
  522. /**
  523. * The rendered grid line of the tick.
  524. * @name Highcharts.Tick#gridLine
  525. * @type {Highcharts.SVGElement|undefined}
  526. */
  527. tick.gridLine = gridLine = renderer.path()
  528. .attr(attribs)
  529. .addClass('highcharts-' + (type ? type + '-' : '') + 'grid-line')
  530. .add(axis.gridGroup);
  531. }
  532. if (gridLine) {
  533. gridLinePath = axis.getPlotLinePath({
  534. value: pos + tickmarkOffset,
  535. lineWidth: gridLine.strokeWidth() * reverseCrisp,
  536. force: 'pass',
  537. old: old
  538. });
  539. // If the parameter 'old' is set, the current call will be followed
  540. // by another call, therefore do not do any animations this time
  541. if (gridLinePath) {
  542. gridLine[old || tick.isNew ? 'attr' : 'animate']({
  543. d: gridLinePath,
  544. opacity: opacity
  545. });
  546. }
  547. }
  548. };
  549. /**
  550. * Renders the tick mark.
  551. *
  552. * @private
  553. * @param {Highcharts.PositionObject} xy The position vector of the mark
  554. * @param {number} opacity The opacity of the mark
  555. * @param {number} reverseCrisp Modifier for avoiding overlapping 1 or -1
  556. * @return {void}
  557. */
  558. Tick.prototype.renderMark = function (xy, opacity, reverseCrisp) {
  559. var tick = this, axis = tick.axis, options = axis.options, renderer = axis.chart.renderer, type = tick.type, tickPrefix = type ? type + 'Tick' : 'tick', tickSize = axis.tickSize(tickPrefix), mark = tick.mark, isNewMark = !mark, x = xy.x, y = xy.y, tickWidth = pick(options[tickPrefix + 'Width'], !type && axis.isXAxis ? 1 : 0), // X axis defaults to 1
  560. tickColor = options[tickPrefix + 'Color'];
  561. if (tickSize) {
  562. // negate the length
  563. if (axis.opposite) {
  564. tickSize[0] = -tickSize[0];
  565. }
  566. // First time, create it
  567. if (isNewMark) {
  568. /**
  569. * The rendered mark of the tick.
  570. * @name Highcharts.Tick#mark
  571. * @type {Highcharts.SVGElement|undefined}
  572. */
  573. tick.mark = mark = renderer.path()
  574. .addClass('highcharts-' + (type ? type + '-' : '') + 'tick')
  575. .add(axis.axisGroup);
  576. if (!axis.chart.styledMode) {
  577. mark.attr({
  578. stroke: tickColor,
  579. 'stroke-width': tickWidth
  580. });
  581. }
  582. }
  583. mark[isNewMark ? 'attr' : 'animate']({
  584. d: tick.getMarkPath(x, y, tickSize[0], mark.strokeWidth() * reverseCrisp, axis.horiz, renderer),
  585. opacity: opacity
  586. });
  587. }
  588. };
  589. /**
  590. * Renders the tick label.
  591. * Note: The label should already be created in init(), so it should only
  592. * have to be moved into place.
  593. *
  594. * @private
  595. * @param {Highcharts.PositionObject} xy The position vector of the label
  596. * @param {boolean} old Whether or not the tick is old
  597. * @param {number} opacity The opacity of the label
  598. * @param {number} index The index of the tick
  599. * @return {void}
  600. */
  601. Tick.prototype.renderLabel = function (xy, old, opacity, index) {
  602. var tick = this, axis = tick.axis, horiz = axis.horiz, options = axis.options, label = tick.label, labelOptions = options.labels, step = labelOptions.step, tickmarkOffset = pick(tick.tickmarkOffset, axis.tickmarkOffset), show = true, x = xy.x, y = xy.y;
  603. if (label && isNumber(x)) {
  604. label.xy = xy = tick.getLabelPosition(x, y, label, horiz, labelOptions, tickmarkOffset, index, step);
  605. // Apply show first and show last. If the tick is both first and
  606. // last, it is a single centered tick, in which case we show the
  607. // label anyway (#2100).
  608. if ((tick.isFirst &&
  609. !tick.isLast &&
  610. !pick(options.showFirstLabel, 1)) ||
  611. (tick.isLast &&
  612. !tick.isFirst &&
  613. !pick(options.showLastLabel, 1))) {
  614. show = false;
  615. // Handle label overflow and show or hide accordingly
  616. }
  617. else if (horiz &&
  618. !labelOptions.step &&
  619. !labelOptions.rotation &&
  620. !old &&
  621. opacity !== 0) {
  622. tick.handleOverflow(xy);
  623. }
  624. // apply step
  625. if (step && index % step) {
  626. // show those indices dividable by step
  627. show = false;
  628. }
  629. // Set the new position, and show or hide
  630. if (show && isNumber(xy.y)) {
  631. xy.opacity = opacity;
  632. label[tick.isNewLabel ? 'attr' : 'animate'](xy);
  633. tick.isNewLabel = false;
  634. }
  635. else {
  636. label.attr('y', -9999); // #1338
  637. tick.isNewLabel = true;
  638. }
  639. }
  640. };
  641. /**
  642. * Replace labels with the moved ones to perform animation. Additionally
  643. * destroy unused labels.
  644. *
  645. * @private
  646. * @function Highcharts.Tick#replaceMovedLabel
  647. * @return {void}
  648. */
  649. Tick.prototype.replaceMovedLabel = function () {
  650. var tick = this, label = tick.label, axis = tick.axis, reversed = axis.reversed, x, y;
  651. // Animate and destroy
  652. if (label && !tick.isNew) {
  653. x = axis.horiz ? (reversed ? axis.left : axis.width + axis.left) : label.xy.x;
  654. y = axis.horiz ?
  655. label.xy.y :
  656. (reversed ? axis.width + axis.top : axis.top);
  657. label.animate({ x: x, y: y, opacity: 0 }, void 0, label.destroy);
  658. delete tick.label;
  659. }
  660. axis.isDirty = true;
  661. tick.label = tick.movedLabel;
  662. delete tick.movedLabel;
  663. };
  664. return Tick;
  665. }());
  666. H.Tick = Tick;
  667. export default H.Tick;