KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.pentaho.plugin.jfreereport.reportcharts;
14
15 import org.jfree.chart.ChartFactory;
16 import org.jfree.chart.JFreeChart;
17 import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
18 import org.jfree.chart.plot.PiePlot;
19 import org.jfree.chart.plot.Plot;
20 import org.jfree.data.general.PieDataset;
21 import org.jfree.util.Log;
22 import org.jfree.util.Rotation;
23 import org.pentaho.core.session.IPentahoSession;
24 import org.pentaho.messages.Messages;
25
26 import java.lang.Number JavaDoc;
27
28 public class PieChartExpression extends AbstractChartExpression {
29
30     private static final long serialVersionUID = 5755617219149952355L;
31
32     private boolean rotationClockwise = true;
33
34     private String JavaDoc explodeSegment;
35
36     private Double JavaDoc explodePct;
37
38     private boolean ignoreNulls = true;
39
40     private boolean ignoreZeros = true;
41
42     private String JavaDoc pieLabelFormat = StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT;
43
44     private boolean circular = true;
45
46     private String JavaDoc pieLegendLabelFormat = StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT;
47
48     protected PieDataset pieDS;
49
50   public PieChartExpression() {
51     super();
52   }
53   
54   protected PieChartExpression(IPentahoSession session) {
55         super(session);
56     }
57
58     public boolean isRotationClockwise() {
59         return rotationClockwise;
60     }
61
62     public void setRotationClockwise(boolean value) {
63         rotationClockwise = value;
64     }
65
66     public boolean isIgnoreNulls() {
67         return ignoreNulls;
68     }
69
70     public void setIgnoreNulls(boolean value) {
71         ignoreNulls = value;
72     }
73
74     public boolean isIgnoreZeros() {
75         return ignoreZeros;
76     }
77
78     public void setIgnoreZeros(boolean value) {
79         ignoreZeros = value;
80     }
81
82     public String JavaDoc getExplodeSegment() {
83         return explodeSegment;
84     }
85
86     public void setExplodeSegment(String JavaDoc value) {
87         explodeSegment = value;
88     }
89
90     public Double JavaDoc getExplodePct() {
91         return explodePct;
92     }
93
94     public void setExplodePct(Double JavaDoc value) {
95         explodePct = value;
96     }
97
98     public String JavaDoc getPieLabelFormat() {
99         return pieLabelFormat;
100     }
101
102     public void setPieLabelFormat(String JavaDoc value) {
103         pieLabelFormat = value;
104     }
105
106     public String JavaDoc getPieLegendLabelFormat() {
107         return pieLegendLabelFormat;
108     }
109
110     public void setPieLegendLabelFormat(String JavaDoc value) {
111         pieLegendLabelFormat = value;
112     }
113
114     public boolean getIsCircular() {
115         return circular;
116     }
117
118     public void setCircular(boolean value) {
119         circular = value;
120     }
121
122     public JFreeChart getChart(PieDataset pieDataset) {
123         pieDS = pieDataset;
124         JFreeChart rtn = null;
125         if (isThreeD()) {
126             rtn = ChartFactory.createPieChart3D(getTitle(), pieDataset, isShowLegend(), false, false);
127         } else {
128             rtn = ChartFactory.createPieChart(getTitle(), pieDataset, isShowLegend(), false, false);
129         }
130
131         return rtn;
132     }
133
134     public Object JavaDoc getValue() {
135         final Object JavaDoc maybePieDataset = getDataRow().get(getDataSource());
136         if (maybePieDataset instanceof PieDataset == false) {
137             Log.debug(Messages.getString("CATEGORICALCHARTEXPRESSION.USER_NOT_A_DATASET")); //$NON-NLS-1$
138
return null;
139         }
140
141         PieDataset pieDataset = (PieDataset) maybePieDataset;
142
143         final Integer JavaDoc row = new Integer JavaDoc(getRuntime().getCurrentRow());
144         final Object JavaDoc o = getChartFromCache(row);
145         JFreeChart chart = null;
146         if (o != null) {
147             chart = (JFreeChart) o;
148         } else {
149             chart = getChart(pieDataset);
150             putChartInCache(chart, row);
151         }
152
153         setChartProperties(chart);
154
155         return getValue(chart);
156     }
157
158     protected void setExplode(PiePlot pp) {
159         int explodeType = 0; // 0 = value, 1 = min, 2 = max
160
if ("minValue".equals(explodeSegment)) { //$NON-NLS-1$
161
explodeType = 1;
162         } else if ("maxValue".equals(explodeSegment)) { //$NON-NLS-1$
163
explodeType = 2;
164         }
165         int actualSegment = 0;
166         if (explodeType == 0) {
167             try {
168                 actualSegment = Integer.parseInt(explodeSegment);
169                 pp.setExplodePercent(actualSegment, explodePct.doubleValue());
170             } catch (Exception JavaDoc ignored) {
171             }
172             return;
173         }
174         int minSegment = -1;
175         int maxSegment = -1;
176         Number JavaDoc minNum = new Double JavaDoc(Integer.MAX_VALUE);
177         Number JavaDoc maxNum = new Double JavaDoc(Integer.MIN_VALUE);
178         // Calculate min and max...
179
if (pieDS != null) {
180             int itemCount = pieDS.getItemCount();
181             for (int i = 0; i < itemCount; i++) {
182                 Number JavaDoc nbr = pieDS.getValue(i);
183                 if (nbr.doubleValue() > maxNum.doubleValue()) {
184                     maxNum = nbr;
185                     maxSegment = i;
186                 }
187                 if (nbr.doubleValue() < minNum.doubleValue()) {
188                     minNum = nbr;
189                     minSegment = i;
190                 }
191             }
192             if ("minValue".equals(explodeSegment)) { //$NON-NLS-1$
193
pp.setExplodePercent(minSegment, explodePct.doubleValue());
194             } else {
195                 pp.setExplodePercent(maxSegment, explodePct.doubleValue());
196             }
197         }
198
199     }
200
201     protected void setChartProperties(JFreeChart chart) {
202         super.setChartProperties(chart);
203         Plot plot = chart.getPlot();
204         PiePlot pp = (PiePlot) plot;
205         pp.setDirection(rotationClockwise ? Rotation.CLOCKWISE : Rotation.ANTICLOCKWISE);
206         if ((explodeSegment != null) && (explodePct != null)) {
207             setExplode(pp);
208             // pp.setExplodePercent(explodeSegment.intValue(),
209
// explodePct.doubleValue());
210
}
211         pp.setIgnoreNullValues(ignoreNulls);
212         pp.setIgnoreZeroValues(ignoreZeros);
213         if (!pieLabelFormat.equals(StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT)) {
214             StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator(pieLabelFormat);
215             pp.setLabelGenerator(labelGen);
216         }
217
218         if (!pieLegendLabelFormat.equals(StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT)) {
219             StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator(pieLegendLabelFormat);
220             pp.setLegendLabelGenerator(labelGen);
221         }
222
223         pp.setCircular(circular);
224     }
225
226 }
227
Popular Tags