com.jrefinery.chart
Class XYPlot

java.lang.Object
  |
  +--com.jrefinery.chart.Plot
        |
        +--com.jrefinery.chart.XYPlot
All Implemented Interfaces:
AxisChangeListener, AxisConstants, DatasetChangeListener, java.util.EventListener, HorizontalValuePlot, java.beans.PropertyChangeListener, VerticalValuePlot
Direct Known Subclasses:
CombinedXYPlot, OverlaidXYPlot, PeriodMarkerPlot

public class XYPlot
extends Plot
implements HorizontalValuePlot, VerticalValuePlot, java.beans.PropertyChangeListener

A general class for plotting data in the form of (x, y) pairs. XYPlot can use data from any class that implements the XYDataset interface (in the com.jrefinery.data package).

XYPlot makes use of a renderer to draw each point on the plot. By using different renderers, various chart types can be produced. The ChartFactory class contains static methods for creating pre-configured charts.

Author:
DG
See Also:
ChartFactory, Plot, XYDataset

Fields inherited from class com.jrefinery.chart.Plot
backgroundAlpha, backgroundImage, backgroundPaint, dataset, DEFAULT_BACKGROUND_ALPHA, DEFAULT_BACKGROUND_PAINT, DEFAULT_FOREGROUND_ALPHA, DEFAULT_INSETS, DEFAULT_OUTLINE_PAINT, DEFAULT_OUTLINE_STROKE, foregroundAlpha, insets, listenerList, MINIMUM_HEIGHT_TO_DRAW, MINIMUM_WIDTH_TO_DRAW, noDataMessage, noDataMessageFont, outlinePaint, outlineStroke, seriesOutlinePaint, seriesOutlineStroke, seriesPaint, seriesStroke, shapeFactory, ZERO
 
Fields inherited from interface com.jrefinery.chart.AxisConstants
DEFAULT_AXIS_LABEL_FONT, DEFAULT_AXIS_LABEL_INSETS, DEFAULT_AXIS_LABEL_PAINT, DEFAULT_TICK_LABEL_FONT, DEFAULT_TICK_LABEL_INSETS, DEFAULT_TICK_LABEL_PAINT, DEFAULT_TICK_PAINT, DEFAULT_TICK_STROKE
 
Constructor Summary
XYPlot(XYDataset data, ValueAxis domainAxis, ValueAxis rangeAxis)
          Constructs an XYPlot with the specified axes (other attributes take default values).
XYPlot(XYDataset data, ValueAxis domainAxis, ValueAxis rangeAxis, java.awt.Insets insets, java.awt.Paint backgroundPaint, java.awt.Image backgroundImage, float backgroundAlpha, java.awt.Stroke outlineStroke, java.awt.Paint outlinePaint, float alpha, XYItemRenderer renderer)
          Constructs a new XY plot.
XYPlot(XYDataset data, ValueAxis domainAxis, ValueAxis rangeAxis, XYItemRenderer renderer)
          Constructs an XYPlot with the specified axes and renderer (other attributes take default values).
 
Method Summary
 void addAnnotation(Annotation annotation)
          Adds an annotation to the plot.
 void addDomainMarker(Marker marker)
          Adds a marker for the domain axis.
 void addHorizontalLine(java.lang.Number value)
          Deprecated. Use addRangeMarker(...).
 void addHorizontalLine(java.lang.Number location, java.awt.Paint color)
          Deprecated. Use addRangeMarker(...).
 void addRangeMarker(Marker marker)
          Adds a marker for the range axis.
 void addVerticalLine(java.lang.Number location)
          Deprecated. Use addDomainMarker(...).
 void addVerticalLine(java.lang.Number location, java.awt.Paint color)
          Deprecated. Use addDomainMarker(...).
 void clearAnnotations()
          Clears all the annotations.
 void clearDomainMarkers()
          Clears all the domain markers.
 void clearRangeMarkers()
          Clears all the range markers.
 void datasetChanged(DatasetChangeEvent event)
          Receives notification of a change to the plot's dataset.
 void draw(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D plotArea, ChartRenderingInfo info)
          Draws the XY plot on a Java 2D graphics device (such as the screen or a printer).
