0

这是我在 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" >   

如何在图例中添加百分比?谢谢!

4

2 回答 2

4

StandardPieSectionLabelGenerator可以做到这一点。

PiePlot plot = (PiePlot) chart.getPlot();
plot.setLegendLabelGenerator(
    new StandardPieSectionLabelGenerator("{0} {1} {2}"));
于 2012-07-04T03:30:40.050 回答
0

第 1 步:首先从http://www.java2s.com/Code/Jar/j/Downloadjfreechartjar.htm下载 jfreechart

第 2 步:在构建路径中包含 jar 文件

第三步:添加以下代码

 PiePlot plot2 = (PiePlot) chart.getPlot();
       plot2.setLabelGenerator(new StandardPieItemLabelGenerator(
                "{0} ({2})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()
            ));
于 2014-02-12T09:37:55.390 回答