CurrentDateIndication.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* *
  2. *
  3. * (c) 2016-2021 Highsoft AS
  4. *
  5. * Author: Lars A. V. Cabrera
  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 Axis from '../Core/Axis/Axis.js';
  14. import Palette from '../Core/Color/Palette.js';
  15. import PlotLineOrBand from '../Core/Axis/PlotLineOrBand.js';
  16. import U from '../Core/Utilities.js';
  17. var addEvent = U.addEvent, merge = U.merge, wrap = U.wrap;
  18. /**
  19. * Show an indicator on the axis for the current date and time. Can be a
  20. * boolean or a configuration object similar to
  21. * [xAxis.plotLines](#xAxis.plotLines).
  22. *
  23. * @sample gantt/current-date-indicator/demo
  24. * Current date indicator enabled
  25. * @sample gantt/current-date-indicator/object-config
  26. * Current date indicator with custom options
  27. *
  28. * @declare Highcharts.CurrentDateIndicatorOptions
  29. * @type {boolean|CurrentDateIndicatorOptions}
  30. * @default true
  31. * @extends xAxis.plotLines
  32. * @excluding value
  33. * @product gantt
  34. * @apioption xAxis.currentDateIndicator
  35. */
  36. var defaultOptions = {
  37. color: Palette.highlightColor20,
  38. width: 2,
  39. /**
  40. * @declare Highcharts.AxisCurrentDateIndicatorLabelOptions
  41. */
  42. label: {
  43. /**
  44. * Format of the label. This options is passed as the fist argument to
  45. * [dateFormat](/class-reference/Highcharts#.dateFormat) function.
  46. *
  47. * @type {string}
  48. * @default %a, %b %d %Y, %H:%M
  49. * @product gantt
  50. * @apioption xAxis.currentDateIndicator.label.format
  51. */
  52. format: '%a, %b %d %Y, %H:%M',
  53. formatter: function (value, format) {
  54. return this.axis.chart.time.dateFormat(format || '', value);
  55. },
  56. rotation: 0,
  57. /**
  58. * @type {Highcharts.CSSObject}
  59. */
  60. style: {
  61. /** @internal */
  62. fontSize: '10px'
  63. }
  64. }
  65. };
  66. /* eslint-disable no-invalid-this */
  67. addEvent(Axis, 'afterSetOptions', function () {
  68. var options = this.options, cdiOptions = options.currentDateIndicator;
  69. if (cdiOptions) {
  70. var plotLineOptions = typeof cdiOptions === 'object' ?
  71. merge(defaultOptions, cdiOptions) :
  72. merge(defaultOptions);
  73. plotLineOptions.value = Date.now();
  74. plotLineOptions.className = 'highcharts-current-date-indicator';
  75. if (!options.plotLines) {
  76. options.plotLines = [];
  77. }
  78. options.plotLines.push(plotLineOptions);
  79. }
  80. });
  81. addEvent(PlotLineOrBand, 'render', function () {
  82. // If the label already exists, update its text
  83. if (this.label) {
  84. this.label.attr({
  85. text: this.getLabelText(this.options.label)
  86. });
  87. }
  88. });
  89. wrap(PlotLineOrBand.prototype, 'getLabelText', function (defaultMethod, defaultLabelOptions) {
  90. var options = this.options;
  91. if (options &&
  92. options.className &&
  93. options.className.indexOf('highcharts-current-date-indicator') !== -1 &&
  94. options.label &&
  95. typeof options.label.formatter === 'function') {
  96. options.value = Date.now();
  97. return options.label.formatter
  98. .call(this, options.value, options.label.format);
  99. }
  100. return defaultMethod.call(this, defaultLabelOptions);
  101. });