CurrentDateIndication.js 3.0 KB

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