⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 meterlegend.java

📁 jfreechart安装程序和使用说明
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ----------------
 * MeterLegend.java
 * ----------------
 * (C) Copyright 2000-2004, by Hari and Contributors.
 *
 * Original Author:  Hari (ourhari@hotmail.com);
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * $Id: MeterLegend.java,v 1.1 2004/08/31 14:26:08 mungady Exp $
 *
 * Changes
 * -------
 * 01-Apr-2002 : Version 1, contributed by Hari (DG);
 * 25-Jun-2002 : Updated imports and Javadoc comments (DG);
 * 18-Sep-2002 : Updated for changes to StandardLegend (DG);
 * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
 * 14-Jan-2003 : Changed outer gap to a Spacer (DG);
 * 11-Feb-2003 : Removed constructor in line with changes to StandardLegend class (DG);
 * 07-Apr-2004 : Changed text bounds calculation (DG);
 *
 */

package org.jfree.chart;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.font.LineMetrics;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.event.LegendChangeEvent;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.data.general.ValueDataset;

/**
 * A legend for meter plots.
 */
public class MeterLegend extends StandardLegend {

    /** The legend text. */
    private String legendText;

    /** Show the normal range? */
    private boolean showNormal = true;

    /** Show the warning range? */
    private boolean showWarning = true;

    /** Show the critical range? */
    private boolean showCritical = true;

    /**
     * The default constructor; sets an empty legend text.
     */
    public MeterLegend() {
        this ("");
    }

    /**
     * Constructs a new legend.
     *
     * @param legendText  the legend text.
     *
     */
    public MeterLegend(String legendText) {
        this.legendText = legendText;
    }

    /**
     * Creates a new legend.
     *
     * @param chart  the chart that the legend belongs to.
     * @param legendText  the legend text.
     * 
     * @deprecated use the default constructor instead and let JFreeChart manage
     * the chart reference
     */
    public MeterLegend(JFreeChart chart, String legendText) {
        super(chart);
        this.legendText = legendText;
    }

    /**
     * Returns the legend text.
     *
     * @return the legend text.
     */
    public String getLegendText() {
        return this.legendText;
    }

    /**
     * Sets the legend text.
     *
     * @param text the new legend text.
     */
    public void setLegendText(String text) {
        this.legendText = text;
        notifyListeners(new LegendChangeEvent(this));
    }

    /**
     * Draws the legend.
     *
     * @param g2  the graphics device.
     * @param available  the available area.
     *
     * @return the remaining available drawing area.
     */
    public Rectangle2D draw(Graphics2D g2, Rectangle2D available) {

        return draw(g2, available, (getAnchor() & HORIZONTAL) != 0, (getAnchor() & INVERTED) != 0);

    }

    /**
     * Updates the legend information.
     *
     * @param plot  the plot.
     * @param data  the dataset.
     * @param type  the type.
     * @param index  the index.
     * @param legendItems  the legend items.
     * @param legendItemColors  the colors.
     *
     * @return boolean.
     */
    private boolean updateInformation(MeterPlot plot, ValueDataset data, int type, int index,
                                      LegendItem[] legendItems, Paint[] legendItemColors) {

        boolean ret = false;
        String label = null;
//        double minValue = 0.0;
//        double maxValue = 0.0;
        Paint paint = null;

        switch(type) {
            case MeterPlot.NORMAL_DATA_RANGE:
//                minValue = plot.getNormalRange().getLowerBound();
//                maxValue = plot.getNormalRange().getUpperBound();
                paint = plot.getNormalPaint();
                label = MeterPlot.NORMAL_TEXT;
                break;
            case MeterPlot.WARNING_DATA_RANGE:
//                minValue = plot.getWarningRange().getLowerBound();
//                maxValue = plot.getWarningRange().getUpperBound();
                paint = plot.getWarningPaint();
                label = MeterPlot.WARNING_TEXT;
                break;
            case MeterPlot.CRITICAL_DATA_RANGE:
//                minValue = plot.getCriticalRange().getLowerBound();
//                maxValue = plot.getCriticalRange().getUpperBound();
                paint = plot.getCriticalPaint();
                label = MeterPlot.CRITICAL_TEXT;
                break;
            case MeterPlot.FULL_DATA_RANGE:
//                minValue = plot.getRange().getLowerBound();
//                maxValue = plot.getRange().getUpperBound();
                paint = MeterPlot.DEFAULT_BACKGROUND_PAINT;
                label = "Meter Graph";
                break;
            default:
                return false;
        }

//        if (minValue != null && maxValue != null) {
//            if (data.getBorderType() == type) {
                label += "  Range: ";
//                      + data.getMinimumValue().toString() + " to "
//                      + minValue.toString()
//                      + "  and  "
//                      + maxValue.toString() + " to "
//                      + data.getMaximumValue().toString();
//            }
//            else {
//                label += "  Range: " + minValue.toString() + " to " + maxValue.toString();
//            }
            legendItems[index] = new LegendItem(label, label, null, true, null, null, null, null);
            legendItemColors[index] = paint;
            ret = true;
        //}
        return ret;
    }

    /**
     * Draws the legend.
     *
     * @param g2  the graphics device.
     * @param available  the available drawing area.
     * @param horizontal  if <code>true</code> draw a horizontal legend.
     * @param inverted  ???
     *
     * @return the remaining available drawing area.
     */
    protected Rectangle2D draw(Graphics2D g2, Rectangle2D available,
                               boolean horizontal, boolean inverted) {

        int legendCount = 0;
        Plot plot = getChart().getPlot();
        if (!(plot instanceof MeterPlot)) {
            throw new IllegalArgumentException("Plot must be MeterPlot");
        }
        MeterPlot meterPlot = (MeterPlot) plot;
        ValueDataset data = meterPlot.getDataset();

        legendCount = 1;  // Name of the Chart.
        legendCount++;    // Display Full Range

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -