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.

130 lines
2.8 KiB

  1. /**
  2. * @license Highcharts JS v3.0.10 (2014-03-10)
  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) { // 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. if (seriesTypes.pie) {
  42. seriesTypes.pie.prototype.hasData = hasDataPie;
  43. }
  44. if (seriesTypes.gauge) {
  45. seriesTypes.gauge.prototype.hasData = hasDataPie;
  46. }
  47. if (seriesTypes.waterfall) {
  48. seriesTypes.waterfall.prototype.hasData = hasDataPie;
  49. }
  50. H.Series.prototype.hasData = function () {
  51. return this.dataMax !== undefined && this.dataMin !== undefined;
  52. };
  53. /**
  54. * Display a no-data message.
  55. *
  56. * @param {String} str An optional message to show in place of the default one
  57. */
  58. chartPrototype.showNoData = function (str) {
  59. var chart = this,
  60. options = chart.options,
  61. text = str || options.lang.noData,
  62. noDataOptions = options.noData;
  63. if (!chart.noDataLabel) {
  64. chart.noDataLabel = chart.renderer.label(text, 0, 0, null, null, null, null, null, 'no-data')
  65. .attr(noDataOptions.attr)
  66. .css(noDataOptions.style)
  67. .add();
  68. chart.noDataLabel.align(extend(chart.noDataLabel.getBBox(), noDataOptions.position), false, 'plotBox');
  69. }
  70. };
  71. /**
  72. * Hide no-data message
  73. */
  74. chartPrototype.hideNoData = function () {
  75. var chart = this;
  76. if (chart.noDataLabel) {
  77. chart.noDataLabel = chart.noDataLabel.destroy();
  78. }
  79. };
  80. /**
  81. * Returns true if there are data points within the plot area now
  82. */
  83. chartPrototype.hasData = function () {
  84. var chart = this,
  85. series = chart.series,
  86. i = series.length;
  87. while (i--) {
  88. if (series[i].hasData() && !series[i].options.isInternal) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. };
  94. /**
  95. * Show no-data message if there is no data in sight. Otherwise, hide it.
  96. */
  97. function handleNoData() {
  98. var chart = this;
  99. if (chart.hasData()) {
  100. chart.hideNoData();
  101. } else {
  102. chart.showNoData();
  103. }
  104. }
  105. /**
  106. * Add event listener to handle automatic display of no-data message
  107. */
  108. chartPrototype.callbacks.push(function (chart) {
  109. H.addEvent(chart, 'load', handleNoData);
  110. H.addEvent(chart, 'redraw', handleNoData);
  111. });
  112. }(Highcharts));