GanttChart.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Chart from './Chart.js';
  14. import H from '../Globals.js';
  15. import U from '../Utilities.js';
  16. var getOptions = U.getOptions, isArray = U.isArray, merge = U.merge, splat = U.splat;
  17. import '../../Series/GanttSeries.js';
  18. /**
  19. * Factory function for Gantt charts.
  20. *
  21. * @example
  22. * // Render a chart in to div#container
  23. * var chart = Highcharts.ganttChart('container', {
  24. * title: {
  25. * text: 'My chart'
  26. * },
  27. * series: [{
  28. * data: ...
  29. * }]
  30. * });
  31. *
  32. * @function Highcharts.ganttChart
  33. *
  34. * @param {string|Highcharts.HTMLDOMElement} renderTo
  35. * The DOM element to render to, or its id.
  36. *
  37. * @param {Highcharts.Options} options
  38. * The chart options structure.
  39. *
  40. * @param {Highcharts.ChartCallbackFunction} [callback]
  41. * Function to run when the chart has loaded and and all external images
  42. * are loaded. Defining a
  43. * [chart.events.load](https://api.highcharts.com/highcharts/chart.events.load)
  44. * handler is equivalent.
  45. *
  46. * @return {Highcharts.Chart}
  47. * Returns the Chart object.
  48. */
  49. H.ganttChart = function (renderTo, options, callback) {
  50. var hasRenderToArg = typeof renderTo === 'string' || renderTo.nodeName, seriesOptions = options.series, defaultOptions = getOptions(), defaultLinkedTo, userOptions = options;
  51. options = arguments[hasRenderToArg ? 1 : 0];
  52. // If user hasn't defined axes as array, make it into an array and add a
  53. // second axis by default.
  54. if (!isArray(options.xAxis)) {
  55. options.xAxis = [options.xAxis || {}, {}];
  56. }
  57. // apply X axis options to both single and multi x axes
  58. options.xAxis = options.xAxis.map(function (xAxisOptions, i) {
  59. if (i === 1) { // Second xAxis
  60. defaultLinkedTo = 0;
  61. }
  62. return merge(defaultOptions.xAxis, {
  63. grid: {
  64. enabled: true
  65. },
  66. opposite: true,
  67. linkedTo: defaultLinkedTo
  68. }, xAxisOptions, // user options
  69. {
  70. type: 'datetime'
  71. });
  72. });
  73. // apply Y axis options to both single and multi y axes
  74. options.yAxis = (splat(options.yAxis || {})).map(function (yAxisOptions) {
  75. return merge(defaultOptions.yAxis, // #3802
  76. {
  77. grid: {
  78. enabled: true
  79. },
  80. staticScale: 50,
  81. reversed: true,
  82. // Set default type treegrid, but only if 'categories' is
  83. // undefined
  84. type: yAxisOptions.categories ? yAxisOptions.type : 'treegrid'
  85. }, yAxisOptions // user options
  86. );
  87. });
  88. options.series = null;
  89. options = merge(true, {
  90. chart: {
  91. type: 'gantt'
  92. },
  93. title: {
  94. text: null
  95. },
  96. legend: {
  97. enabled: false
  98. },
  99. navigator: {
  100. series: { type: 'gantt' }
  101. }
  102. }, options, // user's options
  103. // forced options
  104. {
  105. isGantt: true
  106. });
  107. options.series = userOptions.series = seriesOptions;
  108. (options.series || []).forEach(function (series) {
  109. if (series.data) {
  110. series.data.forEach(function (point) {
  111. H.seriesTypes.gantt.prototype.setGanttPointAliases(point);
  112. });
  113. }
  114. });
  115. return hasRenderToArg ?
  116. new Chart(renderTo, options, callback) :
  117. new Chart(options, options); // @todo does not look correct
  118. };