forked from GoogleWebComponents/google-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-chart-loader.html
342 lines (327 loc) · 10.5 KB
/
google-chart-loader.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../promise-polyfill/promise-polyfill.html">
<link rel="import" href="charts-loader.html">
<script>
(function() {
"use strict";
/** @type {string} Most charts use this package. */
var DEFACTO_CHART_PACKAGE = 'corechart';
/**
* A collection of chart type details.
*
* @type {!Object<string, {ctor: string, pkg: (string|undefined)}>}
*/
var CHART_CONSTRUCTORS = {
'area': {
ctor: 'AreaChart',
},
'bar': {
ctor: 'BarChart',
},
'md-bar': {
ctor: 'Bar',
pkg: 'bar',
},
'bubble': {
ctor: 'BubbleChart',
},
'candlestick': {
ctor: 'CandlestickChart',
},
'column': {
ctor: 'ColumnChart',
},
'combo': {
ctor: 'ComboChart',
},
'geo': {
ctor: 'GeoChart',
},
'histogram': {
ctor: 'Histogram',
},
'line': {
ctor: 'LineChart',
},
'md-line': {
ctor: 'Line',
pkg: 'line',
},
'org': {
ctor: 'OrgChart',
pkg: 'orgchart',
},
'pie': {
ctor: 'PieChart',
},
'scatter': {
ctor: 'ScatterChart',
},
'md-scatter': {
ctor: 'Scatter',
pkg: 'scatter',
},
'stepped-area': {
ctor: 'SteppedAreaChart',
},
'table': {
ctor: 'Table',
pkg: 'table',
},
'timeline': {
ctor: 'Timeline',
pkg: 'timeline',
},
'gauge': {
ctor: 'Gauge',
pkg: 'gauge',
},
'treemap': {
ctor: 'TreeMap',
pkg: 'treemap',
},
'calendar': {
ctor: 'Calendar',
pkg: 'calendar',
}
};
/**
* Returns the namespace for the given chart type.
* @param {string} type the type of the chart
* @return {!Object} the namespace that contains the chart's constructor
*/
function namespaceForType(type) {
return google[type.indexOf('md-') === 0 ? 'charts' : 'visualization'];
}
/** @type {!Object<string, boolean>} set-like object of gviz packages to load */
var packagesToLoad = {};
/** @type {!Object<string, !Promise>} promises for the various packages */
var promises = {};
/** @type {!Object<string, function(!Object)>} resolves for the package promises */
var resolves = {};
Polymer({
is: 'google-chart-loader',
properties: {
/**
* Adds packages to the list of packages to load.
*
* This is an array consisting of any Google Visualization package names.
*
* @attribute packages
* @type {!Array<string>}
*/
packages: {
type: Array,
value: function() { return []; },
observer: '_loadPackages',
},
/**
* Loads the package for the chart type specified.
*
* This may be any of the supported `google-chart` types.
* This is mainly used by the `google-chart` element internally.
*
* @attribute type
* @type {string}
*/
type: {
type: String,
observer: '_loadPackageForType',
},
},
/**
* Gets a promise for the `corechart` package being loaded.
* @return {!Promise<!Object>} google.visualization package promise
* @private
*/
get _corePackage() {
if (promises[DEFACTO_CHART_PACKAGE]) {
return promises[DEFACTO_CHART_PACKAGE];
}
return this._loadPackages([DEFACTO_CHART_PACKAGE]).then(function(pkgs) {
return pkgs[0];
});
},
/**
* Debounces the actual call to load the packages requested.
* We debounce so that load is only called once.
* @private
*/
_loadPackagesDebounce: function() {
this.debounce('loadPackages', function() {
var packages = Object.keys(packagesToLoad);
if (!packages.length) {
return;
}
packagesToLoad = {};
google.charts.load('current', {
'packages': packages,
'language': document.documentElement.lang || 'en'
});
google.charts.setOnLoadCallback(function() {
packages.forEach(function(pkg) {
this.fire('loaded', pkg);
resolves[pkg](google.visualization);
}.bind(this));
}.bind(this));
}, 100);
},
/**
* Adds a list of packages to load.
*
* @param {!Array<string>} pkgs list of packages to load
* @return {!Promise} Promise resolved when all packages are loaded
* @private
*/
_loadPackages: function(pkgs) {
var returns = [];
pkgs.forEach(function(pkg) {
if (!promises[pkg]) {
packagesToLoad[pkg] = true;
promises[pkg] = new Promise(function(resolve) {
resolves[pkg] = resolve;
});
this._loadPackagesDebounce();
}
returns.push(promises[pkg]);
}.bind(this));
return Promise.all(returns);
},
/**
* Adds a package to load for the given type.
*
* @param {string} type the chart type for which we should load the package
* @return {!Promise<!Function>} Promise for the chart type constructor
* @private
*/
_loadPackageForType: function(type) {
var chartData = CHART_CONSTRUCTORS[type];
return this._loadPackages([chartData.pkg || DEFACTO_CHART_PACKAGE])
.then(function() {
return namespaceForType(type)[chartData.ctor];
});
},
/**
* Creates a chart object by type in the specified element.
* Use *only* if you need total control of a chart object.
* Most should just use the `google-chart` element.
*
* @param {string} type the type of chart to create
* @param {!Element} el the element in which to create the chart
* @return {!Promise<!Object>} promise for the created chart object
*/
create: function(type, el) {
return this._loadPackageForType(type).then(function(ctor) {
return new ctor(el);
});
},
/**
* Begins firing Polymer events for the requested chart event.
* Use *only* if you have control of a chart object.
* Most should just use the `google-chart` element.
*
* Events fired all have the same detail object:
* {{
* chart: !Object, // The chart target object
* data: (Object|undefined), // The chart event details
* }}
*
* @param {!Object} chart the chart object to which we should listen
* @param {string} eventName the name of the chart event
* @param {boolean=} opt_once whether to listen only one time
* @return {!Promise<void>} promise resolved when listener is attached
*/
fireOnChartEvent: function(chart, eventName, opt_once) {
return this._corePackage.then(function(viz) {
var adder = opt_once ?
viz.events.addOneTimeListener : viz.events.addListener;
adder(chart, eventName, function(event) {
this.fire('google-chart-' + eventName, {
chart: chart,
data: event,
});
}.bind(this));
}.bind(this));
},
/**
* Creates a DataTable object for use with a chart.
*
* Multiple different argument types are supported. This is because the
* result of loading the JSON data URL is fed into this function for
* DataTable construction and its format is unknown.
*
* The data argument can be one of a few options:
*
* - null/undefined: An empty DataTable is created. Columns must be added
* - !DataTable: The object is simply returned
* - {{cols: !Array, rows: !Array}}: A DataTable in object format
* - {{cols: !Array}}: A DataTable in object format without rows
* - !Array<!Array>: A DataTable in 2D array format
*
* Un-supported types:
*
* - Empty !Array<!Array>: (e.g. `[]`) While technically a valid data
* format, this is rejected as charts will not render empty DataTables.
* DataTables must at least have columns specified. An empty array is most
* likely due to a bug or bad data. If one wants an empty DataTable, pass
* no arguments.
* - Anything else
*
* See <a href="https://developers.google.com/chart/interactive/docs/reference#datatable-class">the docs</a> for more details.
*
* @param {Array|{cols: !Array, rows: (!Array<!Array>|undefined)}|undefined} data
* the data with which we should use to construct the new DataTable object
* @return {!Promise<!google.visualization.DataTable>} promise for the created DataTable
*/
dataTable: function(data) {
return this._corePackage.then(function(viz) {
if (data == null) {
return new viz.DataTable();
} else if (data.getNumberOfRows) {
// Data is already a DataTable
return data;
} else if (data.cols) { // data.rows may also be specified
// Data is in the form of object DataTable structure
return new viz.DataTable(data);
} else if (data.length > 0) {
// Data is in the form of a two dimensional array.
return viz.arrayToDataTable(data);
} else if (data.length === 0) {
// Chart data was empty.
// We return null instead of creating an empty DataTable because most
// (if not all) charts will render a sticky error in this situation.
return Promise.reject('Data was empty.');
}
return Promise.reject('Data format was not recognized.');
});
},
/**
* Creates a DataView object from a DataTable for use with a chart.
*
* See <a href="https://developers.google.com/chart/interactive/docs/reference#dataview-class">the docs</a> for more details.
*
* @param {!google.visualization.DataTable} data the DataTable to use
* @return {!Promise<!google.visualization.DataView>} promise for the created DataView
*/
dataView: function(data) {
return this._corePackage.then(function(viz) {
return new viz.DataView(data);
});
},
/**
* Creates a Query object to be sent to a DataSource protocol implementation.
*
* See <a href="https://developers.google.com/chart/interactive/docs/reference#query-classes">the docs</a> for more details.
*
* @param {string} url the URL of the DataSource protocol implementer
* @param {!Object=} opt_options options for the Query object
* @return {!Promise<!google.visualization.Query>} promise for the created DataView
*/
query: function(url, opt_options) {
return this._corePackage.then(function(viz) {
return new viz.Query(url, opt_options);
});
}
});
})();
</script>