1 28 package net.sf.jasperreports.charts.fill; 29 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import net.sf.jasperreports.charts.JRTimePeriodDataset; 36 import net.sf.jasperreports.charts.JRTimePeriodSeries; 37 import net.sf.jasperreports.charts.util.TimePeriodDatasetLabelGenerator; 38 import net.sf.jasperreports.engine.JRChartDataset; 39 import net.sf.jasperreports.engine.JRExpressionCollector; 40 import net.sf.jasperreports.engine.JRRuntimeException; 41 import net.sf.jasperreports.engine.design.JRVerifier; 42 import net.sf.jasperreports.engine.fill.JRCalculator; 43 import net.sf.jasperreports.engine.fill.JRExpressionEvalException; 44 import net.sf.jasperreports.engine.fill.JRFillChartDataset; 45 import net.sf.jasperreports.engine.fill.JRFillObjectFactory; 46 47 import org.jfree.data.general.Dataset; 48 import org.jfree.data.time.SimpleTimePeriod; 49 import org.jfree.data.time.TimePeriodValues; 50 import org.jfree.data.time.TimePeriodValuesCollection; 51 52 53 57 public class JRFillTimePeriodDataset extends JRFillChartDataset implements JRTimePeriodDataset 58 { 59 60 63 protected JRFillTimePeriodSeries[] timePeriodSeries = null; 64 65 private List seriesNames = null; 66 private Map seriesMap = null; 67 private Map labelsMap = null; 68 private Map itemHyperlinks; 69 70 71 74 public JRFillTimePeriodDataset( 75 JRTimePeriodDataset timePeriodDataset, 76 JRFillObjectFactory factory 77 ) 78 { 79 super(timePeriodDataset, factory); 80 81 JRTimePeriodSeries[] srcTimePeriodSeries = timePeriodDataset.getSeries(); 82 if (srcTimePeriodSeries != null && srcTimePeriodSeries.length > 0) 83 { 84 timePeriodSeries = new JRFillTimePeriodSeries[srcTimePeriodSeries.length]; 85 for (int i = 0; i < timePeriodSeries.length; i++) 86 { 87 timePeriodSeries[i] = 88 (JRFillTimePeriodSeries)factory.getTimePeriodSeries(srcTimePeriodSeries[i]); 89 } 90 } 91 } 92 93 public JRTimePeriodSeries[] getSeries() 94 { 95 return timePeriodSeries; 96 } 97 98 protected void customInitialize() 99 { 100 seriesNames = null; 101 seriesMap = null; 102 labelsMap = null; 103 itemHyperlinks = null; 104 } 105 106 protected void customEvaluate(JRCalculator calculator) 107 throws JRExpressionEvalException 108 { 109 if (timePeriodSeries != null && timePeriodSeries.length > 0) 110 { 111 for (int i = 0; i < timePeriodSeries.length; i++) 112 { 113 timePeriodSeries[i].evaluate(calculator); 114 } 115 } 116 } 117 118 protected void customIncrement() 119 { 120 if (timePeriodSeries != null && timePeriodSeries.length > 0) 121 { 122 if (seriesNames == null) 123 { 124 seriesNames = new ArrayList (); 125 seriesMap = new HashMap (); 126 labelsMap = new HashMap (); 127 itemHyperlinks = new HashMap (); 128 } 129 130 for (int i = 0; i < timePeriodSeries.length; i++) 131 { 132 JRFillTimePeriodSeries crtTimePeriodSeries = timePeriodSeries[i]; 133 134 Comparable seriesName = crtTimePeriodSeries.getSeries(); 135 if (seriesName == null) 136 { 137 throw new JRRuntimeException("Time period series name is null."); 138 } 139 140 TimePeriodValues timePeriodValues = (TimePeriodValues)seriesMap.get(seriesName); 141 if (timePeriodValues == null) 142 { 143 timePeriodValues = new TimePeriodValues(seriesName.toString()); 144 seriesNames.add(seriesName); 145 seriesMap.put(seriesName, timePeriodValues); 146 } 147 148 SimpleTimePeriod stp = 149 new SimpleTimePeriod( 150 crtTimePeriodSeries.getStartDate(), 151 crtTimePeriodSeries.getEndDate() 152 ); 153 154 timePeriodValues.add(stp, crtTimePeriodSeries.getValue()); 155 156 if (crtTimePeriodSeries.getLabelExpression() != null) 157 { 158 Map seriesLabels = (Map )labelsMap.get(seriesName); 159 if (seriesLabels == null) 160 { 161 seriesLabels = new HashMap (); 162 labelsMap.put(seriesName, seriesLabels); 163 } 164 165 seriesLabels.put(stp, crtTimePeriodSeries.getLabel()); 166 } 167 168 if (crtTimePeriodSeries.hasItemHyperlink()) 169 { 170 Map seriesLinks = (Map ) itemHyperlinks.get(seriesName); 171 if (seriesLinks == null) 172 { 173 seriesLinks = new HashMap (); 174 itemHyperlinks.put(seriesName, seriesLinks); 175 } 176 177 seriesLinks.put(stp, crtTimePeriodSeries.getPrintItemHyperlink()); 178 } 179 } 180 } 181 } 182 183 public Dataset getCustomDataset() 184 { 185 TimePeriodValuesCollection dataset = new TimePeriodValuesCollection(); 186 if (seriesNames != null) 187 { 188 for(int i = 0; i < seriesNames.size(); i++) 189 { 190 Comparable seriesName = (Comparable )seriesNames.get(i); 191 dataset.addSeries((TimePeriodValues)seriesMap.get(seriesName)); 192 } 193 } 194 return dataset; 195 } 196 197 200 public byte getDatasetType() 201 { 202 return JRChartDataset.TIMEPERIOD_DATASET; 203 } 204 205 208 public TimePeriodDatasetLabelGenerator getLabelGenerator() 209 { 210 return new TimePeriodDatasetLabelGenerator(labelsMap); 211 } 212 213 216 public void collectExpressions(JRExpressionCollector collector) 217 { 218 collector.collect(this); 219 } 220 221 222 public boolean hasItemHyperlinks() 223 { 224 boolean foundLinks = false; 225 if (timePeriodSeries != null && timePeriodSeries.length > 0) 226 { 227 for (int i = 0; i < timePeriodSeries.length && !foundLinks; i++) 228 { 229 foundLinks = timePeriodSeries[i].hasItemHyperlink(); 230 } 231 } 232 return foundLinks; 233 } 234 235 236 public Map getItemHyperlinks() 237 { 238 return itemHyperlinks; 239 } 240 241 242 public void validate(JRVerifier verifier) 243 { 244 verifier.verify(this); 245 } 246 247 } 248 | Popular Tags |