KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------------------------
27  * IntervalCategoryItemLabelGenerator.java
28  * ---------------------------------------
29  * (C) Copyright 2004, 2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: IntervalCategoryItemLabelGenerator.java,v 1.2 2005/05/19 15:43:00 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 11-May-2004 : Version 1, split from IntervalCategoryItemLabelGenerator (DG);
39  *
40  */

41
42 package org.jfree.chart.labels;
43
44 import java.io.Serializable JavaDoc;
45 import java.text.DateFormat JavaDoc;
46 import java.text.NumberFormat JavaDoc;
47
48 import org.jfree.data.category.CategoryDataset;
49 import org.jfree.data.category.IntervalCategoryDataset;
50 import org.jfree.util.PublicCloneable;
51
52 /**
53  * A label generator for plots that use data from an
54  * {@link IntervalCategoryDataset}.
55  */

56 public class IntervalCategoryItemLabelGenerator
57     extends StandardCategoryItemLabelGenerator
58     implements CategoryItemLabelGenerator, PublicCloneable, Cloneable JavaDoc,
59                Serializable JavaDoc {
60
61     /** For serialization. */
62     private static final long serialVersionUID = 5056909225610630529L;
63     
64     /** The default format string. */
65     public static final String JavaDoc DEFAULT_LABEL_FORMAT_STRING
66         = "({0}, {1}) = {3} - {4}";
67     
68     /**
69      * Creates a new generator with a default number formatter.
70      */

71     public IntervalCategoryItemLabelGenerator() {
72         super(DEFAULT_LABEL_FORMAT_STRING, NumberFormat.getInstance());
73     }
74
75     /**
76      * Creates a new generator with the specified number formatter.
77      *
78      * @param labelFormat the label format string (<code>null</code> not
79      * permitted).
80      * @param formatter the number formatter (<code>null</code> not permitted).
81      */

82     public IntervalCategoryItemLabelGenerator(String JavaDoc labelFormat,
83                                               NumberFormat JavaDoc formatter) {
84         super(labelFormat, formatter);
85     }
86     
87     /**
88      * Creates a new generator with the specified date formatter.
89      *
90      * @param labelFormat the label format string (<code>null</code> not
91      * permitted).
92      * @param formatter the date formatter (<code>null</code> not permitted).
93      */

94     public IntervalCategoryItemLabelGenerator(String JavaDoc labelFormat,
95                                               DateFormat JavaDoc formatter) {
96         super(labelFormat, formatter);
97     }
98     
99     /**
100      * Creates the array of items that can be passed to the
101      * <code>MessageFormat</code> class for creating labels.
102      *
103      * @param dataset the dataset (<code>null</code> not permitted).
104      * @param row the row index (zero-based).
105      * @param column the column index (zero-based).
106      *
107      * @return The items (never <code>null</code>).
108      */

109     protected Object JavaDoc[] createItemArray(CategoryDataset dataset,
110                                        int row, int column) {
111         Object JavaDoc[] result = new Object JavaDoc[5];
112         result[0] = dataset.getRowKey(row).toString();
113         result[1] = dataset.getColumnKey(column).toString();
114         Number JavaDoc value = dataset.getValue(row, column);
115         if (getNumberFormat() != null) {
116             result[2] = getNumberFormat().format(value);
117         }
118         else if (getDateFormat() != null) {
119             result[2] = getDateFormat().format(value);
120         }
121         
122         if (dataset instanceof IntervalCategoryDataset) {
123             IntervalCategoryDataset icd = (IntervalCategoryDataset) dataset;
124             Number JavaDoc start = icd.getStartValue(row, column);
125             Number JavaDoc end = icd.getEndValue(row, column);
126             if (getNumberFormat() != null) {
127                 result[3] = getNumberFormat().format(start);
128                 result[4] = getNumberFormat().format(end);
129             }
130             else if (getDateFormat() != null) {
131                 result[3] = getDateFormat().format(start);
132                 result[4] = getDateFormat().format(end);
133             }
134         }
135         return result;
136     }
137
138 }
139
Popular Tags