我的图表没有加载。我尝试使用 javascript 将 JSON 从 JSP 页面解析为 HTML 页面。
我不知道问题出在哪里。这是详细信息:
我的网页 :
<!DOCTYPE html>
<html>
<head>
<title>Google Chart with jsp Mysql Json</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
var obj="";
google.setOnLoadCallback(drawChart);
function drawChart() {
$.ajax({
url : 'getDataGlucose.jsp',
dataType:'json',
success : function(glucoseData) {
obj = JSON.stringify(glucoseData)
alert('Pass? : '+obj);
},
error : function(xhr, type) {
alert('server error occoured')
}
});
var google_data = {
"cols": [{
"type": "number"
}, {
"type": "number"
}],
rows: []
};
for (var i = 0; i < obj.length; i++) {
google_data.rows.push({
c: [{
v: obj[i].xValue
}, {
v: obj[i].glucose
}]
});
}
var data = new google.visualization.DataTable(google_data);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
var options = {
title: 'Glucose Data Intake',
hAxis: {
title: 'Intake'
},
curveType: 'function',
vAxis: {
title: 'mmHg'
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data,options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
我从 JSP 页面生成 JSON:
[{"葡萄糖":5.5,"xValue":0}, {"葡萄糖":6.2,"xValue":1}, {"葡萄糖":5.8,"xValue":2}, {"葡萄糖":7,"xValue":3}, {"葡萄糖":6,"xValue":4}, {"葡萄糖":7.5,"xValue":5}, {"葡萄糖":6,"xValue":6}, {"葡萄糖":5.5,"xValue":7}, {"葡萄糖":6,"xValue":8}, {"葡萄糖":5.8,"xValue":9}]
找到了我的出路。我从 :
obj = JSON.stringify(glucoseData);
自
obj = eval('(' + JSON.stringify(glucoseData) + ')');