Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass cols and rows to arrayToDataTable #341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading