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