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