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 * StandardXYItemLabelGenerator.java 029 * --------------------------------- 030 * (C) Copyright 2001-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.NumberFormat; 042import org.jfree.chart.util.PublicCloneable; 043 044import org.jfree.data.xy.XYDataset; 045 046/** 047 * A standard item label generator for plots that use data from an 048 * {@link org.jfree.data.xy.XYDataset}. 049 */ 050public class StandardXYItemLabelGenerator extends AbstractXYItemLabelGenerator 051 implements XYItemLabelGenerator, Cloneable, PublicCloneable, 052 Serializable { 053 054 /** For serialization. */ 055 private static final long serialVersionUID = 7807668053171837925L; 056 057 /** The default item label format. */ 058 public static final String DEFAULT_ITEM_LABEL_FORMAT = "{2}"; 059 060 /** 061 * Creates an item label generator using default number formatters. 062 */ 063 public StandardXYItemLabelGenerator() { 064 this(DEFAULT_ITEM_LABEL_FORMAT, NumberFormat.getNumberInstance(), 065 NumberFormat.getNumberInstance()); 066 } 067 068 /** 069 * Creates an item label generator using the specified number formatters. 070 * 071 * @param formatString the item label format string ({@code null} not 072 * permitted). 073 * 074 * @since 1.0.14 075 */ 076 public StandardXYItemLabelGenerator(String formatString) { 077 this(formatString, NumberFormat.getNumberInstance(), 078 NumberFormat.getNumberInstance()); 079 } 080 081 /** 082 * Creates an item label generator using the specified number formatters. 083 * 084 * @param formatString the item label format string ({@code null} not 085 * permitted). 086 * @param xFormat the format object for the x values ({@code null} 087 * not permitted). 088 * @param yFormat the format object for the y values ({@code null} 089 * not permitted). 090 */ 091 public StandardXYItemLabelGenerator(String formatString, 092 NumberFormat xFormat, NumberFormat yFormat) { 093 094 super(formatString, xFormat, yFormat); 095 } 096 097 /** 098 * Creates an item label generator using the specified formatters. 099 * 100 * @param formatString the item label format string ({@code null} 101 * not permitted). 102 * @param xFormat the format object for the x values ({@code null} 103 * not permitted). 104 * @param yFormat the format object for the y values ({@code null} 105 * not permitted). 106 */ 107 public StandardXYItemLabelGenerator(String formatString, 108 DateFormat xFormat, NumberFormat yFormat) { 109 110 super(formatString, xFormat, yFormat); 111 } 112 113 /** 114 * Creates an item label generator using the specified formatters (a 115 * number formatter for the x-values and a date formatter for the 116 * y-values). 117 * 118 * @param formatString the item label format string ({@code null} 119 * not permitted). 120 * @param xFormat the format object for the x values ({@code null} 121 * permitted). 122 * @param yFormat the format object for the y values ({@code null} 123 * not permitted). 124 * 125 * @since 1.0.4 126 */ 127 public StandardXYItemLabelGenerator(String formatString, 128 NumberFormat xFormat, DateFormat yFormat) { 129 130 super(formatString, xFormat, yFormat); 131 } 132 133 /** 134 * Creates a label generator using the specified date formatters. 135 * 136 * @param formatString the label format string ({@code null} not 137 * permitted). 138 * @param xFormat the format object for the x values ({@code null} 139 * not permitted). 140 * @param yFormat the format object for the y values ({@code null} 141 * not permitted). 142 */ 143 public StandardXYItemLabelGenerator(String formatString, 144 DateFormat xFormat, DateFormat yFormat) { 145 146 super(formatString, xFormat, yFormat); 147 } 148 149 /** 150 * Generates the item label text for an item in a dataset. 151 * 152 * @param dataset the dataset ({@code null} not permitted). 153 * @param series the series index (zero-based). 154 * @param item the item index (zero-based). 155 * 156 * @return The label text (possibly {@code null}). 157 */ 158 @Override 159 public String generateLabel(XYDataset dataset, int series, int item) { 160 return generateLabelString(dataset, series, item); 161 } 162 163 /** 164 * Returns an independent copy of the generator. 165 * 166 * @return A clone. 167 * 168 * @throws CloneNotSupportedException if cloning is not supported. 169 */ 170 @Override 171 public Object clone() throws CloneNotSupportedException { 172 return super.clone(); 173 } 174 175 /** 176 * Tests this object for equality with an arbitrary object. 177 * 178 * @param obj the other object ({@code null} permitted). 179 * 180 * @return A boolean. 181 */ 182 @Override 183 public boolean equals(Object obj) { 184 if (obj == this) { 185 return true; 186 } 187 if (!(obj instanceof StandardXYItemLabelGenerator)) { 188 return false; 189 } 190 return super.equals(obj); 191 } 192 193}