显示JFreeChart数值

3

我已经在JFreeChart中创建了一个饼图。然而,“分片”上没有出现数值。我该如何让它显示出来?


在“slices”上是指什么?是直接在“slices”上,还是在“slice”的标签上? - Jes
直接将此应用于条形图(另一方面的条形上)。 - codix
1个回答

6
PiePlot 上,可以使用 setSimpleLabels 方法实现此功能。
假设您已经创建了图表:
Chart chart = // your created chart
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSimpleLabels(true); 

如果你需要更简单的外观,可以使用透明颜色来设置标签阴影、轮廓和背景绘制:

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)); 

编辑:显示切片中的值而不是图例:

plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}"));
StandardPieSectionLabelGenerator 还有一个数字格式化器,如果您需要的话可以使用它。
对于饼图来说,就这样了。
至于柱状图,您可以使用 BarRenderer 上的 setPositiveItemLabelPositionFallback(ItemLabelPosition position) 方法设置标签位置。对于内部标签,您可以使用以下代码:
barRenderer.setPositiveItemLabelPositionFallback(
     new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));

请确保字体大小适合条形码,当您尝试此操作时。

嗯... 那么显示的值是与图例中的值相同吗? - codix
1
请参考此答案 - trashgod
我使用了以下代码来绘制条形图,但标签没有显示: CategoryPlot plot = chart.getCategoryPlot(); BarRenderer barRenderer = (BarRenderer) plot.getRenderer();barRenderer.setNegativeItemLabelPositionFallback( new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)); - codix
你应该尝试使用 setPositiveLabelPositionFallback。如果你有 JFreeChart 的开发者手册,可以查阅标签锚点/位置的定义。 - Jes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接