Source for org.jfree.chart.labels.IntervalCategoryItemLabelGenerator

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