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 * SymbolicXYItemLabelGenerator.java 029 * --------------------------------- 030 * (C) Copyright 2001-2021, by Anthony Boulestreau and Contributors. 031 * 032 * Original Author: Anthony Boulestreau; 033 * Contributor(s): David Gilbert; 034 * 035 */ 036 037package org.jfree.chart.labels; 038 039import java.io.Serializable; 040import org.jfree.chart.internal.Args; 041import org.jfree.chart.api.PublicCloneable; 042 043import org.jfree.data.time.RegularTimePeriod; 044import org.jfree.data.time.TimeSeriesCollection; 045import org.jfree.data.xy.XYDataset; 046import org.jfree.data.xy.XisSymbolic; 047import org.jfree.data.xy.YisSymbolic; 048 049/** 050 * A standard item label generator for plots that use data from an 051 * {@link XYDataset}. 052 */ 053public class SymbolicXYItemLabelGenerator implements XYItemLabelGenerator, 054 XYToolTipGenerator, Cloneable, PublicCloneable, Serializable { 055 056 /** For serialization. */ 057 private static final long serialVersionUID = 3963400354475494395L; 058 059 /** 060 * Generates a tool tip text item for a particular item within a series. 061 * 062 * @param dataset the dataset ({@code null} not permitted). 063 * @param series the series number (zero-based index). 064 * @param item the item number (zero-based index). 065 * 066 * @return The tool tip text (possibly {@code null}). 067 */ 068 @Override 069 public String generateToolTip(XYDataset dataset, int series, int item) { 070 Args.nullNotPermitted(dataset, "dataset"); 071 String xStr, yStr; 072 if (dataset instanceof YisSymbolic) { 073 yStr = ((YisSymbolic) dataset).getYSymbolicValue(series, item); 074 } 075 else { 076 double y = dataset.getYValue(series, item); 077 yStr = Double.toString(round(y, 2)); 078 } 079 if (dataset instanceof XisSymbolic) { 080 xStr = ((XisSymbolic) dataset).getXSymbolicValue(series, item); 081 } 082 else if (dataset instanceof TimeSeriesCollection) { 083 RegularTimePeriod p 084 = ((TimeSeriesCollection) dataset).getSeries(series) 085 .getTimePeriod(item); 086 xStr = p.toString(); 087 } 088 else { 089 double x = dataset.getXValue(series, item); 090 xStr = Double.toString(round(x, 2)); 091 } 092 return "X: " + xStr + ", Y: " + yStr; 093 } 094 095 /** 096 * Generates a label for the specified item. The label is typically a 097 * formatted version of the data value, but any text can be used. 098 * 099 * @param dataset the dataset ({@code null} not permitted). 100 * @param series the series index (zero-based). 101 * @param category the category index (zero-based). 102 * 103 * @return The label (possibly {@code null}). 104 */ 105 @Override 106 public String generateLabel(XYDataset dataset, int series, int category) { 107 return null; //TODO: implement this method properly 108 } 109 110 /** 111 * Round a double value. 112 * 113 * @param value the value. 114 * @param nb the exponent. 115 * 116 * @return The rounded value. 117 */ 118 private static double round(double value, int nb) { 119 if (nb <= 0) { 120 return Math.floor(value + 0.5d); 121 } 122 double p = Math.pow(10, nb); 123 double tempval = Math.floor(value * p + 0.5d); 124 return tempval / p; 125 } 126 127 /** 128 * Returns an independent copy of the generator. 129 * 130 * @return A clone. 131 * 132 * @throws CloneNotSupportedException if cloning is not supported. 133 */ 134 @Override 135 public Object clone() throws CloneNotSupportedException { 136 return super.clone(); 137 } 138 139 /** 140 * Tests if this object is equal to another. 141 * 142 * @param obj the other object. 143 * 144 * @return A boolean. 145 */ 146 @Override 147 public boolean equals(Object obj) { 148 if (obj == this) { 149 return true; 150 } 151 if (obj instanceof SymbolicXYItemLabelGenerator) { 152 return true; 153 } 154 return false; 155 } 156 157 /** 158 * Returns a hash code for this instance. 159 * 160 * @return A hash code. 161 */ 162 @Override 163 public int hashCode() { 164 int result = 127; 165 return result; 166 } 167 168}