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