CurrentDateIndication.js 3.1 KB

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