我有谷歌的饼图,我试图填充数据库中的数据。我可以使用我想要的结果得到一个结果集但是我无法让它工作,因为我不知道如何获取我的java变量并将它们变成javascript变量,以便它们与google代码一起使用。
当我尝试下面的代码时,我在这一行上收到错误
var zipie = <%= zip1 %>;
说zip1 cannot be resolved to a variable
试图直接在javascript中调用我的java变量
<script type="text/javascript">
google.load('visualization', '1', {
'packages' : [ 'corechart' ]
});
<%
try {
PieChartDAO pDao = new PieChartDAO();
String sql = PieChartSQLCreation. getTopZipCode();
ResultSet zipr = pDao.getTopZip(sql);
int[] zip = new int[5];
String[] zips = new String[5];
for(int i = 0; i <5; i++) {
zip[i] = zipr.getInt("num");
zips[i] = zipr.getString("zip_code");
zipr.next();
}
int zip1 = zip[0];
}
catch (Exception e) {
e.printStackTrace();
}
%>
var zipie = <%= zip1 %> ;
google.setOnLoadCallback(drawVisualization);
function drawVisualization() {
visualization_data = new google.visualization.DataTable();
visualization_data.addColumn('string', 'Zip Code');
visualization_data.addColumn('number', 'Number of Students');
visualization_data.addRow([ 'Work', zipie ]);
visualization_data.addRow([ 'Eat', 2 ]);
visualization_data.addRow([ 'Commute', 2 ]);
visualization_data.addRow([ 'Watch TV', 2 ]);
visualization_data.addRow([ 'Sleep', 7 ]);
visualization = new google.visualization.PieChart(
document.getElementById('piechart'));
visualization.draw(visualization_data, {
title : 'Zip Codes',
height : 260
});
}
</script>
答案 0 :(得分:1)
zip1
是try
块的本地。事先声明它,使其处于正确的范围内。
int zip1 = -1; // some default value
try {
// [...]
}