001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): ______________________________________.
018: *
019: ********************************************************************************/package org.openi.chart;
020:
021: import org.jfree.chart.labels.CategoryItemLabelGenerator; //import org.jfree.chart.labels.CategoryLabelGenerator;
022: import org.jfree.data.category.CategoryDataset;
023: import java.text.NumberFormat;
024:
025: /**
026: * @author plucas
027: *
028: * TODO need to impement new methods of CategoryItemLabelGenerator(JFreeChart 1.0.0)
029: *
030: */
031: public class LabelGenerator implements CategoryItemLabelGenerator {//CategoryLabelGenerator {
032: /**
033: * The index of the category on which to base the percentage
034: * (null = use series total).
035: */
036: private Integer category;
037:
038: /** A percent formatter. */
039: private NumberFormat formatter = NumberFormat.getPercentInstance();
040:
041: /**
042: * Creates a new label generator that displays the item value and a
043: * percentage relative to the value in the same series for the
044: * specified category.
045: *
046: * @param category the category index (zero-based).
047: */
048: public LabelGenerator(final int category) {
049: this (new Integer(category));
050: }
051:
052: /**
053: * Creates a new label generator that displays the item value and
054: * a percentage relative to the value in the same series for the
055: * specified category. If the category index is <code>null</code>,
056: * the total of all items in the series is used.
057: *
058: * @param category the category index (<code>null</code> permitted).
059: */
060: public LabelGenerator(Integer category) {
061: this .category = category;
062: }
063:
064: /**
065: * Generates a label for the specified item. The label is typically
066: * a formatted version of the data value, but any text can be used.
067: *
068: * @param dataset the dataset (<code>null</code> not permitted).
069: * @param series the series index (zero-based).
070: * @param category the category index (zero-based).
071: *
072: * @return the label (possibly <code>null</code>).
073: */
074: public String generateLabel(CategoryDataset dataset, int series,
075: int category) {
076: String result = null;
077: double base = 0.0;
078:
079: if (this .category != null) {
080: final Number b = dataset.getValue(series, this .category
081: .intValue());
082: base = b.doubleValue();
083: } else {
084: base = calculateSeriesTotal(dataset, series);
085: }
086:
087: Number value = dataset.getValue(series, category);
088:
089: if (value != null) {
090: final double v = value.doubleValue();
091: // you could apply some formatting here
092: result = value.toString() + " ("
093: + this .formatter.format(v / base) + ")";
094: }
095:
096: return result;
097: }
098:
099: /**
100: * Calculates a series total.
101: *
102: * @param dataset the dataset.
103: * @param series the series index.
104: *
105: * @return The total.
106: */
107: private double calculateSeriesTotal(CategoryDataset dataset,
108: int series) {
109: double result = 0.0;
110:
111: for (int i = 0; i < dataset.getColumnCount(); i++) {
112: Number value = dataset.getValue(series, i);
113:
114: if (value != null) {
115: result = result + value.doubleValue();
116: }
117: }
118:
119: return result;
120: }
121:
122: public String generateColumnLabel(CategoryDataset arg0, int arg1) {
123: // TODO Auto-generated method stub
124: return null;
125: }
126:
127: public String generateRowLabel(CategoryDataset arg0, int arg1) {
128: // TODO Auto-generated method stub
129: return null;
130: }
131:
132: }
|