1

I am trying to generate a piechart from an existing application but am getting errors. The latest error is:

createPieChart(java.lang.String,org.jfree.data.general.PieDataset,org.jfree.chart.labels.PieSectionLabelGenerator) in efa.util.chart.ChartUtil cannot be applied to (java.lang.String,org.jfree.data.general.PieDataset) [javac] sectorChart = ChartUtil.createPieChart("",xy); [javac] ^

Besides this, I also need to add a third argument in the function call, which is PieSectionLabelGenerator labelGenerator. However, I don't know how to do that. Have read the docs/api but still am unsure. What would you advise?

The code I am using is

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,);
}

I don't think there is anything wrong with my imports, nor declaration of the object. Please let me know if you'd like to see these, I'm leaving them out for brevity of this post.

I'm unable to make the answer below work for my app specifically. But I have this fragment of code in my ChartUtil.java file

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;

    }

I believe we need to figure out how to pass a PieSectionLabelGenerator into the function as an argument...

stretchr
  • 615
  • 2
  • 9
  • 24

1 Answers1

1

Regarding your edit:

There is a class that implements the interface you require, the StandardPieSectionLabelGenerator which when passed to your method should allow for chart creation. The alternative is creating a class to implement the method yourself. I think it depends on exactly what you are trying to achieve with the labels.

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;
}
}
Levenal
  • 3,796
  • 3
  • 24
  • 29
  • If you just pass null for the labelGenerator that code (which is not part of JFreeChart) will create the label generator for you. – David Gilbert Mar 14 '14 at 13:55