📄 standardlegend.java
字号:
/* ======================================
* 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.
*
* -------------------
* StandardLegend.java
* -------------------
* (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Andrzej Porebski;
*
* $Id: StandardLegend.java,v 1.9 2003/09/02 10:06:15 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);
*
*/
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.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
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.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
import org.jfree.util.ObjectUtils;
/**
* A chart legend shows the names and visual representations of the series
* that are plotted in a chart.
*
* @author David Gilbert
*/
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);
/** The amount of blank space around the legend. */
private Spacer outerGap;
/** The pen/brush used to draw the outline of the legend. */
private transient Stroke outlineStroke;
/** The color used to draw the outline of the legend. */
private transient Paint outlinePaint;
/** The color 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 color 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;
/**
* Constructs a new legend with default settings.
*
* @param chart the chart that the legend belongs to.
*/
public StandardLegend(JFreeChart chart) {
super(chart);
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;
}
/**
* Returns the outer gap for the legend.
* <P>
* This is the amount of blank space around the outside of the legend.
*
* @return the gap.
*/
public Spacer getOuterGap() {
return this.outerGap;
}
/**
* Sets the outer gap for the legend. A {@link LegendChangeEvent} is sent to all
* registered listeners.
*
* @param outerGap the outer gap.
*/
public void setOuterGap(Spacer outerGap) {
this.outerGap = outerGap;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the background color for the legend.
*
* @return the background color.
*/
public Paint getBackgroundPaint() {
return this.backgroundPaint;
}
/**
* Sets the background color of the legend.
* <P>
* Registered listeners are notified that the legend has changed.
*
* @param paint the new background color.
*/
public void setBackgroundPaint(Paint paint) {
this.backgroundPaint = paint;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the outline pen/brush.
*
* @return the outline pen/brush.
*/
public Stroke getOutlineStroke() {
return this.outlineStroke;
}
/**
* Sets the outline pen/brush.
* <P>
* Registered listeners are notified that the legend has changed.
*
* @param stroke the new outline pen/brush.
*/
public void setOutlineStroke(Stroke stroke) {
this.outlineStroke = stroke;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the outline color.
*
* @return the outline color.
*/
public Paint getOutlinePaint() {
return this.outlinePaint;
}
/**
* Sets the outline color.
* <P>
* Registered listeners are notified that the legend has changed.
*
* @param paint the new outline color.
*/
public void setOutlinePaint(Paint paint) {
this.outlinePaint = paint;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Gets the title for the legend.
*
* @return The title of the legend; which may be null.
*/
public String getTitle() {
return title;
}
/**
* Sets the title of the legend.
*
* @param title The title to use. Pass <code>null</code> if you don't
* want a title.
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Returns the title font.
*
* @return The font.
*/
public Font getTitleFont() {
return this.titleFont;
}
/**
* Sets the title font.
*
* @param font the new font.
*/
public void setTitleFont(Font font) {
this.titleFont = font;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the series label font.
*
* @return the series label font.
*/
public Font getItemFont() {
return this.itemFont;
}
/**
* Sets the series label font.
* <P>
* Registered listeners are notified that the legend has changed.
*
* @param font the new series label font.
*/
public void setItemFont(Font font) {
this.itemFont = font;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the series label color.
*
* @return the series label color.
*/
public Paint getItemPaint() {
return this.itemPaint;
}
/**
* Sets the series label color.
* <P>
* Registered listeners are notified that the legend has changed.
*
* @param paint the new series label color.
*/
public void setItemPaint(Paint paint) {
this.itemPaint = paint;
notifyListeners(new LegendChangeEvent(this));
}
/**
* Returns the flag that indicates whether or not outlines are drawn around shapes.
*
* @return the flag.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -