Reputation: 3
I'm newbie in JS programming. I want to make a select option where I can change the chart type e.g. from line to bar. The data source comes from Google Spreadsheet. The JS code is as follow:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Select Chart Type
</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['linechart',corechart','barchart', 'columnchart']});
</script>
<script type="text/javascript">
var graphview;
function init() {
graphview = new google.visualization.ChartWrapper({
dataSourceUrl: 'https://docs.google.com/spreadsheet/tq?tq=select%20A,B......etc
containerId: 'visualization1', chartType: 'LineChart'
graphview.draw();
}
google.setOnLoadCallback(init);
}
</script>
</head>
<body>
<div id="panelgraph", width="600", height="200">
</body>
</html>
The options are as follow:
<select id="graphbox" name="graphbox" onChange="getChart()">
<option value="Line">Line Chart </option>
<option value="Bar">Bar Chart</option>
<option value="Core">Area Chart</option>
<option value="Column">Column Chart</option>
</select>
I try to make a function, but was not sure how and where to insert them:
var mygraphbox = document.getElementById("graphbox");
var myinsertgraphhere = document.getElementById("panelgraph");
mygraphbox.onchange =function()
I follow some example of how to make onchange select option function. But when I want to apply it for my case (e.g. using if-else and chart the map) I have troubles. Please help me how to formulate it and how to inserting it in the JS block.
Thank you in advance, Dian
Upvotes: 0
Views: 543
Reputation: 26340
Here's a quick solution; change your javascript to this:
function init () {
var graphview = new google.visualization.ChartWrapper({
chartType: 'LineChart',
containerId: 'visualization1',
dataSourceUrl: 'https://docs.google.com/spreadsheet/tq?key=<key>&gid=0',
options: {
// chart options
},
query: 'select a, b, c'
});
graphview.draw();
function changeHandler () {
graphview.setChartType(this.options[this.selectedIndex].value);
graphview.draw();
}
var mygraphbox = document.querySelector("#graphbox");
if (typeof(window.addEventListener) == 'function') {
mygraphbox.addEventListener('change', changeHandler, false);
}
else if (typeof(window.attachEvent) == 'function') {
mygraphbox.attachEvent('onchange', changeHandler);
}
}
google.load('visualization', '1', {packages: ['corechart'], callback: init});
and change the select box like this:
<select id="graphbox">
<option value="LineChart">Line Chart </option>
<option value="BarChart">Bar Chart</option>
<option value="AreaChart">Area Chart</option>
<option value="ColumnChart">Column Chart</option>
</select>
With this, you can change the chart type without resubmitting the query.
Upvotes: 2