001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------------
028: * IntervalBarRenderer.java
029: * ------------------------
030: * (C) Copyright 2002-2007, by Jeremy Bowman.
031: *
032: * Original Author: Jeremy Bowman;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: * Christian W. Zuckschwerdt;
035: *
036: * $Id: IntervalBarRenderer.java,v 1.6.2.4 2007/06/01 15:12:15 mungady Exp $
037: *
038: * Changes
039: * -------
040: * 29-Apr-2002 : Version 1, contributed by Jeremy Bowman (DG);
041: * 11-May-2002 : Use CategoryPlot.getLabelsVisible() (JB);
042: * 29-May-2002 : Added constructors (DG);
043: * 26-Jun-2002 : Added axis to initialise method (DG);
044: * 20-Sep-2002 : Added basic support for chart entities (DG);
045: * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and
046: * CategoryToolTipGenerator interface (DG);
047: * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
048: * 25-Mar-2003 : Implemented Serializable (DG);
049: * 30-Jul-2003 : Modified entity constructor (CZ);
050: * 19-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
051: * 08-Sep-2003 : Added checks for null values (DG);
052: * 07-Oct-2003 : Added renderer state (DG);
053: * 21-Oct-2003 : Bar width moved into renderer state (DG);
054: * 23-Dec-2003 : Removed the deprecated MultiIntervalCategoryDataset
055: * interface (DG);
056: * 05-Nov-2004 : Modified drawItem() signature (DG);
057: * 20-Apr-2005 : Renamed CategoryLabelGenerator
058: * --> CategoryItemLabelGenerator (DG);
059: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
060: *
061: */
062:
063: package org.jfree.chart.renderer.category;
064:
065: import java.awt.Graphics2D;
066: import java.awt.Paint;
067: import java.awt.Stroke;
068: import java.awt.geom.Rectangle2D;
069: import java.io.Serializable;
070:
071: import org.jfree.chart.axis.CategoryAxis;
072: import org.jfree.chart.axis.ValueAxis;
073: import org.jfree.chart.entity.CategoryItemEntity;
074: import org.jfree.chart.entity.EntityCollection;
075: import org.jfree.chart.labels.CategoryItemLabelGenerator;
076: import org.jfree.chart.labels.CategoryToolTipGenerator;
077: import org.jfree.chart.plot.CategoryPlot;
078: import org.jfree.chart.plot.PlotOrientation;
079: import org.jfree.data.category.CategoryDataset;
080: import org.jfree.data.category.IntervalCategoryDataset;
081: import org.jfree.ui.RectangleEdge;
082: import org.jfree.util.PublicCloneable;
083:
084: /**
085: * A renderer that handles the drawing of bars for a bar plot where
086: * each bar has a high and low value.
087: * <p>
088: * For use with the {@link CategoryPlot} class.
089: */
090: public class IntervalBarRenderer extends BarRenderer implements
091: CategoryItemRenderer, Cloneable, PublicCloneable, Serializable {
092:
093: /** For serialization. */
094: private static final long serialVersionUID = -5068857361615528725L;
095:
096: /**
097: * Constructs a new renderer.
098: */
099: public IntervalBarRenderer() {
100: super ();
101: }
102:
103: /**
104: * Draws the bar for a single (series, category) data item.
105: *
106: * @param g2 the graphics device.
107: * @param state the renderer state.
108: * @param dataArea the data area.
109: * @param plot the plot.
110: * @param domainAxis the domain axis.
111: * @param rangeAxis the range axis.
112: * @param dataset the dataset.
113: * @param row the row index (zero-based).
114: * @param column the column index (zero-based).
115: * @param pass the pass index.
116: */
117: public void drawItem(Graphics2D g2,
118: CategoryItemRendererState state, Rectangle2D dataArea,
119: CategoryPlot plot, CategoryAxis domainAxis,
120: ValueAxis rangeAxis, CategoryDataset dataset, int row,
121: int column, int pass) {
122:
123: if (dataset instanceof IntervalCategoryDataset) {
124: IntervalCategoryDataset d = (IntervalCategoryDataset) dataset;
125: drawInterval(g2, state, dataArea, plot, domainAxis,
126: rangeAxis, d, row, column);
127: } else {
128: super .drawItem(g2, state, dataArea, plot, domainAxis,
129: rangeAxis, dataset, row, column, pass);
130: }
131:
132: }
133:
134: /**
135: * Draws a single interval.
136: *
137: * @param g2 the graphics device.
138: * @param state the renderer state.
139: * @param dataArea the data plot area.
140: * @param plot the plot.
141: * @param domainAxis the domain axis.
142: * @param rangeAxis the range axis.
143: * @param dataset the data.
144: * @param row the row index (zero-based).
145: * @param column the column index (zero-based).
146: */
147: protected void drawInterval(Graphics2D g2,
148: CategoryItemRendererState state, Rectangle2D dataArea,
149: CategoryPlot plot, CategoryAxis domainAxis,
150: ValueAxis rangeAxis, IntervalCategoryDataset dataset,
151: int row, int column) {
152:
153: int seriesCount = getRowCount();
154: int categoryCount = getColumnCount();
155:
156: PlotOrientation orientation = plot.getOrientation();
157:
158: double rectX = 0.0;
159: double rectY = 0.0;
160:
161: RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
162: RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
163:
164: // Y0
165: Number value0 = dataset.getEndValue(row, column);
166: if (value0 == null) {
167: return;
168: }
169: double java2dValue0 = rangeAxis.valueToJava2D(value0
170: .doubleValue(), dataArea, rangeAxisLocation);
171:
172: // Y1
173: Number value1 = dataset.getStartValue(row, column);
174: if (value1 == null) {
175: return;
176: }
177: double java2dValue1 = rangeAxis.valueToJava2D(value1
178: .doubleValue(), dataArea, rangeAxisLocation);
179:
180: if (java2dValue1 < java2dValue0) {
181: double temp = java2dValue1;
182: java2dValue1 = java2dValue0;
183: java2dValue0 = temp;
184: Number tempNum = value1;
185: value1 = value0;
186: value0 = tempNum;
187: }
188:
189: // BAR WIDTH
190: double rectWidth = state.getBarWidth();
191:
192: // BAR HEIGHT
193: double rectHeight = Math.abs(java2dValue1 - java2dValue0);
194:
195: if (orientation == PlotOrientation.HORIZONTAL) {
196: // BAR Y
197: rectY = domainAxis.getCategoryStart(column,
198: getColumnCount(), dataArea, domainAxisLocation);
199: if (seriesCount > 1) {
200: double seriesGap = dataArea.getHeight()
201: * getItemMargin()
202: / (categoryCount * (seriesCount - 1));
203: rectY = rectY + row * (state.getBarWidth() + seriesGap);
204: } else {
205: rectY = rectY + row * state.getBarWidth();
206: }
207:
208: rectX = java2dValue0;
209:
210: rectHeight = state.getBarWidth();
211: rectWidth = Math.abs(java2dValue1 - java2dValue0);
212:
213: } else if (orientation == PlotOrientation.VERTICAL) {
214: // BAR X
215: rectX = domainAxis.getCategoryStart(column,
216: getColumnCount(), dataArea, domainAxisLocation);
217:
218: if (seriesCount > 1) {
219: double seriesGap = dataArea.getWidth()
220: * getItemMargin()
221: / (categoryCount * (seriesCount - 1));
222: rectX = rectX + row * (state.getBarWidth() + seriesGap);
223: } else {
224: rectX = rectX + row * state.getBarWidth();
225: }
226:
227: rectY = java2dValue0;
228:
229: }
230: Rectangle2D bar = new Rectangle2D.Double(rectX, rectY,
231: rectWidth, rectHeight);
232: Paint seriesPaint = getItemPaint(row, column);
233: g2.setPaint(seriesPaint);
234: g2.fill(bar);
235:
236: // draw the outline...
237: if (state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
238: Stroke stroke = getItemOutlineStroke(row, column);
239: Paint paint = getItemOutlinePaint(row, column);
240: if (stroke != null && paint != null) {
241: g2.setStroke(stroke);
242: g2.setPaint(paint);
243: g2.draw(bar);
244: }
245: }
246:
247: CategoryItemLabelGenerator generator = getItemLabelGenerator(
248: row, column);
249: if (generator != null && isItemLabelVisible(row, column)) {
250: drawItemLabel(g2, dataset, row, column, plot, generator,
251: bar, false);
252: }
253:
254: // collect entity and tool tip information...
255: if (state.getInfo() != null) {
256: EntityCollection entities = state.getEntityCollection();
257: if (entities != null) {
258: String tip = null;
259: CategoryToolTipGenerator tipster = getToolTipGenerator(
260: row, column);
261: if (tipster != null) {
262: tip = tipster.generateToolTip(dataset, row, column);
263: }
264: String url = null;
265: if (getItemURLGenerator(row, column) != null) {
266: url = getItemURLGenerator(row, column).generateURL(
267: dataset, row, column);
268: }
269: CategoryItemEntity entity = new CategoryItemEntity(bar,
270: tip, url, dataset, dataset.getRowKey(row),
271: dataset.getColumnKey(column));
272: entities.add(entity);
273: }
274: }
275:
276: }
277:
278: }
|