Skip to content

Commit

Permalink
Pass cols and rows to arrayToDataTable
Browse files Browse the repository at this point in the history
It allows dates to parsed and columns types to be omitted.
  • Loading branch information
rslawik committed Jul 21, 2024
1 parent fcf13c0 commit bfe16a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
18 changes: 17 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ <h2>Chart gallery</h2>
<google-chart
type="bar"
options='{"title": "Days in a month"}'
cols='[{"label": "Month", "type": "string"},{"label": "Days", "type": "number"}]'
cols='["Month", "Days"]'
rows='[["Jan", 31],["Feb", 28],["Mar", 31],["Apr", 30],["May", 31],["Jun", 30]]'>
</google-chart>

Expand Down Expand Up @@ -441,6 +441,22 @@ <h2>Chart gallery</h2>
});
</script>

<p>Here's the same timeline chart that parses dates from <code>rows</code>:</p>

<google-chart
type="timeline"
cols='[
"Name",
{"type": "date", "label": "Start"},
{"type": "date", "label": "End"}
]'
rows='[
["Washington", "Date(1789, 3, 30)", "Date(1797, 2, 4)"],
["Adams", "Date(1797, 2, 4)", "Date(1801, 2, 4)"],
["Jefferson", "Date(1801, 2, 4)", "Date(1809, 2, 4)"]
]'>
</google-chart>

<p>Here's a wordtree:</p>

<google-chart
Expand Down
3 changes: 1 addition & 2 deletions google-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,7 @@ export class GoogleChart extends LitElement {
const {rows, cols} = this;
if (!rows || !cols) return;
try {
const dt = await dataTable({cols});
dt.addRows(rows);
const dt = await dataTable([cols, ...rows]);
this._data = dt;
} catch (reason) {
this.shadowRoot!.getElementById('chartdiv')!.textContent = String(reason);
Expand Down
40 changes: 20 additions & 20 deletions test/basic-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,34 +231,34 @@ suite('<google-chart>', function() {
});

suite('Data Source Types', function() {
test('[rows] and [cols]', function (done) {
chart.cols = [
{'label': 'Data', 'type': 'string'},
{'label': 'Value', 'type': 'number'}
];
function waitForRender(done: () => void) {
chart.addEventListener('google-chart-ready', () => void done());
}
test('[rows] and [cols] without type', function (done) {
chart.cols = ['Data', 'Value'];
chart.rows = [
['Something', 1]
];
chart.addEventListener('google-chart-ready', function() {
done();
});
waitForRender(done);
});

test('[rows] and [cols] with date string repr is broken', function(done) {
chart.cols = [ { 'type': 'date' } ];
chart.rows = [ ['Date(1789, 3, 30)'] ];
waitCheckAndDone(function() {
const chartDiv = chart.shadowRoot!.getElementById('chartdiv')!;
return chartDiv.innerHTML ==
'Error: Type mismatch. Value Date(1789, 3, 30) ' +
'does not match type date in column index 0';
}, done);
test('[rows] and [cols] with type', function(done) {
chart.type = 'timeline';
chart.cols = [
'Data',
{type: 'date', label: 'Start'},
{type: 'date', label: 'End'}
];
chart.rows = [[
'Something',
'Date(2024, 6, 1)',
'Date(2024, 7, 1)'
]];
waitForRender(done);
});
var setDataAndWaitForRender = function(data: DataTableLike|string, done: () => void) {
chart.data = data;
chart.addEventListener('google-chart-ready', function() {
done();
});
waitForRender(done);
};
test('[data] is 2D Array', function(done) {
setDataAndWaitForRender([ ['Data', 'Value'], ['Something', 1] ], done);
Expand Down

0 comments on commit bfe16a7

Please sign in to comment.