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.

125 lines
3.2 KiB

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