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