📄 standardlegend.java
字号:
/* ===========================================================
* 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.]
*
* -------------------
* StandardLegend.java
* -------------------
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Andrzej Porebski;
* Luke Quinane;
* Barak Naveh;
*
* $Id: StandardLegend.java,v 1.1 2004/08/31 14:26:08 mungady Exp $
*
* Changes (from 20-Jun-2001)
* --------------------------
* 20-Jun-2001 : Modifications submitted by Andrzej Porebski for legend placement;
* 18-Sep-2001 : Updated header and fixed DOS encoding problem (DG);
* 16-Oct-2001 : Moved data source classes to com.jrefinery.data.* (DG);
* 19-Oct-2001 : Moved some methods [getSeriesPaint(...) etc.] from JFreeChart to Plot (DG);
* 22-Jan-2002 : Fixed bug correlating legend labels with pie data (DG);
* 06-Feb-2002 : Bug fix for legends in small areas (DG);
* 23-Apr-2002 : Legend item labels are now obtained from the plot, not the chart (DG);
* 20-Jun-2002 : Added outline paint and stroke attributes for the key boxes (DG);
* 18-Sep-2002 : Fixed errors reported by Checkstyle (DG);
* 23-Sep-2002 : Changed the name of LegendItem --> DrawableLegendItem (DG);
* 02-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 16-Oct-2002 : Adjusted vertical text position in legend item (DG);
* 17-Oct-2002 : Fixed bug where legend items are not using the font that has been set (DG);
* 11-Feb-2003 : Added title code by Donald Mitchell, removed unnecessary constructor (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 22-Sep-2003 : Added nullpointer checks (TM);
* 23-Sep-2003 : Fixed bug in equals(...) method (DG);
* 08-Oct-2003 : Applied patch for displaying series line style, contributed by Luke Quinane (DG);
* 23-Dec-2003 : Added scale factors (x and y) for shapes displayed in legend (DG);
* 26-Mar-2004 : Added option to control item order, contributed by Angel (DG);
* 26-Mar-2004 : Added support for 8 more anchor points (BN);
* 27-Mar-2004 : Added support for round corners of bounding box (BN);
* 07-Apr-2004 : Changed text bounds calculation (DG);
* 21-Apr-2004 : Barak Naveh has contributed word-wrapping for legend items (BN);
*
*/
package org.jfree.chart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.LineMetrics;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.awt.geom.RoundRectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.entity.LegendItemEntity;
import org.jfree.chart.event.LegendChangeEvent;
import org.jfree.io.SerialUtilities;
import org.jfree.text.TextUtilities;
import org.jfree.ui.Spacer;
import org.jfree.ui.TextAnchor;
import org.jfree.util.Log;
import org.jfree.util.LogContext;
import org.jfree.util.ObjectUtils;
/**
* A chart legend shows the names and visual representations of the series
* that are plotted in a chart.
*/
public class StandardLegend extends Legend implements Serializable {
/** The default outer gap. */
public static final Spacer DEFAULT_OUTER_GAP = new Spacer(Spacer.ABSOLUTE, 3, 3, 3, 3);
/** The default inner gap. */
public static final Spacer DEFAULT_INNER_GAP = new Spacer(Spacer.ABSOLUTE, 2, 2, 2, 2);
/** The default outline stroke. */
public static final Stroke DEFAULT_OUTLINE_STROKE = new BasicStroke();
/** The default outline paint. */
public static final Paint DEFAULT_OUTLINE_PAINT = Color.gray;
/** The default background paint. */
public static final Paint DEFAULT_BACKGROUND_PAINT = Color.white;
/** The default title font. */
public static final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 11);
/** The default item font. */
public static final Font DEFAULT_ITEM_FONT = new Font("SansSerif", Font.PLAIN, 10);
/**
* Used with {@link #setPreferredWidth(double)} to indicate that no preferred
* width is desired and defaults are to be used.
*/
public static final double NO_PREFERRED_WIDTH = Double.MAX_VALUE;
/** Reported when illegal legend is unexpectedly found. */
private static final String UNEXPECTED_LEGEND_ANCHOR = "Unexpected legend anchor";
/** The amount of blank space around the legend. */
private Spacer outerGap;
/** The stroke used to draw the outline of the legend. */
private transient Stroke outlineStroke;
/** The paint used to draw the outline of the legend. */
private transient Paint outlinePaint;
/** The paint used to draw the background of the legend. */
private transient Paint backgroundPaint;
/** The blank space inside the legend box. */
private Spacer innerGap;
/** An optional title for the legend. */
private String title;
/** The font used to display the legend title. */
private Font titleFont;
/** The font used to display the legend item names. */
private Font itemFont;
/** The paint used to display the legend item names. */
private transient Paint itemPaint;
/** A flag controlling whether or not outlines are drawn around shapes.*/
private boolean outlineShapes;
/** The stroke used to outline item shapes. */
private transient Stroke shapeOutlineStroke = new BasicStroke(0.5f);
/** The paint used to outline item shapes. */
private transient Paint shapeOutlinePaint = Color.lightGray;
/** A flag that controls whether the legend displays the series shapes. */
private boolean displaySeriesShapes;
/** The x scale factor for shapes displayed in the legend. */
private double shapeScaleX = 1.0;
/** The y scale factor for shapes displayed in the legend. */
private double shapeScaleY = 1.0;
/** A flag that controls whether the legend displays the series line */
private boolean displaySeriesLines;
/** The order of the legend items. */
private LegendRenderingOrder renderingOrder = LegendRenderingOrder.STANDARD;
/** The width of the arc used to round off the corners of the bounding box. */
private int boundingBoxArcWidth = 0;
/** The height of the arc used to round off the corners of the bounding box. */
private int boundingBoxArcHeight = 0;
/** The preferred width of the legend bounding box. */
private double preferredWidth = NO_PREFERRED_WIDTH;
/** Access to logging facilities. */
private static final LogContext LOGGER = Log.createContext(StandardLegend.class);
/**
* Constructs a new legend with default settings.
*/
public StandardLegend() {
this.outerGap = DEFAULT_OUTER_GAP;
this.innerGap = DEFAULT_INNER_GAP;
this.backgroundPaint = DEFAULT_BACKGROUND_PAINT;
this.outlineStroke = DEFAULT_OUTLINE_STROKE;
this.outlinePaint = DEFAULT_OUTLINE_PAINT;
this.title = null;
this.titleFont = DEFAULT_TITLE_FONT;
this.itemFont = DEFAULT_ITEM_FONT;
this.itemPaint = Color.black;
this.displaySeriesShapes = false;
this.displaySeriesLines = false;
}
/**
* Creates a new legend.
*
* @param chart the chart that the legend belongs to.
*
* @deprecated use the default constructor instead and let JFreeChart manage
* the chart reference
*/
public StandardLegend(JFreeChart chart) {
this();
}
/**
* Returns the outer gap for the legend. This is the amount of blank space around the outside
* of the legend.
*
* @return The gap (never <code>null</code>).
*/
public Spacer getOuterGap() {
return this.outerGap;
}
/**
* Sets the outer gap for the legend and sends a {@link LegendChangeEvent} to all
* registered listeners.
*
* @param outerGap the outer gap (<code>null</code> not permitted).
*/
public void setOuterGap(Spacer outerGap) {
if (outerGap == null) {
throw new NullPointerException("Null 'outerGap' argument.");
}
this.outerGap = outerGap;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the inner gap for the legend. This is the amount of blank space around the inside
* of the legend.
*
* @return The gap (never <code>null</code>).
*/
public Spacer getInnerGap() {
return this.innerGap;
}
/**
* Sets the inner gap for the legend and sends a {@link LegendChangeEvent} to all
* registered listeners.
*
* @param innerGap the inner gap (<code>null</code> not permitted).
*/
public void setInnerGap(Spacer innerGap) {
if (innerGap == null) {
throw new NullPointerException("Null 'innerGap' argument.");
}
this.innerGap = innerGap;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the background paint for the legend.
*
* @return The background paint (never <code>null</code>).
*/
public Paint getBackgroundPaint() {
return this.backgroundPaint;
}
/**
* Sets the background paint for the legend and sends a {@link LegendChangeEvent} to all
* registered listeners.
*
* @param paint the paint (<code>null</code> not permitted).
*/
public void setBackgroundPaint(Paint paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
this.backgroundPaint = paint;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the outline stroke.
*
* @return The outline stroke (never <code>null</code>).
*/
public Stroke getOutlineStroke() {
return this.outlineStroke;
}
/**
* Sets the outline stroke and sends a {@link LegendChangeEvent} to all registered
* listeners.
*
* @param stroke the stroke (<code>null</code> not permitted).
*/
public void setOutlineStroke(Stroke stroke) {
if (stroke == null) {
throw new NullPointerException("Null 'stroke' argument.");
}
this.outlineStroke = stroke;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the outline paint.
*
* @return The outline paint (never <code>null</code>).
*/
public Paint getOutlinePaint() {
return this.outlinePaint;
}
/**
* Sets the outline paint and sends a {@link LegendChangeEvent} to all registered listeners.
*
* @param paint the paint (<code>null</code> not permitted).
*/
public void setOutlinePaint(Paint paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
this.outlinePaint = paint;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Gets the title for the legend.
*
* @return The title (possibly <code>null</code>).
*/
public String getTitle() {
return this.title;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -