开发者

Display JFreeChart Values

开发者 https://www.devze.com 2023-03-29 10:00 出处:网络
I have created a pie chart in JFreeChart. However, numerical values are not appearing on the \"slices\". How d开发者_如何学Co I make it appear there?On a PiePlot this is possible using a setter setSim

I have created a pie chart in JFreeChart. However, numerical values are not appearing on the "slices". How d开发者_如何学Co I make it appear there?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消