我正在尝试从现有应用程序生成饼图,但出现错误。 最新的错误是:
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/