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