CurrentDateIndication.js 3.1 KB

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