Skip to content

Commit ad7db2a

Browse files
authored
Migrate to ChartWrapper (#250)
Migrate to ChartWrapper Replace `<google-chart-loader>` with `google.visualization.ChartWrapper`. `ChartWrapper` provides similar functionality. API of the component stays the same.
1 parent dfee25b commit ad7db2a

3 files changed

Lines changed: 201 additions & 160 deletions

File tree

google-chart-loader.js

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -373,26 +373,7 @@ Polymer({
373373
* @return {!Promise<!google.visualization.DataTable>} promise for the created DataTable
374374
*/
375375
dataTable: function(data) {
376-
return this._corePackage.then(function(viz) {
377-
if (data == null) {
378-
return new viz.DataTable();
379-
} else if (data.getNumberOfRows) {
380-
// Data is already a DataTable
381-
return data;
382-
} else if (data.cols) { // data.rows may also be specified
383-
// Data is in the form of object DataTable structure
384-
return new viz.DataTable(data);
385-
} else if (data.length > 0) {
386-
// Data is in the form of a two dimensional array.
387-
return viz.arrayToDataTable(data);
388-
} else if (data.length === 0) {
389-
// Chart data was empty.
390-
// We return null instead of creating an empty DataTable because most
391-
// (if not all) charts will render a sticky error in this situation.
392-
return Promise.reject('Data was empty.');
393-
}
394-
return Promise.reject('Data format was not recognized.');
395-
});
376+
return dataTable(data);
396377
},
397378

398379
/**
@@ -424,3 +405,56 @@ Polymer({
424405
});
425406
}
426407
});
408+
409+
/**
410+
* Creates a DataTable object for use with a chart.
411+
*
412+
* Multiple different argument types are supported. This is because the
413+
* result of loading the JSON data URL is fed into this function for
414+
* DataTable construction and its format is unknown.
415+
*
416+
* The data argument can be one of a few options:
417+
*
418+
* - null/undefined: An empty DataTable is created. Columns must be added
419+
* - !DataTable: The object is simply returned
420+
* - {{cols: !Array, rows: !Array}}: A DataTable in object format
421+
* - {{cols: !Array}}: A DataTable in object format without rows
422+
* - !Array<!Array>: A DataTable in 2D array format
423+
*
424+
* Un-supported types:
425+
*
426+
* - Empty !Array<!Array>: (e.g. `[]`) While technically a valid data
427+
* format, this is rejected as charts will not render empty DataTables.
428+
* DataTables must at least have columns specified. An empty array is most
429+
* likely due to a bug or bad data. If one wants an empty DataTable, pass
430+
* no arguments.
431+
* - Anything else
432+
*
433+
* See <a href="https://developers.google.com/chart/interactive/docs/reference#datatable-class">the docs</a> for more details.
434+
*
435+
* @param {!Array|{cols: !Array, rows: (!Array<!Array>|undefined)}|undefined} data
436+
* the data with which we should use to construct the new DataTable object
437+
* @return {!Promise<!google.visualization.DataTable>} promise for the created DataTable
438+
*/
439+
export async function dataTable(data) {
440+
// Ensure that `google.visualization` namespace is added to the document.
441+
await load();
442+
if (data == null) {
443+
return new google.visualization.DataTable();
444+
} else if (data.getNumberOfRows) {
445+
// Data is already a DataTable
446+
return /** @type {!google.visualization.DataTable} */ (data);
447+
} else if (data.cols) { // data.rows may also be specified
448+
// Data is in the form of object DataTable structure
449+
return new google.visualization.DataTable(data);
450+
} else if (data.length > 0) {
451+
// Data is in the form of a two dimensional array.
452+
return google.visualization.arrayToDataTable(data);
453+
} else if (data.length === 0) {
454+
// Chart data was empty.
455+
// We throw instead of creating an empty DataTable because most
456+
// (if not all) charts will render a sticky error in this situation.
457+
throw new Error('Data was empty.');
458+
}
459+
throw new Error('Data format was not recognized.');
460+
}

0 commit comments

Comments
 (0)