001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * --------------------------------- 028 * IntervalXYItemLabelGenerator.java 029 * --------------------------------- 030 * (C) Copyright 2008, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 26-May-2008 : Version 1 (DG); 038 * 039 */ 040 041package org.jfree.chart.labels; 042 043import java.io.Serializable; 044import java.text.DateFormat; 045import java.text.MessageFormat; 046import java.text.NumberFormat; 047import java.util.Date; 048 049import org.jfree.data.xy.IntervalXYDataset; 050import org.jfree.data.xy.XYDataset; 051import org.jfree.util.PublicCloneable; 052 053/** 054 * An item label generator for datasets that implement the 055 * {@link IntervalXYDataset} interface. 056 * 057 * @since 1.0.10 058 */ 059public class IntervalXYItemLabelGenerator extends AbstractXYItemLabelGenerator 060 implements XYItemLabelGenerator, Cloneable, PublicCloneable, 061 Serializable { 062 063 /** The default item label format. */ 064 public static final String DEFAULT_ITEM_LABEL_FORMAT = "{5} - {6}"; 065 066 /** 067 * Creates an item label generator using default number formatters. 068 */ 069 public IntervalXYItemLabelGenerator() { 070 this(DEFAULT_ITEM_LABEL_FORMAT, NumberFormat.getNumberInstance(), 071 NumberFormat.getNumberInstance()); 072 } 073 074 /** 075 * Creates an item label generator using the specified number formatters. 076 * 077 * @param formatString the item label format string (<code>null</code> not 078 * permitted). 079 * @param xFormat the format object for the x values (<code>null</code> 080 * not permitted). 081 * @param yFormat the format object for the y values (<code>null</code> 082 * not permitted). 083 */ 084 public IntervalXYItemLabelGenerator(String formatString, 085 NumberFormat xFormat, NumberFormat yFormat) { 086 087 super(formatString, xFormat, yFormat); 088 } 089 090 /** 091 * Creates an item label generator using the specified formatters. 092 * 093 * @param formatString the item label format string (<code>null</code> 094 * not permitted). 095 * @param xFormat the format object for the x values (<code>null</code> 096 * not permitted). 097 * @param yFormat the format object for the y values (<code>null</code> 098 * not permitted). 099 */ 100 public IntervalXYItemLabelGenerator(String formatString, 101 DateFormat xFormat, NumberFormat yFormat) { 102 103 super(formatString, xFormat, yFormat); 104 } 105 106 /** 107 * Creates an item label generator using the specified formatters (a 108 * number formatter for the x-values and a date formatter for the 109 * y-values). 110 * 111 * @param formatString the item label format string (<code>null</code> 112 * not permitted). 113 * @param xFormat the format object for the x values (<code>null</code> 114 * permitted). 115 * @param yFormat the format object for the y values (<code>null</code> 116 * not permitted). 117 */ 118 public IntervalXYItemLabelGenerator(String formatString, 119 NumberFormat xFormat, DateFormat yFormat) { 120 121 super(formatString, xFormat, yFormat); 122 } 123 124 /** 125 * Creates a label generator using the specified date formatters. 126 * 127 * @param formatString the label format string (<code>null</code> not 128 * permitted). 129 * @param xFormat the format object for the x values (<code>null</code> 130 * not permitted). 131 * @param yFormat the format object for the y values (<code>null</code> 132 * not permitted). 133 */ 134 public IntervalXYItemLabelGenerator(String formatString, 135 DateFormat xFormat, DateFormat yFormat) { 136 137 super(formatString, xFormat, yFormat); 138 } 139 140 /** 141 * Creates the array of items that can be passed to the 142 * {@link MessageFormat} class for creating labels. 143 * 144 * @param dataset the dataset (<code>null</code> not permitted). 145 * @param series the series (zero-based index). 146 * @param item the item (zero-based index). 147 * 148 * @return An array of seven items from the dataset formatted as 149 * <code>String</code> objects (never <code>null</code>). 150 */ 151 @Override 152 protected Object[] createItemArray(XYDataset dataset, int series, 153 int item) { 154 155 IntervalXYDataset intervalDataset = null; 156 if (dataset instanceof IntervalXYDataset) { 157 intervalDataset = (IntervalXYDataset) dataset; 158 } 159 Object[] result = new Object[7]; 160 result[0] = dataset.getSeriesKey(series).toString(); 161 162 double x = dataset.getXValue(series, item); 163 double xs = x; 164 double xe = x; 165 double y = dataset.getYValue(series, item); 166 double ys = y; 167 double ye = y; 168 if (intervalDataset != null) { 169 xs = intervalDataset.getStartXValue(series, item); 170 xe = intervalDataset.getEndXValue(series, item); 171 ys = intervalDataset.getStartYValue(series, item); 172 ye = intervalDataset.getEndYValue(series, item); 173 } 174 175 DateFormat xdf = getXDateFormat(); 176 if (xdf != null) { 177 result[1] = xdf.format(new Date((long) x)); 178 result[2] = xdf.format(new Date((long) xs)); 179 result[3] = xdf.format(new Date((long) xe)); 180 } 181 else { 182 NumberFormat xnf = getXFormat(); 183 result[1] = xnf.format(x); 184 result[2] = xnf.format(xs); 185 result[3] = xnf.format(xe); 186 } 187 188 NumberFormat ynf = getYFormat(); 189 DateFormat ydf = getYDateFormat(); 190 if (Double.isNaN(y) && dataset.getY(series, item) == null) { 191 result[4] = getNullYString(); 192 } 193 else { 194 if (ydf != null) { 195 result[4] = ydf.format(new Date((long) y)); 196 } 197 else { 198 result[4] = ynf.format(y); 199 } 200 } 201 if (Double.isNaN(ys) && intervalDataset != null 202 && intervalDataset.getStartY(series, item) == null) { 203 result[5] = getNullYString(); 204 } 205 else { 206 if (ydf != null) { 207 result[5] = ydf.format(new Date((long) ys)); 208 } 209 else { 210 result[5] = ynf.format(ys); 211 } 212 } 213 if (Double.isNaN(ye) && intervalDataset != null 214 && intervalDataset.getEndY(series, item) == null) { 215 result[6] = getNullYString(); 216 } 217 else { 218 if (ydf != null) { 219 result[6] = ydf.format(new Date((long) ye)); 220 } 221 else { 222 result[6] = ynf.format(ye); 223 } 224 } 225 return result; 226 } 227 228 /** 229 * Generates the item label text for an item in a dataset. 230 * 231 * @param dataset the dataset (<code>null</code> not permitted). 232 * @param series the series index (zero-based). 233 * @param item the item index (zero-based). 234 * 235 * @return The label text (possibly <code>null</code>). 236 */ 237 @Override 238 public String generateLabel(XYDataset dataset, int series, int item) { 239 return generateLabelString(dataset, series, item); 240 } 241 242 /** 243 * Returns an independent copy of the generator. 244 * 245 * @return A clone. 246 * 247 * @throws CloneNotSupportedException if cloning is not supported. 248 */ 249 @Override 250 public Object clone() throws CloneNotSupportedException { 251 return super.clone(); 252 } 253 254 /** 255 * Tests this object for equality with an arbitrary object. 256 * 257 * @param obj the other object (<code>null</code> permitted). 258 * 259 * @return A boolean. 260 */ 261 @Override 262 public boolean equals(Object obj) { 263 if (obj == this) { 264 return true; 265 } 266 if (!(obj instanceof IntervalXYItemLabelGenerator)) { 267 return false; 268 } 269 return super.equals(obj); 270 } 271 272}