org.jfree.chart.plot.dial.DialTextAnnotation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jfreechart Show documentation
Show all versions of jfreechart Show documentation
JFreeChart is a class library, written in Java, for generating charts.
Utilising the Java2D API, it supports a wide range of chart types including
bar charts, pie charts, line charts, XY-plots, time series plots, Sankey charts
and more.
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-present, by David Gilbert 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* -----------------------
* DialTextAnnotation.java
* -----------------------
* (C) Copyright 2006-present, by David Gilbert.
*
* Original Author: David Gilbert;
* Contributor(s): -;
*
*/
package org.jfree.chart.plot.dial;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Arc2D;
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.HashUtils;
import org.jfree.chart.text.TextUtils;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.chart.util.PaintUtils;
import org.jfree.chart.util.Args;
import org.jfree.chart.util.PublicCloneable;
import org.jfree.chart.util.SerialUtils;
/**
* A text annotation for a {@link DialPlot}.
*/
public class DialTextAnnotation extends AbstractDialLayer implements DialLayer,
Cloneable, PublicCloneable, Serializable {
/** For serialization. */
static final long serialVersionUID = 3065267524054428071L;
/** The label text. */
private String label;
/** The font. */
private Font font;
/**
* The paint for the label. This field is transient because it requires
* special handling for serialization.
*/
private transient Paint paint;
/** The angle that defines the anchor point for the annotation. */
private double angle;
/** The radius that defines the anchor point for the annotation. */
private double radius;
/** The text anchor to be aligned to the annotation's anchor point. */
private TextAnchor anchor;
/**
* Creates a new instance of {@code DialTextAnnotation}.
*
* @param label the label ({@code null} not permitted).
*/
public DialTextAnnotation(String label) {
Args.nullNotPermitted(label, "label");
this.angle = -90.0;
this.radius = 0.3;
this.font = new Font("Dialog", Font.BOLD, 14);
this.paint = Color.BLACK;
this.label = label;
this.anchor = TextAnchor.TOP_CENTER;
}
/**
* Returns the label text.
*
* @return The label text (never {@code null}).
*
* @see #setLabel(String)
*/
public String getLabel() {
return this.label;
}
/**
* Sets the label and sends a {@link DialLayerChangeEvent} to all
* registered listeners.
*
* @param label the label ({@code null} not permitted).
*
* @see #getLabel()
*/
public void setLabel(String label) {
Args.nullNotPermitted(label, "label");
this.label = label;
notifyListeners(new DialLayerChangeEvent(this));
}
/**
* Returns the font used to display the label.
*
* @return The font (never {@code null}).
*
* @see #setFont(Font)
*/
public Font getFont() {
return this.font;
}
/**
* Sets the font used to display the label and sends a
* {@link DialLayerChangeEvent} to all registered listeners.
*
* @param font the font ({@code null} not permitted).
*
* @see #getFont()
*/
public void setFont(Font font) {
Args.nullNotPermitted(font, "font");
this.font = font;
notifyListeners(new DialLayerChangeEvent(this));
}
/**
* Returns the paint used to display the label.
*
* @return The paint (never {@code null}).
*
* @see #setPaint(Paint)
*/
public Paint getPaint() {
return this.paint;
}
/**
* Sets the paint used to display the label and sends a
* {@link DialLayerChangeEvent} to all registered listeners.
*
* @param paint the paint ({@code null} not permitted).
*
* @see #getPaint()
*/
public void setPaint(Paint paint) {
Args.nullNotPermitted(paint, "paint");
this.paint = paint;
notifyListeners(new DialLayerChangeEvent(this));
}
/**
* Returns the angle used to calculate the anchor point.
*
* @return The angle (in degrees).
*
* @see #setAngle(double)
* @see #getRadius()
*/
public double getAngle() {
return this.angle;
}
/**
* Sets the angle used to calculate the anchor point and sends a
* {@link DialLayerChangeEvent} to all registered listeners.
*
* @param angle the angle (in degrees).
*
* @see #getAngle()
* @see #setRadius(double)
*/
public void setAngle(double angle) {
this.angle = angle;
notifyListeners(new DialLayerChangeEvent(this));
}
/**
* Returns the radius used to calculate the anchor point. This is
* specified as a percentage relative to the dial's framing rectangle.
*
* @return The radius.
*
* @see #setRadius(double)
* @see #getAngle()
*/
public double getRadius() {
return this.radius;
}
/**
* Sets the radius used to calculate the anchor point and sends a
* {@link DialLayerChangeEvent} to all registered listeners.
*
* @param radius the radius (as a percentage of the dial's framing
* rectangle).
*
* @see #getRadius()
* @see #setAngle(double)
*/
public void setRadius(double radius) {
if (radius < 0.0) {
throw new IllegalArgumentException(
"The 'radius' cannot be negative.");
}
this.radius = radius;
notifyListeners(new DialLayerChangeEvent(this));
}
/**
* Returns the text anchor point that will be aligned to the position
* specified by {@link #getAngle()} and {@link #getRadius()}.
*
* @return The anchor point.
*
* @see #setAnchor(TextAnchor)
*/
public TextAnchor getAnchor() {
return this.anchor;
}
/**
* Sets the text anchor point and sends a {@link DialLayerChangeEvent} to
* all registered listeners.
*
* @param anchor the anchor point ({@code null} not permitted).
*
* @see #getAnchor()
*/
public void setAnchor(TextAnchor anchor) {
Args.nullNotPermitted(anchor, "anchor");
this.anchor = anchor;
notifyListeners(new DialLayerChangeEvent(this));
}
/**
* Returns {@code true} to indicate that this layer should be
* clipped within the dial window.
*
* @return {@code true}.
*/
@Override
public boolean isClippedToWindow() {
return true;
}
/**
* Draws the background to the specified graphics device. If the dial
* frame specifies a window, the clipping region will already have been
* set to this window before this method is called.
*
* @param g2 the graphics device ({@code null} not permitted).
* @param plot the plot (ignored here).
* @param frame the dial frame (ignored here).
* @param view the view rectangle ({@code null} not permitted).
*/
@Override
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
Rectangle2D view) {
// work out the anchor point
Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius,
this.radius);
Arc2D arc = new Arc2D.Double(f, this.angle, 0.0, Arc2D.OPEN);
Point2D pt = arc.getStartPoint();
g2.setPaint(this.paint);
g2.setFont(this.font);
TextUtils.drawAlignedString(this.label, g2, (float) pt.getX(),
(float) pt.getY(), this.anchor);
}
/**
* Tests this instance for equality with an arbitrary object.
*
* @param obj the object ({@code null} permitted).
*
* @return A boolean.
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof DialTextAnnotation)) {
return false;
}
DialTextAnnotation that = (DialTextAnnotation) obj;
if (!this.label.equals(that.label)) {
return false;
}
if (!this.font.equals(that.font)) {
return false;
}
if (!PaintUtils.equal(this.paint, that.paint)) {
return false;
}
if (this.radius != that.radius) {
return false;
}
if (this.angle != that.angle) {
return false;
}
if (!this.anchor.equals(that.anchor)) {
return false;
}
return super.equals(obj);
}
/**
* Returns a hash code for this instance.
*
* @return The hash code.
*/
@Override
public int hashCode() {
int result = 193;
result = 37 * result + HashUtils.hashCodeForPaint(this.paint);
result = 37 * result + this.font.hashCode();
result = 37 * result + this.label.hashCode();
result = 37 * result + this.anchor.hashCode();
long temp = Double.doubleToLongBits(this.angle);
result = 37 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(this.radius);
result = 37 * result + (int) (temp ^ (temp >>> 32));
return result;
}
/**
* Returns a clone of this instance.
*
* @return The clone.
*
* @throws CloneNotSupportedException if some attribute of this instance
* cannot be cloned.
*/
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtils.writePaint(this.paint, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.paint = SerialUtils.readPaint(stream);
}
}