protected  void drawHorizontalLine(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D dataArea, double value, java.awt.Stroke stroke, java.awt.Paint paint)
          Utility method for drawing a crosshair on the chart (if required).
protected  void drawVerticalLine(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D dataArea, double value, java.awt.Stroke stroke, java.awt.Paint paint)
          Utility method for drawing a crosshair on the chart (if required).
 ValueAxis getDomainAxis()
          Returns the domain axis for the plot.
 HorizontalAxis getHorizontalAxis()
          Returns the horizontal axis.
 Range getHorizontalDataRange()
          Returns the range for the horizontal axis.
 ValueAxis getHorizontalValueAxis()
          Returns the horizontal axis.
 XYItemRenderer getItemRenderer()
          Deprecated. Use getRenderer().
 java.util.List getLegendItemLabels()
          Returns an array of labels to be displayed by the legend.
 LegendItemCollection getLegendItems()
          Returns the legend items for the plot.
 Plot getParent()
          Returns the parent plot (or null if this plot is not part of a combined plot).
 java.lang.String getPlotType()
          Returns the plot type as a string.
 ValueAxis getRangeAxis()
          Returns the range axis for the plot.
 XYItemRenderer getRenderer()
          Returns the item renderer.
 int getSeriesCount()
          Returns the number of series in the dataset for this plot.
 VerticalAxis getVerticalAxis()
          Returns the vertical axis.
 Range getVerticalDataRange()
          Returns the range for the vertical axis.
 ValueAxis getVerticalValueAxis()
          Returns the vertical axis.
 int getWeight()
          Returns the weight for this plot when it is used as a subplot within a combined plot.
 XYDataset getXYDataset()
          A convenience method that returns the dataset for the plot, cast as an XYDataset.
 void handleClick(int x, int y, ChartRenderingInfo info)
          Handles a 'click' on the plot by updating the anchor values...
 boolean isCompatibleDomainAxis(ValueAxis axis)
          Checks the compatibility of a domain axis, returning true if the axis is compatible with the plot, and false otherwise.
 boolean isCompatibleRangeAxis(ValueAxis axis)
          Checks the compatibility of a range axis, returning true if the axis is compatible with the plot, and false otherwise.
 boolean isSubplot()
          Returns true if this plot is part of a combined plot structure.
 void propertyChange(java.beans.PropertyChangeEvent event)
          Notifies all registered listeners of a property change.
 void render(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D dataArea, ChartRenderingInfo info, CrosshairInfo crosshairInfo)
          Draws a representation of the data within the dataArea region, using the current renderer.
 void setDomainAxis(ValueAxis axis)
          Sets the domain axis for the plot (this must be compatible with the plot type or an exception is thrown).
 void setParent(XYPlot parent)
          Sets the parent plot.
 void setRangeAxis(ValueAxis axis)
          Sets the range axis for the plot.
 void setRenderer(XYItemRenderer renderer)
          Sets the item renderer, and notifies all listeners of a change to the plot.
 void setWeight(int weight)
          Sets the weight for the plot.
 void setXYItemRenderer(XYItemRenderer renderer)
          Deprecated. Use setRenderer(...).
 void zoom(double percent)
          Zooms the axis ranges by the specified percentage about the anchor point.
 
Methods inherited from class com.jrefinery.chart.Plot
addChangeListener, axisChanged, drawNoDataMessage, drawOutlineAndBackground, getBackgroundAlpha, getBackgroundPaint, getDataset, getDatasetGroup, getForegroundAlpha, getInsets, getNoDataMessage, getNoDataMessageFont, getOutlinePaint, getOutlineStroke, getSeriesOutlinePaint, getSeriesOutlineStroke, getSeriesPaint, getSeriesStroke, getShape, getShape, getShapeFactory, notifyListeners, removeChangeListener, setBackgroundAlpha, setBackgroundImage, setBackgroundPaint, setDataset, setDatasetGroup, setForegroundAlpha, setInsets, setNoDataMessage, setNoDataMessageFont, setOutlinePaint, setOutlineStroke, setSeriesOutlinePaint, setSeriesOutlineStroke, setSeriesOutlineStroke, setSeriesPaint, setSeriesPaint, setSeriesStroke, setSeriesStroke, setShapeFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XYPlot

