codix
codix

Reputation: 275

Display JFreeChart Values

I have created a pie chart in JFreeChart. However, numerical values are not appearing on the "slices". How do I make it appear there?

Upvotes: 3

Views: 5351

Answers (1)

Jes
Jes

Reputation: 2778

On a PiePlot this is possible using a setter setSimpleLabels.

Assuming you have your created chart:

Chart chart = // your created chart
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSimpleLabels(true); 

If you need an even more plain look you can use a transparent color for the label shadow, outline and background paint:

plot.setOutlinePaint(new Color(0, 0, 0, 0));
plot.setLabelShadowPaint(new Color(0, 0, 0, 0));
plot.setLabelBackgroundPaint(new Color(0, 0, 0, 0));
plot.setLabelOutlinePaint(new Color(0, 0, 0, 0)); 

Edit: To display values instead of legends in the slices:

plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}"));

The StandardPieSectionLabelGenerator also has a number formatter if you need one of these.

That should do it for a pie chart.

As for a bar chart you can set the label positions on the BarRenderer using the setPositiveItemLabelPositionFallback(ItemLabelPosition position) method. For inside labels you could use:

barRenderer.setPositiveItemLabelPositionFallback(
     new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));

Be sure the font size can fit inside the bar when you try this.

Upvotes: 6

Related Questions