no-data-to-display.src.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @license Highcharts JS v6.1.0 (2018-04-13)
  3. * Plugin for displaying a message when there is no data visible in chart.
  4. *
  5. * (c) 2010-2017 Highsoft AS
  6. * Author: Oystein Moseng
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. module.exports = factory;
  14. } else {
  15. factory(Highcharts);
  16. }
  17. }(function (Highcharts) {
  18. (function (H) {
  19. /**
  20. * Plugin for displaying a message when there is no data visible in chart.
  21. *
  22. * (c) 2010-2017 Highsoft AS
  23. * Author: Oystein Moseng
  24. *
  25. * License: www.highcharts.com/license
  26. */
  27. var seriesTypes = H.seriesTypes,
  28. chartPrototype = H.Chart.prototype,
  29. defaultOptions = H.getOptions(),
  30. extend = H.extend,
  31. each = H.each;
  32. // Add language option
  33. extend(defaultOptions.lang, {
  34. /**
  35. * The text to display when the chart contains no data. Requires the
  36. * no-data module, see [noData](#noData).
  37. *
  38. * @type {String}
  39. * @default No data to display
  40. * @since 3.0.8
  41. * @product highcharts highstock
  42. * @sample highcharts/no-data-to-display/no-data-line
  43. * No-data text
  44. * @apioption lang.noData
  45. */
  46. noData: 'No data to display'
  47. });
  48. // Add default display options for message
  49. /**
  50. * Options for displaying a message like "No data to display".
  51. * This feature requires the file no-data-to-display.js to be loaded in the
  52. * page. The actual text to display is set in the lang.noData option.
  53. * @type {Object}
  54. *
  55. * @sample highcharts/no-data-to-display/no-data-line
  56. * Line chart with no-data module
  57. * @sample highcharts/no-data-to-display/no-data-pie
  58. * Pie chart with no-data module
  59. * @optionparent noData
  60. */
  61. defaultOptions.noData = {
  62. /**
  63. * An object of additional SVG attributes for the no-data label.
  64. *
  65. * @type {Object}
  66. * @since 3.0.8
  67. * @product highcharts highstock
  68. * @apioption noData.attr
  69. */
  70. /**
  71. * Whether to insert the label as HTML, or as pseudo-HTML rendered with
  72. * SVG.
  73. *
  74. * @type {Boolean}
  75. * @default false
  76. * @since 4.1.10
  77. * @product highcharts highstock
  78. * @apioption noData.useHTML
  79. */
  80. /**
  81. * The position of the no-data label, relative to the plot area.
  82. *
  83. * @type {Object}
  84. * @default { "x": 0, "y": 0, "align": "center", "verticalAlign": "middle" }
  85. * @since 3.0.8
  86. */
  87. position: {
  88. /**
  89. * Horizontal offset of the label, in pixels.
  90. *
  91. * @type {Number}
  92. * @default 0
  93. * @product highcharts highstock
  94. */
  95. x: 0,
  96. /**
  97. * Vertical offset of the label, in pixels.
  98. *
  99. * @type {Number}
  100. * @default 0
  101. * @product highcharts highstock
  102. */
  103. y: 0,
  104. /**
  105. * Horizontal alignment of the label.
  106. *
  107. * @validvalue ["left", "center", "right"]
  108. * @type {String}
  109. * @default center
  110. */
  111. align: 'center',
  112. /**
  113. * Vertical alignment of the label.
  114. *
  115. * @validvalue ["top", "middle", "bottom"]
  116. * @type {String}
  117. * @default middle
  118. * @product highcharts highstock
  119. */
  120. verticalAlign: 'middle'
  121. }
  122. };
  123. // Presentational
  124. /**
  125. * CSS styles for the no-data label.
  126. *
  127. * @sample highcharts/no-data-to-display/no-data-line
  128. * Styled no-data text
  129. * @optionparent noData.style
  130. */
  131. defaultOptions.noData.style = {
  132. fontWeight: 'bold',
  133. fontSize: '12px',
  134. color: '#666666'
  135. };
  136. // Define hasData function for non-cartesian seris. Returns true if the series
  137. // has points at all.
  138. each([
  139. 'bubble',
  140. 'gauge',
  141. 'heatmap',
  142. 'pie',
  143. 'sankey',
  144. 'treemap',
  145. 'waterfall'
  146. ], function (type) {
  147. if (seriesTypes[type]) {
  148. seriesTypes[type].prototype.hasData = function () {
  149. return !!this.points.length; // != 0
  150. };
  151. }
  152. });
  153. /**
  154. * Define hasData functions for series. These return true if there are data
  155. * points on this series within the plot area.
  156. */
  157. H.Series.prototype.hasData = function () {
  158. return (
  159. this.visible &&
  160. this.dataMax !== undefined &&
  161. this.dataMin !== undefined // #3703
  162. );
  163. };
  164. /**
  165. * Display a no-data message.
  166. *
  167. * @param {String} str An optional message to show in place of the default one
  168. */
  169. chartPrototype.showNoData = function (str) {
  170. var chart = this,
  171. options = chart.options,
  172. text = str || (options && options.lang.noData),
  173. noDataOptions = options && options.noData;
  174. if (!chart.noDataLabel && chart.renderer) {
  175. chart.noDataLabel = chart.renderer
  176. .label(
  177. text,
  178. 0,
  179. 0,
  180. null,
  181. null,
  182. null,
  183. noDataOptions.useHTML,
  184. null,
  185. 'no-data'
  186. );
  187. chart.noDataLabel
  188. .attr(noDataOptions.attr)
  189. .css(noDataOptions.style);
  190. chart.noDataLabel.add();
  191. chart.noDataLabel.align(
  192. extend(chart.noDataLabel.getBBox(), noDataOptions.position),
  193. false,
  194. 'plotBox'
  195. );
  196. }
  197. };
  198. /**
  199. * Hide no-data message
  200. */
  201. chartPrototype.hideNoData = function () {
  202. var chart = this;
  203. if (chart.noDataLabel) {
  204. chart.noDataLabel = chart.noDataLabel.destroy();
  205. }
  206. };
  207. /**
  208. * Returns true if there are data points within the plot area now
  209. */
  210. chartPrototype.hasData = function () {
  211. var chart = this,
  212. series = chart.series || [],
  213. i = series.length;
  214. while (i--) {
  215. if (series[i].hasData() && !series[i].options.isInternal) {
  216. return true;
  217. }
  218. }
  219. return chart.loadingShown; // #4588
  220. };
  221. /**
  222. * Add event listener to handle automatic show or hide no-data message
  223. */
  224. H.addEvent(H.Chart, 'render', function handleNoData() {
  225. if (this.hasData()) {
  226. this.hideNoData();
  227. } else {
  228. this.showNoData();
  229. }
  230. });
  231. }(Highcharts));
  232. }));