12.6 实例1

12.6.1 概述

在第一个实例中,目的就是当当条目的值大于某个限定的值时,就显示该标签。如图12.3所示。

如图12.3 超过某个限定值显示条目标签的实例

做到这一点并不困难,需要做以下工作:

  • 写一个实现接口CategoryItemLabelGenerator的类,并且实现generateItemLabel()方法。该方法实现如果条目的值小于限定值时,返回null。
  • 创建该类的实例,将该实例使用renderer的方法setLabelGenerator()设置到renderer中去。

12.6.2 源代码

package demo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.text.DecimalFormat;
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.NumberAxis;
import org.jfree.chart.labels.AbstractCategoryItemLabelGenerator;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class ItemLabelDemo1 extends ApplicationFrame {
    static class LabelGenerator extends AbstractCategoryItemLabelGenerator
        implements CategoryItemLabelGenerator {
        private double threshold;
        public LabelGenerator(double d) {
            super("", NumberFormat.getInstance());
            threshold = d;
        }
        public String generateLabel(CategoryDataset categorydataset, int i,
            int i_0_) {
            String string = null;
            Number number = categorydataset.getValue(i, i_0_);
            if (number != null) {
                double d = number.doubleValue();
                if (d > threshold)
                string = number.toString();
            }
            return string;
        }
    }
    public ItemLabelDemo1(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(11.0, "S1", "C1");
        defaultcategorydataset.addValue(44.3, "S1", "C2");
        defaultcategorydataset.addValue(93.0, "S1", "C3");
        defaultcategorydataset.addValue(35.6, "S1", "C4");
        defaultcategorydataset.addValue(75.1, "S1", "C5");
        return defaultcategorydataset;
    }
    private static JFreeChart createChart(CategoryDataset categorydataset) {
        JFreeChart jfreechart = ChartFactory.createBarChart(
            "Item Label Demo 1", "Category", "Value", categorydataset,
        PlotOrientation.VERTICAL, false, true, false);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setDomainGridlinePaint(Color.white);
        categoryplot.setRangeGridlinePaint(Color.white);
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setUpperMargin(0.15);
        return jfreechart;
    }
    public static JPanel createDemoPanel() {
        JFreeChart jfreechart = createChart(createDataset());
        return new ChartPanel(jfreechart);
    }
    public static void main(String[] strings) {
        ItemLabelDemo1 itemlabeldemo1 = new ItemLabelDemo1("Item Label Demo 1");
        itemlabeldemo1.pack();
        RefineryUtilities.centerFrameOnScreen(itemlabeldemo1);
        itemlabeldemo1.setVisible(true);
    }
}