2014-03-14 126 views
1

我想從現有的應用程序生成一個餅圖,但我得到的錯誤。 最新的錯誤是:Jfreechart餅圖生成

createPieChart(java.lang.String中,org.jfree.data.general.PieDataset,org.jfree.chart.labels.PieSectionLabelGenerator)在efa.util.chart.ChartUtil不能被應用到(java.lang.String,org.jfree.data.general.PieDataset) [javac] sectorChart = 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到函數作爲參數...

回答

1

關於你的編輯:

有一個類實現了您需要的接口,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; 
} 
} 
+0

感謝這個,你能不能有一個重新審視我的QN ... – stretchr

+0

的編輯後,如果您只是通過空的labelGenerator該代碼(這是不是JFreeChart的部分)將爲您創建的標籤生成器。 –