🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 12.7 实例2 ### 12.7.1 概述 在本实例中,目的是在每个系列的标签上显示出值和百分比值(这个百分比值,这个系列在某一部分的条形直方图或全部条形直方图的总值中的比值)。如下图12.4所示。 ![](img/jfc80114.png) 图12.4 带有比值的直方图 该实现中,标签产生器计算出百分比。如果传入构造函数的是一个种类索引,那么这个百分比的基数就是指定种类的当前系列的值。如果种类索引是无效的,那么这个基数就是指定种类的全部系列总和。 标签产生器会默认创建一个百分比格式——一种比较成熟的格式,提供格式化能力。 ### 12.7.2 源代码 ``` package demo; import java.awt.Color; import java.awt.Dimension; import java.text.NumberFormat; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.AxisLocation; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.labels.AbstractCategoryItemLabelGenerator; import org.jfree.chart.labels.CategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class ItemLabelDemo2 extends ApplicationFrame { static class LabelGenerator extends AbstractCategoryItemLabelGenerator implements CategoryItemLabelGenerator { private Integer category; private NumberFormat formatter = NumberFormat.getPercentInstance(); public LabelGenerator(int i) { this(new Integer(i)); } public LabelGenerator(Integer integer) { super("", NumberFormat.getInstance()); category = integer; } public String generateLabel(CategoryDataset categorydataset, int i, int i_0_) { String string = null; double d = 0.0; if (category != null) { Number number = categorydataset .getValue(i, category.intValue()); d = number.doubleValue(); } else d = calculateSeriesTotal(categorydataset, i); Number number = categorydataset.getValue(i, i_0_); if (number != null) { double d_1_ = number.doubleValue(); string = (number.toString() + " (" + formatter.format(d_1_ / d) + ")"); } return string; } private double calculateSeriesTotal(CategoryDataset categorydataset, int i) { double d = 0.0; for (int i_2_ = 0; i_2_ < categorydataset.getColumnCount(); i_2_++) { Number number = categorydataset.getValue(i, i_2_); if (number != null) d += number.doubleValue(); } return d; } } public ItemLabelDemo2(String string) { super(string); CategoryDataset categorydataset = createDataset(); JFreeChart jfreechart = createChart(categorydataset); ChartPanel chartpanel = new ChartPanel(jfreechart); chartpanel.setPreferredSize(new Dimension(500, 270)); setContentPane(chartpanel); } private static CategoryDataset createDataset() { DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset(); defaultcategorydataset.addValue(100.0, "S1", "C1"); defaultcategorydataset.addValue(44.3, "S1", "C2"); defaultcategorydataset.addValue(93.0, "S1", "C3"); defaultcategorydataset.addValue(80.0, "S2", "C1"); defaultcategorydataset.addValue(75.1, "S2", "C2"); defaultcategorydataset.addValue(15.1, "S2", "C3"); return defaultcategorydataset; } private static JFreeChart createChart(CategoryDataset categorydataset) { JFreeChart jfreechart = ChartFactory.createBarChart( "Item Label Demo 2", "Category", "Value", categorydataset, PlotOrientation.HORIZONTAL, true, true, false); jfreechart.setBackgroundPaint(Color.white); CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setDomainGridlinePaint(Color.white); categoryplot.setRangeGridlinePaint(Color.white); categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setUpperMargin(0.25); CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer(); categoryitemrenderer.setBaseItemLabelsVisible(true); categoryitemrenderer.setBaseItemLabelGenerator(new LabelGenerator( (Integer) null)); return jfreechart; } public static JPanel createDemoPanel() { JFreeChart jfreechart = createChart(createDataset()); return new ChartPanel(jfreechart); } public static void main(String[] strings) { ItemLabelDemo2 itemlabeldemo2 = new ItemLabelDemo2("Item Label Demo 2"); itemlabeldemo2.pack(); RefineryUtilities.centerFrameOnScreen(itemlabeldemo2); itemlabeldemo2.setVisible(true); } } ```