KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.jfree.chart.ChartFactory;
19 import org.jfree.chart.JFreeChart;
20 import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
21 import org.jfree.chart.plot.MultiplePiePlot;
22 import org.jfree.chart.plot.PiePlot;
23 import org.jfree.chart.plot.Plot;
24 import org.jfree.data.category.CategoryDataset;
25 import org.jfree.util.Log;
26 import org.jfree.util.TableOrder;
27 import org.pentaho.core.session.IPentahoSession;
28 import org.pentaho.messages.Messages;
29
30 public class MultiPieChartExpression extends AbstractChartExpression {
31
32     private static final long serialVersionUID = -7796999107015376070L;
33
34     private boolean multipieByRow = true;
35
36     private String JavaDoc multipieLabelFormat = StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT;
37
38     private List JavaDoc seriesColors = new ArrayList JavaDoc();
39
40   protected MultiPieChartExpression() {
41     super();
42   }
43     
44   protected MultiPieChartExpression(IPentahoSession session) {
45         super(session);
46         // TODO Auto-generated constructor stub
47
}
48
49     public String JavaDoc getMultipieLabelFormat() {
50         return multipieLabelFormat;
51     }
52
53     public void setMultipieLabelFormat(String JavaDoc value) {
54         multipieLabelFormat = value;
55     }
56
57     public boolean isMultipieByRow() {
58         return multipieByRow;
59     }
60
61     public void setMultipieByRow(boolean value) {
62         multipieByRow = value;
63     }
64
65     public void setSeriesColor(int index, String JavaDoc color) {
66         if (seriesColors.size() == index) {
67             seriesColors.add(color);
68         } else {
69             seriesColors.set(index, color);
70         }
71     }
72
73     public List JavaDoc getSeriesColors() {
74         return seriesColors;
75     }
76
77     public JFreeChart getChart(CategoryDataset categoryDataset) {
78         JFreeChart rtn = null;
79         if (isThreeD()) {
80             rtn = ChartFactory.createMultiplePieChart3D(getTitle(), categoryDataset, isMultipieByRow() ? TableOrder.BY_ROW : TableOrder.BY_COLUMN, isShowLegend(), false, false);
81         } else {
82             rtn = ChartFactory.createMultiplePieChart(getTitle(), categoryDataset, isMultipieByRow() ? TableOrder.BY_ROW : TableOrder.BY_COLUMN, isShowLegend(), false, false);
83         }
84         return rtn;
85     }
86
87     public Object JavaDoc getValue() {
88         final Object JavaDoc maybeCategoryDataset = getDataRow().get(getDataSource());
89         if (maybeCategoryDataset instanceof CategoryDataset == false) {
90             Log.debug(Messages.getString("CATEGORICALCHARTEXPRESSION.USER_NOT_A_DATASET")); //$NON-NLS-1$
91
return null;
92         }
93
94         CategoryDataset categoryDataset = (CategoryDataset) maybeCategoryDataset;
95
96         final Integer JavaDoc row = new Integer JavaDoc(getRuntime().getCurrentRow());
97         final Object JavaDoc o = getChartFromCache(row);
98         JFreeChart chart = null;
99         if (o != null) {
100             chart = (JFreeChart) o;
101         } else {
102             chart = getChart(categoryDataset);
103             putChartInCache(chart, row);
104         }
105
106         setChartProperties(chart);
107
108         return getValue(chart);
109     }
110
111     protected void setChartProperties(JFreeChart chart) {
112         super.setChartProperties(chart);
113
114         Plot plot = chart.getPlot();
115         MultiplePiePlot mpp = (MultiplePiePlot) plot;
116         JFreeChart pc = mpp.getPieChart();
117         PiePlot pp = (PiePlot) pc.getPlot();
118         for (int i = 0; i < getSeriesColors().size(); i++) {
119             pp.setSectionPaint(i, getColorFromString((String JavaDoc) getSeriesColors().get(i)));
120         }
121
122         if (!multipieLabelFormat.equals(StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT)) {
123             StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator(multipieLabelFormat);
124             pp.setLabelGenerator(labelGen);
125         }
126     }
127 }
128
Popular Tags