1 28 package net.sf.jasperreports.charts.fill; 29 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 import net.sf.jasperreports.charts.JRCategoryDataset; 34 import net.sf.jasperreports.charts.JRCategorySeries; 35 import net.sf.jasperreports.charts.util.CategoryLabelGenerator; 36 import net.sf.jasperreports.engine.JRChartDataset; 37 import net.sf.jasperreports.engine.JRExpressionCollector; 38 import net.sf.jasperreports.engine.JRRuntimeException; 39 import net.sf.jasperreports.engine.design.JRVerifier; 40 import net.sf.jasperreports.engine.fill.JRCalculator; 41 import net.sf.jasperreports.engine.fill.JRExpressionEvalException; 42 import net.sf.jasperreports.engine.fill.JRFillChartDataset; 43 import net.sf.jasperreports.engine.fill.JRFillObjectFactory; 44 45 import org.jfree.data.category.DefaultCategoryDataset; 46 import org.jfree.data.general.Dataset; 47 48 49 53 public class JRFillCategoryDataset extends JRFillChartDataset implements JRCategoryDataset 54 { 55 56 59 protected JRFillCategorySeries[] categorySeries = null; 60 61 private DefaultCategoryDataset dataset = null; 62 private Map labelsMap = null; 63 64 private Map itemHyperlinks; 65 66 67 70 public JRFillCategoryDataset( 71 JRCategoryDataset categoryDataset, 72 JRFillObjectFactory factory 73 ) 74 { 75 super(categoryDataset, factory); 76 77 78 JRCategorySeries[] srcCategorySeries = categoryDataset.getSeries(); 79 if (srcCategorySeries != null && srcCategorySeries.length > 0) 80 { 81 categorySeries = new JRFillCategorySeries[srcCategorySeries.length]; 82 for(int i = 0; i < categorySeries.length; i++) 83 { 84 categorySeries[i] = (JRFillCategorySeries)factory.getCategorySeries(srcCategorySeries[i]); 85 } 86 } 87 } 88 89 90 93 public JRCategorySeries[] getSeries() 94 { 95 return categorySeries; 96 } 97 98 99 102 protected void customInitialize() 103 { 104 dataset = null; 105 labelsMap = null; 106 itemHyperlinks = null; 107 } 108 109 112 protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException 113 { 114 if (categorySeries != null && categorySeries.length > 0) 115 { 116 for(int i = 0; i < categorySeries.length; i++) 117 { 118 categorySeries[i].evaluate(calculator); 119 } 120 } 121 } 122 123 126 protected void customIncrement() 127 { 128 if (categorySeries != null && categorySeries.length > 0) 129 { 130 if (dataset == null) 131 { 132 dataset = new DefaultCategoryDataset(); 133 labelsMap = new HashMap (); 134 itemHyperlinks = new HashMap (); 135 } 136 137 for(int i = 0; i < categorySeries.length; i++) 138 { 139 JRFillCategorySeries crtCategorySeries = categorySeries[i]; 140 141 Comparable seriesName = crtCategorySeries.getSeries(); 142 if (seriesName == null) 143 { 144 throw new JRRuntimeException("Category series name is null."); 145 } 146 147 dataset.addValue( 148 crtCategorySeries.getValue(), 149 crtCategorySeries.getSeries(), 150 crtCategorySeries.getCategory() 151 ); 152 153 if (crtCategorySeries.getLabelExpression() != null) 154 { 155 Map seriesLabels = (Map )labelsMap.get(seriesName); 156 if (seriesLabels == null) 157 { 158 seriesLabels = new HashMap (); 159 labelsMap.put(seriesName, seriesLabels); 160 } 161 162 seriesLabels.put(crtCategorySeries.getCategory(), crtCategorySeries.getLabel()); 163 } 164 165 if (crtCategorySeries.hasItemHyperlinks()) 166 { 167 Map seriesLinks = (Map ) itemHyperlinks.get(seriesName); 168 if (seriesLinks == null) 169 { 170 seriesLinks = new HashMap (); 171 itemHyperlinks.put(seriesName, seriesLinks); 172 } 173 seriesLinks.put(crtCategorySeries.getCategory(), crtCategorySeries.getPrintItemHyperlink()); 174 } 175 } 176 } 177 } 178 179 182 public Dataset getCustomDataset() 183 { 184 return dataset; 185 } 186 187 188 191 public byte getDatasetType() { 192 return JRChartDataset.CATEGORY_DATASET; 193 } 194 195 196 199 public CategoryLabelGenerator getLabelGenerator() 200 { 201 return new CategoryLabelGenerator(labelsMap); 202 } 203 204 205 208 public void collectExpressions(JRExpressionCollector collector) 209 { 210 collector.collect(this); 211 } 212 213 214 public Map getItemHyperlinks() 215 { 216 return itemHyperlinks; 217 } 218 219 220 public boolean hasItemHyperlinks() 221 { 222 boolean foundLinks = false; 223 if (categorySeries != null && categorySeries.length > 0) 224 { 225 for (int i = 0; i < categorySeries.length && !foundLinks; i++) 226 { 227 JRFillCategorySeries serie = categorySeries[i]; 228 foundLinks = serie.hasItemHyperlinks(); 229 } 230 } 231 return foundLinks; 232 } 233 234 235 public void validate(JRVerifier verifier) 236 { 237 verifier.verify(this); 238 } 239 240 } 241 | Popular Tags |