Dynamic Change - Google Chart Type

蹲街弑〆低调 提交于 2019-11-27 07:18:50

问题


Depends on the select type, the chart should display. I prepared JSON. But I need to change chart type dynamically.

I tried differently. But got an error. Is there any alternative solution is available to fulfil this senorio.

JS Code:

google.load('visualization', '1', {'packages':['corechart']}); 

// Set a callback to run when the Google Visualization API is loaded. 
google.setOnLoadCallback(drawChart); 

function drawChart() { 
  .......
  var chartType = document.getElementById("chartType").value;
  var chartTypeContent = "new google.visualization." + chartType + "(document.getElementById('chart_div'))";
  var chart =  chartTypeContent ; 
  chart.draw(data, options_fullStacked); 
}

HTML Code:

<select id="chartType" onchange="drawChart()">
<option value="ColumnChart">Column Chart</option>
<option value="BarChart">Bar Chart</option>
</select>

Error is: Uncaught TypeError: chart.draw is not a function

I'm new to google chart. Please help me solve this issue.


回答1:


try it like this...

var chartTypeContent = new google.visualization[chartType](document.getElementById('chart_div'));


来源:https://stackoverflow.com/questions/40161527/dynamic-change-google-chart-type

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