NoDataToDisplay.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* *
  2. *
  3. * Plugin for displaying a message when there is no data visible in chart.
  4. *
  5. * (c) 2010-2020 Highsoft AS
  6. *
  7. * Author: Oystein Moseng
  8. *
  9. * License: www.highcharts.com/license
  10. *
  11. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  12. *
  13. * */
  14. import Chart from '../Core/Chart/Chart.js';
  15. import U from '../Core/Utilities.js';
  16. var addEvent = U.addEvent, extend = U.extend, getOptions = U.getOptions, merge = U.merge;
  17. var chartPrototype = Chart.prototype, defaultOptions = getOptions();
  18. // Add language option
  19. extend(defaultOptions.lang,
  20. /**
  21. * @optionparent lang
  22. */
  23. {
  24. /**
  25. * The text to display when the chart contains no data.
  26. *
  27. * @see [noData](#noData)
  28. *
  29. * @sample highcharts/no-data-to-display/no-data-line
  30. * No-data text
  31. *
  32. * @since 3.0.8
  33. * @product highcharts highstock
  34. * @requires modules/no-data-to-display
  35. */
  36. noData: 'No data to display'
  37. });
  38. // Add default display options for message
  39. /**
  40. * Options for displaying a message like "No data to display".
  41. * This feature requires the file no-data-to-display.js to be loaded in the
  42. * page. The actual text to display is set in the lang.noData option.
  43. *
  44. * @sample highcharts/no-data-to-display/no-data-line
  45. * Line chart with no-data module
  46. * @sample highcharts/no-data-to-display/no-data-pie
  47. * Pie chart with no-data module
  48. *
  49. * @product highcharts highstock gantt
  50. * @requires modules/no-data-to-display
  51. * @optionparent noData
  52. */
  53. defaultOptions.noData = {
  54. /**
  55. * An object of additional SVG attributes for the no-data label.
  56. *
  57. * @type {Highcharts.SVGAttributes}
  58. * @since 3.0.8
  59. * @product highcharts highstock gantt
  60. * @apioption noData.attr
  61. */
  62. attr: {
  63. zIndex: 1
  64. },
  65. /**
  66. * Whether to insert the label as HTML, or as pseudo-HTML rendered with
  67. * SVG.
  68. *
  69. * @type {boolean}
  70. * @default false
  71. * @since 4.1.10
  72. * @product highcharts highstock gantt
  73. * @apioption noData.useHTML
  74. */
  75. /**
  76. * The position of the no-data label, relative to the plot area.
  77. *
  78. * @type {Highcharts.AlignObject}
  79. * @since 3.0.8
  80. */
  81. position: {
  82. /**
  83. * Horizontal offset of the label, in pixels.
  84. */
  85. x: 0,
  86. /**
  87. * Vertical offset of the label, in pixels.
  88. */
  89. y: 0,
  90. /**
  91. * Horizontal alignment of the label.
  92. *
  93. * @type {Highcharts.AlignValue}
  94. */
  95. align: 'center',
  96. /**
  97. * Vertical alignment of the label.
  98. *
  99. * @type {Highcharts.VerticalAlignValue}
  100. */
  101. verticalAlign: 'middle'
  102. },
  103. /**
  104. * CSS styles for the no-data label.
  105. *
  106. * @sample highcharts/no-data-to-display/no-data-line
  107. * Styled no-data text
  108. *
  109. * @type {Highcharts.CSSObject}
  110. */
  111. style: {
  112. /** @ignore */
  113. fontWeight: 'bold',
  114. /** @ignore */
  115. fontSize: '12px',
  116. /** @ignore */
  117. color: '#666666'
  118. }
  119. };
  120. /**
  121. * Display a no-data message.
  122. * @private
  123. * @function Highcharts.Chart#showNoData
  124. * @param {string} [str]
  125. * An optional message to show in place of the default one
  126. * @return {void}
  127. * @requires modules/no-data-to-display
  128. */
  129. chartPrototype.showNoData = function (str) {
  130. var chart = this, options = chart.options, text = str || (options && options.lang.noData), noDataOptions = options && (options.noData || {});
  131. if (chart.renderer) { // Meaning chart is not destroyed
  132. if (!chart.noDataLabel) {
  133. chart.noDataLabel = chart.renderer
  134. .label(text, 0, 0, void 0, void 0, void 0, noDataOptions.useHTML, void 0, 'no-data')
  135. .add();
  136. }
  137. if (!chart.styledMode) {
  138. chart.noDataLabel
  139. .attr(noDataOptions.attr)
  140. .css(noDataOptions.style || {});
  141. }
  142. chart.noDataLabel.align(extend(chart.noDataLabel.getBBox(), noDataOptions.position || {}), false, 'plotBox');
  143. }
  144. };
  145. /**
  146. * Hide no-data message.
  147. *
  148. * @private
  149. * @function Highcharts.Chart#hideNoData
  150. * @return {void}
  151. * @requires modules/no-data-to-display
  152. */
  153. chartPrototype.hideNoData = function () {
  154. var chart = this;
  155. if (chart.noDataLabel) {
  156. chart.noDataLabel = chart.noDataLabel.destroy();
  157. }
  158. };
  159. /**
  160. * Returns true if there are data points within the plot area now.
  161. *
  162. * @private
  163. * @function Highcharts.Chart#hasData
  164. * @return {boolean|undefined}
  165. * True, if there are data points.
  166. * @requires modules/no-data-to-display
  167. */
  168. chartPrototype.hasData = function () {
  169. var chart = this, series = chart.series || [], i = series.length;
  170. while (i--) {
  171. if (series[i].hasData() && !series[i].options.isInternal) {
  172. return true;
  173. }
  174. }
  175. return chart.loadingShown; // #4588
  176. };
  177. /* eslint-disable no-invalid-this */
  178. // Add event listener to handle automatic show or hide no-data message.
  179. addEvent(Chart, 'render', function handleNoData() {
  180. if (this.hasData()) {
  181. this.hideNoData();
  182. }
  183. else {
  184. this.showNoData();
  185. }
  186. });