Gauge update from a button click

63 views
Skip to first unread message

scott_t_ct

unread,
Nov 23, 2011, 4:43:53 PM11/23/11
to Google Visualization API
I am having real trouble updating a gauge from a button click event
and text value. I have looked at all of the online help, and docs and
tried to modify the Temp up and down to accomplish the event.
I am a PE teacher and would like to use this to show my students how
close to 60 minutes of activity they have gotten as a class during PE.
Here is my code so far:

<!--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type='text/javascript' src='https://www.google.com/
jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', { packages:
['gauge'] });
google.setOnLoadCallback(drawChart);

function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Label');
data.addColumn('number', 'Value');
data.addRows(1);
data.setValue(0, 0, 'Minutes');
data.setValue(0, 1, 18);
//// draw Chart
var chart = new
google.visualization.Gauge(document.getElementById('chart_div'));
var options = { width: 300, height: 200, redFrom:
0, redTo: 30, yellowFrom: 30, yellowTo: 45, greenFrom: 45, greenTo:
60, minorTicks: 5, max: 60 };
chart.draw(data, options);
}
function setTime() {

var dir =
document.getElementById("gaugeForm").gaugeText.value;
data.setValue(0, 0, gaugeData.getValue(0, 0) +
dir);


chart.draw(data, options);
}
</script>
</head>
<body>
<div id='chart_div'></div>
<br />
<br />
<form id="gaugeForm" >
<input type="text" id="gaugeText" />
<button id="guageBut" value="Activity Time" onclick=
"setTime()">Activity Time</button>
</form>
</body>
</html>-->

Thanks for your help. T

asgallant

unread,
Nov 23, 2011, 6:53:23 PM11/23/11
to google-visua...@googlegroups.com
You were pretty close to what you need with your code.  The main problem is that data and chart are not in scope for the setTime() function, but that is easily fixed by assigning the event handler to the button inside the drawChart() function.  There were a few other minor things that I cleaned up for you; working version is available here: http://jsfiddle.net/xu5CB/ 

Replace the contents of your <script> and <body> tags with the javascript and HTML from my version.
Reply all
Reply to author
Forward
0 new messages