| 1 16 package org.pentaho.plugin.jfreereport.reportcharts; 17 18 import java.awt.Font ; 19 import java.text.DecimalFormat ; 20 import java.text.SimpleDateFormat ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import org.jfree.chart.JFreeChart; 25 import org.jfree.chart.axis.CategoryAxis; 26 import org.jfree.chart.axis.CategoryLabelPositions; 27 import org.jfree.chart.axis.ValueAxis; 28 import org.jfree.chart.labels.ItemLabelPosition; 29 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 30 import org.jfree.chart.plot.CategoryPlot; 31 import org.jfree.chart.renderer.category.AbstractCategoryItemRenderer; 32 import org.jfree.chart.renderer.category.CategoryItemRenderer; 33 import org.jfree.data.category.CategoryDataset; 34 import org.jfree.util.Log; 35 import org.pentaho.core.session.IPentahoSession; 36 import org.pentaho.messages.Messages; 37 38 44 public abstract class CategoricalChartExpression extends AbstractChartExpression { 45 private static final long serialVersionUID = -402500824047401239L; 46 47 private String valueAxisLabel; 48 49 private String categoryAxisLabel; 50 51 private boolean horizontal; 52 53 private boolean showGridLines = true; 54 55 private Double labelRotation; 56 57 private List seriesColors = new ArrayList (); 58 59 private Float maxCategoryLabelWidthRatio; 60 61 private String categoricalLabelFormat; 62 63 private String categoricalLabelDecimalFormat; 64 65 private String categoricalLabelDateFormat; 66 67 private Double categoricalItemLabelRotation; 68 69 public CategoricalChartExpression() { 70 super(); 71 } 72 73 public CategoricalChartExpression(IPentahoSession session) { 74 super(session); 75 } 76 77 84 85 public void setSeriesColor(int index, String color) { 86 if (seriesColors.size() == index) { 87 seriesColors.add(color); 88 } else { 89 seriesColors.set(index, color); 90 } 91 } 92 93 public List getSeriesColors() { 94 return seriesColors; 95 } 96 97 public Double getLabelRotation() { 98 return labelRotation; 99 } 100 101 public void setLabelRotation(Double value) { 102 labelRotation = value; 103 } 104 105 public Double getCategoricalItemLabelRotation() { 106 return this.categoricalItemLabelRotation; 107 } 108 109 public void setCategoricalItemLabelRotation(Double value) { 110 this.categoricalItemLabelRotation = value; 111 } 112 113 public void setMaxCategoryLabelWidthRatio(Float value) { 114 maxCategoryLabelWidthRatio = value; 115 } 116 117 public Float getMaxCategoryLabelWidthRatio() { 118 return maxCategoryLabelWidthRatio; 119 } 120 121 public boolean isShowGridlines() { 122 return showGridLines; 123 } 124 125 public void setShowGridlines(boolean value) { 126 showGridLines = value; 127 } 128 129 public boolean isHorizontal() { 130 return horizontal; 131 } 132 133 public void setHorizontal(boolean value) { 134 horizontal = value; 135 } 136 137 public String getValueAxisLabel() { 138 return valueAxisLabel; 139 } 140 141 public void setValueAxisLabel(final String valueAxisLabel) { 142 this.valueAxisLabel = valueAxisLabel; 143 } 144 145 public String getCategoryAxisLabel() { 146 return categoryAxisLabel; 147 } 148 149 public void setCategoryAxisLabel(final String categoryAxisLabel) { 150 this.categoryAxisLabel = categoryAxisLabel; 151 } 152 153 public void setCategoricalLabelFormat(final String value) { 154 this.categoricalLabelFormat = value; 155 } 156 157 public String getCategoricalLabelFormat() { 158 return this.categoricalLabelFormat; 159 } 160 161 public void setCategoricalLabelDecimalFormat(final String value) { 162 this.categoricalLabelDecimalFormat = value; 163 } 164 165 public String getCategoricalLabelDecimalFormat() { 166 return this.categoricalLabelDecimalFormat; 167 } 168 169 public void setCategoricalLabelDateFormat(final String value) { 170 this.categoricalLabelDateFormat = value; 171 } 172 173 public String getCategoricalLabelDateFormat() { 174 return this.categoricalLabelDateFormat; 175 } 176 177 public abstract JFreeChart getChart(CategoryDataset categoryDataset); 178 179 public Object getValue() { 180 final Object maybeCategoryDataset = getDataRow().get(getDataSource()); 181 if (maybeCategoryDataset instanceof CategoryDataset == false) { 182 Log.debug(Messages.getString("CATEGORICALCHARTEXPRESSION.USER_NOT_A_DATASET")); return null; 184 } 185 186 CategoryDataset categoryDataset = (CategoryDataset) maybeCategoryDataset; 187 final Integer row = new Integer (getRuntime().getCurrentRow()); 188 final Object o = getChartFromCache(row); 189 JFreeChart chart = null; 190 if (o != null) { 191 chart = (JFreeChart) o; 192 } else { 193 chart = getChart(categoryDataset); 194 putChartInCache(chart, row); 195 } 196 197 setChartProperties(chart); 198 return getValue(chart); 199 200 } 201 202 protected void setChartProperties(JFreeChart chart) { 203 super.setChartProperties(chart); 204 205 CategoryPlot cpl = chart.getCategoryPlot(); 206 CategoryAxis axis = cpl.getDomainAxis(); 207 CategoryItemRenderer renderer = cpl.getRenderer(); 208 209 if (this.categoricalLabelFormat != null) { 210 StandardCategoryItemLabelGenerator scilg = null; 211 if (categoricalLabelDecimalFormat != null) { 212 scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, new DecimalFormat (categoricalLabelDecimalFormat)); 213 } else if (categoricalLabelDateFormat != null) { 214 scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, new SimpleDateFormat (categoricalLabelDateFormat)); 215 } else { 216 scilg = new StandardCategoryItemLabelGenerator(categoricalLabelFormat, new DecimalFormat ()); 217 } 218 renderer.setItemLabelGenerator(scilg); 219 renderer.setItemLabelsVisible(true); 220 if (categoricalItemLabelRotation != null) { 221 ItemLabelPosition posn = renderer.getPositiveItemLabelPosition(); 222 ItemLabelPosition pos2 = new ItemLabelPosition(posn.getItemLabelAnchor(), posn.getTextAnchor(), posn.getRotationAnchor(), categoricalItemLabelRotation.doubleValue()); 223 renderer.setPositiveItemLabelPosition(pos2); 224 } 225 226 } 227 228 Font lFont = Font.decode(getLabelFont()); 229 axis.setLabelFont(lFont); 230 ValueAxis rAxis = cpl.getRangeAxis(); 231 if (rAxis != null) { 232 rAxis.setLabelFont(lFont); 233 } 234 axis.setTickLabelFont(lFont); 235 if (maxCategoryLabelWidthRatio != null) { 236 axis.setMaximumCategoryLabelWidthRatio(maxCategoryLabelWidthRatio.floatValue()); 237 } 238 cpl.setDomainGridlinesVisible(showGridLines); 239 if (labelRotation != null) { 240 axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / labelRotation.doubleValue())); 241 } 242 AbstractCategoryItemRenderer acir = (AbstractCategoryItemRenderer) renderer; 243 244 for (int i = 0; i < seriesColors.size(); i++) { 245 acir.setSeriesPaint(i, getColorFromString((String ) seriesColors.get(i))); 246 } 247 248 } 249 250 } 251 | Popular Tags |