Reputation: 373
I have a piechart from google that I am trying to populate with data from my database. I can get a result set with my desired results but I am unable to get this to work because I am not sure how to take my java variables and make them into javascript variables so that they work with the google code.
When I try the code below I get an error on this line
var zipie = <%= zip1 %>;
saying that zip1 cannot be resolved to a variable
Trying to call my java variable in the javascript directly doesn't work
<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>
Upvotes: 1
Views: 90
Reputation: 3456
zip1
is local to the try
block. Declare this beforehand for it to be in the correct scope.
int zip1 = -1; // some default value
try {
// [...]
}
Upvotes: 1