KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > chart > LabelGenerator


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.chart;
21
22 import org.jfree.chart.labels.CategoryLabelGenerator;
23 import org.jfree.data.category.CategoryDataset;
24 import java.text.NumberFormat JavaDoc;
25
26
27 /**
28  * @author plucas
29  *
30  * TODO To change the template for this generated type comment go to
31  * Window - Preferences - Java - Code Style - Code Templates
32  */

33 public class LabelGenerator implements CategoryLabelGenerator {
34     /**
35      * The index of the category on which to base the percentage
36      * (null = use series total).
37      */

38     private Integer JavaDoc category;
39
40     /** A percent formatter. */
41     private NumberFormat JavaDoc formatter = NumberFormat.getPercentInstance();
42
43     /**
44      * Creates a new label generator that displays the item value and a
45      * percentage relative to the value in the same series for the
46      * specified category.
47      *
48      * @param category the category index (zero-based).
49      */

50     public LabelGenerator(final int category) {
51         this(new Integer JavaDoc(category));
52     }
53
54     /**
55      * Creates a new label generator that displays the item value and
56      * a percentage relative to the value in the same series for the
57      * specified category. If the category index is <code>null</code>,
58      * the total of all items in the series is used.
59      *
60      * @param category the category index (<code>null</code> permitted).
61      */

62     public LabelGenerator(Integer JavaDoc category) {
63         this.category = category;
64     }
65
66     /**
67      * Generates a label for the specified item. The label is typically
68      * a formatted version of the data value, but any text can be used.
69      *
70      * @param dataset the dataset (<code>null</code> not permitted).
71      * @param series the series index (zero-based).
72      * @param category the category index (zero-based).
73      *
74      * @return the label (possibly <code>null</code>).
75      */

76     public String JavaDoc generateLabel(CategoryDataset dataset, int series,
77         int category) {
78         String JavaDoc result = null;
79         double base = 0.0;
80
81         if (this.category != null) {
82             final Number JavaDoc b = dataset.getValue(series, this.category.intValue());
83             base = b.doubleValue();
84         } else {
85             base = calculateSeriesTotal(dataset, series);
86         }
87
88         Number JavaDoc value = dataset.getValue(series, category);
89
90         if (value != null) {
91             final double v = value.doubleValue();
92             // you could apply some formatting here
93
result = value.toString() + " (" + this.formatter.format(v / base)
94                 + ")";
95         }
96
97         return result;
98     }
99
100     /**
101      * Calculates a series total.
102      *
103      * @param dataset the dataset.
104      * @param series the series index.
105      *
106      * @return The total.
107      */

108     private double calculateSeriesTotal(CategoryDataset dataset, int series) {
109         double result = 0.0;
110
111         for (int i = 0; i < dataset.getColumnCount(); i++) {
112             Number JavaDoc value = dataset.getValue(series, i);
113
114             if (value != null) {
115                 result = result + value.doubleValue();
116             }
117         }
118
119         return result;
120     }
121 }
122
Popular Tags