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

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