关于JfreeChart柱状图,数值显示的问题
发布网友
发布时间:2022-04-08 19:33
我来回答
共2个回答
懂视网
时间:2022-04-08 23:54
extendedstackedbarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{3}",
2 NumberFormat.getPercentInstance(), new DecimalFormat("#0.0%")));
3
但是柱体的返回的值是10.1/50.1=20.2%
,40.1/50.1=79.8%
这不符合预期目标,所以就去看了一下源码,在StandardCategoryItemLabelGenerator
的父类AbstractCategoryItemLabelGenerator
中发现有createItemArray
这么一个方法:
可以发现柱体的标签值应该是由这个方法进行返回的,因此自己就对StandardCategoryItemLabelGenerator
进行了继承,并重写了这个方法。
在ExtendedStandardCategoryItemLabelGeneratory
中增加了isPercent
作为标签值是显示百分比还是仅仅格式化的判断参数,并重写了createItemArray
这个方法。
此时得到的效果如图:
然而,柱体的总值还是没有格式化。再看了一下ExtendedStackedBarRenderer
这个类的代码,找到了totalFormat
这个属性,将其值赋为new DecimalFormat("#0.0%")
。测试:
基本是想要的最终结果,下面是测试代码
- 1 <dependency>
- 2 <groupId>jfree</groupId>
- 3 <artifactId>jfreechart</artifactId>
- 4 <version>1.0.13</version>
- 5 </dependency>
ExtendedStackedBarRenderer
- 1package com.springapp.jfreechar;
- 2
- 3import java.awt.Color;
- 4import java.awt.Font;
- 5import java.awt.Graphics2D;
- 6import java.awt.geom.Rectangle2D;
- 7import java.text.DecimalFormat;
- 8import java.text.NumberFormat;
- 9
- 10import org.jfree.chart.axis.CategoryAxis;
- 11import org.jfree.chart.axis.ValueAxis;
- 12import org.jfree.chart.entity.CategoryItemEntity;
- 13import org.jfree.chart.entity.EntityCollection;
- 14import org.jfree.chart.labels.CategoryToolTipGenerator;
- 15import org.jfree.chart.plot.CategoryPlot;
- 16import org.jfree.chart.plot.PlotOrientation;
- 17import org.jfree.chart.renderer.category.CategoryItemRendererState;
- 18import org.jfree.chart.renderer.category.StackedBarRenderer;
- 19import org.jfree.data.category.CategoryDataset;
- 20import org.jfree.text.TextUtilities;
- 21import org.jfree.ui.TextAnchor;
- 22
- 23public class ExtendedStackedBarRenderer extends StackedBarRenderer {
- 24
- 25 private static final long serialVersionUID = 1L;
- 26 private boolean showPositiveTotal;
- 27 private boolean showNegativeTotal;
- 28 private Font totalLabelFont;
- 29 private NumberFormat totalFormatter;
- 30 public ExtendedStackedBarRenderer() {
- 31 showPositiveTotal = true;
- 32 showNegativeTotal = true;
- 33 totalLabelFont = new Font("SansSerif", 1, 12);
- 34 totalFormatter = new DecimalFormat("#0.0%");
- 35 }
- 36
- 37 /**
- 38 * StackedBarRenderer 没有这个构造方法的,传入一个NumberFormat类,可以自定义实现每个柱体值显示格式
- 39 * @param totalFormatter
- 40 */
- 41 public ExtendedStackedBarRenderer(NumberFormat totalFormatter) {
- 42 showPositiveTotal = true;
- 43 showNegativeTotal = true;
- 44 totalLabelFont = new Font("SansSerif", 1, 12);
- 45 this.totalFormatter = totalFormatter;
- 46 }
- 47
- 48 public NumberFormat getTotalFormatter() {
- 49 return totalFormatter;
- 50 }
- 51
- 52 public void setTotalFormatter(NumberFormat numberformat) {
- 53 if (numberformat == null) {
- 54 throw new IllegalArgumentException("Null format not permitted.");
- 55 } else {
- 56 totalFormatter = numberformat;
- 57 return;
- 58 }
- 59 }
- 60
- 61 public void drawItem(Graphics2D graphics2d, CategoryItemRendererState categoryitemrendererstate, Rectangle2D rectangle2d, CategoryPlot categoryplot, CategoryAxis categoryaxis,
- 62 ValueAxis valueaxis, CategoryDataset categorydataset, int i, int j, int k) {
- 63 Number number = categorydataset.getValue(i, j);
- 64 if (number ==
var cpro_id = "u6292429";
热心网友
时间:2022-04-08 21:02
你试试这句话:
BarRenderer renderer=new BarRenderer();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);