GanttChart.js 3.5 KB

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