grid-axis.src.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /**
  2. * @license Highcharts Gantt JS v8.2.2 (2020-10-22)
  3. *
  4. * GridAxis
  5. *
  6. * (c) 2016-2019 Lars A. V. Cabrera
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/grid-axis', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Core/Axis/GridAxis.js', [_modules['Core/Axis/Axis.js'], _modules['Core/Globals.js'], _modules['Core/Options.js'], _modules['Core/Axis/Tick.js'], _modules['Core/Utilities.js']], function (Axis, H, O, Tick, U) {
  32. /* *
  33. *
  34. * (c) 2016 Highsoft AS
  35. * Authors: Lars A. V. Cabrera
  36. *
  37. * License: www.highcharts.com/license
  38. *
  39. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  40. *
  41. * */
  42. var dateFormat = O.dateFormat;
  43. var addEvent = U.addEvent,
  44. defined = U.defined,
  45. erase = U.erase,
  46. find = U.find,
  47. isArray = U.isArray,
  48. isNumber = U.isNumber,
  49. merge = U.merge,
  50. pick = U.pick,
  51. timeUnits = U.timeUnits,
  52. wrap = U.wrap;
  53. var argsToArray = function (args) {
  54. return Array.prototype.slice.call(args, 1);
  55. }, isObject = function (x) {
  56. // Always use strict mode
  57. return U.isObject(x, true);
  58. }, Chart = H.Chart;
  59. var applyGridOptions = function applyGridOptions(axis) {
  60. var options = axis.options;
  61. // Center-align by default
  62. if (!options.labels) {
  63. options.labels = {};
  64. }
  65. options.labels.align = pick(options.labels.align, 'center');
  66. // @todo: Check against tickLabelPlacement between/on etc
  67. /* Prevents adding the last tick label if the axis is not a category
  68. axis.
  69. Since numeric labels are normally placed at starts and ends of a
  70. range of value, and this module makes the label point at the value,
  71. an "extra" label would appear. */
  72. if (!axis.categories) {
  73. options.showLastLabel = false;
  74. }
  75. // Prevents rotation of labels when squished, as rotating them would not
  76. // help.
  77. axis.labelRotation = 0;
  78. options.labels.rotation = 0;
  79. };
  80. /**
  81. * @productdesc {gantt}
  82. * For grid axes (like in Gantt charts),
  83. * it is possible to declare as a list to provide different
  84. * formats depending on available space.
  85. *
  86. * Defaults to:
  87. * ```js
  88. * {
  89. * hour: { list: ['%H:%M', '%H'] },
  90. * day: { list: ['%A, %e. %B', '%a, %e. %b', '%E'] },
  91. * week: { list: ['Week %W', 'W%W'] },
  92. * month: { list: ['%B', '%b', '%o'] }
  93. * }
  94. * ```
  95. *
  96. * @sample {gantt} gantt/grid-axis/date-time-label-formats
  97. * Gantt chart with custom axis date format.
  98. *
  99. * @apioption xAxis.dateTimeLabelFormats
  100. */
  101. /**
  102. * Set grid options for the axis labels. Requires Highcharts Gantt.
  103. *
  104. * @since 6.2.0
  105. * @product gantt
  106. * @apioption xAxis.grid
  107. */
  108. /**
  109. * Enable grid on the axis labels. Defaults to true for Gantt charts.
  110. *
  111. * @type {boolean}
  112. * @default true
  113. * @since 6.2.0
  114. * @product gantt
  115. * @apioption xAxis.grid.enabled
  116. */
  117. /**
  118. * Set specific options for each column (or row for horizontal axes) in the
  119. * grid. Each extra column/row is its own axis, and the axis options can be set
  120. * here.
  121. *
  122. * @sample gantt/demo/left-axis-table
  123. * Left axis as a table
  124. *
  125. * @type {Array<Highcharts.XAxisOptions>}
  126. * @apioption xAxis.grid.columns
  127. */
  128. /**
  129. * Set border color for the label grid lines.
  130. *
  131. * @type {Highcharts.ColorString}
  132. * @apioption xAxis.grid.borderColor
  133. */
  134. /**
  135. * Set border width of the label grid lines.
  136. *
  137. * @type {number}
  138. * @default 1
  139. * @apioption xAxis.grid.borderWidth
  140. */
  141. /**
  142. * Set cell height for grid axis labels. By default this is calculated from font
  143. * size. This option only applies to horizontal axes.
  144. *
  145. * @sample gantt/grid-axis/cellheight
  146. * Gant chart with custom cell height
  147. * @type {number}
  148. * @apioption xAxis.grid.cellHeight
  149. */
  150. ''; // detach doclets above
  151. /**
  152. * Get the largest label width and height.
  153. *
  154. * @private
  155. * @function Highcharts.Axis#getMaxLabelDimensions
  156. *
  157. * @param {Highcharts.Dictionary<Highcharts.Tick>} ticks
  158. * All the ticks on one axis.
  159. *
  160. * @param {Array<number|string>} tickPositions
  161. * All the tick positions on one axis.
  162. *
  163. * @return {Highcharts.SizeObject}
  164. * Object containing the properties height and width.
  165. *
  166. * @todo Move this to the generic axis implementation, as it is used there.
  167. */
  168. Axis.prototype.getMaxLabelDimensions = function (ticks, tickPositions) {
  169. var dimensions = {
  170. width: 0,
  171. height: 0
  172. };
  173. tickPositions.forEach(function (pos) {
  174. var tick = ticks[pos],
  175. labelHeight = 0,
  176. labelWidth = 0,
  177. label;
  178. if (isObject(tick)) {
  179. label = isObject(tick.label) ? tick.label : {};
  180. // Find width and height of label
  181. labelHeight = label.getBBox ? label.getBBox().height : 0;
  182. if (label.textStr && !isNumber(label.textPxLength)) {
  183. label.textPxLength = label.getBBox().width;
  184. }
  185. labelWidth = isNumber(label.textPxLength) ?
  186. // Math.round ensures crisp lines
  187. Math.round(label.textPxLength) :
  188. 0;
  189. if (label.textStr) {
  190. // Set the tickWidth same as the label width after ellipsis
  191. // applied #10281
  192. labelWidth = Math.round(label.getBBox().width);
  193. }
  194. // Update the result if width and/or height are larger
  195. dimensions.height = Math.max(labelHeight, dimensions.height);
  196. dimensions.width = Math.max(labelWidth, dimensions.width);
  197. }
  198. });
  199. return dimensions;
  200. };
  201. // Adds week date format
  202. H.dateFormats.W = function (timestamp) {
  203. var d = new this.Date(timestamp);
  204. var firstDay = (this.get('Day',
  205. d) + 6) % 7;
  206. var thursday = new this.Date(d.valueOf());
  207. this.set('Date', thursday, this.get('Date', d) - firstDay + 3);
  208. var firstThursday = new this.Date(this.get('FullYear',
  209. thursday), 0, 1);
  210. if (this.get('Day', firstThursday) !== 4) {
  211. this.set('Month', d, 0);
  212. this.set('Date', d, 1 + (11 - this.get('Day', firstThursday)) % 7);
  213. }
  214. return (1 +
  215. Math.floor((thursday.valueOf() - firstThursday.valueOf()) / 604800000)).toString();
  216. };
  217. // First letter of the day of the week, e.g. 'M' for 'Monday'.
  218. H.dateFormats.E = function (timestamp) {
  219. return this.dateFormat('%a', timestamp, true).charAt(0);
  220. };
  221. /* eslint-disable no-invalid-this */
  222. addEvent(Chart, 'afterSetChartSize', function () {
  223. this.axes.forEach(function (axis) {
  224. (axis.grid && axis.grid.columns || []).forEach(function (column) {
  225. column.setAxisSize();
  226. column.setAxisTranslation();
  227. });
  228. });
  229. });
  230. // Center tick labels in cells.
  231. addEvent(Tick, 'afterGetLabelPosition', function (e) {
  232. var tick = this,
  233. label = tick.label,
  234. axis = tick.axis,
  235. reversed = axis.reversed,
  236. chart = axis.chart,
  237. options = axis.options,
  238. gridOptions = options.grid || {},
  239. labelOpts = axis.options.labels,
  240. align = labelOpts.align,
  241. // verticalAlign is currently not supported for axis.labels.
  242. verticalAlign = 'middle', // labelOpts.verticalAlign,
  243. side = GridAxis.Side[axis.side],
  244. tickmarkOffset = e.tickmarkOffset,
  245. tickPositions = axis.tickPositions,
  246. tickPos = tick.pos - tickmarkOffset,
  247. nextTickPos = (isNumber(tickPositions[e.index + 1]) ?
  248. tickPositions[e.index + 1] - tickmarkOffset :
  249. axis.max + tickmarkOffset),
  250. tickSize = axis.tickSize('tick'),
  251. tickWidth = tickSize ? tickSize[0] : 0,
  252. crispCorr = tickSize ? tickSize[1] / 2 : 0,
  253. labelHeight,
  254. lblMetrics,
  255. lines,
  256. bottom,
  257. top,
  258. left,
  259. right;
  260. // Only center tick labels in grid axes
  261. if (gridOptions.enabled === true) {
  262. // Calculate top and bottom positions of the cell.
  263. if (side === 'top') {
  264. bottom = axis.top + axis.offset;
  265. top = bottom - tickWidth;
  266. }
  267. else if (side === 'bottom') {
  268. top = chart.chartHeight - axis.bottom + axis.offset;
  269. bottom = top + tickWidth;
  270. }
  271. else {
  272. bottom = axis.top + axis.len - axis.translate(reversed ? nextTickPos : tickPos);
  273. top = axis.top + axis.len - axis.translate(reversed ? tickPos : nextTickPos);
  274. }
  275. // Calculate left and right positions of the cell.
  276. if (side === 'right') {
  277. left = chart.chartWidth - axis.right + axis.offset;
  278. right = left + tickWidth;
  279. }
  280. else if (side === 'left') {
  281. right = axis.left + axis.offset;
  282. left = right - tickWidth;
  283. }
  284. else {
  285. left = Math.round(axis.left + axis.translate(reversed ? nextTickPos : tickPos)) - crispCorr;
  286. right = Math.round(axis.left + axis.translate(reversed ? tickPos : nextTickPos)) - crispCorr;
  287. }
  288. tick.slotWidth = right - left;
  289. // Calculate the positioning of the label based on
  290. // alignment.
  291. e.pos.x = (align === 'left' ?
  292. left :
  293. align === 'right' ?
  294. right :
  295. left + ((right - left) / 2) // default to center
  296. );
  297. e.pos.y = (verticalAlign === 'top' ?
  298. top :
  299. verticalAlign === 'bottom' ?
  300. bottom :
  301. top + ((bottom - top) / 2) // default to middle
  302. );
  303. lblMetrics = chart.renderer.fontMetrics(labelOpts.style.fontSize, label.element);
  304. labelHeight = label.getBBox().height;
  305. // Adjustment to y position to align the label correctly.
  306. // Would be better to have a setter or similar for this.
  307. if (!labelOpts.useHTML) {
  308. lines = Math.round(labelHeight / lblMetrics.h);
  309. e.pos.y += (
  310. // Center the label
  311. // TODO: why does this actually center the label?
  312. ((lblMetrics.b - (lblMetrics.h - lblMetrics.f)) / 2) +
  313. // Adjust for height of additional lines.
  314. -(((lines - 1) * lblMetrics.h) / 2));
  315. }
  316. else {
  317. e.pos.y += (
  318. // Readjust yCorr in htmlUpdateTransform
  319. lblMetrics.b +
  320. // Adjust for height of html label
  321. -(labelHeight / 2));
  322. }
  323. e.pos.x += (axis.horiz && labelOpts.x || 0);
  324. }
  325. });
  326. /* eslint-enable no-invalid-this */
  327. /**
  328. * Additions for grid axes.
  329. * @private
  330. * @class
  331. */
  332. var GridAxisAdditions = /** @class */ (function () {
  333. /* *
  334. *
  335. * Constructors
  336. *
  337. * */
  338. function GridAxisAdditions(axis) {
  339. this.axis = axis;
  340. }
  341. /* *
  342. *
  343. * Functions
  344. *
  345. * */
  346. /**
  347. * Checks if an axis is the outer axis in its dimension. Since
  348. * axes are placed outwards in order, the axis with the highest
  349. * index is the outermost axis.
  350. *
  351. * Example: If there are multiple x-axes at the top of the chart,
  352. * this function returns true if the axis supplied is the last
  353. * of the x-axes.
  354. *
  355. * @private
  356. *
  357. * @return {boolean}
  358. * True if the axis is the outermost axis in its dimension; false if
  359. * not.
  360. */
  361. GridAxisAdditions.prototype.isOuterAxis = function () {
  362. var axis = this.axis;
  363. var chart = axis.chart;
  364. var columnIndex = axis.grid.columnIndex;
  365. var columns = (axis.linkedParent && axis.linkedParent.grid.columns ||
  366. axis.grid.columns);
  367. var parentAxis = columnIndex ? axis.linkedParent : axis;
  368. var thisIndex = -1,
  369. lastIndex = 0;
  370. chart[axis.coll].forEach(function (otherAxis, index) {
  371. if (otherAxis.side === axis.side && !otherAxis.options.isInternal) {
  372. lastIndex = index;
  373. if (otherAxis === parentAxis) {
  374. // Get the index of the axis in question
  375. thisIndex = index;
  376. }
  377. }
  378. });
  379. return (lastIndex === thisIndex &&
  380. (isNumber(columnIndex) ? columns.length === columnIndex : true));
  381. };
  382. /**
  383. * Add extra border based on the provided path.
  384. * *
  385. * @private
  386. *
  387. * @param {SVGPath} path
  388. * The path of the border.
  389. *
  390. * @return {Highcharts.SVGElement}
  391. */
  392. GridAxisAdditions.prototype.renderBorder = function (path) {
  393. var axis = this.axis,
  394. renderer = axis.chart.renderer,
  395. options = axis.options,
  396. extraBorderLine = renderer.path(path)
  397. .addClass('highcharts-axis-line')
  398. .add(axis.axisBorder);
  399. if (!renderer.styledMode) {
  400. extraBorderLine.attr({
  401. stroke: options.lineColor,
  402. 'stroke-width': options.lineWidth,
  403. zIndex: 7
  404. });
  405. }
  406. return extraBorderLine;
  407. };
  408. return GridAxisAdditions;
  409. }());
  410. /**
  411. * Axis with grid support.
  412. * @private
  413. * @class
  414. */
  415. var GridAxis = /** @class */ (function () {
  416. function GridAxis() {
  417. }
  418. /* *
  419. *
  420. * Static Functions
  421. *
  422. * */
  423. /* eslint-disable valid-jsdoc */
  424. /**
  425. * Extends axis class with grid support.
  426. * @private
  427. */
  428. GridAxis.compose = function (AxisClass) {
  429. Axis.keepProps.push('grid');
  430. wrap(AxisClass.prototype, 'unsquish', GridAxis.wrapUnsquish);
  431. // Add event handlers
  432. addEvent(AxisClass, 'init', GridAxis.onInit);
  433. addEvent(AxisClass, 'afterGetOffset', GridAxis.onAfterGetOffset);
  434. addEvent(AxisClass, 'afterGetTitlePosition', GridAxis.onAfterGetTitlePosition);
  435. addEvent(AxisClass, 'afterInit', GridAxis.onAfterInit);
  436. addEvent(AxisClass, 'afterRender', GridAxis.onAfterRender);
  437. addEvent(AxisClass, 'afterSetAxisTranslation', GridAxis.onAfterSetAxisTranslation);
  438. addEvent(AxisClass, 'afterSetOptions', GridAxis.onAfterSetOptions);
  439. addEvent(AxisClass, 'afterSetOptions', GridAxis.onAfterSetOptions2);
  440. addEvent(AxisClass, 'afterSetScale', GridAxis.onAfterSetScale);
  441. addEvent(AxisClass, 'afterTickSize', GridAxis.onAfterTickSize);
  442. addEvent(AxisClass, 'trimTicks', GridAxis.onTrimTicks);
  443. addEvent(AxisClass, 'destroy', GridAxis.onDestroy);
  444. };
  445. /**
  446. * Handle columns and getOffset.
  447. * @private
  448. */
  449. GridAxis.onAfterGetOffset = function () {
  450. var grid = this.grid;
  451. (grid && grid.columns || []).forEach(function (column) {
  452. column.getOffset();
  453. });
  454. };
  455. /**
  456. * @private
  457. */
  458. GridAxis.onAfterGetTitlePosition = function (e) {
  459. var axis = this;
  460. var options = axis.options;
  461. var gridOptions = options.grid || {};
  462. if (gridOptions.enabled === true) {
  463. // compute anchor points for each of the title align options
  464. var title = axis.axisTitle,
  465. axisHeight = axis.height,
  466. horiz = axis.horiz,
  467. axisLeft = axis.left,
  468. offset = axis.offset,
  469. opposite = axis.opposite,
  470. _a = axis.options.title,
  471. axisTitleOptions = _a === void 0 ? {} : _a,
  472. axisTop = axis.top,
  473. axisWidth = axis.width;
  474. var tickSize = axis.tickSize();
  475. var titleWidth = title && title.getBBox().width;
  476. var xOption = axisTitleOptions.x || 0;
  477. var yOption = axisTitleOptions.y || 0;
  478. var titleMargin = pick(axisTitleOptions.margin,
  479. horiz ? 5 : 10);
  480. var titleFontSize = axis.chart.renderer.fontMetrics(axisTitleOptions.style &&
  481. axisTitleOptions.style.fontSize,
  482. title).f;
  483. var crispCorr = tickSize ? tickSize[0] / 2 : 0;
  484. // TODO account for alignment
  485. // the position in the perpendicular direction of the axis
  486. var offAxis = ((horiz ? axisTop + axisHeight : axisLeft) +
  487. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  488. (opposite ? -1 : 1) * // so does opposite axes
  489. crispCorr +
  490. (axis.side === GridAxis.Side.bottom ? titleFontSize : 0));
  491. e.titlePosition.x = horiz ?
  492. axisLeft - titleWidth / 2 - titleMargin + xOption :
  493. offAxis + (opposite ? axisWidth : 0) + offset + xOption;
  494. e.titlePosition.y = horiz ?
  495. (offAxis -
  496. (opposite ? axisHeight : 0) +
  497. (opposite ? titleFontSize : -titleFontSize) / 2 +
  498. offset +
  499. yOption) :
  500. axisTop - titleMargin + yOption;
  501. }
  502. };
  503. /**
  504. * @private
  505. */
  506. GridAxis.onAfterInit = function () {
  507. var axis = this;
  508. var chart = axis.chart,
  509. _a = axis.options.grid,
  510. gridOptions = _a === void 0 ? {} : _a,
  511. userOptions = axis.userOptions;
  512. if (gridOptions.enabled) {
  513. applyGridOptions(axis);
  514. /* eslint-disable no-invalid-this */
  515. // TODO: wrap the axis instead
  516. wrap(axis, 'labelFormatter', function (proceed) {
  517. var _a = this,
  518. axis = _a.axis,
  519. value = _a.value;
  520. var tickPos = axis.tickPositions;
  521. var series = (axis.isLinked ?
  522. axis.linkedParent :
  523. axis).series[0];
  524. var isFirst = value === tickPos[0];
  525. var isLast = value === tickPos[tickPos.length - 1];
  526. var point = series && find(series.options.data,
  527. function (p) {
  528. return p[axis.isXAxis ? 'x' : 'y'] === value;
  529. });
  530. var pointCopy;
  531. if (point && series.is('gantt')) {
  532. // For the Gantt set point aliases to the pointCopy
  533. // to do not change the original point
  534. pointCopy = merge(point);
  535. H.seriesTypes.gantt.prototype.setGanttPointAliases(pointCopy);
  536. }
  537. // Make additional properties available for the
  538. // formatter
  539. this.isFirst = isFirst;
  540. this.isLast = isLast;
  541. this.point = pointCopy;
  542. // Call original labelFormatter
  543. return proceed.call(this);
  544. });
  545. /* eslint-enable no-invalid-this */
  546. }
  547. if (gridOptions.columns) {
  548. var columns = axis.grid.columns = [],
  549. columnIndex = axis.grid.columnIndex = 0;
  550. // Handle columns, each column is a grid axis
  551. while (++columnIndex < gridOptions.columns.length) {
  552. var columnOptions = merge(userOptions,
  553. gridOptions.columns[gridOptions.columns.length - columnIndex - 1], {
  554. linkedTo: 0,
  555. // Force to behave like category axis
  556. type: 'category',
  557. // Disable by default the scrollbar on the grid axis
  558. scrollbar: {
  559. enabled: false
  560. }
  561. });
  562. delete columnOptions.grid.columns; // Prevent recursion
  563. var column = new Axis(axis.chart,
  564. columnOptions);
  565. column.grid.isColumn = true;
  566. column.grid.columnIndex = columnIndex;
  567. // Remove column axis from chart axes array, and place it
  568. // in the columns array.
  569. erase(chart.axes, column);
  570. erase(chart[axis.coll], column);
  571. columns.push(column);
  572. }
  573. }
  574. };
  575. /**
  576. * Draw an extra line on the far side of the outermost axis,
  577. * creating floor/roof/wall of a grid. And some padding.
  578. * ```
  579. * Make this:
  580. * (axis.min) __________________________ (axis.max)
  581. * | | | | |
  582. * Into this:
  583. * (axis.min) __________________________ (axis.max)
  584. * ___|____|____|____|____|__
  585. * ```
  586. * @private
  587. */
  588. GridAxis.onAfterRender = function () {
  589. var _a;
  590. var axis = this,
  591. grid = axis.grid,
  592. options = axis.options,
  593. gridOptions = options.grid || {};
  594. if (gridOptions.enabled === true) {
  595. // @todo acutual label padding (top, bottom, left, right)
  596. axis.maxLabelDimensions = axis.getMaxLabelDimensions(axis.ticks, axis.tickPositions);
  597. // Remove right wall before rendering if updating
  598. if (axis.rightWall) {
  599. axis.rightWall.destroy();
  600. }
  601. /*
  602. Draw an extra axis line on outer axes
  603. >
  604. Make this: |______|______|______|___
  605. > _________________________
  606. Into this: |______|______|______|__|
  607. */
  608. if (axis.grid && axis.grid.isOuterAxis() && axis.axisLine) {
  609. var lineWidth = options.lineWidth;
  610. if (lineWidth) {
  611. var linePath = axis.getLinePath(lineWidth),
  612. startPoint = linePath[0],
  613. endPoint = linePath[1],
  614. // Negate distance if top or left axis
  615. // Subtract 1px to draw the line at the end of the tick
  616. tickLength = (axis.tickSize('tick') || [1])[0],
  617. distance = (tickLength - 1) * ((axis.side === GridAxis.Side.top ||
  618. axis.side === GridAxis.Side.left) ? -1 : 1);
  619. // If axis is horizontal, reposition line path vertically
  620. if (startPoint[0] === 'M' && endPoint[0] === 'L') {
  621. if (axis.horiz) {
  622. startPoint[2] += distance;
  623. endPoint[2] += distance;
  624. }
  625. else {
  626. startPoint[1] += distance;
  627. endPoint[1] += distance;
  628. }
  629. }
  630. // If it doesn't exist, add an upper and lower border
  631. // for the vertical grid axis.
  632. if (!axis.horiz && axis.chart.marginRight) {
  633. var upperBorderStartPoint = startPoint, upperBorderEndPoint = ['L', axis.left, startPoint[2]], upperBorderPath = [upperBorderStartPoint, upperBorderEndPoint], lowerBorderEndPoint = ['L', axis.chart.chartWidth - axis.chart.marginRight, axis.toPixels(axis.max + axis.tickmarkOffset)], lowerBorderStartPoint = ['M', endPoint[1], axis.toPixels(axis.max + axis.tickmarkOffset)], lowerBorderPath = [lowerBorderStartPoint, lowerBorderEndPoint];
  634. if (!axis.grid.upperBorder && axis.min % 1 !== 0) {
  635. axis.grid.upperBorder = axis.grid.renderBorder(upperBorderPath);
  636. }
  637. if (axis.grid.upperBorder) {
  638. axis.grid.upperBorder.animate({
  639. d: upperBorderPath
  640. });
  641. }
  642. if (!axis.grid.lowerBorder && axis.max % 1 !== 0) {
  643. axis.grid.lowerBorder = axis.grid.renderBorder(lowerBorderPath);
  644. }
  645. if (axis.grid.lowerBorder) {
  646. axis.grid.lowerBorder.animate({
  647. d: lowerBorderPath
  648. });
  649. }
  650. }
  651. // Render an extra line parallel to the existing axes,
  652. // to close the grid.
  653. if (!axis.grid.axisLineExtra) {
  654. axis.grid.axisLineExtra = axis.grid.renderBorder(linePath);
  655. }
  656. else {
  657. axis.grid.axisLineExtra.animate({
  658. d: linePath
  659. });
  660. }
  661. // show or hide the line depending on
  662. // options.showEmpty
  663. axis.axisLine[axis.showAxis ? 'show' : 'hide'](true);
  664. }
  665. }
  666. (grid && grid.columns || []).forEach(function (column) {
  667. column.render();
  668. });
  669. // Manipulate the tick mark visibility
  670. // based on the axis.max- allows smooth scrolling.
  671. if (!axis.horiz && axis.chart.hasRendered && (axis.scrollbar || ((_a = axis.linkedParent) === null || _a === void 0 ? void 0 : _a.scrollbar))) {
  672. var max = axis.max,
  673. min = axis.min,
  674. tickmarkOffset = axis.tickmarkOffset,
  675. lastTick = axis.tickPositions[axis.tickPositions.length - 1],
  676. firstTick = axis.tickPositions[0];
  677. // Hide/show firts tick label.
  678. if (min - firstTick > tickmarkOffset) {
  679. axis.ticks[firstTick].label.hide();
  680. }
  681. else {
  682. axis.ticks[firstTick].label.show();
  683. }
  684. // Hide/show last tick mark/label.
  685. if (lastTick - max > tickmarkOffset) {
  686. axis.ticks[lastTick].label.hide();
  687. }
  688. else {
  689. axis.ticks[lastTick].label.show();
  690. }
  691. if (lastTick - max < tickmarkOffset && lastTick - max > 0 && axis.ticks[lastTick].isLast) {
  692. axis.ticks[lastTick].mark.hide();
  693. }
  694. else {
  695. axis.ticks[lastTick - 1].mark.show();
  696. }
  697. }
  698. }
  699. };
  700. /**
  701. * @private
  702. */
  703. GridAxis.onAfterSetAxisTranslation = function () {
  704. var _a;
  705. var axis = this;
  706. var tickInfo = axis.tickPositions && axis.tickPositions.info;
  707. var options = axis.options;
  708. var gridOptions = options.grid || {};
  709. var userLabels = axis.userOptions.labels || {};
  710. if (axis.horiz) {
  711. if (gridOptions.enabled === true) {
  712. axis.series.forEach(function (series) {
  713. series.options.pointRange = 0;
  714. });
  715. }
  716. // Lower level time ticks, like hours or minutes, represent
  717. // points in time and not ranges. These should be aligned
  718. // left in the grid cell by default. The same applies to
  719. // years of higher order.
  720. if (tickInfo &&
  721. options.dateTimeLabelFormats &&
  722. options.labels &&
  723. !defined(userLabels.align) &&
  724. (options.dateTimeLabelFormats[tickInfo.unitName].range === false ||
  725. tickInfo.count > 1 // years
  726. )) {
  727. options.labels.align = 'left';
  728. if (!defined(userLabels.x)) {
  729. options.labels.x = 3;
  730. }
  731. }
  732. }
  733. else {
  734. // Don't trim ticks which not in min/max range but
  735. // they are still in the min/max plus tickInterval.
  736. if (this.options.type !== 'treegrid' && ((_a = axis.grid) === null || _a === void 0 ? void 0 : _a.columns)) {
  737. this.minPointOffset = this.tickInterval;
  738. }
  739. }
  740. };
  741. /**
  742. * Creates a left and right wall on horizontal axes:
  743. * - Places leftmost tick at the start of the axis, to create a left
  744. * wall
  745. * - Ensures that the rightmost tick is at the end of the axis, to
  746. * create a right wall.
  747. * @private
  748. */
  749. GridAxis.onAfterSetOptions = function (e) {
  750. var options = this.options,
  751. userOptions = e.userOptions,
  752. gridAxisOptions,
  753. gridOptions = ((options && isObject(options.grid)) ? options.grid : {});
  754. if (gridOptions.enabled === true) {
  755. // Merge the user options into default grid axis options so
  756. // that when a user option is set, it takes presedence.
  757. gridAxisOptions = merge(true, {
  758. className: ('highcharts-grid-axis ' + (userOptions.className || '')),
  759. dateTimeLabelFormats: {
  760. hour: {
  761. list: ['%H:%M', '%H']
  762. },
  763. day: {
  764. list: ['%A, %e. %B', '%a, %e. %b', '%E']
  765. },
  766. week: {
  767. list: ['Week %W', 'W%W']
  768. },
  769. month: {
  770. list: ['%B', '%b', '%o']
  771. }
  772. },
  773. grid: {
  774. borderWidth: 1
  775. },
  776. labels: {
  777. padding: 2,
  778. style: {
  779. fontSize: '13px'
  780. }
  781. },
  782. margin: 0,
  783. title: {
  784. text: null,
  785. reserveSpace: false,
  786. rotation: 0
  787. },
  788. // In a grid axis, only allow one unit of certain types,
  789. // for example we shouln't have one grid cell spanning
  790. // two days.
  791. units: [[
  792. 'millisecond',
  793. [1, 10, 100]
  794. ], [
  795. 'second',
  796. [1, 10]
  797. ], [
  798. 'minute',
  799. [1, 5, 15]
  800. ], [
  801. 'hour',
  802. [1, 6]
  803. ], [
  804. 'day',
  805. [1]
  806. ], [
  807. 'week',
  808. [1]
  809. ], [
  810. 'month',
  811. [1]
  812. ], [
  813. 'year',
  814. null
  815. ]]
  816. }, userOptions);
  817. // X-axis specific options
  818. if (this.coll === 'xAxis') {
  819. // For linked axes, tickPixelInterval is used only if
  820. // the tickPositioner below doesn't run or returns
  821. // undefined (like multiple years)
  822. if (defined(userOptions.linkedTo) &&
  823. !defined(userOptions.tickPixelInterval)) {
  824. gridAxisOptions.tickPixelInterval = 350;
  825. }
  826. // For the secondary grid axis, use the primary axis'
  827. // tick intervals and return ticks one level higher.
  828. if (
  829. // Check for tick pixel interval in options
  830. !defined(userOptions.tickPixelInterval) &&
  831. // Only for linked axes
  832. defined(userOptions.linkedTo) &&
  833. !defined(userOptions.tickPositioner) &&
  834. !defined(userOptions.tickInterval)) {
  835. gridAxisOptions.tickPositioner = function (min, max) {
  836. var parentInfo = (this.linkedParent &&
  837. this.linkedParent.tickPositions &&
  838. this.linkedParent.tickPositions.info);
  839. if (parentInfo) {
  840. var unitIdx,
  841. count,
  842. unitName,
  843. i,
  844. units = gridAxisOptions.units,
  845. unitRange;
  846. for (i = 0; i < units.length; i++) {
  847. if (units[i][0] ===
  848. parentInfo.unitName) {
  849. unitIdx = i;
  850. break;
  851. }
  852. }
  853. // Get the first allowed count on the next
  854. // unit.
  855. if (units[unitIdx + 1]) {
  856. unitName = units[unitIdx + 1][0];
  857. count =
  858. (units[unitIdx + 1][1] || [1])[0];
  859. // In case the base X axis shows years, make
  860. // the secondary axis show ten times the
  861. // years (#11427)
  862. }
  863. else if (parentInfo.unitName === 'year') {
  864. unitName = 'year';
  865. count = parentInfo.count * 10;
  866. }
  867. unitRange = timeUnits[unitName];
  868. this.tickInterval = unitRange * count;
  869. return this.getTimeTicks({
  870. unitRange: unitRange,
  871. count: count,
  872. unitName: unitName
  873. }, min, max, this.options.startOfWeek);
  874. }
  875. };
  876. }
  877. }
  878. // Now merge the combined options into the axis options
  879. merge(true, this.options, gridAxisOptions);
  880. if (this.horiz) {
  881. /* _________________________
  882. Make this: ___|_____|_____|_____|__|
  883. ^ ^
  884. _________________________
  885. Into this: |_____|_____|_____|_____|
  886. ^ ^ */
  887. options.minPadding = pick(userOptions.minPadding, 0);
  888. options.maxPadding = pick(userOptions.maxPadding, 0);
  889. }
  890. // If borderWidth is set, then use its value for tick and
  891. // line width.
  892. if (isNumber(options.grid.borderWidth)) {
  893. options.tickWidth = options.lineWidth = gridOptions.borderWidth;
  894. }
  895. }
  896. };
  897. /**
  898. * @private
  899. */
  900. GridAxis.onAfterSetOptions2 = function (e) {
  901. var axis = this;
  902. var userOptions = e.userOptions;
  903. var gridOptions = userOptions && userOptions.grid || {};
  904. var columns = gridOptions.columns;
  905. // Add column options to the parent axis. Children has their column
  906. // options set on init in onGridAxisAfterInit.
  907. if (gridOptions.enabled && columns) {
  908. merge(true, axis.options, columns[columns.length - 1]);
  909. }
  910. };
  911. /**
  912. * Handle columns and setScale.
  913. * @private
  914. */
  915. GridAxis.onAfterSetScale = function () {
  916. var axis = this;
  917. (axis.grid.columns || []).forEach(function (column) {
  918. column.setScale();
  919. });
  920. };
  921. /**
  922. * Draw vertical axis ticks extra long to create cell floors and roofs.
  923. * Overrides the tickLength for vertical axes.
  924. * @private
  925. */
  926. GridAxis.onAfterTickSize = function (e) {
  927. var defaultLeftAxisOptions = Axis.defaultLeftAxisOptions;
  928. var _a = this,
  929. horiz = _a.horiz,
  930. maxLabelDimensions = _a.maxLabelDimensions,
  931. _b = _a.options.grid,
  932. gridOptions = _b === void 0 ? {} : _b;
  933. if (gridOptions.enabled && maxLabelDimensions) {
  934. var labelPadding = (Math.abs(defaultLeftAxisOptions.labels.x) * 2);
  935. var distance = horiz ?
  936. gridOptions.cellHeight || labelPadding + maxLabelDimensions.height :
  937. labelPadding + maxLabelDimensions.width;
  938. if (isArray(e.tickSize)) {
  939. e.tickSize[0] = distance;
  940. }
  941. else {
  942. e.tickSize = [distance, 0];
  943. }
  944. }
  945. };
  946. /**
  947. * @private
  948. */
  949. GridAxis.onDestroy = function (e) {
  950. var grid = this.grid;
  951. (grid.columns || []).forEach(function (column) {
  952. column.destroy(e.keepEvents);
  953. });
  954. grid.columns = void 0;
  955. };
  956. /**
  957. * Wraps axis init to draw cell walls on vertical axes.
  958. * @private
  959. */
  960. GridAxis.onInit = function (e) {
  961. var axis = this;
  962. var userOptions = e.userOptions || {};
  963. var gridOptions = userOptions.grid || {};
  964. if (gridOptions.enabled && defined(gridOptions.borderColor)) {
  965. userOptions.tickColor = userOptions.lineColor = gridOptions.borderColor;
  966. }
  967. if (!axis.grid) {
  968. axis.grid = new GridAxisAdditions(axis);
  969. }
  970. };
  971. /**
  972. * Makes tick labels which are usually ignored in a linked axis
  973. * displayed if they are within range of linkedParent.min.
  974. * ```
  975. * _____________________________
  976. * | | | | |
  977. * Make this: | | 2 | 3 | 4 |
  978. * |___|_______|_______|_______|
  979. * ^
  980. * _____________________________
  981. * | | | | |
  982. * Into this: | 1 | 2 | 3 | 4 |
  983. * |___|_______|_______|_______|
  984. * ^
  985. * ```
  986. * @private
  987. * @todo Does this function do what the drawing says? Seems to affect
  988. * ticks and not the labels directly?
  989. */
  990. GridAxis.onTrimTicks = function () {
  991. var axis = this;
  992. var options = axis.options;
  993. var gridOptions = options.grid || {};
  994. var categoryAxis = axis.categories;
  995. var tickPositions = axis.tickPositions;
  996. var firstPos = tickPositions[0];
  997. var lastPos = tickPositions[tickPositions.length - 1];
  998. var linkedMin = axis.linkedParent && axis.linkedParent.min;
  999. var linkedMax = axis.linkedParent && axis.linkedParent.max;
  1000. var min = linkedMin || axis.min;
  1001. var max = linkedMax || axis.max;
  1002. var tickInterval = axis.tickInterval;
  1003. var endMoreThanMin = (firstPos < min &&
  1004. firstPos + tickInterval > min);
  1005. var startLessThanMax = (lastPos > max &&
  1006. lastPos - tickInterval < max);
  1007. if (gridOptions.enabled === true &&
  1008. !categoryAxis &&
  1009. (axis.horiz || axis.isLinked)) {
  1010. if (endMoreThanMin && !options.startOnTick) {
  1011. tickPositions[0] = min;
  1012. }
  1013. if (startLessThanMax && !options.endOnTick) {
  1014. tickPositions[tickPositions.length - 1] = max;
  1015. }
  1016. }
  1017. };
  1018. /**
  1019. * Avoid altering tickInterval when reserving space.
  1020. * @private
  1021. */
  1022. GridAxis.wrapUnsquish = function (proceed) {
  1023. var axis = this;
  1024. var _a = axis.options.grid,
  1025. gridOptions = _a === void 0 ? {} : _a;
  1026. if (gridOptions.enabled === true && axis.categories) {
  1027. return axis.tickInterval;
  1028. }
  1029. return proceed.apply(axis, argsToArray(arguments));
  1030. };
  1031. return GridAxis;
  1032. }());
  1033. (function (GridAxis) {
  1034. /**
  1035. * Enum for which side the axis is on. Maps to axis.side.
  1036. * @private
  1037. */
  1038. var Side;
  1039. (function (Side) {
  1040. Side[Side["top"] = 0] = "top";
  1041. Side[Side["right"] = 1] = "right";
  1042. Side[Side["bottom"] = 2] = "bottom";
  1043. Side[Side["left"] = 3] = "left";
  1044. })(Side = GridAxis.Side || (GridAxis.Side = {}));
  1045. })(GridAxis || (GridAxis = {}));
  1046. GridAxis.compose(Axis);
  1047. return GridAxis;
  1048. });
  1049. _registerModule(_modules, 'masters/modules/grid-axis.src.js', [], function () {
  1050. });
  1051. }));