VariwideComposition.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* *
  2. *
  3. * Highcharts variwide module
  4. *
  5. * (c) 2010-2021 Torstein Honsi
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import Tick from '../../Core/Axis/Tick.js';
  14. import Axis from '../../Core/Axis/Axis.js';
  15. import U from '../../Core/Utilities.js';
  16. var addEvent = U.addEvent, wrap = U.wrap;
  17. /* *
  18. *
  19. * Composition
  20. *
  21. * */
  22. Tick.prototype.postTranslate = function (xy, xOrY, index) {
  23. var axis = this.axis, pos = xy[xOrY] - axis.pos;
  24. if (!axis.horiz) {
  25. pos = axis.len - pos;
  26. }
  27. pos = axis.series[0].postTranslate(index, pos);
  28. if (!axis.horiz) {
  29. pos = axis.len - pos;
  30. }
  31. xy[xOrY] = axis.pos + pos;
  32. };
  33. /* eslint-disable no-invalid-this */
  34. // Same width as the category (#8083)
  35. addEvent(Axis, 'afterDrawCrosshair', function (e) {
  36. if (this.variwide && this.cross) {
  37. this.cross.attr('stroke-width', (e.point && e.point.crosshairWidth));
  38. }
  39. });
  40. // On a vertical axis, apply anti-collision logic to the labels.
  41. addEvent(Axis, 'afterRender', function () {
  42. var axis = this;
  43. if (!this.horiz && this.variwide) {
  44. this.chart.labelCollectors.push(function () {
  45. return axis.tickPositions
  46. .filter(function (pos) {
  47. return axis.ticks[pos].label;
  48. })
  49. .map(function (pos, i) {
  50. var label = axis.ticks[pos].label;
  51. label.labelrank = axis.zData[i];
  52. return label;
  53. });
  54. });
  55. }
  56. });
  57. addEvent(Tick, 'afterGetPosition', function (e) {
  58. var axis = this.axis, xOrY = axis.horiz ? 'x' : 'y';
  59. if (axis.variwide) {
  60. this[xOrY + 'Orig'] = e.pos[xOrY];
  61. this.postTranslate(e.pos, xOrY, this.pos);
  62. }
  63. });
  64. wrap(Tick.prototype, 'getLabelPosition', function (proceed, x, y, label, horiz, labelOptions, tickmarkOffset, index) {
  65. var args = Array.prototype.slice.call(arguments, 1), xy, xOrY = horiz ? 'x' : 'y';
  66. // Replace the x with the original x
  67. if (this.axis.variwide &&
  68. typeof this[xOrY + 'Orig'] === 'number') {
  69. args[horiz ? 0 : 1] = this[xOrY + 'Orig'];
  70. }
  71. xy = proceed.apply(this, args);
  72. // Post-translate
  73. if (this.axis.variwide && this.axis.categories) {
  74. this.postTranslate(xy, xOrY, this.pos);
  75. }
  76. return xy;
  77. });