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

📄 meterlegend.java

📁 大家打开看看啊, 很有用的东西
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ======================================
 * JFreeChart : a free Java chart library
 * ======================================
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 * Project Lead:  David Gilbert (david.gilbert@object-refinery.com);
 *
 * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
 *
 * 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.
 *
 * ----------------
 * MeterLegend.java
 * ----------------
 * (C) Copyright 2000-2003, by Hari and Contributors.
 *
 * Original Author:  Hari (ourhari@hotmail.com);
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * $Id: MeterLegend.java,v 1.3 2003/06/13 15:44:46 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);
 *
 */

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.MeterDataset;

/**
 * A legend for meter plots.
 *
 * @author Hari
 */
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;


    /**
     * Constructs a new legend.
     *
     * @param chart  the chart.
     * @param legendText  the legend text.
     *
     */
    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, MeterDataset data, int type, int index,
                                      LegendItem[] legendItems, Paint[] legendItemColors) {

        boolean ret = false;
        String label = null;
        Number minValue = null;
        Number maxValue = null;
        Paint paint = null;

        switch(type) {
            case MeterDataset.NORMAL_DATA:
                minValue = data.getMinimumNormalValue();
                maxValue = data.getMaximumNormalValue();
                paint = plot.getNormalPaint();
                label = MeterPlot.NORMAL_TEXT;
                break;
            case MeterDataset.WARNING_DATA:
                minValue = data.getMinimumWarningValue();
                maxValue = data.getMaximumWarningValue();
                paint = plot.getWarningPaint();
                label = MeterPlot.WARNING_TEXT;
                break;
            case MeterDataset.CRITICAL_DATA:
                minValue = data.getMinimumCriticalValue();
                maxValue = data.getMaximumCriticalValue();
                paint = plot.getCriticalPaint();
                label = MeterPlot.CRITICAL_TEXT;
                break;
            case MeterDataset.FULL_DATA:
                minValue = data.getMinimumValue();
                maxValue = data.getMaximumValue();
                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, 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;
        MeterDataset data = meterPlot.getMeterDataset();

        legendCount = 1;  // Name of the Chart.
        legendCount++;    // Display Full Range
        if (showCritical && data.getMinimumCriticalValue() != null) {
            legendCount++;
        }
        if (showWarning && data.getMinimumWarningValue() != null) {
            legendCount++;
        }
        if (showNormal && data.getMinimumNormalValue() != null) {
            legendCount++;

⌨️ 快捷键说明

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