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.MultiplePiePlot;
022: import org.jfree.chart.plot.PiePlot;
023: import org.jfree.chart.plot.Plot;
024: import org.jfree.data.category.CategoryDataset;
025: import org.jfree.data.general.Dataset;
026: import org.jfree.util.TableOrder;
027:
028: public class MultiPieChartExpression extends AbstractChartExpression {
029:
030: private static final long serialVersionUID = -7796999107015376070L;
031:
032: private boolean multipieByRow = true;
033:
034: private String multipieLabelFormat = StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT;
035:
036: private List seriesColors = new ArrayList();
037:
038: public MultiPieChartExpression() {
039: super ();
040: }
041:
042: public String getMultipieLabelFormat() {
043: return multipieLabelFormat;
044: }
045:
046: public void setMultipieLabelFormat(String value) {
047: multipieLabelFormat = value;
048: }
049:
050: public boolean isMultipieByRow() {
051: return multipieByRow;
052: }
053:
054: public void setMultipieByRow(boolean value) {
055: multipieByRow = value;
056: }
057:
058: public void setSeriesColor(int index, String color) {
059: if (seriesColors.size() == index) {
060: seriesColors.add(color);
061: } else {
062: seriesColors.set(index, color);
063: }
064: }
065:
066: public List getSeriesColors() {
067: return seriesColors;
068: }
069:
070: public JFreeChart getChart(Dataset dataset) {
071:
072: CategoryDataset categoryDataset = null;
073: if (dataset instanceof CategoryDataset) {
074: categoryDataset = (CategoryDataset) dataset;
075: }
076:
077: JFreeChart rtn = null;
078: if (isThreeD()) {
079: rtn = ChartFactory.createMultiplePieChart3D(getTitle(),
080: categoryDataset,
081: isMultipieByRow() ? TableOrder.BY_ROW
082: : TableOrder.BY_COLUMN, isShowLegend(),
083: false, false);
084: } else {
085: rtn = ChartFactory.createMultiplePieChart(getTitle(),
086: categoryDataset,
087: isMultipieByRow() ? TableOrder.BY_ROW
088: : TableOrder.BY_COLUMN, isShowLegend(),
089: false, false);
090: }
091: return rtn;
092: }
093:
094: protected void setChartProperties(JFreeChart chart) {
095: super .setChartProperties(chart);
096:
097: Plot plot = chart.getPlot();
098: MultiplePiePlot mpp = (MultiplePiePlot) plot;
099: JFreeChart pc = mpp.getPieChart();
100: PiePlot pp = (PiePlot) pc.getPlot();
101:
102: CategoryDataset c = mpp.getDataset();
103: if (c != null) {
104: for (int i = 0; i < getSeriesColors().size(); i++) {
105: if (c.getColumnKeys().size() > i) {
106: pp
107: .setSectionPaint(
108: c.getColumnKey(i),
109: getColorFromString((String) getSeriesColors()
110: .get(i)));
111: }
112: }
113: }
114:
115: if (!multipieLabelFormat
116: .equals(StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT)) {
117: StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator(
118: multipieLabelFormat);
119: pp.setLabelGenerator(labelGen);
120: }
121: }
122: }
|