Skip to content

Commit 977fe56

Browse files
authored
Pass cols and rows to arrayToDataTable (#341)
* Pass cols and rows to arrayToDataTable It allows dates to parsed and columns types to be omitted. * Document waitForRender testing helper
1 parent e1b0a41 commit 977fe56

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

demo/index.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ <h2>Chart gallery</h2>
227227
<google-chart
228228
type="bar"
229229
options='{"title": "Days in a month"}'
230-
cols='[{"label": "Month", "type": "string"},{"label": "Days", "type": "number"}]'
230+
cols='["Month", "Days"]'
231231
rows='[["Jan", 31],["Feb", 28],["Mar", 31],["Apr", 30],["May", 31],["Jun", 30]]'>
232232
</google-chart>
233233

@@ -441,6 +441,22 @@ <h2>Chart gallery</h2>
441441
});
442442
</script>
443443

444+
<p>Here's the same timeline chart that parses dates from <code>rows</code>:</p>
445+
446+
<google-chart
447+
type="timeline"
448+
cols='[
449+
"Name",
450+
{"type": "date", "label": "Start"},
451+
{"type": "date", "label": "End"}
452+
]'
453+
rows='[
454+
["Washington", "Date(1789, 3, 30)", "Date(1797, 2, 4)"],
455+
["Adams", "Date(1797, 2, 4)", "Date(1801, 2, 4)"],
456+
["Jefferson", "Date(1801, 2, 4)", "Date(1809, 2, 4)"]
457+
]'>
458+
</google-chart>
459+
444460
<p>Here's a wordtree:</p>
445461

446462
<google-chart

google-chart.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,7 @@ export class GoogleChart extends LitElement {
489489
const {rows, cols} = this;
490490
if (!rows || !cols) return;
491491
try {
492-
const dt = await dataTable({cols});
493-
dt.addRows(rows);
492+
const dt = await dataTable([cols, ...rows]);
494493
this._data = dt;
495494
} catch (reason) {
496495
this.shadowRoot!.getElementById('chartdiv')!.textContent = String(reason);

test/basic-tests.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -231,34 +231,36 @@ suite('<google-chart>', function() {
231231
});
232232

233233
suite('Data Source Types', function() {
234-
test('[rows] and [cols]', function (done) {
235-
chart.cols = [
236-
{'label': 'Data', 'type': 'string'},
237-
{'label': 'Value', 'type': 'number'}
238-
];
234+
// Waits for the event reporting a successful render. Invalid or missing data sources do not
235+
// trigger the event and will timeout the tests.
236+
function waitForRender(done: () => void) {
237+
chart.addEventListener('google-chart-ready', () => void done());
238+
}
239+
test('[rows] and [cols] without type', function (done) {
240+
chart.cols = ['Data', 'Value'];
239241
chart.rows = [
240242
['Something', 1]
241243
];
242-
chart.addEventListener('google-chart-ready', function() {
243-
done();
244-
});
244+
waitForRender(done);
245245
});
246246

247-
test('[rows] and [cols] with date string repr is broken', function(done) {
248-
chart.cols = [ { 'type': 'date' } ];
249-
chart.rows = [ ['Date(1789, 3, 30)'] ];
250-
waitCheckAndDone(function() {
251-
const chartDiv = chart.shadowRoot!.getElementById('chartdiv')!;
252-
return chartDiv.innerHTML ==
253-
'Error: Type mismatch. Value Date(1789, 3, 30) ' +
254-
'does not match type date in column index 0';
255-
}, done);
247+
test('[rows] and [cols] with type', function(done) {
248+
chart.type = 'timeline';
249+
chart.cols = [
250+
'Data',
251+
{type: 'date', label: 'Start'},
252+
{type: 'date', label: 'End'}
253+
];
254+
chart.rows = [[
255+
'Something',
256+
'Date(2024, 6, 1)',
257+
'Date(2024, 7, 1)'
258+
]];
259+
waitForRender(done);
256260
});
257261
var setDataAndWaitForRender = function(data: DataTableLike|string, done: () => void) {
258262
chart.data = data;
259-
chart.addEventListener('google-chart-ready', function() {
260-
done();
261-
});
263+
waitForRender(done);
262264
};
263265
test('[data] is 2D Array', function(done) {
264266
setDataAndWaitForRender([ ['Data', 'Value'], ['Something', 1] ], done);

0 commit comments

Comments
 (0)