KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreereport > reportcharts > CategoricalChartExpression


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Oct 31, 2005
14  * @author Thomas Morgner / Marc Batchelor
15  */

16 package org.pentaho.plugin.jfreereport.reportcharts;
17
18 import java.awt.Font JavaDoc;
19 import java.text.DecimalFormat JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.jfree.chart.JFreeChart;
25 import org.jfree.chart.axis.CategoryAxis;
26 import org.jfree.chart.axis.CategoryLabelPositions;
27 import org.jfree.chart.axis.ValueAxis;
28 import org.jfree.chart.labels.ItemLabelPosition;
29 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
30 import org.jfree.chart.plot.CategoryPlot;
31 import org.jfree.chart.renderer.category.AbstractCategoryItemRenderer;
32 import org.jfree.chart.renderer.category.CategoryItemRenderer;
33 import org.jfree.data.category.CategoryDataset;
34 import org.jfree.util.Log;
35 import org.pentaho.core.session.IPentahoSession;
36 import org.pentaho.messages.Messages;
37
38 /**
39  * This class allows you to embed categorical charts into JFreeReport XML definitions.
40  *
41  * @author mbatchel
42  *
43  */

44 public abstract class CategoricalChartExpression extends AbstractChartExpression {
45     private static final long serialVersionUID = -402500824047401239L;
46
47     private String JavaDoc valueAxisLabel;
48
49     private String JavaDoc categoryAxisLabel;
50
51     private boolean horizontal;
52
53     private boolean showGridLines = true;
54
55     private Double JavaDoc labelRotation;
56
57     private List JavaDoc seriesColors = new ArrayList JavaDoc();
58
59     private Float JavaDoc maxCategoryLabelWidthRatio;
60
61     private String JavaDoc categoricalLabelFormat;
62
63     private String JavaDoc categoricalLabelDecimalFormat;
64
65     private String JavaDoc categoricalLabelDateFormat;
66
67     private Double JavaDoc categoricalItemLabelRotation;
68
69   public CategoricalChartExpression() {
70     super();
71   }
72   
73     public CategoricalChartExpression(IPentahoSession session) {
74         super(session);
75     }
76
77     // private Map chartProperties = new HashMap();
78
/*
79      * TODO: Use reflection to set various properties. But, the chart isn't really bean-able.
80      *
81      * public void setChartProperty(int ignored, String data) { String propName, propValue; int idx = data.indexOf("="); if (idx>0) { propName = data.substring(0, idx); propValue = data.substring(idx+1); chartProperties.put(propName,
82      * propValue); } }
83      */

84
85     public void setSeriesColor(int index, String JavaDoc color) {
86         if (seriesColors.size() == index) {
87             seriesColors.add(color);
88         } else {
89             seriesColors.set(index, color);
90         }
91     }
92
93     public List JavaDoc getSeriesColors() {
94         return seriesColors;
95     }
96
97     public Double JavaDoc getLabelRotation() {
98         return labelRotation;
99     }
100
101     public void setLabelRotation(Double JavaDoc value) {
102         labelRotation = value;
103     }
104
105     public Double JavaDoc getCategoricalItemLabelRotation() {
106         return this.categoricalItemLabelRotation;
107     }
108
109     public void setCategoricalItemLabelRotation(Double JavaDoc value) {
110         this.categoricalItemLabelRotation = value;
111     }
112
113     public void setMaxCategoryLabelWidthRatio(Float JavaDoc value) {
114         maxCategoryLabelWidthRatio = value;
115     }
116
117     public Float JavaDoc getMaxCategoryLabelWidthRatio() {
118         return maxCategoryLabelWidthRatio;
119     }
120
121     public boolean isShowGridlines() {
122         return showGridLines;
123     }
124
125     public void setShowGridlines(boolean value) {
126         showGridLines = value;
127     }
128
129     public boolean isHorizontal() {
130         return horizontal;
131     }
132
133     public void setHorizontal(boolean value) {
134         horizontal = value;
135     }
136
137     public String JavaDoc getValueAxisLabel() {
138         return valueAxisLabel;
139     }
140
141     public void setValueAxisLabel(final String JavaDoc valueAxisLabel) {
142         this.valueAxisLabel = valueAxisLabel;
143     }
144
145     public String JavaDoc getCategoryAxisLabel() {
146         return categoryAxisLabel;
147     }
148
149     public void setCategoryAxisLabel(final String JavaDoc categoryAxisLabel) {
150         this.categoryAxisLabel = categoryAxisLabel;
151     }
152
153     public void setCategoricalLabelFormat(final String JavaDoc value) {
154         this.categoricalLabelFormat = value;
155     }
156
157     public String JavaDoc getCategoricalLabelFormat() {
158         return this.categoricalLabelFormat;
159     }
160
161     public void setCategoricalLabelDecimalFormat(final String JavaDoc value) {
162         this.categoricalLabelDecimalFormat = value;
163     }
164
165     public String JavaDoc getCategoricalLabelDecimalFormat() {
166         return this.categoricalLabelDecimalFormat;
167     }
168
169     public void setCategoricalLabelDateFormat(final String JavaDoc value) {
170         this.categoricalLabelDateFormat = value;
171     }
172
173     public String JavaDoc getCategoricalLabelDateFormat() {
174         return this.categoricalLabelDateFormat;
175     }
176
177     public abstract JFreeChart getChart(CategoryDataset categoryDataset);
178
179     public Object JavaDoc getValue() {
180         final Object JavaDoc maybeCategoryDataset = getDataRow().get(getDataSource());
181         if (maybeCategoryDataset instanceof CategoryDataset == false) {
182             Log.debug(Messages.getString("CATEGORICALCHARTEXPRESSION.USER_NOT_A_DATASET")); //$NON-NLS-1$
183
return null;
184         }
185
186         CategoryDataset categoryDataset = (CategoryDataset) maybeCategoryDataset;
187         final Integer JavaDoc row = new Integer JavaDoc(getRuntime().getCurrentRow());
188         final Object JavaDoc o = getChartFromCache(row);
189         JFreeChart chart = null;
190         if (o != null) {
191             chart = (JFreeChart) o;
192         } else {
193             chart = getChart(categoryDataset);
194             putChartInCache(chart, row);
195         }
196
197         setChartProperties(chart);
198         return getValue(chart);
199
200     }
201
202     protected void setChartProperties(JFreeChart chart) {
203         super.setChartProperties(chart);
204
205         CategoryPlot cpl = chart.getCategoryPlot();
206         CategoryAxis axis = cpl.getDomainAxis();
207         CategoryItemRenderer renderer = cpl.getRenderer();
208
209         if (this.categoricalLabelFormat != null) {
210             StandardCategoryItemLabelGenerator scilg = null;
211             if (categoricalLabelDecimalFormat != null) {
212                 scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, new DecimalFormat JavaDoc(categoricalLabelDecimalFormat));
213             } else if (categoricalLabelDateFormat != null) {
214                 scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, new SimpleDateFormat JavaDoc(categoricalLabelDateFormat));
215             } else {
216                 scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, new DecimalFormat JavaDoc());
217             }
218             renderer.setItemLabelGenerator(scilg);
219             renderer.setItemLabelsVisible(true);
220             if (categoricalItemLabelRotation != null) {
221                 ItemLabelPosition posn = renderer.getPositiveItemLabelPosition();
222                 ItemLabelPosition pos2 = new ItemLabelPosition(posn.getItemLabelAnchor(), posn.getTextAnchor(), posn.getRotationAnchor(), categoricalItemLabelRotation.doubleValue());
223                 renderer.setPositiveItemLabelPosition(pos2);
224             }
225
226         }
227
228         Font JavaDoc lFont = Font.decode(getLabelFont());
229         axis.setLabelFont(lFont);
230         ValueAxis rAxis = cpl.getRangeAxis();
231         if (rAxis != null) {
232             rAxis.setLabelFont(lFont);
233         }
234         axis.setTickLabelFont(lFont);
235         if (maxCategoryLabelWidthRatio != null) {
236             axis.setMaximumCategoryLabelWidthRatio(maxCategoryLabelWidthRatio.floatValue());
237         }
238         cpl.setDomainGridlinesVisible(showGridLines);
239         if (labelRotation != null) {
240             axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / labelRotation.doubleValue()));
241         }
242         AbstractCategoryItemRenderer acir = (AbstractCategoryItemRenderer) renderer;
243
244         for (int i = 0; i < seriesColors.size(); i++) {
245             acir.setSeriesPaint(i, getColorFromString((String JavaDoc) seriesColors.get(i)));
246         }
247
248     }
249
250 }
251
Popular Tags