public XYPlot(XYDataset data,
              ValueAxis domainAxis,
              ValueAxis rangeAxis)
Constructs an XYPlot with the specified axes (other attributes take default values).
Parameters:
data - The dataset.
domainAxis - The domain axis.
rangeAxis - The range axis.

XYPlot

public XYPlot(XYDataset data,
              ValueAxis domainAxis,
              ValueAxis rangeAxis,
              XYItemRenderer renderer)
Constructs an XYPlot with the specified axes and renderer (other attributes take default values).
Parameters:
data - the dataset.
domainAxis - the domain axis.
rangeAxis - the range axis.
renderer - the renderer

XYPlot

public XYPlot(XYDataset data,
              ValueAxis domainAxis,
              ValueAxis rangeAxis,
              java.awt.Insets insets,
              java.awt.Paint backgroundPaint,
              java.awt.Image backgroundImage,
              float backgroundAlpha,
              java.awt.Stroke outlineStroke,
              java.awt.Paint outlinePaint,
              float alpha,
              XYItemRenderer renderer)
Constructs a new XY plot.
Parameters:
data - the dataset.
domainAxis - the domain axis.
rangeAxis - the range axis.
insets - amount of blank space around the plot area.
backgroundPaint - an optional color for the plot's background.
backgroundImage - an optional image for the plot's background.
backgroundAlpha - alpha-transparency for the plot's background.
outlineStroke - the Stroke used to draw an outline around the plot.
outlinePaint - the color used to draw the plot outline.
alpha - the alpha-transparency.
renderer - the renderer.
Method Detail

getParent

public Plot getParent()
Returns the parent plot (or null if this plot is not part of a combined plot).
Returns:
The parent plot.

setParent

public void setParent(XYPlot parent)
Sets the parent plot.
Parameters:
parent - The parent plot.

isSubplot

public boolean isSubplot()
Returns true if this plot is part of a combined plot structure.
Overrides:
isSubplot in class Plot
Returns:
true if this plot is part of a combined plot structure.

getSeriesCount

public int getSeriesCount()
Returns the number of series in the dataset for this plot.
Returns:
The series count.

getLegendItemLabels

public java.util.List getLegendItemLabels()
Returns an array of labels to be displayed by the legend.
Overrides:
getLegendItemLabels in class Plot
Returns:
An array of legend item labels (or null).

getLegendItems

public LegendItemCollection getLegendItems()
Returns the legend items for the plot.
Overrides:
getLegendItems in class Plot
Returns:
the legend items.

getWeight

public int getWeight()
Returns the weight for this plot when it is used as a subplot within a combined plot.
Returns:
The weight.

setWeight

public void setWeight(int weight)
Sets the weight for the plot.
Parameters:
weight - The weight.

getRenderer

public XYItemRenderer getRenderer()
Returns the item renderer.
Returns:
The item renderer (possibly null).

setRenderer

public void setRenderer(XYItemRenderer renderer)
Sets the item renderer, and notifies all listeners of a change to the plot.

If the renderer is set to null, no chart will be drawn.

Parameters:
renderer - The new renderer (null permitted).

getXYDataset

public XYDataset getXYDataset()
A convenience method that returns the dataset for the plot, cast as an XYDataset.
Returns:
The dataset for the plot, cast as an XYDataset.

getDomainAxis

public ValueAxis getDomainAxis()
Returns the domain axis for the plot. If the domain axis for this plot is null, then the method will return the parent plot's domain axis (if there is a parent plot).
Returns:
The domain axis.

