Google Charts: Switching between Line and Column charts

跟風遠走 提交于 2019-12-06 03:27:57

You should use the ChartWrapper object

This way you only need one charting object

 var chart = new google.visualization.ChartWrapper({
     containerId: 'chart-div'
 });

and then you can change its type with the .setChartType method

 var barsButton = document.getElementById('b1');
 var lineButton = document.getElementById('b2');

 chart.setOptions(options);

 function drawBars() {
     chart.setChartType('ColumnChart');
     chart.setDataTable(data[0]);
     chart.draw();
 }

 function drawLine() {
     chart.setChartType('LineChart');
     chart.setDataTable(data[1]);
     chart.draw();
 }

 barsButton.onclick = function () {
     drawBars();
 }

 lineButton.onclick = function () {
     drawLine();
 }
 drawBars();

Demo at http://jsfiddle.net/gaby/Xmj6j/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!