Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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: * IntervalCategoryItemLabelGenerator.java 29: * --------------------------------------- 30: * (C) Copyright 2004-2007, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes 36: * ------- 37: * 11-May-2004 : Version 1, split from IntervalCategoryItemLabelGenerator (DG); 38: * 39: */ 40: 41: package org.jfree.chart.labels; 42: 43: import java.io.Serializable; 44: import java.text.DateFormat; 45: import java.text.NumberFormat; 46: 47: import org.jfree.data.category.CategoryDataset; 48: import org.jfree.data.category.IntervalCategoryDataset; 49: import org.jfree.util.PublicCloneable; 50: 51: /** 52: * A label generator for plots that use data from an 53: * {@link IntervalCategoryDataset}. 54: */ 55: public class IntervalCategoryItemLabelGenerator 56: extends StandardCategoryItemLabelGenerator 57: implements CategoryItemLabelGenerator, PublicCloneable, Cloneable, 58: Serializable { 59: 60: /** For serialization. */ 61: private static final long serialVersionUID = 5056909225610630529L; 62: 63: /** The default format string. */ 64: public static final String DEFAULT_LABEL_FORMAT_STRING 65: = "({0}, {1}) = {3} - {4}"; 66: 67: /** 68: * Creates a new generator with a default number formatter. 69: */ 70: public IntervalCategoryItemLabelGenerator() { 71: super(DEFAULT_LABEL_FORMAT_STRING, NumberFormat.getInstance()); 72: } 73: 74: /** 75: * Creates a new generator with the specified number formatter. 76: * 77: * @param labelFormat the label format string (<code>null</code> not 78: * permitted). 79: * @param formatter the number formatter (<code>null</code> not permitted). 80: */ 81: public IntervalCategoryItemLabelGenerator(String labelFormat, 82: NumberFormat formatter) { 83: super(labelFormat, formatter); 84: } 85: 86: /** 87: * Creates a new generator with the specified date formatter. 88: * 89: * @param labelFormat the label format string (<code>null</code> not 90: * permitted). 91: * @param formatter the date formatter (<code>null</code> not permitted). 92: */ 93: public IntervalCategoryItemLabelGenerator(String labelFormat, 94: DateFormat formatter) { 95: super(labelFormat, formatter); 96: } 97: 98: /** 99: * Creates the array of items that can be passed to the 100: * <code>MessageFormat</code> class for creating labels. 101: * 102: * @param dataset the dataset (<code>null</code> not permitted). 103: * @param row the row index (zero-based). 104: * @param column the column index (zero-based). 105: * 106: * @return The items (never <code>null</code>). 107: */ 108: protected Object[] createItemArray(CategoryDataset dataset, 109: int row, int column) { 110: Object[] result = new Object[5]; 111: result[0] = dataset.getRowKey(row).toString(); 112: result[1] = dataset.getColumnKey(column).toString(); 113: Number value = dataset.getValue(row, column); 114: if (getNumberFormat() != null) { 115: result[2] = getNumberFormat().format(value); 116: } 117: else if (getDateFormat() != null) { 118: result[2] = getDateFormat().format(value); 119: } 120: 121: if (dataset instanceof IntervalCategoryDataset) { 122: IntervalCategoryDataset icd = (IntervalCategoryDataset) dataset; 123: Number start = icd.getStartValue(row, column); 124: Number end = icd.getEndValue(row, column); 125: if (getNumberFormat() != null) { 126: result[3] = getNumberFormat().format(start); 127: result[4] = getNumberFormat().format(end); 128: } 129: else if (getDateFormat() != null) { 130: result[3] = getDateFormat().format(start); 131: result[4] = getDateFormat().format(end); 132: } 133: } 134: return result; 135: } 136: 137: }