CurrentDateIndicator.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* *
  2. *
  3. * (c) 2016-2019 Highsoft AS
  4. *
  5. * Author: Lars A. V. Cabrera
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * */
  10. 'use strict';
  11. import H from '../parts/Globals.js';
  12. var addEvent = H.addEvent,
  13. Axis = H.Axis,
  14. PlotLineOrBand = H.PlotLineOrBand,
  15. merge = H.merge;
  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. * @type {boolean|*}
  28. * @default true
  29. * @extends xAxis.plotLines
  30. * @excluding value
  31. * @product gantt
  32. * @apioption xAxis.currentDateIndicator
  33. */
  34. currentDateIndicator: true,
  35. color: '#ccd6eb',
  36. width: 2,
  37. label: {
  38. format: '%a, %b %d %Y, %H:%M',
  39. formatter: undefined,
  40. rotation: 0,
  41. style: {
  42. fontSize: '10px'
  43. }
  44. }
  45. };
  46. addEvent(Axis, 'afterSetOptions', function () {
  47. var options = this.options,
  48. cdiOptions = options.currentDateIndicator;
  49. if (cdiOptions) {
  50. if (typeof cdiOptions === 'object') {
  51. // Ignore formatter if custom format is defined
  52. if (cdiOptions.label && cdiOptions.label.format) {
  53. cdiOptions.label.formatter = undefined;
  54. }
  55. cdiOptions = merge(defaultConfig, cdiOptions);
  56. } else {
  57. cdiOptions = merge(defaultConfig);
  58. }
  59. cdiOptions.value = new Date();
  60. if (!options.plotLines) {
  61. options.plotLines = [];
  62. }
  63. options.plotLines.push(cdiOptions);
  64. }
  65. });
  66. addEvent(PlotLineOrBand, 'render', function () {
  67. var options = this.options,
  68. format,
  69. formatter;
  70. if (options.currentDateIndicator && options.label) {
  71. format = options.label.format;
  72. formatter = options.label.formatter;
  73. options.value = new Date();
  74. if (typeof formatter === 'function') {
  75. options.label.text = formatter(this);
  76. } else {
  77. options.label.text = H.dateFormat(format, new Date());
  78. }
  79. // If the label already exists, update its text
  80. if (this.label) {
  81. this.label.attr({
  82. text: options.label.text
  83. });
  84. }
  85. }
  86. });