setDomainAxis

public void setDomainAxis(ValueAxis axis)
                   throws AxisNotCompatibleException
Sets the domain axis for the plot (this must be compatible with the plot type or an exception is thrown).
Parameters:
axis - The new axis.
Throws:
AxisNotCompatibleException - if the axis is not compatible.

getRangeAxis

public ValueAxis getRangeAxis()
Returns the range axis for the plot. If the range axis for this plot is null, then the method will return the parent plot's range axis (if there is a parent plot).
Returns:
The range axis.

setRangeAxis

public void setRangeAxis(ValueAxis axis)
                  throws AxisNotCompatibleException
Sets the range axis for the plot.

An exception is thrown if the new axis and the plot are not mutually compatible.

Parameters:
axis - The new axis (null permitted).
Throws:
AxisNotCompatibleException - if the axis is not compatible.

addDomainMarker

public void addDomainMarker(Marker marker)
Adds a marker for the domain axis.

Typically a marker will be drawn by the renderer as a line perpendicular to the range axis, however this is entirely up to the renderer.

Parameters:
marker - the marker.

clearDomainMarkers

public void clearDomainMarkers()
Clears all the domain markers.

addRangeMarker

public void addRangeMarker(Marker marker)
Adds a marker for the range axis.

Typically a marker will be drawn by the renderer as a line perpendicular to the range axis, however this is entirely up to the renderer.

Parameters:
marker - The marker.

clearRangeMarkers

public void clearRangeMarkers()
Clears all the range markers.

addAnnotation

public void addAnnotation(Annotation annotation)
Adds an annotation to the plot.
Parameters:
annotation - the annotation.

clearAnnotations

public void clearAnnotations()
Clears all the annotations.

isCompatibleDomainAxis

public boolean isCompatibleDomainAxis(ValueAxis axis)
Checks the compatibility of a domain axis, returning true if the axis is compatible with the plot, and false otherwise.
Parameters:
axis - The proposed axis.
Returns:
true if the axis is compatible with the plot.

isCompatibleRangeAxis

public boolean isCompatibleRangeAxis(ValueAxis axis)
Checks the compatibility of a range axis, returning true if the axis is compatible with the plot, and false otherwise.
Parameters:
axis - The proposed axis.
Returns:
true if the axis is compatible with the plot.

draw

public void draw(java.awt.Graphics2D g2,
                 java.awt.geom.Rectangle2D plotArea,
                 ChartRenderingInfo info)
Draws the XY plot on a Java 2D graphics device (such as the screen or a printer).

XYPlot relies on an XYItemRenderer to draw each item in the plot. This allows the visual representation of the data to be changed easily.

The optional info argument collects information about the rendering of the plot (dimensions, tooltip information etc). Just pass in null if you do not need this information.

Overrides:
draw in class Plot
Parameters:
g2 - The graphics device.
plotArea - The area within which the plot (including axis labels) should be drawn.
info - Collects chart drawing information (null permitted).

render

public void render(java.awt.Graphics2D g2,
                   java.awt.geom.Rectangle2D dataArea,
                   ChartRenderingInfo info,
                   CrosshairInfo crosshairInfo)
Draws a representation of the data within the dataArea region, using the current renderer.

The info and crosshairInfo arguments may be null.

Parameters:
g2 - the graphics device.
dataArea - the region in which the data is to be drawn.
info - an optional object for collection dimension information.
crosshairInfo - an optional object for collecting crosshair info.

drawVerticalLine

protected void drawVerticalLine(java.awt.Graphics2D g2,
                                java.awt.geom.Rectangle2D dataArea,
                                double value,
                                java.awt.Stroke stroke,
                                java.awt.Paint paint)
Utility method for drawing a crosshair on the chart (if required).
Parameters:
g2 - The graphics device.
dataArea - The data area.
value - The coordinate, where to draw the line.
stroke - The stroke to use.
paint - The paint to use.

drawHorizontalLine

