0

I have a number of categories defined with uppercase alphabet letters (say 'Cat-1', 'Cat-2', ... 'Cat-10') and I would like to display in a JFreeChart with a Pie dataset the short names on the pie itself and the long descriptions in the legend, instead of the long descriptions appearing both on the legend and on the pie as shown in the following example (which results in a very cluttered pie due to the long descriptions):

enter image description here

How do I do that?

Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331

1 Answers1

1

You have to use a LabelGenerator to generate different labels.

Here is some sample code:

public JFreeChart createChart()
{
  chart = ...
  PiePlot plot = (PiePlot)chart.getPlot();
  plot.setLabelGenerator(new CustomLabelGenerator());

  return chart;
}

public class CustomLabelGenerator implements PieSectionLabelGenerator {
  public String generateSectionLabel(PieDataset dataset, Comparable key)
  {
    return "my Label for " + dataset.getValue(key);
  }
  public String generateSectionLabel(PieDataset dataset, Comparable key)
  {
    return null;
  }
}

See also this thread form the JFreeChart forum for some more details.

Martin Höller
  • 2,714
  • 26
  • 44
  • 1
    `CategoryPlot` does not contain a `setLabelGenerator` method: http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/plot/CategoryPlot.html – Marcus Junius Brutus Feb 25 '14 at 18:14
  • Sorry, I was mixing up different sources of information. PiePlot has this method and is what you are actually using. I hopefully sorted this out in my answer now. Let me know if there is still something wrong. – Martin Höller Feb 26 '14 at 08:15