| 1 13 package org.pentaho.plugin.jfreereport.reportcharts; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 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 multipieLabelFormat = StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT; 37 38 private List seriesColors = new ArrayList (); 39 40 protected MultiPieChartExpression() { 41 super(); 42 } 43 44 protected MultiPieChartExpression(IPentahoSession session) { 45 super(session); 46 } 48 49 public String getMultipieLabelFormat() { 50 return multipieLabelFormat; 51 } 52 53 public void setMultipieLabelFormat(String 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 color) { 66 if (seriesColors.size() == index) { 67 seriesColors.add(color); 68 } else { 69 seriesColors.set(index, color); 70 } 71 } 72 73 public List 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 getValue() { 88 final Object maybeCategoryDataset = getDataRow().get(getDataSource()); 89 if (maybeCategoryDataset instanceof CategoryDataset == false) { 90 Log.debug(Messages.getString("CATEGORICALCHARTEXPRESSION.USER_NOT_A_DATASET")); return null; 92 } 93 94 CategoryDataset categoryDataset = (CategoryDataset) maybeCategoryDataset; 95 96 final Integer row = new Integer (getRuntime().getCurrentRow()); 97 final Object 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 ) 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 |