关于JFreeChart中柱状图展示数字小数位数处理

mmp,JFreeChart就是个坑,这两天就为了解决柱状图展示的小数位数问题

1、BarRenderer renderer = new BarRenderer();
2、renderer.setDefaultItemLabelsVisible(true);
3、renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());

第二行是展示柱状图后的数据
第三行是也是关于柱状图后的数据设置,但是默认是保留三位小数
在这里插入图片描述
我后面设置了

renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator("",new DecimalFormat("#.####")));

发现第一个参数是决定柱状图后面展示的数据
找了N久的资料,发现有个占位符
改成

renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("#.####")));

即可,第二个刚好是传入的参数,还有其他占位参数,可以试试不同下标

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 `PieSectionLabelGenerator` 接口的 `generateSectionLabel` 方法来设置饼图标签的显示格式。具体来说,你需要实现该接口并重写 `generateSectionLabel` 方法来返回你想要显示的标签字符串。 下面是一个示例代码,展示如何生成一个带有百分比小数位的饼图: ```java import org.jfree.chart.*; import org.jfree.chart.labels.*; import org.jfree.chart.plot.*; import org.jfree.data.general.*; public class PieChartExample { public static void main(String[] args) { // 创建据集 DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("A", 30); dataset.setValue("B", 20); dataset.setValue("C", 50); // 创建饼图并设置标签显示格式 JFreeChart chart = ChartFactory.createPieChart( "Pie Chart Example", dataset, true, // legend true, // tooltips false // URLs ); PiePlot plot = (PiePlot) chart.getPlot(); plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})", null, "%.1f%%")); // 显示图形 ChartFrame frame = new ChartFrame("Pie Chart", chart); frame.pack(); frame.setVisible(true); } } ``` 在上面的代码,我们使用 `StandardPieSectionLabelGenerator` 类来设置标签字符串格式。其第一个参 `{0} ({2})` 表示使用 `{0}` 来表示饼图扇形的名称,使用 `{2}` 来表示饼图扇形所占百分比。第二个参 `null` 表示不使用其他标签信息。第三个参 `"%.1f%%"` 表示将百分比格式化为一位小数,并在后面添加 `%` 符号。 你可以根据需要修改上述代码以生成所需的饼图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值