Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display JFreeChart Values

Tags:

jfreechart

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

like image 201
codix Avatar asked Jul 08 '26 04:07

codix


1 Answers

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.

like image 195
Jes Avatar answered Jul 11 '26 22:07

Jes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!