1 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 ; 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 explodeSegment; 35 36 private Double explodePct; 37 38 private boolean ignoreNulls = true; 39 40 private boolean ignoreZeros = true; 41 42 private String pieLabelFormat = StandardPieSectionLabelGenerator.DEFAULT_SECTION_LABEL_FORMAT; 43 44 private boolean circular = true; 45 46 private String 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 getExplodeSegment() { 83 return explodeSegment; 84 } 85 86 public void setExplodeSegment(String value) { 87 explodeSegment = value; 88 } 89 90 public Double getExplodePct() { 91 return explodePct; 92 } 93 94 public void setExplodePct(Double value) { 95 explodePct = value; 96 } 97 98 public String getPieLabelFormat() { 99 return pieLabelFormat; 100 } 101 102 public void setPieLabelFormat(String value) { 103 pieLabelFormat = value; 104 } 105 106 public String getPieLegendLabelFormat() { 107 return pieLegendLabelFormat; 108 } 109 110 public void setPieLegendLabelFormat(String 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 getValue() { 135 final Object maybePieDataset = getDataRow().get(getDataSource()); 136 if (maybePieDataset instanceof PieDataset == false) { 137 Log.debug(Messages.getString("CATEGORICALCHARTEXPRESSION.USER_NOT_A_DATASET")); return null; 139 } 140 141 PieDataset pieDataset = (PieDataset) maybePieDataset; 142 143 final Integer row = new Integer (getRuntime().getCurrentRow()); 144 final Object 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; if ("minValue".equals(explodeSegment)) { explodeType = 1; 162 } else if ("maxValue".equals(explodeSegment)) { 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 ignored) { 171 } 172 return; 173 } 174 int minSegment = -1; 175 int maxSegment = -1; 176 Number minNum = new Double (Integer.MAX_VALUE); 177 Number maxNum = new Double (Integer.MIN_VALUE); 178 if (pieDS != null) { 180 int itemCount = pieDS.getItemCount(); 181 for (int i = 0; i < itemCount; i++) { 182 Number 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)) { 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 } 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 |