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.JRTimeSeries; 36 import net.sf.jasperreports.charts.JRTimeSeriesDataset; 37 import net.sf.jasperreports.charts.util.TimeSeriesLabelGenerator; 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.RegularTimePeriod; 49 import org.jfree.data.time.TimeSeries; 50 import org.jfree.data.time.TimeSeriesCollection; 51 52 56 public class JRFillTimeSeriesDataset extends JRFillChartDataset implements JRTimeSeriesDataset 57 { 58 59 62 protected JRFillTimeSeries[] timeSeries = null; 63 64 private List seriesNames = null; 65 private Map seriesMap = null; 66 private Map labelsMap = null; 67 private Map itemHyperlinks; 68 69 70 public JRFillTimeSeriesDataset( 71 JRTimeSeriesDataset timeSeriesDataset, 72 JRFillObjectFactory factory 73 ) 74 { 75 super( timeSeriesDataset, factory ); 76 77 JRTimeSeries[] srcTimeSeries = timeSeriesDataset.getSeries(); 78 if( srcTimeSeries != null && srcTimeSeries.length > 0) 79 { 80 timeSeries = new JRFillTimeSeries[srcTimeSeries.length]; 81 for (int i = 0; i < timeSeries.length; i++) 82 { 83 timeSeries[i] = (JRFillTimeSeries)factory.getTimeSeries(srcTimeSeries[i]); 84 } 85 } 86 } 87 88 public JRTimeSeries[] getSeries() 89 { 90 return timeSeries; 91 } 92 93 protected void customInitialize() 94 { 95 seriesNames = null; 96 seriesMap = null; 97 labelsMap = null; 98 itemHyperlinks = null; 99 } 100 101 protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException 102 { 103 if(timeSeries != null && timeSeries.length > 0) 104 { 105 for (int i = 0; i < timeSeries.length; i++) 106 { 107 timeSeries[i].evaluate( calculator ); 108 } 109 } 110 } 111 112 113 protected void customIncrement() 114 { 115 if (timeSeries != null && timeSeries.length > 0) 116 { 117 if (seriesNames == null) 118 { 119 seriesNames = new ArrayList (); 120 seriesMap = new HashMap (); 121 labelsMap = new HashMap (); 122 itemHyperlinks = new HashMap (); 123 } 124 125 for (int i = 0; i < timeSeries.length; i++) 126 { 127 JRFillTimeSeries crtTimeSeries = timeSeries[i]; 128 129 Comparable seriesName = crtTimeSeries.getSeries(); 130 if (seriesName == null) 131 { 132 throw new JRRuntimeException("Time series name is null."); 133 } 134 135 TimeSeries series = (TimeSeries)seriesMap.get(seriesName); 136 if(series == null) 137 { 138 series = new TimeSeries(seriesName.toString(), getTimePeriod()); 139 seriesNames.add(seriesName); 140 seriesMap.put(seriesName, series); 141 } 142 143 RegularTimePeriod tp = 144 RegularTimePeriod.createInstance( 145 getTimePeriod(), 146 crtTimeSeries.getTimePeriod(), 147 getTimeZone() 148 ); 149 150 series.addOrUpdate(tp, crtTimeSeries.getValue()); 151 152 if (crtTimeSeries.getLabelExpression() != null) 153 { 154 Map seriesLabels = (Map )labelsMap.get(seriesName); 155 if (seriesLabels == null) 156 { 157 seriesLabels = new HashMap (); 158 labelsMap.put(seriesName, seriesLabels); 159 } 160 161 seriesLabels.put(tp, crtTimeSeries.getLabel()); 162 } 163 164 if (crtTimeSeries.hasItemHyperlink()) 165 { 166 Map seriesLinks = (Map ) itemHyperlinks.get(seriesName); 167 if (seriesLinks == null) 168 { 169 seriesLinks = new HashMap (); 170 itemHyperlinks.put(seriesName, seriesLinks); 171 } 172 seriesLinks.put(tp, crtTimeSeries.getPrintItemHyperlink()); 173 } 174 } 175 } 176 } 177 178 public Dataset getCustomDataset() 179 { 180 TimeSeriesCollection dataset = new TimeSeriesCollection(); 181 if (seriesNames != null) 182 { 183 for(int i = 0; i < seriesNames.size(); i++) 184 { 185 Comparable seriesName = (Comparable )seriesNames.get(i); 186 dataset.addSeries((TimeSeries)seriesMap.get(seriesName)); 187 } 188 } 189 return dataset; 190 } 191 192 193 public Class getTimePeriod() { 194 return ((JRTimeSeriesDataset)parent).getTimePeriod(); 195 } 196 197 public void setTimePeriod(Class timePeriod) { 198 } 199 200 203 public byte getDatasetType() { 204 return JRChartDataset.TIMESERIES_DATASET; 205 } 206 207 208 public TimeSeriesLabelGenerator getLabelGenerator(){ 209 return new TimeSeriesLabelGenerator(labelsMap); 210 } 211 212 213 216 public void collectExpressions(JRExpressionCollector collector) 217 { 218 collector.collect(this); 219 } 220 221 222 public Map getItemHyperlinks() 223 { 224 return itemHyperlinks; 225 } 226 227 228 public boolean hasItemHyperlinks() 229 { 230 boolean foundLinks = false; 231 if (timeSeries != null && timeSeries.length > 0) 232 { 233 for (int i = 0; i < timeSeries.length && !foundLinks; i++) 234 { 235 foundLinks = timeSeries[i].hasItemHyperlink(); 236 } 237 } 238 return foundLinks; 239 } 240 241 242 public void validate(JRVerifier verifier) 243 { 244 verifier.verify(this); 245 } 246 247 248 } 249 | Popular Tags |