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.JRXyDataset; 36 import net.sf.jasperreports.charts.JRXySeries; 37 import net.sf.jasperreports.charts.util.XYDatasetLabelGenerator; 38 import net.sf.jasperreports.engine.JRChartDataset; 39 import net.sf.jasperreports.engine.JRExpressionCollector; 40 import net.sf.jasperreports.engine.design.JRVerifier; 41 import net.sf.jasperreports.engine.fill.JRCalculator; 42 import net.sf.jasperreports.engine.fill.JRExpressionEvalException; 43 import net.sf.jasperreports.engine.fill.JRFillChartDataset; 44 import net.sf.jasperreports.engine.fill.JRFillObjectFactory; 45 import net.sf.jasperreports.engine.util.Pair; 46 47 import org.jfree.data.general.Dataset; 48 import org.jfree.data.xy.XYSeries; 49 import org.jfree.data.xy.XYSeriesCollection; 50 51 52 56 public class JRFillXyDataset extends JRFillChartDataset implements JRXyDataset 57 { 58 59 62 protected JRFillXySeries[] xySeries = null; 63 64 private List seriesNames = null; 65 private Map seriesMap = null; 66 private Map labelsMap = null; 67 68 private Map itemHyperlinks; 69 70 71 74 public JRFillXyDataset( 75 JRXyDataset xyDataset, 76 JRFillObjectFactory factory 77 ) 78 { 79 super(xyDataset, factory); 80 81 82 JRXySeries[] srcXySeries = xyDataset.getSeries(); 83 if (srcXySeries != null && srcXySeries.length > 0) 84 { 85 xySeries = new JRFillXySeries[srcXySeries.length]; 86 for(int i = 0; i < xySeries.length; i++) 87 { 88 xySeries[i] = (JRFillXySeries)factory.getXySeries(srcXySeries[i]); 89 } 90 } 91 } 92 93 94 97 public JRXySeries[] getSeries() 98 { 99 return xySeries; 100 } 101 102 103 106 protected void customInitialize() 107 { 108 seriesNames = null; 109 seriesMap = null; 110 labelsMap = null; 111 itemHyperlinks = null; 112 } 113 114 115 118 protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException 119 { 120 if (xySeries != null && xySeries.length > 0) 121 { 122 for(int i = 0; i < xySeries.length; i++) 123 { 124 xySeries[i].evaluate(calculator); 125 } 126 } 127 } 128 129 130 133 protected void customIncrement() 134 { 135 if (xySeries != null && xySeries.length > 0) 136 { 137 if (seriesNames == null) 138 { 139 seriesNames = new ArrayList (); 140 seriesMap = new HashMap (); 141 labelsMap = new HashMap (); 142 itemHyperlinks = new HashMap (); 143 } 144 145 for(int i = 0; i < xySeries.length; i++) 146 { 147 JRFillXySeries crtXySeries = xySeries[i]; 148 149 Comparable seriesName = crtXySeries.getSeries(); 150 XYSeries xySrs = (XYSeries)seriesMap.get(seriesName); 151 if (xySrs == null) 152 { 153 xySrs = new XYSeries(seriesName); 154 seriesNames.add(seriesName); 155 seriesMap.put(seriesName, xySrs); 156 } 157 158 xySrs.addOrUpdate( 159 crtXySeries.getXValue(), 160 crtXySeries.getYValue() 161 ); 162 163 if (crtXySeries.getLabelExpression() != null) 164 { 165 Map seriesLabels = (Map )labelsMap.get(seriesName); 166 if (seriesLabels == null) 167 { 168 seriesLabels = new HashMap (); 169 labelsMap.put(seriesName, seriesLabels); 170 } 171 172 seriesLabels.put(crtXySeries.getXValue(), crtXySeries.getLabel()); 173 } 174 175 if (crtXySeries.hasItemHyperlinks()) 176 { 177 Map seriesLinks = (Map ) itemHyperlinks.get(seriesName); 178 if (seriesLinks == null) 179 { 180 seriesLinks = new HashMap (); 181 itemHyperlinks.put(seriesName, seriesLinks); 182 } 183 Pair xyKey = new Pair(crtXySeries.getXValue(), crtXySeries.getYValue()); 184 seriesLinks.put(xyKey, crtXySeries.getPrintItemHyperlink()); 185 } 186 } 187 } 188 } 189 190 191 194 public Dataset getCustomDataset() 195 { 196 XYSeriesCollection dataset = new XYSeriesCollection(); 197 if (seriesNames != null) 198 { 199 for(int i = 0; i < seriesNames.size(); i++) 200 { 201 Comparable seriesName = (Comparable )seriesNames.get(i); 202 dataset.addSeries((XYSeries)seriesMap.get(seriesName)); 203 } 204 } 205 return dataset; 206 } 207 208 209 212 public byte getDatasetType() { 213 return JRChartDataset.XY_DATASET; 214 } 215 216 217 220 public XYDatasetLabelGenerator getLabelGenerator(){ 221 return new XYDatasetLabelGenerator(labelsMap); 222 } 223 224 225 228 public void collectExpressions(JRExpressionCollector collector) 229 { 230 collector.collect(this); 231 } 232 233 234 public Map getItemHyperlinks() 235 { 236 return itemHyperlinks; 237 } 238 239 240 public boolean hasItemHyperlinks() 241 { 242 boolean foundLinks = false; 243 if (xySeries != null && xySeries.length > 0) 244 { 245 for (int i = 0; i < xySeries.length && !foundLinks; i++) 246 { 247 JRFillXySeries serie = xySeries[i]; 248 foundLinks = serie.hasItemHyperlinks(); 249 } 250 } 251 return foundLinks; 252 } 253 254 255 public void validate(JRVerifier verifier) 256 { 257 verifier.verify(this); 258 } 259 260 261 } 262 | Popular Tags |