java - Jfreechart饼图生成

标签 java charts jfreechart pie-chart

我正在尝试从现有应用程序生成饼图,但出现错误。 最新的错误是:

efa.util.chart.ChartUtil 中的 createPieChart(java.lang.String,org.jfree.data.general.PieDataset,org.jfree.chart.labels.PieSectionLabelGenerator) 无法应用于 (java.lang.String,org.jfree.data.general.PieDataset) [javac] zoneChart = ChartUtil.createPieChart("",xy); [javac]^

除此之外,我还需要在函数调用中添加第三个参数,即 PieSectionLabelGenerator labelGenerator。但是,我不知道该怎么做。已阅读 docs/api 但仍然不确定。你有什么建议?

我使用的代码是

imports (please see below)
declaration in the code is JFreeChart sectorChart;

the function: 

private void genChart(){
  DefaultPieDataset pieDataset = new DefaultPieDataset();
  pieDataset.setValue("JavaWorld", new Integer(75));
  pieDataset.setValue("Other", new Integer(25));

  PieDataset xy = pieDataset;
  sectorChart = ChartUtil.createPieChart("",xy,);
}

我不认为我的导入或对象声明有任何问题。如果您想查看这些内容,请告诉我,为了本文的简洁,我将它们保留在其中。

我无法让下面的答案专门适用于我的应用程序。但我的 ChartUtil.java 文件中有这段代码

public static JFreeChart createPieChart(final String title,
            final PieDataset dataset, PieSectionLabelGenerator labelGenerator) {

        final JFreeChart chart = ChartFactory.createPieChart(title, dataset,
                false, true, true);

        labelGenerator = labelGenerator != null ? labelGenerator
                : new StandardPieSectionLabelGenerator("{2} {0}",
                        NumberFormat.getNumberInstance(),
                        NumberFormat.getPercentInstance());

        PiePlot plot = (PiePlot) chart.getPlot();
        ChartUtil.formatPiePlot(plot);
        plot.setLabelGenerator(labelGenerator);

        PieRenderer renderer = new PieRenderer();
        renderer.setColor((PiePlot) chart.getPlot(), dataset);

        return chart;

    }

我相信我们需要弄清楚如何将 PieSectionLabelGenerator 作为参数传递到函数中......

最佳答案

关于您的编辑:

有一个类实现了您需要的接口(interface),StandardPieSectionLabelGenerator,当传递给您的方法时应该允许创建图表。另一种方法是创建一个类来自己实现该方法。我认为这取决于您想要通过标签实现的目标。

public class SO {
public static void main(String[] args){
      DefaultPieDataset pieDataset = new DefaultPieDataset();
      pieDataset.setValue("JavaWorld", new Integer(75));
      pieDataset.setValue("Other", new Integer(25));

      JFreeChart chart = SO.createPieChart("Pie", pieDataset, new StandardPieSectionLabelGenerator());
      JFrame frame = new JFrame("Pie");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(new ChartPanel(chart));
      frame.pack();
      RefineryUtilities.centerFrameOnScreen(frame);
      frame.setVisible(true);
}

public static JFreeChart createPieChart(final String title,
        final PieDataset dataset, PieSectionLabelGenerator labelGenerator) {

    final JFreeChart chart = ChartFactory.createPieChart(title, dataset, false, true, true);
    PiePlot plot = (PiePlot) chart.getPlot();
    ChartUtil.formatPiePlot(plot);
    plot.setLabelGenerator(labelGenerator);
    PieRenderer renderer = new PieRenderer();
    renderer.setColor((PiePlot) chart.getPlot(), dataset);
    return chart;
}
}

关于java - Jfreechart饼图生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22399759/

相关文章:

方法的Java模糊类型?

java - 如何在java中通过递归连接字符串生成方程?

javascript - NVD3 图表抑制隐藏图表的渲染

c# - 如何在折线图中显示附加点信息?

java - 如何使用 JFreeChart 固定自动范围避免负值

java - Maven - 无法执行目标 org.apache.maven.plugins :maven-compiler-plugin:3. 7.0:使用 java 10 进行编译

用于 Web 应用程序的 Java 图形库?

java - 启用/禁用 JFreeChart 的绘制

java - 具有可选域轴和缩放的 JFreeChart

java - 如何启用 java HotSpot VM 编译器