001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.plugin.jfreereport.reportcharts;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import org.jfree.chart.ChartFactory;
019: import org.jfree.chart.JFreeChart;
020: import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
021: import org.jfree.chart.plot.PiePlot;
022: import org.jfree.chart.plot.Plot;
023: import org.jfree.data.general.Dataset;
024: import org.jfree.data.general.PieDataset;
025: import org.jfree.util.Rotation;
026:
027: public class PieChartExpression extends AbstractChartExpression {
028:
029: private static final long serialVersionUID = 5755617219149952355L;
030:
031: private boolean rotationClockwise = true;
032:
033: private String explodeSegment;
034:
035: private Double explodePct;
036:
037: private boolean ignoreNulls = true;
038:
039: private boolean ignoreZeros = true;
040:
041: private String pieLabelFormat = StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT;
042:
043: private boolean circular = true;
044:
045: private String pieLegendLabelFormat = StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT;
046:
047: protected PieDataset pieDS;
048:
049: private List seriesColors = new ArrayList();
050:
051: public PieChartExpression() {
052: super ();
053: }
054:
055: public boolean isRotationClockwise() {
056: return rotationClockwise;
057: }
058:
059: public void setRotationClockwise(boolean value) {
060: rotationClockwise = value;
061: }
062:
063: public boolean isIgnoreNulls() {
064: return ignoreNulls;
065: }
066:
067: public void setIgnoreNulls(boolean value) {
068: ignoreNulls = value;
069: }
070:
071: public boolean isIgnoreZeros() {
072: return ignoreZeros;
073: }
074:
075: public void setIgnoreZeros(boolean value) {
076: ignoreZeros = value;
077: }
078:
079: public String getExplodeSegment() {
080: return explodeSegment;
081: }
082:
083: public void setExplodeSegment(String value) {
084: explodeSegment = value;
085: }
086:
087: public Double getExplodePct() {
088: return explodePct;
089: }
090:
091: public void setExplodePct(Double value) {
092: explodePct = value;
093: }
094:
095: public String getPieLabelFormat() {
096: return pieLabelFormat;
097: }
098:
099: public void setPieLabelFormat(String value) {
100: pieLabelFormat = value;
101: }
102:
103: public String getPieLegendLabelFormat() {
104: return pieLegendLabelFormat;
105: }
106:
107: public void setPieLegendLabelFormat(String value) {
108: pieLegendLabelFormat = value;
109: }
110:
111: public boolean getIsCircular() {
112: return circular;
113: }
114:
115: public void setCircular(boolean value) {
116: circular = value;
117: }
118:
119: public JFreeChart getChart(Dataset dataset) {
120:
121: PieDataset pieDataset = null;
122: if (dataset instanceof PieDataset) {
123: pieDataset = (PieDataset) dataset;
124: }
125:
126: pieDS = pieDataset;
127: JFreeChart rtn = null;
128: if (isThreeD()) {
129: rtn = ChartFactory.createPieChart3D(getTitle(), pieDataset,
130: isShowLegend(), false, false);
131: } else {
132: rtn = ChartFactory.createPieChart(getTitle(), pieDataset,
133: isShowLegend(), false, false);
134: }
135:
136: return rtn;
137: }
138:
139: protected void setExplode(PiePlot pp) {
140: int explodeType = 0; // 0 = value, 1 = min, 2 = max
141: if ("minValue".equals(explodeSegment)) { //$NON-NLS-1$
142: explodeType = 1;
143: } else if ("maxValue".equals(explodeSegment)) { //$NON-NLS-1$
144: explodeType = 2;
145: }
146: int actualSegment = 0;
147: if (explodeType == 0) {
148: try {
149: actualSegment = Integer.parseInt(explodeSegment);
150: if (actualSegment >= 0) {
151: pp.setExplodePercent(pieDS.getKey(actualSegment),
152: explodePct.doubleValue());
153: }
154: } catch (Exception ignored) {
155: }
156: return;
157: }
158: int minSegment = -1;
159: int maxSegment = -1;
160: Number minNum = new Double(Integer.MAX_VALUE);
161: Number maxNum = new Double(Integer.MIN_VALUE);
162: // Calculate min and max...
163: if (pieDS != null) {
164: int itemCount = pieDS.getItemCount();
165: for (int i = 0; i < itemCount; i++) {
166: Number nbr = pieDS.getValue(i);
167: if (nbr.doubleValue() > maxNum.doubleValue()) {
168: maxNum = nbr;
169: maxSegment = i;
170: }
171: if (nbr.doubleValue() < minNum.doubleValue()) {
172: minNum = nbr;
173: minSegment = i;
174: }
175: }
176: if ("minValue".equals(explodeSegment)) { //$NON-NLS-1$
177: if (minSegment >= 0) {
178: pp.setExplodePercent(pieDS.getKey(minSegment),
179: explodePct.doubleValue());
180: }
181: } else {
182: if (maxSegment >= 0) {
183: pp.setExplodePercent(pieDS.getKey(maxSegment),
184: explodePct.doubleValue());
185: }
186: }
187: }
188:
189: }
190:
191: /*
192: * Getter/ setter methods for slice colors
193: * Ingo Klose 23.10.2007
194: */
195: public String getSeriesColor(final int index) {
196: return (String) seriesColors.get(index);
197: }
198:
199: public int getSeriesColorCount() {
200: return seriesColors.size();
201: }
202:
203: public String[] getSeriesColor() {
204: return (String[]) seriesColors.toArray(new String[seriesColors
205: .size()]);
206: }
207:
208: public void setSeriesColor(final int index, final String field) {
209: if (seriesColors.size() == index) {
210: seriesColors.add(field);
211: } else {
212: seriesColors.set(index, field);
213: }
214: }
215:
216: protected void setChartProperties(JFreeChart chart) {
217: super .setChartProperties(chart);
218: Plot plot = chart.getPlot();
219: PiePlot pp = (PiePlot) plot;
220: pp.setDirection(rotationClockwise ? Rotation.CLOCKWISE
221: : Rotation.ANTICLOCKWISE);
222: if ((explodeSegment != null) && (explodePct != null)) {
223: setExplode(pp);
224: // pp.setExplodePercent(explodeSegment.intValue(),
225: // explodePct.doubleValue());
226: }
227: pp.setIgnoreNullValues(ignoreNulls);
228: pp.setIgnoreZeroValues(ignoreZeros);
229: if (!pieLabelFormat
230: .equals(StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT)) {
231: StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator(
232: pieLabelFormat);
233: pp.setLabelGenerator(labelGen);
234: }
235:
236: if (!pieLegendLabelFormat
237: .equals(StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT)) {
238: StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator(
239: pieLegendLabelFormat);
240: pp.setLegendLabelGenerator(labelGen);
241: }
242:
243: /*
244: * Setting the color of the slices
245: * Ingo Klose 23.10.2007
246: */
247: for (int i = 0; i < seriesColors.size(); i++) {
248: pp.setSectionPaint(pieDS.getKey(i),
249: getColorFromString((String) seriesColors.get(i)));
250: }
251:
252: pp.setCircular(circular);
253: }
254:
255: }
|