1 28 package net.sf.jasperreports.charts.fill; 29 30 import java.util.ArrayList ; 31 import java.util.Date ; 32 import java.util.List ; 33 34 import net.sf.jasperreports.charts.JRHighLowDataset; 35 import net.sf.jasperreports.engine.JRChartDataset; 36 import net.sf.jasperreports.engine.JRException; 37 import net.sf.jasperreports.engine.JRExpression; 38 import net.sf.jasperreports.engine.JRExpressionCollector; 39 import net.sf.jasperreports.engine.JRHyperlink; 40 import net.sf.jasperreports.engine.JRPrintHyperlink; 41 import net.sf.jasperreports.engine.JRRuntimeException; 42 import net.sf.jasperreports.engine.design.JRVerifier; 43 import net.sf.jasperreports.engine.fill.JRCalculator; 44 import net.sf.jasperreports.engine.fill.JRExpressionEvalException; 45 import net.sf.jasperreports.engine.fill.JRFillChartDataset; 46 import net.sf.jasperreports.engine.fill.JRFillHyperlinkHelper; 47 import net.sf.jasperreports.engine.fill.JRFillObjectFactory; 48 49 import org.jfree.data.general.Dataset; 50 import org.jfree.data.xy.DefaultHighLowDataset; 51 52 53 57 public class JRFillHighLowDataset extends JRFillChartDataset implements JRHighLowDataset 58 { 59 60 63 private String series = null; 64 private List elements = new ArrayList (); 65 private Date date = null; 66 private Number high = null; 67 private Number low = null; 68 private Number open = null; 69 private Number close = null; 70 private Number volume = null; 71 72 private JRPrintHyperlink itemHyperlink; 73 private List itemHyperlinks; 74 75 76 79 public JRFillHighLowDataset(JRHighLowDataset dataset, JRFillObjectFactory factory) 80 { 81 super(dataset, factory); 82 } 83 84 85 protected void customInitialize() 86 { 87 elements = new ArrayList (); 88 itemHyperlinks = new ArrayList (); 89 } 90 91 92 protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException 93 { 94 series = (String ) calculator.evaluate(getSeriesExpression()); 95 date = (Date ) calculator.evaluate(getDateExpression()); 96 high = (Number ) calculator.evaluate(getHighExpression()); 97 low = (Number ) calculator.evaluate(getLowExpression()); 98 open = (Number ) calculator.evaluate(getOpenExpression()); 99 close = (Number ) calculator.evaluate(getCloseExpression()); 100 volume = (Number ) calculator.evaluate(getVolumeExpression()); 101 102 if (hasItemHyperlink()) 103 { 104 evaluateSectionHyperlink(calculator); 105 } 106 } 107 108 109 protected void evaluateSectionHyperlink(JRCalculator calculator) throws JRExpressionEvalException 110 { 111 try 112 { 113 itemHyperlink = JRFillHyperlinkHelper.evaluateHyperlink(getItemHyperlink(), calculator, JRExpression.EVALUATION_DEFAULT); 114 } 115 catch (JRExpressionEvalException e) 116 { 117 throw e; 118 } 119 catch (JRException e) 120 { 121 throw new JRRuntimeException(e); 122 } 123 } 124 125 126 protected void customIncrement() 127 { 128 elements.add(new HighLowElement(date, high, low, open, close, volume)); 129 130 if (hasItemHyperlink()) 131 { 132 itemHyperlinks.add(itemHyperlink); 133 } 134 } 135 136 137 public Dataset getCustomDataset() 138 { 139 int size = elements.size(); 140 if (size > 0) 141 { 142 Date [] dateArray = new Date [size]; 143 double[] highArray = new double[size]; 144 double[] lowArray = new double[size]; 145 double[] openArray = new double[size]; 146 double[] closeArray = new double[size]; 147 double[] volumeArray = new double[size]; 148 149 for (int i = 0; i < elements.size(); i++) { 150 HighLowElement bean = (HighLowElement) elements.get(i); 151 dateArray[i] = new Date (bean.getDate().getTime()); 152 highArray[i] = bean.getHigh().doubleValue(); 153 lowArray[i] = bean.getLow().doubleValue(); 154 openArray[i] = bean.getOpen().doubleValue(); 155 closeArray[i] = bean.getClose().doubleValue(); 156 volumeArray[i] = bean.getVolume().doubleValue(); 157 } 158 159 return new DefaultHighLowDataset(series, dateArray, highArray, lowArray, openArray, closeArray, volumeArray); 160 } 161 162 return null; 163 } 164 165 166 public JRExpression getSeriesExpression() 167 { 168 return ((JRHighLowDataset)parent).getSeriesExpression(); 169 } 170 171 172 public JRExpression getDateExpression() 173 { 174 return ((JRHighLowDataset)parent).getDateExpression(); 175 } 176 177 178 public JRExpression getHighExpression() 179 { 180 return ((JRHighLowDataset)parent).getHighExpression(); 181 } 182 183 184 public JRExpression getLowExpression() 185 { 186 return ((JRHighLowDataset)parent).getLowExpression(); 187 } 188 189 190 public JRExpression getOpenExpression() 191 { 192 return ((JRHighLowDataset)parent).getOpenExpression(); 193 } 194 195 196 public JRExpression getCloseExpression() 197 { 198 return ((JRHighLowDataset)parent).getCloseExpression(); 199 } 200 201 202 public JRExpression getVolumeExpression() 203 { 204 return ((JRHighLowDataset)parent).getVolumeExpression(); 205 } 206 207 210 private static class HighLowElement 211 { 212 Date date; 213 Number high; 214 Number low; 215 Number open; 216 Number close; 217 Number volume; 218 219 220 public HighLowElement( 221 Date date, 222 Number high, 223 Number low, 224 Number open, 225 Number close, 226 Number volume 227 ) 228 { 229 if (date == null) 230 throw new JRRuntimeException("Date value is null in high-low series."); 231 this.date = date; 232 233 if (high == null) 234 throw new JRRuntimeException("High value is null in high-low series."); 235 this.high = high; 236 237 if (low == null) 238 throw new JRRuntimeException("Low value is null in high-low series."); 239 this.low = low; 240 241 if (open == null) 242 throw new JRRuntimeException("Open value is null in high-low series."); 243 this.open = open; 244 245 if (close == null) 246 throw new JRRuntimeException("Close value is null in high-low series."); 247 this.close = close; 248 249 if (volume == null) 250 throw new JRRuntimeException("Volume value is null in high-low series."); 251 this.volume = volume; 252 } 253 254 255 public Date getDate() 256 { 257 return date; 258 } 259 260 261 public void setDate(Date date) 262 { 263 this.date = date; 264 } 265 266 267 public Number getHigh() 268 { 269 return high; 270 } 271 272 273 public void setHigh(Number high) 274 { 275 this.high = high; 276 } 277 278 279 public Number getLow() 280 { 281 return low; 282 } 283 284 285 public void setLow(Number low) 286 { 287 this.low = low; 288 } 289 290 291 public Number getOpen() 292 { 293 return open; 294 } 295 296 297 public void setOpen(Number open) 298 { 299 this.open = open; 300 } 301 302 303 public Number getClose() 304 { 305 return close; 306 } 307 308 309 public void setClose(Number close) 310 { 311 this.close = close; 312 } 313 314 315 public Number getVolume() 316 { 317 return volume; 318 } 319 320 321 public void setVolume(Number volume) 322 { 323 this.volume = volume; 324 } 325 } 326 327 330 public byte getDatasetType() { 331 return JRChartDataset.HIGHLOW_DATASET; 332 } 333 334 335 338 public void collectExpressions(JRExpressionCollector collector) 339 { 340 collector.collect(this); 341 } 342 343 344 public JRHyperlink getItemHyperlink() 345 { 346 return ((JRHighLowDataset) parent).getItemHyperlink(); 347 } 348 349 350 public boolean hasItemHyperlink() 351 { 352 return getItemHyperlink() != null; 353 } 354 355 356 public List getItemHyperlinks() 357 { 358 return itemHyperlinks; 359 } 360 361 362 public void validate(JRVerifier verifier) 363 { 364 verifier.verify(this); 365 } 366 367 } 368 | Popular Tags |