You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
2.9 KiB

9 years ago
  1. /**
  2. * @license Highcharts JS v4.1.8 (2015-08-20)
  3. * Plugin for displaying a message when there is no data visible in chart.
  4. *
  5. * (c) 2010-2014 Highsoft AS
  6. * Author: Oystein Moseng
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. (function (H) {
  11. var seriesTypes = H.seriesTypes,
  12. chartPrototype = H.Chart.prototype,
  13. defaultOptions = H.getOptions(),
  14. extend = H.extend,
  15. each = H.each;
  16. // Add language option
  17. extend(defaultOptions.lang, {
  18. noData: 'No data to display'
  19. });
  20. // Add default display options for message
  21. defaultOptions.noData = {
  22. position: {
  23. x: 0,
  24. y: 0,
  25. align: 'center',
  26. verticalAlign: 'middle'
  27. },
  28. attr: {
  29. },
  30. style: {
  31. fontWeight: 'bold',
  32. fontSize: '12px',
  33. color: '#60606a'
  34. }
  35. // useHTML: false // docs
  36. };
  37. /**
  38. * Define hasData functions for series. These return true if there are data points on this series within the plot area
  39. */
  40. function hasDataPie() {
  41. return !!this.points.length; /* != 0 */
  42. }
  43. each(['pie', 'gauge', 'waterfall', 'bubble'], function (type) {
  44. if (seriesTypes[type]) {
  45. seriesTypes[type].prototype.hasData = hasDataPie;
  46. }
  47. });
  48. H.Series.prototype.hasData = function () {
  49. return this.visible && this.dataMax !== undefined && this.dataMin !== undefined; // #3703
  50. };
  51. /**
  52. * Display a no-data message.
  53. *
  54. * @param {String} str An optional message to show in place of the default one
  55. */
  56. chartPrototype.showNoData = function (str) {
  57. var chart = this,
  58. options = chart.options,
  59. text = str || options.lang.noData,
  60. noDataOptions = options.noData;
  61. if (!chart.noDataLabel) {
  62. chart.noDataLabel = chart.renderer
  63. .label(
  64. text,
  65. 0,
  66. 0,
  67. null,
  68. null,
  69. null,
  70. noDataOptions.useHTML,
  71. null,
  72. 'no-data'
  73. )
  74. .attr(noDataOptions.attr)
  75. .css(noDataOptions.style)
  76. .add();
  77. chart.noDataLabel.align(extend(chart.noDataLabel.getBBox(), noDataOptions.position), false, 'plotBox');
  78. }
  79. };
  80. /**
  81. * Hide no-data message
  82. */
  83. chartPrototype.hideNoData = function () {
  84. var chart = this;
  85. if (chart.noDataLabel) {
  86. chart.noDataLabel = chart.noDataLabel.destroy();
  87. }
  88. };
  89. /**
  90. * Returns true if there are data points within the plot area now
  91. */
  92. chartPrototype.hasData = function () {
  93. var chart = this,
  94. series = chart.series,
  95. i = series.length;
  96. while (i--) {
  97. if (series[i].hasData() && !series[i].options.isInternal) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. };
  103. /**
  104. * Show no-data message if there is no data in sight. Otherwise, hide it.
  105. */
  106. function handleNoData() {
  107. var chart = this;
  108. if (chart.hasData()) {
  109. chart.hideNoData();
  110. } else {
  111. chart.showNoData();
  112. }
  113. }
  114. /**
  115. * Add event listener to handle automatic display of no-data message
  116. */
  117. chartPrototype.callbacks.push(function (chart) {
  118. H.addEvent(chart, 'load', handleNoData);
  119. H.addEvent(chart, 'redraw', handleNoData);
  120. });
  121. }(Highcharts));