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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @license Highcharts JS v4.1.5 (2015-04-13)
  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. };
  36. /**
  37. * Define hasData functions for series. These return true if there are data points on this series within the plot area
  38. */
  39. function hasDataPie() {
  40. return !!this.points.length; /* != 0 */
  41. }
  42. each(['pie', 'gauge', 'waterfall', 'bubble'], function (type) {
  43. if (seriesTypes[type]) {
  44. seriesTypes[type].prototype.hasData = hasDataPie;
  45. }
  46. });
  47. H.Series.prototype.hasData = function () {
  48. return this.visible && this.dataMax !== undefined && this.dataMin !== undefined; // #3703
  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));