|
jfreechart example source code file (SymbolAxis.java)
The jfreechart SymbolAxis.java source code
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2008, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* SymbolAxis.java
* ---------------
* (C) Copyright 2002-2008, by Anthony Boulestreau and Contributors.
*
* Original Author: Anthony Boulestreau;
* Contributor(s): David Gilbert (for Object Refinery Limited);
*
*
* Changes
* -------
* 29-Mar-2002 : First version (AB);
* 19-Apr-2002 : Updated formatting and import statements (DG);
* 21-Jun-2002 : Make change to use the class TickUnit - remove valueToString()
* method and add SymbolicTickUnit (AB);
* 25-Jun-2002 : Removed redundant code (DG);
* 25-Jul-2002 : Changed order of parameters in ValueAxis constructor (DG);
* 05-Sep-2002 : Updated constructor to reflect changes in the Axis class (DG);
* 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
* 14-Feb-2003 : Added back missing constructor code (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 14-May-2003 : Renamed HorizontalSymbolicAxis --> SymbolicAxis and merged in
* VerticalSymbolicAxis (DG);
* 12-Aug-2003 : Fixed bug where refreshTicks() method has different signature
* to super class (DG);
* 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
* 02-Nov-2003 : Added code to avoid overlapping labels (MR);
* 07-Nov-2003 : Modified to use new tick classes (DG);
* 18-Nov-2003 : Fixed bug where symbols are not being displayed on the
* axis (DG);
* 24-Nov-2003 : Added fix for gridlines on zooming (bug id 834643) (DG);
* 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
* 11-Mar-2004 : Modified the way the background grid color is being drawn, see
* this thread:
* http://www.jfree.org/phpBB2/viewtopic.php?p=22973 (DG);
* 16-Mar-2004 : Added plotState to draw() method (DG);
* 07-Apr-2004 : Modified string bounds calculation (DG);
* 28-Mar-2005 : Renamed autoRangeIncludesZero() --> getAutoRangeIncludesZero()
* and autoRangeStickyZero() --> getAutoRangeStickyZero() (DG);
* 05-Jul-2005 : Fixed signature on refreshTicks() method - see bug report
* 1232264 (DG);
* 06-Jul-2005 : Renamed SymbolicAxis --> SymbolAxis, added equals() method,
* renamed getSymbolicValue() --> getSymbols(), renamed
* symbolicGridPaint --> gridBandPaint, fixed serialization of
* gridBandPaint, renamed symbolicGridLinesVisible -->
* gridBandsVisible, eliminated symbolicGridLineList (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
* 28-Feb-2007 : Fixed bug 1669302 (tick label overlap) (DG);
* 25-Jul-2007 : Added new field for alternate grid band paint (DG);
* 15-Aug-2008 : Use alternate grid band paint when drawing (DG);
*
*/
package org.jfree.chart.axis;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.ValueAxisPlot;
import org.jfree.data.Range;
import org.jfree.io.SerialUtilities;
import org.jfree.text.TextUtilities;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.TextAnchor;
import org.jfree.util.PaintUtilities;
/**
* A standard linear value axis that replaces integer values with symbols.
*/
public class SymbolAxis extends NumberAxis implements Serializable {
/** For serialization. */
private static final long serialVersionUID = 7216330468770619716L;
/** The default grid band paint. */
public static final Paint DEFAULT_GRID_BAND_PAINT
= new Color(232, 234, 232, 128);
/**
* The default paint for alternate grid bands.
*
* @since 1.0.7
*/
public static final Paint DEFAULT_GRID_BAND_ALTERNATE_PAINT
= new Color(0, 0, 0, 0); // transparent
/** The list of symbols to display instead of the numeric values. */
private List symbols;
/** Flag that indicates whether or not grid bands are visible. */
private boolean gridBandsVisible;
/** The paint used to color the grid bands (if the bands are visible). */
private transient Paint gridBandPaint;
/**
* The paint used to fill the alternate grid bands.
*
* @since 1.0.7
*/
private transient Paint gridBandAlternatePaint;
/**
* Constructs a symbol axis, using default attribute values where
* necessary.
*
* @param label the axis label (<code>null permitted).
* @param sv the list of symbols to display instead of the numeric
* values.
*/
public SymbolAxis(String label, String[] sv) {
super(label);
this.symbols = Arrays.asList(sv);
this.gridBandsVisible = true;
this.gridBandPaint = DEFAULT_GRID_BAND_PAINT;
this.gridBandAlternatePaint = DEFAULT_GRID_BAND_ALTERNATE_PAINT;
setAutoTickUnitSelection(false, false);
setAutoRangeStickyZero(false);
}
/**
* Returns an array of the symbols for the axis.
*
* @return The symbols.
*/
public String[] getSymbols() {
String[] result = new String[this.symbols.size()];
result = (String[]) this.symbols.toArray(result);
return result;
}
/**
* Returns <code>true if the grid bands are showing, and
* <code>false otherwise.
*
* @return <code>true if the grid bands are showing, and
* <code>false otherwise.
*
* @see #setGridBandsVisible(boolean)
*/
public boolean isGridBandsVisible() {
return this.gridBandsVisible;
}
/**
* Sets the visibility of the grid bands and notifies registered
* listeners that the axis has been modified.
*
* @param flag the new setting.
*
* @see #isGridBandsVisible()
*/
public void setGridBandsVisible(boolean flag) {
if (this.gridBandsVisible != flag) {
this.gridBandsVisible = flag;
notifyListeners(new AxisChangeEvent(this));
}
}
/**
* Returns the paint used to color the grid bands.
*
* @return The grid band paint (never <code>null).
*
* @see #setGridBandPaint(Paint)
* @see #isGridBandsVisible()
*/
public Paint getGridBandPaint() {
return this.gridBandPaint;
}
/**
* Sets the grid band paint and sends an {@link AxisChangeEvent} to
* all registered listeners.
*
* @param paint the paint (<code>null not permitted).
*
* @see #getGridBandPaint()
*/
public void setGridBandPaint(Paint paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
this.gridBandPaint = paint;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the paint used for alternate grid bands.
*
* @return The paint (never <code>null).
*
* @see #setGridBandAlternatePaint(Paint)
* @see #getGridBandPaint()
*
* @since 1.0.7
*/
public Paint getGridBandAlternatePaint() {
return this.gridBandAlternatePaint;
}
/**
* Sets the paint used for alternate grid bands and sends a
* {@link AxisChangeEvent} to all registered listeners.
*
* @param paint the paint (<code>null not permitted).
*
* @see #getGridBandAlternatePaint()
* @see #setGridBandPaint(Paint)
*
* @since 1.0.7
*/
public void setGridBandAlternatePaint(Paint paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
this.gridBandAlternatePaint = paint;
notifyListeners(new AxisChangeEvent(this));
}
/**
* This operation is not supported by this axis.
*
* @param g2 the graphics device.
* @param dataArea the area in which the plot and axes should be drawn.
* @param edge the edge along which the axis is drawn.
*/
protected void selectAutoTickUnit(Graphics2D g2, Rectangle2D dataArea,
RectangleEdge edge) {
throw new UnsupportedOperationException();
}
/**
* Draws the axis on a Java 2D graphics device (such as the screen or a
* printer).
*
* @param g2 the graphics device (<code>null not permitted).
* @param cursor the cursor location.
* @param plotArea the area within which the plot and axes should be drawn
* (<code>null not permitted).
* @param dataArea the area within which the data should be drawn
* (<code>null not permitted).
* @param edge the axis location (<code>null not permitted).
* @param plotState collects information about the plot
* (<code>null permitted).
*
* @return The axis state (never <code>null).
*/
public AxisState draw(Graphics2D g2,
double cursor,
Rectangle2D plotArea,
Rectangle2D dataArea,
RectangleEdge edge,
PlotRenderingInfo plotState) {
AxisState info = new AxisState(cursor);
if (isVisible()) {
info = super.draw(g2, cursor, plotArea, dataArea, edge, plotState);
}
if (this.gridBandsVisible) {
drawGridBands(g2, plotArea, dataArea, edge, info.getTicks());
}
return info;
}
/**
* Draws the grid bands. Alternate bands are colored using
* <CODE>gridBandPaint
Other jfreechart examples (source code examples)Here is a short list of links related to this jfreechart SymbolAxis.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.