问题
I'm getting the issue "couldn't decode Array as JSON: " for the below chart. Rest of the charts seem to work with this format but not this one.
<google-chart type='timeline' options='{"title": "Timeline",
"legend" : {"position" : "bottom", "alignment": "start"}
}' cols='[{"label":"Question", "type":"string"},
{"label":"Number", "type":"string"},
{"label":"Start", "type":"date"},
{"label":"End", "type":"date"} ]'
rows='[["Question1", "1", new Date(0,0,0,12,0,0), new Date(0,0,0,12,30,0)],
["Question2", "2", new Date(0,0,0,12,30,0), new Date(0,0,0,12,45,0)],
["Question3", "3", new Date(0,0,0,12,45,0), new Date(0,0,0,1,0,0)],
["Question4", "4", new Date(0,0,0,1,0,0), new Date(0,0,0,1,12,0)],
["Question5", "5", new Date(0,0,0,1,12,0), new Date(0,0,0,1,30,0)]]'>
</google-chart>
回答1:
I little changed your code and its now working.
DEMO
<google-chart id="timeline" type='timeline' options='{"title": "Timeline"}'>
</google-chart>
and at the script:
ready() {
super.ready();
Polymer.RenderStatus.afterNextRender(this, () => {this.showChartTable(); })
}
showChartTable(){
var chart = this.shadowRoot.querySelector('#timeline');
document.createElement('google-chart-loader').dataTable([
['Question', 'Number', 'Start','End'],
["Question1", "1", new Date(1789, 3, 30), new Date(1800, 3, 30)], ["Question2", "2", new Date(1800, 3, 30), new Date(1900, 3, 30)], ["Question3", "3", new Date(1900, 3, 30), new Date(2000, 3, 30)], ["Question4", "4", new Date(2000, 3, 30), new Date(2018, 3, 30)], ["Question5", "5", new Date(2018, 3, 30),new Date(2020, 3, 30)]] ).then(function(dataTable) {
chart.data = dataTable;
});
}
Due to timeline logic, you should define a valid time line. Your dates gives error.
EDIT: no need to keep this chart inside connectedCallback
. just move on own named functions as: showChartTable
(I changed)
来源:https://stackoverflow.com/questions/49783933/timelinechart-couldnt-decode-array-as-json