我一直在尝试动态更新 Google Gauge,但尽管查看了其他几个问题,但我无法让它工作。
代码在下面,但我需要做的是将滑块设置为一个值,然后按 go 链接。这应该更新仪表。我所能做的就是改变文本。
谁能指出我哪里出错了?
<!doctype html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript' src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:['gauge']});
google.setOnLoadCallback(drawChart);
</script>
<script type='text/javascript'>
function recordValue() {
var val = document.getElementById("amount").value;
setValue(val);
return true;
}
</script>
</head>
<body>
<div id='chart_div'></div>
<p>
<label for="amount">Slider value:</label>
<input type="text" id="amount" style="border: 0; color: #f6931f; font-weight: bold;" />
</p>
<div id="slider-range-max"></div>
<div class="go"><a href="#" class="button" onClick="javascript:return recordValue();">Go</a></div>
<script>
$(function() {
$( "#slider-range-max" ).slider({
range: "max",
min: 1,
max: 100,
value: 50,
slide: function( event, ui ) {
$( "#amount" ).val( ui.value );
}
});
$( "#amount" ).val( $( "#slider-range-max" ).slider( "value" ) );
});
</script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['gauge']});
google.setOnLoadCallback(drawChart);
var data;
var chart;
var options;
function drawChart() {
data = new google.visualization.DataTable();
data.addColumn('string', 'Gauge');
data.addColumn('number', 'Value');
data.addRows([
['Value', 20]
]);
options= {
animation: {
duration: 2000
},
height: 300,
width: 300,
greenFrom: 40,
greenTo: 100,
yellowFrom: 20,
yellowTo: 40,
redFrom: 0,
redTo: 20,
max: 100
};
// make a temporary view to draw the chart in a blank state
var view = new google.visualization.DataView(data);
view.setColumns([0, {
// create a calculated column that contains all zero's
type: 'number',
calc: function () {
return 0;
}
}]);
chart = new google.visualization.ChartWrapper({
chartType: 'Gauge',
containerId: 'chart_div',
dataTable: view,
options: {
animation: {
duration: 2000
},
height: 300,
width: 300,
greenFrom: 40,
greenTo: 100,
yellowFrom: 20,
yellowTo: 40,
redFrom: 0,
redTo: 20,
max: 100
} });
var runOnce = google.visualization.events.addListener(chart, 'ready', function () {
google.visualization.events.removeListener(runOnce);
// use the real data to redraw
chart.setDataTable(data);
chart.draw();
});
chart.draw();
}
function setValue(val){
data.setValue(0, 0, "['Value', val]");
chart.draw(data, options);
}
</script>
</body>
</html>