这是我在 JSP 中创建饼图的代码。
Connection conn = DBConnection.getConnection();
String strQuery = "SELECT keyword,count(*) from paper_keywords group by keyword";
PreparedStatement preparedStatement = conn.prepareStatement(strQuery);
ResultSet rs = preparedStatement.executeQuery();
DefaultPieDataset pieDataset = new DefaultPieDataset();
while(rs.next())
{
pieDataset.setValue(rs.getString(1),rs.getInt(2));
}
rs.close();
preparedStatement.close();
JFreeChart chart = ChartFactory.createPieChart ("Popular Keywords", pieDataset, true,true,true);
try {
final ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection());
String test = getServletContext().getRealPath("/");
final File file1 = new File(test+"chart/areachart.png");
ChartUtilities.saveChartAsPNG(file1, chart, 550, 400, info);
}
catch (Exception e) {
out.println(e);
}
我稍后会在 jsp 页面中使用它:
<IMG SRC="../chart/areachart.png" >
如何在图例中添加百分比?谢谢!