在饼图中显示百分比值

时间:2022-12-03 17:37:55

I did the coding to create a pie chart file in png format file; the pie chart shows the percentage, but it's in the bottom of the file. I want the percentage to appear in the chart itself. This is the code I have used:

我做了编码,以png格式文件创建饼图文件;饼图显示百分比,但它位于文件的底部。我希望百分比出现在图表本身中。这是我用过的代码:

String query = "SELECT name,flag from mawarid";

JDBCPieDataset dataset = new JDBCPieDataset(
    "jdbc:oracle:thin:@ 127.0.0.1:1521:XE", "oracle.jdbc.OracleDriver", "", "");

dataset.executeQuery(query);

JFreeChart chart = ChartFactory.createPieChart(
    "Test", dataset, true, true, false);
try {
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLegendLabelGenerator(
        new StandardPieSectionLabelGenerator("{0} {2}"));

    final ChartRenderingInfo info = new ChartRenderingInfo(
        new StandardEntityCollection());
    final File file1 = new File("Chart5.png");
    ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);

} catch (Exception e) {
    // log exception
}

2 个解决方案

#1


2  

You can use the same message format in a label generator as you do in your legend label generator:

您可以在标签生成器中使用与图例标签生成器中相同的消息格式:

plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {2}"));

#2


1  

Try using PieLabelDistributor on the PiePlot.

尝试在PiePlot上使用PieLabelDistributor。

plot.setLabelDistributor(PieLabelDistributor aDistributor)

#1


2  

You can use the same message format in a label generator as you do in your legend label generator:

您可以在标签生成器中使用与图例标签生成器中相同的消息格式:

plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {2}"));

#2


1  

Try using PieLabelDistributor on the PiePlot.

尝试在PiePlot上使用PieLabelDistributor。

plot.setLabelDistributor(PieLabelDistributor aDistributor)