1 32 33 package it.businesslogic.ireport.chart; 34 import it.businesslogic.ireport.*; 35 import it.businesslogic.ireport.util.*; 36 import java.awt.Image ; 37 import org.jfree.data.*; 38 import org.jfree.chart.*; 39 import org.jfree.ui.*; 40 import org.jfree.chart.plot.*; 41 import org.jfree.data.general.DefaultPieDataset; 42 import org.jfree.data.category.DefaultCategoryDataset; 43 import org.jfree.data.xy.XYSeries; 44 import org.jfree.data.xy.DefaultHighLowDataset; 45 import org.jfree.data.xy.OHLCDataset; 46 import java.util.*; 47 48 52 public class DefaultChartFactory extends ChartFactory { 53 54 static final String PIE3D = "Pie3D"; 55 static final String PIE = "Pie"; 56 static final String BAR = "Bar"; 57 static final String BAR3D = "Bar3D"; 58 static final String LINE = "Line"; 59 static final String AREA = "Area"; 60 static final String CANDLESTICK = "Candlestick"; 61 62 public static Image drawChart(String [] parameters, IReportScriptlet scriptlet) 63 { 64 java.util.Properties props = parseProperties(parameters); 65 int width = getParameterAsInteger("width",props,250); 66 int height = getParameterAsInteger("height",props,100); 67 int quality = getParameterAsInteger("quality",props,1); 68 String chartName = props.getProperty("chartName"); 69 70 boolean showLegend = getParameterAsBoolean("legend",props,false); 71 boolean showTooltips = getParameterAsBoolean("tooltips",props,false); 72 73 74 if ( chartName.equals(PIE3D)) 75 { 76 Vector labels = getSeries("serie0", props,scriptlet); 77 Vector values = getSeries("serie1", props,scriptlet); 78 79 DefaultKeyedValues dkv = new DefaultKeyedValues(); 80 81 for (int i=0; i<values.size(); ++i) 82 { 83 84 String key = (i+1)+""; 85 if (labels != null) 86 { 87 key = ""+labels.get(i); 88 } 89 dkv.addValue( key, new Double (""+ ((values.get( i ) != null) ? values.get( i ) : "0")) ); 90 } 91 92 double depthFactor = getParameterAsDouble("depthFactor",props, 0.2); 93 JFreeChart chart = org.jfree.chart.ChartFactory.createPieChart3D("", new DefaultPieDataset(dkv), showLegend, showTooltips, false); 94 ((PiePlot3D)(chart.getPlot())).setDepthFactor( depthFactor ); 95 96 ((PiePlot3D)(chart.getPlot())).setForegroundAlpha( (float)getParameterAsDouble("foregroundAlpha",props, 0.0)); 97 setChartProperties(props, chart); 98 99 100 return chart.createBufferedImage(width*quality,height*quality); 101 } 102 else if ( chartName.equals(PIE)) 103 { 104 Vector labels = getSeries("serie0", props,scriptlet); 105 Vector values = getSeries("serie1", props,scriptlet); 106 107 DefaultKeyedValues dkv = new DefaultKeyedValues(); 108 109 for (int i=0; i<values.size(); ++i) 110 { 111 112 String key = (i+1)+""; 113 if (labels != null) 114 { 115 key = ""+labels.get(i); 116 } 117 dkv.addValue( key, new Double (""+ ((values.get( i ) != null) ? values.get( i ) : "0") )); 118 } 119 120 JFreeChart chart = org.jfree.chart.ChartFactory.createPieChart("", new DefaultPieDataset(dkv), showLegend, showTooltips, false); 121 setChartProperties(props, chart); 122 return chart.createBufferedImage(width*quality,height*quality); 123 } 124 else if ( chartName.equals(BAR) || chartName.equals(BAR3D)) 125 { 126 Vector values = getSeries("serie0", props,scriptlet); 127 Vector theCategories = getSeries("serie1", props,scriptlet); 128 Vector theSeries = getSeries("serie2", props,scriptlet); 129 130 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 131 132 if (scriptlet == null) 133 { 134 dataset = getSampleCategoryDataset(); 135 } 136 else 137 { 138 for (int i=0; i<values.size(); ++i) 139 { 140 141 String category = (i+1)+""; 142 if (theCategories != null && theCategories.size() > i) 143 { 144 category = ""+theCategories.get(i); 145 } 146 147 String theSerie = ""; 148 if (theSeries != null && theSeries.size() > i) 149 { 150 theSerie = ""+theSeries.get(i); 151 } 152 153 154 dataset.addValue(new Double (""+ ((values.get( i ) != null) ? values.get( i ) : "0")) ,theSerie, category); 155 } 156 } 157 158 int plotOrientation = getParameterAsInteger("plotOrientation", props, 1); 159 160 JFreeChart chart = null; 161 162 if ( chartName.equals(BAR) ) 163 { 164 chart = org.jfree.chart.ChartFactory.createBarChart("", 165 Misc.nvl(props.getProperty("categoryLabel"),""), 166 Misc.nvl(props.getProperty("valueLabel"),""), 167 dataset, 168 (plotOrientation == 1) ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, showLegend, showTooltips, false ); 173 } 174 else 175 { 176 chart = org.jfree.chart.ChartFactory.createBarChart3D("", 177 Misc.nvl(props.getProperty("categoryLabel"),""), 178 Misc.nvl(props.getProperty("valueLabel"),""), 179 dataset, 180 (plotOrientation == 1) ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, showLegend, showTooltips, false ); 185 } 186 setChartProperties(props, chart); 187 return chart.createBufferedImage(width*quality,height*quality); 188 } 189 else if ( chartName.equals(LINE) ) 190 { 191 Vector valuesX = getSeries("serie0", props,scriptlet); 192 Vector valuesY = getSeries("serie1", props,scriptlet); 193 Vector theSeries = getSeries("serie2", props,scriptlet); 194 195 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 197 198 int plotOrientation = getParameterAsInteger("plotOrientation", props, 1); 199 if (scriptlet == null) 200 { 201 dataset = getSampleCategoryDataset(); 202 } 203 else 204 { 205 for (int i=0; i<valuesX.size(); ++i) 206 { 207 String theSerie = ""; 208 if (theSeries != null && theSeries.size() > i) 209 { 210 theSerie = ""+theSeries.get(i); 211 } 212 213 dataset.addValue( (Number )new Double (""+ ((valuesX.get( i ) != null) ? valuesX.get( i ) : "0")), 214 (Comparable )theSerie, 215 (Comparable )new Double (""+ ((valuesY.get( i ) != null) ? valuesY.get( i ) : "0"))); 216 } 217 } 218 219 JFreeChart chart = null; 220 221 chart = org.jfree.chart.ChartFactory.createLineChart("", 222 Misc.nvl(props.getProperty("categoryLabel"),""), 223 Misc.nvl(props.getProperty("valueLabel"),""), 224 dataset, 225 (plotOrientation == 1) ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, showLegend, showTooltips, false ); 230 231 setChartProperties(props, chart); 232 return chart.createBufferedImage(width*quality,height*quality); 233 } 234 else if ( chartName.equals(AREA) ) 235 { 236 Vector valuesX = getSeries("serie0", props,scriptlet); 237 Vector valuesY = getSeries("serie1", props,scriptlet); 238 Vector theSeries = getSeries("serie2", props,scriptlet); 239 240 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 242 243 int plotOrientation = getParameterAsInteger("plotOrientation", props, 1); 244 245 if (scriptlet == null) 246 { 247 dataset = getSampleCategoryDataset(); 248 } 249 else 250 { 251 for (int i=0; i<valuesX.size(); ++i) 252 { 253 String theSerie = ""; 254 if (theSeries != null && theSeries.size() > i) 255 { 256 theSerie = ""+theSeries.get(i); 257 } 258 259 dataset.addValue( (Number )new Double (""+ ((valuesX.get( i ) != null) ? valuesX.get( i ) : "0")), 260 (Comparable )theSerie, 261 (Comparable )new Double (""+ ((valuesY.get( i ) != null) ? valuesY.get( i ) : "0"))); 262 } 263 264 } 265 JFreeChart chart = null; 266 267 chart = org.jfree.chart.ChartFactory.createAreaChart("", 268 Misc.nvl(props.getProperty("categoryLabel"),""), 269 Misc.nvl(props.getProperty("valueLabel"),""), 270 dataset, 271 (plotOrientation == 1) ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, showLegend, showTooltips, false ); 276 277 setChartProperties(props, chart); 278 CategoryPlot cplot = (CategoryPlot)chart.getPlot(); 279 cplot.setForegroundAlpha( (float)getParameterAsDouble("foregroundAlpha",props, 0.0) ); 280 281 return chart.createBufferedImage(width*quality,height*quality); 282 } 283 284 285 return null; 286 } 287 288 protected static void setChartProperties(java.util.Properties props,JFreeChart chart) 289 { 290 String title = props.getProperty("title"); 291 if (title!=null && title.length() >0) 292 { 293 chart.setTitle(title); 294 int titlePosition = getParameterAsInteger("titlePosition", props, 1); 295 if (titlePosition == 1) chart.getTitle().setPosition(RectangleEdge.TOP ); 296 if (titlePosition == 2) chart.getTitle().setPosition(RectangleEdge.BOTTOM ); 297 if (titlePosition == 3) chart.getTitle().setPosition(RectangleEdge.LEFT ); 298 if (titlePosition == 4) chart.getTitle().setPosition(RectangleEdge.RIGHT ); 299 300 } 301 302 title = props.getProperty("subtitle"); 303 if (title!=null && title.length() >0) 304 { 305 org.jfree.chart.title.TextTitle subtitle1 = new org.jfree.chart.title.TextTitle(title); 306 chart.addSubtitle(subtitle1); 307 } 308 309 java.awt.Color bgColor = getParameterAsColor("chartBackground",props, java.awt.Color.WHITE); 310 if (bgColor!=null) chart.setBackgroundPaint( bgColor ); 311 312 bgColor = getParameterAsColor("plotBackground",props, java.awt.Color.WHITE); 313 if (bgColor!=null) chart.getPlot().setBackgroundPaint( bgColor ); 314 315 } 316 317 318 319 protected static java.util.Properties parseProperties(String [] properties) 320 { 321 java.util.Properties props = new java.util.Properties (); 322 323 for (int i=0; i<properties.length; ++i) 324 { 325 String key = properties[i].substring(0, properties[i].indexOf("=")); 326 String val = properties[i].substring(key.length()+1); 327 props.setProperty(key,val); 328 } 329 return props; 330 } 331 332 protected static DefaultCategoryDataset getSampleCategoryDataset() 333 { 334 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 335 dataset.addValue(1.0, "Series 1", "Type 1"); 336 dataset.addValue(4.0, "Series 1", "Type 2"); 337 dataset.addValue(3.0, "Series 1", "Type 3"); 338 dataset.addValue(5.0, "Series 1", "Type 4"); 339 dataset.addValue(5.0, "Series 1", "Type 5"); 340 dataset.addValue(7.0, "Series 1", "Type 6"); 341 dataset.addValue(7.0, "Series 1", "Type 7"); 342 dataset.addValue(8.0, "Series 1", "Type 8"); 343 dataset.addValue(5.0, "Series 2", "Type 1"); 344 dataset.addValue(7.0, "Series 2", "Type 2"); 345 dataset.addValue(6.0, "Series 2", "Type 3"); 346 dataset.addValue(8.0, "Series 2", "Type 4"); 347 dataset.addValue(4.0, "Series 2", "Type 5"); 348 dataset.addValue(4.0, "Series 2", "Type 6"); 349 dataset.addValue(2.0, "Series 2", "Type 7"); 350 dataset.addValue(1.0, "Series 2", "Type 8"); 351 dataset.addValue(4.0, "Series 3", "Type 1"); 352 dataset.addValue(3.0, "Series 3", "Type 2"); 353 dataset.addValue(2.0, "Series 3", "Type 3"); 354 dataset.addValue(3.0, "Series 3", "Type 4"); 355 dataset.addValue(6.0, "Series 3", "Type 5"); 356 dataset.addValue(3.0, "Series 3", "Type 6"); 357 dataset.addValue(4.0, "Series 3", "Type 7"); 358 dataset.addValue(3.0, "Series 3", "Type 8"); 359 return dataset; 360 } 361 } 362 | Popular Tags |