I have created a pie chart in JFreeChart. However, numerical values are not appearing on the "slices". How do I make it appear there?
Asked
Active
Viewed 5,323 times
3
-
What do you mean on the slices? As in directly on the slices, or on a label on the slice? – Jes Aug 19 '11 at 08:51
-
directly. I also want to apply this on a bar chart (on the bar on the other hand) – codix Aug 19 '11 at 08:54
1 Answers
6
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.

Jes
- 2,748
- 18
- 22
-
hmm..so the values that will display is the same as that of the values in the legend? – codix Aug 19 '11 at 10:36
-
1See also this [answer](http://stackoverflow.com/questions/3547244/setting-label-and-value-of-the-chart/3551238#3551238). – trashgod Aug 19 '11 at 11:40
-
I used this code for the bar graph but the labels didn't show: CategoryPlot plot = chart.getCategoryPlot(); BarRenderer barRenderer = (BarRenderer) plot.getRenderer(); barRenderer.setNegativeItemLabelPositionFallback( new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));` – codix Aug 22 '11 at 01:40
-
You should try `setPositiveLabelPositionFallback`. If you have the developer manual for JFreeChart, go look up the label anchor/position definitions. – Jes Aug 22 '11 at 10:41