org.jfree.chart.renderer.category.LineAndShapeRenderer Maven / Gradle / Ivy
Show all versions of jfreechart Show documentation
/* ===========================================================
* 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.]
*
* -------------------------
* LineAndShapeRenderer.java
* -------------------------
* (C) Copyright 2001-present, by David Gilbert and Contributors.
*
* Original Author: David Gilbert;
* Contributor(s): Mark Watson (www.markwatson.com);
* Jeremy Bowman;
* Richard Atkinson;
* Christian W. Zuckschwerdt;
* Peter Kolb (patch 2497611);
*
*/
package org.jfree.chart.renderer.category;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.Objects;
import org.jfree.chart.LegendItem;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.event.RendererChangeEvent;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.util.BooleanList;
import org.jfree.chart.util.PublicCloneable;
import org.jfree.chart.util.ShapeUtils;
import org.jfree.data.category.CategoryDataset;
/**
* A renderer that draws shapes for each data item, and lines between data
* items (for use with the {@link CategoryPlot} class).
* The example shown here is generated by the {@code LineChartDemo1.java}
* program included in the JFreeChart Demo Collection:
*
*
*/
public class LineAndShapeRenderer extends AbstractCategoryItemRenderer
implements Cloneable, PublicCloneable, Serializable {
/** For serialization. */
private static final long serialVersionUID = -197749519869226398L;
/**
* A table of flags that control (per series) whether or not lines are
* visible.
*/
private BooleanList seriesLinesVisible;
/**
* A flag indicating whether or not lines are drawn between non-null
* points.
*/
private boolean defaultLinesVisible;
/**
* A table of flags that control (per series) whether or not shapes are
* visible.
*/
private BooleanList seriesShapesVisible;
/** The default value returned by the getShapeVisible() method. */
private boolean defaultShapesVisible;
/**
* A table of flags that control (per series) whether or not shapes are
* filled.
*/
private BooleanList seriesShapesFilled;
/** The default value returned by the getShapeFilled() method. */
private boolean defaultShapesFilled;
/**
* A flag that controls whether the fill paint is used for filling
* shapes.
*/
private boolean useFillPaint;
/** A flag that controls whether outlines are drawn for shapes. */
private boolean drawOutlines;
/**
* A flag that controls whether the outline paint is used for drawing shape
* outlines - if not, the regular series paint is used.
*/
private boolean useOutlinePaint;
/**
* A flag that controls whether or not the x-position for each item is
* offset within the category according to the series.
*/
private boolean useSeriesOffset;
/**
* The item margin used for series offsetting - this allows the positioning
* to match the bar positions of the {@link BarRenderer} class.
*/
private double itemMargin;
/**
* Creates a renderer with both lines and shapes visible by default.
*/
public LineAndShapeRenderer() {
this(true, true);
}
/**
* Creates a new renderer with lines and/or shapes visible.
*
* @param lines draw lines?
* @param shapes draw shapes?
*/
public LineAndShapeRenderer(boolean lines, boolean shapes) {
super();
this.seriesLinesVisible = new BooleanList();
this.defaultLinesVisible = lines;
this.seriesShapesVisible = new BooleanList();
this.defaultShapesVisible = shapes;
this.seriesShapesFilled = new BooleanList();
this.defaultShapesFilled = true;
this.useFillPaint = false;
this.drawOutlines = true;
this.useOutlinePaint = false;
this.useSeriesOffset = false; // preserves old behaviour
this.itemMargin = 0.0;
}
// LINES VISIBLE
/**
* Returns the flag used to control whether or not the line for an item is
* visible.
*
* @param series the series index (zero-based).
* @param item the item index (zero-based).
*
* @return A boolean.
*/
public boolean getItemLineVisible(int series, int item) {
Boolean flag = getSeriesLinesVisible(series);
if (flag != null) {
return flag;
}
return this.defaultLinesVisible;
}
/**
* Returns the flag used to control whether or not the lines for a series
* are visible.
*
* @param series the series index (zero-based).
*
* @return The flag (possibly {@code null}).
*
* @see #setSeriesLinesVisible(int, Boolean)
*/
public Boolean getSeriesLinesVisible(int series) {
return this.seriesLinesVisible.getBoolean(series);
}
/**
* Sets the 'lines visible' flag for a series and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param series the series index (zero-based).
* @param flag the flag ({@code null} permitted).
*
* @see #getSeriesLinesVisible(int)
*/
public void setSeriesLinesVisible(int series, Boolean flag) {
this.seriesLinesVisible.setBoolean(series, flag);
fireChangeEvent();
}
/**
* Sets the 'lines visible' flag for a series and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param series the series index (zero-based).
* @param visible the flag.
*
* @see #getSeriesLinesVisible(int)
*/
public void setSeriesLinesVisible(int series, boolean visible) {
setSeriesLinesVisible(series, Boolean.valueOf(visible));
}
/**
* Returns the default 'lines visible' attribute.
*
* @return The default flag.
*
* @see #getDefaultLinesVisible()
*/
public boolean getDefaultLinesVisible() {
return this.defaultLinesVisible;
}
/**
* Sets the default 'lines visible' flag and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param flag the flag.
*
* @see #getDefaultLinesVisible()
*/
public void setDefaultLinesVisible(boolean flag) {
this.defaultLinesVisible = flag;
fireChangeEvent();
}
// SHAPES VISIBLE
/**
* Returns the flag used to control whether or not the shape for an item is
* visible.
*
* @param series the series index (zero-based).
* @param item the item index (zero-based).
*
* @return A boolean.
*/
public boolean getItemShapeVisible(int series, int item) {
Boolean flag = getSeriesShapesVisible(series);
if (flag != null) {
return flag;
}
return this.defaultShapesVisible;
}
/**
* Returns the flag used to control whether or not the shapes for a series
* are visible.
*
* @param series the series index (zero-based).
*
* @return A boolean.
*
* @see #setSeriesShapesVisible(int, Boolean)
*/
public Boolean getSeriesShapesVisible(int series) {
return this.seriesShapesVisible.getBoolean(series);
}
/**
* Sets the 'shapes visible' flag for a series and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param series the series index (zero-based).
* @param visible the flag.
*
* @see #getSeriesShapesVisible(int)
*/
public void setSeriesShapesVisible(int series, boolean visible) {
setSeriesShapesVisible(series, Boolean.valueOf(visible));
}
/**
* Sets the 'shapes visible' flag for a series and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param series the series index (zero-based).
* @param flag the flag.
*
* @see #getSeriesShapesVisible(int)
*/
public void setSeriesShapesVisible(int series, Boolean flag) {
this.seriesShapesVisible.setBoolean(series, flag);
fireChangeEvent();
}
/**
* Returns the default 'shape visible' attribute.
*
* @return The base flag.
*
* @see #setDefaultShapesVisible(boolean)
*/
public boolean getDefaultShapesVisible() {
return this.defaultShapesVisible;
}
/**
* Sets the default 'shapes visible' flag and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param flag the flag.
*
* @see #getDefaultShapesVisible()
*/
public void setDefaultShapesVisible(boolean flag) {
this.defaultShapesVisible = flag;
fireChangeEvent();
}
/**
* Returns {@code true} if outlines should be drawn for shapes, and
* {@code false} otherwise.
*
* @return A boolean.
*
* @see #setDrawOutlines(boolean)
*/
public boolean getDrawOutlines() {
return this.drawOutlines;
}
/**
* Sets the flag that controls whether outlines are drawn for
* shapes, and sends a {@link RendererChangeEvent} to all registered
* listeners.
*
* In some cases, shapes look better if they do NOT have an outline, but
* this flag allows you to set your own preference.
*
* @param flag the flag.
*
* @see #getDrawOutlines()
*/
public void setDrawOutlines(boolean flag) {
this.drawOutlines = flag;
fireChangeEvent();
}
/**
* Returns the flag that controls whether the outline paint is used for
* shape outlines. If not, the regular series paint is used.
*
* @return A boolean.
*
* @see #setUseOutlinePaint(boolean)
*/
public boolean getUseOutlinePaint() {
return this.useOutlinePaint;
}
/**
* Sets the flag that controls whether the outline paint is used for shape
* outlines, and sends a {@link RendererChangeEvent} to all registered
* listeners.
*
* @param use the flag.
*
* @see #getUseOutlinePaint()
*/
public void setUseOutlinePaint(boolean use) {
this.useOutlinePaint = use;
fireChangeEvent();
}
// SHAPES FILLED
/**
* Returns the flag used to control whether or not the shape for an item
* is filled. The default implementation passes control to the
* {@code getSeriesShapesFilled} method. You can override this method
* if you require different behaviour.
*
* @param series the series index (zero-based).
* @param item the item index (zero-based).
*
* @return A boolean.
*/
public boolean getItemShapeFilled(int series, int item) {
return getSeriesShapesFilled(series);
}
/**
* Returns the flag used to control whether or not the shapes for a series
* are filled.
*
* @param series the series index (zero-based).
*
* @return A boolean.
*/
public boolean getSeriesShapesFilled(int series) {
Boolean flag = this.seriesShapesFilled.getBoolean(series);
if (flag != null) {
return flag;
}
return this.defaultShapesFilled;
}
/**
* Sets the 'shapes filled' flag for a series and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param series the series index (zero-based).
* @param filled the flag.
*
* @see #getSeriesShapesFilled(int)
*/
public void setSeriesShapesFilled(int series, Boolean filled) {
this.seriesShapesFilled.setBoolean(series, filled);
fireChangeEvent();
}
/**
* Sets the 'shapes filled' flag for a series and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param series the series index (zero-based).
* @param filled the flag.
*
* @see #getSeriesShapesFilled(int)
*/
public void setSeriesShapesFilled(int series, boolean filled) {
// delegate
setSeriesShapesFilled(series, Boolean.valueOf(filled));
}
/**
* Returns the default 'shape filled' attribute.
*
* @return The base flag.
*
* @see #setDefaultShapesFilled(boolean)
*/
public boolean getDefaultShapesFilled() {
return this.defaultShapesFilled;
}
/**
* Sets the default 'shapes filled' flag and sends a
* {@link RendererChangeEvent} to all registered listeners.
*
* @param flag the flag.
*
* @see #getDefaultShapesFilled()
*/
public void setDefaultShapesFilled(boolean flag) {
this.defaultShapesFilled = flag;
fireChangeEvent();
}
/**
* Returns {@code true} if the renderer should use the fill paint
* setting to fill shapes, and {@code false} if it should just
* use the regular paint.
*
* @return A boolean.
*
* @see #setUseFillPaint(boolean)
*/
public boolean getUseFillPaint() {
return this.useFillPaint;
}
/**
* Sets the flag that controls whether the fill paint is used to fill
* shapes, and sends a {@link RendererChangeEvent} to all
* registered listeners.
*
* @param flag the flag.
*
* @see #getUseFillPaint()
*/
public void setUseFillPaint(boolean flag) {
this.useFillPaint = flag;
fireChangeEvent();
}
/**
* Returns the flag that controls whether or not the x-position for each
* data item is offset within the category according to the series.
*
* @return A boolean.
*
* @see #setUseSeriesOffset(boolean)
*/
public boolean getUseSeriesOffset() {
return this.useSeriesOffset;
}
/**
* Sets the flag that controls whether or not the x-position for each
* data item is offset within its category according to the series, and
* sends a {@link RendererChangeEvent} to all registered listeners.
*
* @param offset the offset.
*
* @see #getUseSeriesOffset()
*/
public void setUseSeriesOffset(boolean offset) {
this.useSeriesOffset = offset;
fireChangeEvent();
}
/**
* Returns the item margin, which is the gap between items within a
* category (expressed as a percentage of the overall category width).
* This can be used to match the offset alignment with the bars drawn by
* a {@link BarRenderer}).
*
* @return The item margin.
*
* @see #setItemMargin(double)
* @see #getUseSeriesOffset()
*/
public double getItemMargin() {
return this.itemMargin;
}
/**
* Sets the item margin, which is the gap between items within a category
* (expressed as a percentage of the overall category width), and sends
* a {@link RendererChangeEvent} to all registered listeners.
*
* @param margin the margin (0.0 <= margin < 1.0).
*
* @see #getItemMargin()
* @see #getUseSeriesOffset()
*/
public void setItemMargin(double margin) {
if (margin < 0.0 || margin >= 1.0) {
throw new IllegalArgumentException("Requires 0.0 <= margin < 1.0.");
}
this.itemMargin = margin;
fireChangeEvent();
}
/**
* Returns a legend item for a series.
*
* @param datasetIndex the dataset index (zero-based).
* @param series the series index (zero-based).
*
* @return The legend item.
*/
@Override
public LegendItem getLegendItem(int datasetIndex, int series) {
CategoryPlot cp = getPlot();
if (cp == null) {
return null;
}
if (isSeriesVisible(series) && isSeriesVisibleInLegend(series)) {
CategoryDataset dataset = cp.getDataset(datasetIndex);
String label = getLegendItemLabelGenerator().generateLabel(
dataset, series);
String description = label;
String toolTipText = null;
if (getLegendItemToolTipGenerator() != null) {
toolTipText = getLegendItemToolTipGenerator().generateLabel(
dataset, series);
}
String urlText = null;
if (getLegendItemURLGenerator() != null) {
urlText = getLegendItemURLGenerator().generateLabel(
dataset, series);
}
Shape shape = lookupLegendShape(series);
Paint paint = lookupSeriesPaint(series);
Paint fillPaint = (this.useFillPaint
? getItemFillPaint(series, 0) : paint);
boolean shapeOutlineVisible = this.drawOutlines;
Paint outlinePaint = (this.useOutlinePaint
? getItemOutlinePaint(series, 0) : paint);
Stroke outlineStroke = lookupSeriesOutlineStroke(series);
boolean lineVisible = getItemLineVisible(series, 0);
boolean shapeVisible = getItemShapeVisible(series, 0);
LegendItem result = new LegendItem(label, description, toolTipText,
urlText, shapeVisible, shape, getItemShapeFilled(series, 0),
fillPaint, shapeOutlineVisible, outlinePaint, outlineStroke,
lineVisible, new Line2D.Double(-7.0, 0.0, 7.0, 0.0),
getItemStroke(series, 0), getItemPaint(series, 0));
result.setLabelFont(lookupLegendTextFont(series));
Paint labelPaint = lookupLegendTextPaint(series);
if (labelPaint != null) {
result.setLabelPaint(labelPaint);
}
result.setDataset(dataset);
result.setDatasetIndex(datasetIndex);
result.setSeriesKey(dataset.getRowKey(series));
result.setSeriesIndex(series);
return result;
}
return null;
}
/**
* This renderer uses two passes to draw the data.
*
* @return The pass count ({@code 2} for this renderer).
*/
@Override
public int getPassCount() {
return 2;
}
/**
* Draw a single data item.
*
* @param g2 the graphics device.
* @param state the renderer state.
* @param dataArea the area in which the data is drawn.
* @param plot the plot.
* @param domainAxis the domain axis.
* @param rangeAxis the range axis.
* @param dataset the dataset.
* @param row the row index (zero-based).
* @param column the column index (zero-based).
* @param pass the pass index.
*/
@Override
public void drawItem(Graphics2D g2, CategoryItemRendererState state,
Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
ValueAxis rangeAxis, CategoryDataset dataset, int row, int column,
int pass) {
// do nothing if item is not visible
if (!getItemVisible(row, column)) {
return;
}
// do nothing if both the line and shape are not visible
if (!getItemLineVisible(row, column)
&& !getItemShapeVisible(row, column)) {
return;
}
// nothing is drawn for null...
Number v = dataset.getValue(row, column);
if (v == null) {
return;
}
int visibleRow = state.getVisibleSeriesIndex(row);
if (visibleRow < 0) {
return;
}
int visibleRowCount = state.getVisibleSeriesCount();
PlotOrientation orientation = plot.getOrientation();
// current data point...
double x1;
if (this.useSeriesOffset) {
x1 = domainAxis.getCategorySeriesMiddle(column,
dataset.getColumnCount(), visibleRow, visibleRowCount,
this.itemMargin, dataArea, plot.getDomainAxisEdge());
}
else {
x1 = domainAxis.getCategoryMiddle(column, getColumnCount(),
dataArea, plot.getDomainAxisEdge());
}
double value = v.doubleValue();
double y1 = rangeAxis.valueToJava2D(value, dataArea,
plot.getRangeAxisEdge());
if (pass == 0 && getItemLineVisible(row, column)) {
if (column != 0) {
Number previousValue = dataset.getValue(row, column - 1);
if (previousValue != null) {
// previous data point...
double previous = previousValue.doubleValue();
double x0;
if (this.useSeriesOffset) {
x0 = domainAxis.getCategorySeriesMiddle(
column - 1, dataset.getColumnCount(),
visibleRow, visibleRowCount,
this.itemMargin, dataArea,
plot.getDomainAxisEdge());
}
else {
x0 = domainAxis.getCategoryMiddle(column - 1,
getColumnCount(), dataArea,
plot.getDomainAxisEdge());
}
double y0 = rangeAxis.valueToJava2D(previous, dataArea,
plot.getRangeAxisEdge());
Line2D line = null;
if (orientation == PlotOrientation.HORIZONTAL) {
line = new Line2D.Double(y0, x0, y1, x1);
}
else if (orientation == PlotOrientation.VERTICAL) {
line = new Line2D.Double(x0, y0, x1, y1);
}
g2.setPaint(getItemPaint(row, column));
g2.setStroke(getItemStroke(row, column));
g2.draw(line);
}
}
}
if (pass == 1) {
Shape shape = getItemShape(row, column);
if (orientation == PlotOrientation.HORIZONTAL) {
shape = ShapeUtils.createTranslatedShape(shape, y1, x1);
}
else if (orientation == PlotOrientation.VERTICAL) {
shape = ShapeUtils.createTranslatedShape(shape, x1, y1);
}
if (getItemShapeVisible(row, column)) {
if (getItemShapeFilled(row, column)) {
if (this.useFillPaint) {
g2.setPaint(getItemFillPaint(row, column));
}
else {
g2.setPaint(getItemPaint(row, column));
}
g2.fill(shape);
}
if (this.drawOutlines) {
if (this.useOutlinePaint) {
g2.setPaint(getItemOutlinePaint(row, column));
}
else {
g2.setPaint(getItemPaint(row, column));
}
g2.setStroke(getItemOutlineStroke(row, column));
g2.draw(shape);
}
}
// draw the item label if there is one...
if (isItemLabelVisible(row, column)) {
if (orientation == PlotOrientation.HORIZONTAL) {
drawItemLabel(g2, orientation, dataset, row, column, y1,
x1, (value < 0.0));
}
else if (orientation == PlotOrientation.VERTICAL) {
drawItemLabel(g2, orientation, dataset, row, column, x1,
y1, (value < 0.0));
}
}
// submit the current data point as a crosshair candidate
int datasetIndex = plot.indexOf(dataset);
updateCrosshairValues(state.getCrosshairState(),
dataset.getRowKey(row), dataset.getColumnKey(column),
value, datasetIndex, x1, y1, orientation);
// add an item entity, if this information is being collected
EntityCollection entities = state.getEntityCollection();
if (entities != null) {
addItemEntity(entities, dataset, row, column, shape);
}
}
}
/**
* Tests this renderer 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 LineAndShapeRenderer)) {
return false;
}
LineAndShapeRenderer that = (LineAndShapeRenderer) obj;
if (this.defaultLinesVisible != that.defaultLinesVisible) {
return false;
}
if (!Objects.equals(this.seriesLinesVisible,
that.seriesLinesVisible)) {
return false;
}
if (this.defaultShapesVisible != that.defaultShapesVisible) {
return false;
}
if (!Objects.equals(this.seriesShapesVisible,
that.seriesShapesVisible)) {
return false;
}
if (!Objects.equals(this.seriesShapesFilled,
that.seriesShapesFilled)) {
return false;
}
if (this.defaultShapesFilled != that.defaultShapesFilled) {
return false;
}
if (this.useOutlinePaint != that.useOutlinePaint) {
return false;
}
if (this.useSeriesOffset != that.useSeriesOffset) {
return false;
}
if (this.itemMargin != that.itemMargin) {
return false;
}
return super.equals(obj);
}
/**
* Returns an independent copy of the renderer.
*
* @return A clone.
*
* @throws CloneNotSupportedException should not happen.
*/
@Override
public Object clone() throws CloneNotSupportedException {
LineAndShapeRenderer clone = (LineAndShapeRenderer) super.clone();
clone.seriesLinesVisible
= (BooleanList) this.seriesLinesVisible.clone();
clone.seriesShapesVisible
= (BooleanList) this.seriesShapesVisible.clone();
clone.seriesShapesFilled
= (BooleanList) this.seriesShapesFilled.clone();
return clone;
}
}