我正在使用 Google Visualization API 在 JSP 页面上显示图表。使用会话变量,我能够获取包含在 Google Visualization API 中的方法的数据集。我也能够加载数据,但它只检索数组的第一个元素。如何放置数组的所有值?代码如下:
在 AnalyzeUserClient.jsp 中:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
var sessionId = <%= (ArrayList<String>)request.getSession().getAttribute("arrayList") %>;
for(var i=0;i<sessionId.length;i++){
if(i%2==0){
var category = sessionId[i];
}
else{
var value = sessionId[i];
}
}
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['Category', 'Value'],
[category,value],
]);
// Create and draw the visualization.
new google.visualization.PieChart(document.getElementById('visualization')).
draw(data, {title:"Percentage of Category for User:"});
}
google.setOnLoadCallback(drawVisualization);
//HTML Code:
<div id="visualization" style="width: 600px; height: 400px;"></div>