UNPKG

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