Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ----------------------------------- 28: * MultipleXYSeriesLabelGenerator.java 29: * ----------------------------------- 30: * (C) Copyright 2004, 2005, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * $Id: MultipleXYSeriesLabelGenerator.java,v 1.5.2.1 2005/10/25 20:49:02 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 19-Nov-2004 : Version 1 (DG); 40: * 18-Apr-2005 : Use StringBuffer (DG); 41: * 42: */ 43: 44: package org.jfree.chart.labels; 45: 46: import java.io.Serializable; 47: import java.text.MessageFormat; 48: import java.util.HashMap; 49: import java.util.List; 50: import java.util.Map; 51: 52: import org.jfree.data.xy.XYDataset; 53: import org.jfree.util.PublicCloneable; 54: 55: /** 56: * A series label generator for plots that use data from 57: * an {@link org.jfree.data.xy.XYDataset}. 58: */ 59: public class MultipleXYSeriesLabelGenerator implements XYSeriesLabelGenerator, 60: Cloneable, 61: PublicCloneable, 62: Serializable { 63: 64: /** For serialization. */ 65: private static final long serialVersionUID = 138976236941898560L; 66: 67: /** The default item label format. */ 68: public static final String DEFAULT_LABEL_FORMAT = "{0}"; 69: 70: /** The format pattern for the initial part of the label. */ 71: private String formatPattern; 72: 73: /** The format pattern for additional labels. */ 74: private String additionalFormatPattern; 75: 76: /** Storage for the additional series labels. */ 77: private Map seriesLabelLists; 78: 79: /** 80: * Creates an item label generator using default number formatters. 81: */ 82: public MultipleXYSeriesLabelGenerator() { 83: this(DEFAULT_LABEL_FORMAT); 84: } 85: 86: /** 87: * Creates a new series label generator. 88: * 89: * @param format the format pattern (<code>null</code> not permitted). 90: */ 91: public MultipleXYSeriesLabelGenerator(String format) { 92: if (format == null) { 93: throw new IllegalArgumentException("Null 'format' argument."); 94: } 95: this.formatPattern = format; 96: this.additionalFormatPattern = "\n{0}"; 97: this.seriesLabelLists = new HashMap(); 98: } 99: 100: /** 101: * Adds an extra label for the specified series. 102: * 103: * @param series the series index. 104: * @param label the label. 105: */ 106: public void addSeriesLabel(int series, String label) { 107: Integer key = new Integer(series); 108: List labelList = (List) this.seriesLabelLists.get(key); 109: if (labelList == null) { 110: labelList = new java.util.ArrayList(); 111: this.seriesLabelLists.put(key, labelList); 112: } 113: labelList.add(label); 114: } 115: 116: /** 117: * Clears the extra labels for the specified series. 118: * 119: * @param series the series index. 120: */ 121: public void clearSeriesLabels(int series) { 122: Integer key = new Integer(series); 123: this.seriesLabelLists.put(key, null); 124: } 125: 126: /** 127: * Generates a label for the specified series. This label will be 128: * used for the chart legend. 129: * 130: * @param dataset the dataset (<code>null</code> not permitted). 131: * @param series the series. 132: * 133: * @return A series label. 134: */ 135: public String generateLabel(XYDataset dataset, int series) { 136: if (dataset == null) { 137: throw new IllegalArgumentException("Null 'dataset' argument."); 138: } 139: StringBuffer label = new StringBuffer(); 140: label.append( 141: MessageFormat.format( 142: this.formatPattern, createItemArray(dataset, series) 143: ) 144: ); 145: Integer key = new Integer(series); 146: List extraLabels = (List) this.seriesLabelLists.get(key); 147: if (extraLabels != null) { 148: Object[] temp = new Object[1]; 149: for (int i = 0; i < extraLabels.size(); i++) { 150: temp[0] = extraLabels.get(i); 151: String labelAddition = MessageFormat.format( 152: this.additionalFormatPattern, temp 153: ); 154: label.append(labelAddition); 155: } 156: } 157: return label.toString(); 158: } 159: 160: /** 161: * Creates the array of items that can be passed to the 162: * {@link MessageFormat} class for creating labels. 163: * 164: * @param dataset the dataset (<code>null</code> not permitted). 165: * @param series the series (zero-based index). 166: * 167: * @return The items (never <code>null</code>). 168: */ 169: protected Object[] createItemArray(XYDataset dataset, int series) { 170: Object[] result = new Object[1]; 171: result[0] = dataset.getSeriesKey(series).toString(); 172: return result; 173: } 174: 175: /** 176: * Returns an independent copy of the generator. 177: * 178: * @return A clone. 179: * 180: * @throws CloneNotSupportedException if cloning is not supported. 181: */ 182: public Object clone() throws CloneNotSupportedException { 183: return super.clone(); 184: } 185: 186: /** 187: * Tests this object for equality with an arbitrary object. 188: * 189: * @param obj the other object (<code>null</code> permitted). 190: * 191: * @return A boolean. 192: */ 193: public boolean equals(Object obj) { 194: if (obj == this) { 195: return true; 196: } 197: if (!(obj instanceof StandardXYSeriesLabelGenerator)) { 198: return false; 199: } 200: if (!super.equals(obj)) { 201: return false; 202: } 203: return true; 204: } 205: 206: }