protected void drawHorizontalLine(java.awt.Graphics2D g2,
                                  java.awt.geom.Rectangle2D dataArea,
                                  double value,
                                  java.awt.Stroke stroke,
                                  java.awt.Paint paint)
Utility method for drawing a crosshair on the chart (if required).
Parameters:
g2 - The graphics device.
dataArea - The data area.
value - The coordinate, where to draw the line.
stroke - The stroke to use.
paint - The paint to use.

handleClick

public void handleClick(int x,
                        int y,
                        ChartRenderingInfo info)
Handles a 'click' on the plot by updating the anchor values...
Overrides:
handleClick in class Plot
Parameters:
x - x-coordinate, where the click occured.
y - y-coordinate, where the click occured.
info - An object for collection dimension information.

zoom

public void zoom(double percent)
Zooms the axis ranges by the specified percentage about the anchor point.
Overrides:
zoom in class Plot
Parameters:
percent - The amount of the zoom.

getPlotType

public java.lang.String getPlotType()
Returns the plot type as a string.
Overrides:
getPlotType in class Plot
Returns:
A short string describing the type of plot.

getHorizontalDataRange

public Range getHorizontalDataRange()
Returns the range for the horizontal axis.
Specified by:
getHorizontalDataRange in interface HorizontalValuePlot
Returns:
The range for the horizontal axis.

getVerticalDataRange

public Range getVerticalDataRange()
Returns the range for the vertical axis.
Specified by:
getVerticalDataRange in interface VerticalValuePlot
Returns:
The range for the vertical axis.

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent event)
Notifies all registered listeners of a property change.

One source of property change events is the plot's renderer.

Specified by:
propertyChange in interface java.beans.PropertyChangeListener
Parameters:
event - Information about the property change.

datasetChanged

public void datasetChanged(DatasetChangeEvent event)
Receives notification of a change to the plot's dataset.

The chart reacts by passing on a chart change event to all registered listeners.

Overrides:
datasetChanged in class Plot
Parameters:
event - Information about the event (not used here).

getHorizontalAxis

public HorizontalAxis getHorizontalAxis()
Returns the horizontal axis.
Returns:
The horizontal axis.

getHorizontalValueAxis

public ValueAxis getHorizontalValueAxis()
Returns the horizontal axis.

This method is part of the HorizontalValuePlot interface.

Specified by:
getHorizontalValueAxis in interface HorizontalValuePlot
Returns:
The horizontal axis.

getVerticalAxis

public VerticalAxis getVerticalAxis()
Returns the vertical axis.
Returns:
The vertical axis.

getVerticalValueAxis

public ValueAxis getVerticalValueAxis()
Returns the vertical axis.

This method is part of the VerticalValuePlot interface.

Specified by:
getVerticalValueAxis in interface VerticalValuePlot
Returns:
The vertical axis.

getItemRenderer

public XYItemRenderer getItemRenderer()
Deprecated. Use getRenderer().

Returns:
the item renderer.

setXYItemRenderer

public void setXYItemRenderer(XYItemRenderer renderer)
Deprecated. Use setRenderer(...).

Parameters:
renderer - the renderer.

addHorizontalLine

public void addHorizontalLine(java.lang.Number value)
Deprecated. Use addRangeMarker(...).

Adds a horizontal line at the specified data value, using the default color red.
Parameters:
value - The data value.

addHorizontalLine

public void addHorizontalLine(java.lang.Number location,
                              java.awt.Paint color)
Deprecated. Use addRangeMarker(...).

Adds a horizontal line at the specified data value, using the specified color.
Parameters:
location - The location.
color - The line color.

addVerticalLine

public void addVerticalLine(java.lang.Number location)
Deprecated. Use addDomainMarker(...).

Adds a vertical line at location with default color blue.
Parameters:
location - The location.

addVerticalLine

public void addVerticalLine(java.lang.Number location,
                            java.awt.Paint color)
Deprecated. Use addDomainMarker(...).

Adds a vertical of the given color at location with the given color.
Parameters:
location - The location.
color - The color to use.