001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2005, 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 * XYBarDataset.java 029 * ----------------- 030 * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: XYBarDataset.java,v 1.4.2.1 2005/10/25 21:36:51 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 02-Mar-2004 : Version 1 (DG); 040 * 05-May-2004 : Now extends AbstractIntervalXYDataset (DG); 041 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 042 * getYValue() (DG); 043 * 044 */ 045 046 package org.jfree.data.xy; 047 048 import org.jfree.data.general.DatasetChangeEvent; 049 import org.jfree.data.general.DatasetChangeListener; 050 051 /** 052 * A dataset wrapper class that converts a standard {@link XYDataset} into an 053 * {@link IntervalXYDataset} suitable for use in creating XY bar charts. 054 */ 055 public class XYBarDataset extends AbstractIntervalXYDataset 056 implements IntervalXYDataset, DatasetChangeListener { 057 058 /** The underlying dataset. */ 059 private XYDataset underlying; 060 061 /** The bar width. */ 062 private double barWidth; 063 064 /** 065 * Creates a new dataset. 066 * 067 * @param underlying the underlying dataset. 068 * @param barWidth the width of the bars. 069 */ 070 public XYBarDataset(XYDataset underlying, double barWidth) { 071 this.underlying = underlying; 072 this.underlying.addChangeListener(this); 073 this.barWidth = barWidth; 074 } 075 076 /** 077 * Returns the number of series in the dataset. 078 * 079 * @return The series count. 080 */ 081 public int getSeriesCount() { 082 return this.underlying.getSeriesCount(); 083 } 084 085 /** 086 * Returns the key for a series. 087 * 088 * @param series the series index (zero-based). 089 * 090 * @return The series key. 091 */ 092 public Comparable getSeriesKey(int series) { 093 return this.underlying.getSeriesKey(series); 094 } 095 096 /** 097 * Returns the number of items in a series. 098 * 099 * @param series the series index (zero-based). 100 * 101 * @return The item count. 102 */ 103 public int getItemCount(int series) { 104 return this.underlying.getItemCount(series); 105 } 106 107 /** 108 * Returns the x-value for an item within a series. The x-values may or 109 * may not be returned in ascending order, that is up to the class 110 * implementing the interface. 111 * 112 * @param series the series index (zero-based). 113 * @param item the item index (zero-based). 114 * 115 * @return The x-value. 116 */ 117 public Number getX(int series, int item) { 118 return this.underlying.getX(series, item); 119 } 120 121 /** 122 * Returns the y-value for an item within a series. 123 * 124 * @param series the series index (zero-based). 125 * @param item the item index (zero-based). 126 * 127 * @return The y-value (possibly <code>null</code>). 128 */ 129 public Number getY(int series, int item) { 130 return this.underlying.getY(series, item); 131 } 132 133 /** 134 * Returns the starting X value for the specified series and item. 135 * 136 * @param series the series index (zero-based). 137 * @param item the item index (zero-based). 138 * 139 * @return The value. 140 */ 141 public Number getStartX(int series, int item) { 142 Number result = null; 143 Number xnum = this.underlying.getX(series, item); 144 if (xnum != null) { 145 result = new Double(xnum.doubleValue() - this.barWidth / 2.0); 146 } 147 return result; 148 } 149 150 /** 151 * Returns the ending X value for the specified series and item. 152 * 153 * @param series the series index (zero-based). 154 * @param item the item index (zero-based). 155 * 156 * @return The value. 157 */ 158 public Number getEndX(int series, int item) { 159 Number result = null; 160 Number xnum = this.underlying.getX(series, item); 161 if (xnum != null) { 162 result = new Double(xnum.doubleValue() + this.barWidth / 2.0); 163 } 164 return result; 165 } 166 167 /** 168 * Returns the starting Y value for the specified series and item. 169 * 170 * @param series the series index (zero-based). 171 * @param item the item index (zero-based). 172 * 173 * @return The value. 174 */ 175 public Number getStartY(int series, int item) { 176 return this.underlying.getY(series, item); 177 } 178 179 /** 180 * Returns the ending Y value for the specified series and item. 181 * 182 * @param series the series index (zero-based). 183 * @param item the item index (zero-based). 184 * 185 * @return The value. 186 */ 187 public Number getEndY(int series, int item) { 188 return this.underlying.getY(series, item); 189 } 190 191 /** 192 * Receives notification of an dataset change event. 193 * 194 * @param event information about the event. 195 */ 196 public void datasetChanged(DatasetChangeEvent event) { 197 this.notifyListeners(event); 198 } 199 200 }