Reputation: 34
I'm using Google Charts on my web application and would like to change the chart displayed using a dropdown list.
The dropdown list stores the chart type in code behind and I'm trying to insert the VB.net variable in the var chart variable.
var chart = new google.visualization.datachart(document.getElementById('chart_div')); chart.draw(data, options);
The value in bold defines which chart gets displayed (ColumnChart, LineChart etc)
How's best to accomplish this?
Upvotes: 0
Views: 1398
Reputation: 26340
You can handle this a number of different ways. juvian pointed out a couple of them, but if you want something more direct from ASP.net:
var chart = new google.visualization.<%=chartType %>(document.getElementById('chart_div'));
Where chartType
is a string that matches a chart type, like "BarChart"
or "LineChart"
.
If you go this route, you should validate the chart type on the server-side so you don't end up trying to access a chart type that doesn't exist.
Upvotes: 0
Reputation: 16068
Simple way is to set the chart according to the one you want to draw:
var chart;
if(chartType== 'LineChart' ){
chart= new google.visualization.LineChart(document.getElementById('chart_div'));
}else if(chartType== 'BarChart' ){
chart= new google.visualization.BarChart(document.getElementById('chart_div'));
}// and so on
chart.draw(data, options);
For a more dynamic way, you would need to use a chartWrapper, which you can set the chart type with a string, or do:
chartWrapper.setChartType('LineChart');
Upvotes: 0