posts - 14,  comments - 37,  trackbacks - 0
  1封装FreeChart的Bean
  2
  3FreeChart.java
  4
  5package com.function;
  6/*基于JFreeChart 1.0.0 Pre2.jar*/
  7import java.io.*;
  8import java.awt.Font;
  9import java.awt.Color;
 10import java.text.SimpleDateFormat;
 11import java.text.DecimalFormat;
 12import java.util.Calendar;
 13import javax.servlet.http.HttpSession;
 14
 15import org.jfree.data.*;
 16import org.jfree.chart.*;
 17//数据集
 18import org.jfree.data.category.DefaultCategoryDataset;
 19import org.jfree.data.category.CategoryDataset;
 20import org.jfree.data.category.IntervalCategoryDataset;
 21import org.jfree.data.general.DefaultPieDataset;
 22import org.jfree.data.general.PieDataset;
 23import org.jfree.data.xy.TableXYDataset;
 24import org.jfree.data.xy.XYZDataset;
 25import org.jfree.data.xy.IntervalXYDataset;
 26import org.jfree.data.xy.WindDataset;
 27import org.jfree.data.general.WaferMapDataset;
 28import org.jfree.data.xy.XYDataset;
 29import org.jfree.data.xy.OHLCDataset;
 30import org.jfree.data.xy.SignalsDataset;
 31import org.jfree.data.statistics.BoxAndWhiskerXYDataset;
 32
 33//数据集辅助
 34import org.jfree.data.time.Day;
 35import org.jfree.data.time.Hour;
 36import org.jfree.data.time.TimeSeries;
 37import org.jfree.data.time.TimeSeriesCollection;
 38
 39//图集
 40import org.jfree.chart.plot.CategoryPlot;
 41import org.jfree.chart.plot.PiePlot;
 42import org.jfree.chart.plot.MultiplePiePlot;
 43import org.jfree.chart.plot.PiePlot3D;
 44import org.jfree.chart.plot.XYPlot;
 45
 46
 47//表现形式集
 48import org.jfree.chart.renderer.category.BarRenderer;
 49import org.jfree.chart.renderer.category.BarRenderer3D;
 50import org.jfree.chart.renderer.category.StackedBarRenderer;
 51import org.jfree.chart.renderer.category.StackedBarRenderer3D;
 52import org.jfree.chart.renderer.category.StackedAreaRenderer;
 53import org.jfree.chart.renderer.category.LineAndShapeRenderer;
 54import org.jfree.chart.renderer.category.LineRenderer3D;
 55import org.jfree.chart.renderer.category.WaterfallBarRenderer;
 56import org.jfree.chart.renderer.xy.XYItemRenderer;
 57
 58//标签
 59import org.jfree.chart.labels.StandardPieItemLabelGenerator;
 60
 61
 62//刻度
 63import org.jfree.chart.axis.NumberAxis;
 64import org.jfree.chart.axis.DateAxis;
 65import org.jfree.chart.axis.CategoryAxis;
 66import org.jfree.chart.axis.ValueAxis;
 67import org.jfree.chart.axis.SegmentedTimeline;
 68
 69import org.jfree.chart.axis.TickUnit;
 70import org.jfree.chart.axis.TickUnits;
 71import org.jfree.chart.axis.TickUnitSource;
 72
 73//通用
 74import org.jfree.data.general.DatasetUtilities;
 75
 76//常量
 77import org.jfree.chart.axis.AxisLocation;
 78import org.jfree.chart.plot.PlotOrientation;
 79import org.jfree.util.TableOrder;
 80
 81
 82public class FreeChart
 83{
 84 /*定义各类图表所需的Dataset*/
 85 private DefaultCategoryDataset vDefaultCategoryDataset=null;
 86 private CategoryDataset vCategoryDataset=null;
 87 private DefaultPieDataset vDefaultPieDataset=null;
 88 private PieDataset vPieDataset=null;
 89 private XYDataset vXYDataset=null;
 90 private TableXYDataset vTableXYDataset=null;
 91 private XYZDataset vXYZDataset=null;
 92 private IntervalXYDataset vIntervalXYDataset=null;
 93 private WindDataset vWindDataset=null;
 94 private WaferMapDataset vWaferMapDataset=null;
 95 private IntervalCategoryDataset vIntervalCategoryDataset=null;
 96 private OHLCDataset vOHLCDataset=null;
 97 private SignalsDataset vSignalsDataset=null;
 98 private BoxAndWhiskerXYDataset vBoxAndWhiskerXYDataset=null;
 99 /*定义各类图表所需的Dataset辅助*/
100 TimeSeries vTimeSeries=null;
101 TimeSeriesCollection vTimeSeriesCollection=null;
102 /*定义图表*/
103 private JFreeChart vFreeChart=null;
104 private JFreeChart vFreeChartExtend=null;
105 /*映射图片的路径名称*/
106 private String strFileName=null;
107 
108 /*定义效果*/
109 private CategoryPlot vCategoryPlot=null;
110 private PiePlot vPiePlot=null;
111 private MultiplePiePlot vMultiplePiePlot=null;
112 private PiePlot3D vPiePlot3D=null;
113 private XYPlot vXYPlot=null;
114 
115 private StandardPieItemLabelGenerator vStandardPieItemLabelGenerator=null;
116 
117 /*定义刻度*/
118 private NumberAxis vNumberAxis=null;
119 private DateAxis vDateAxis=null;
120 private CategoryAxis vCategoryAxis=null;
121 private ValueAxis vValueAxis=null;
122 private SegmentedTimeline vSegmentedTimeline=null
123 
124 private BarRenderer vBarRenderer=null;
125 private BarRenderer3D vBarRenderer3D=null;
126 private StackedBarRenderer vStackedBarRenderer=null;
127 private StackedBarRenderer3D vStackedBarRenderer3D=null;
128 private StackedAreaRenderer vStackedAreaRenderer=null;
129 private LineAndShapeRenderer vLineAndShapeRenderer=null;
130 private LineRenderer3D vLineRenderer3D=null;
131 private WaterfallBarRenderer vWaterfallBarRenderer=null;
132 private XYItemRenderer vXYItemRenderer=null;
133 
134 //是否启用效果模式
135 private boolean bRender=false;
136 
137 /*定义静态数据*/
138 private String[] strItemArray=null;
139 private String[] strMultipleItemArray=null;
140 private String[] strCategoryArray=null;
141 private String[] strDataArray=null;
142 private String[] strMultipleDataArray=null;
143 private double[][] iMultipleDataArray=null;
144 
145 /**/
146 
147 public static String[] vChartTypeArray={"饼图","分离型饼图","三维饼图","复合饼图","三维分离型饼图","簇状柱形图","三维簇状柱形图","堆积柱形图","三维堆积柱形图","面积图","三维面积图","折线图","三维折线图","三维曲面图","三维柱形图","雷达图","散点图","百分比堆积面积图","三维百分比堆积面积图","折线散点图","面积散点图","时间序列图","三维柱形圆锥图","盘高-盘低-收盘图","开盘-盘高-盘低-收盘图","曲面图(俯视框架图)","气泡图","簇状条形图","数据点折线图","无数据点折线散点图","无数据点平滑线散点图"};
148 public String strTimeAxis="";
149 public void FreeChart()
150 {
151  
152 }
153 public void createDataset(String strMultipleItemList,String strCategoryList,String strMultipleDataList)
154 {
155  strMultipleItemArray=strMultipleItemList.split(",");
156  strCategoryArray=strCategoryList.split(",");
157  strMultipleDataArray=strMultipleDataList.split("#");
158  iMultipleDataArray=new double[strCategoryArray.length][strMultipleItemArray.length];
159  
160  java.text.SimpleDateFormat vSimpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
161  java.text.SimpleDateFormat vSimpleDateFormatToday=new SimpleDateFormat("yyyy-MM-");
162  java.util.Date vItemDate=null;
163  java.util.Date vTodayDate=null;
164  Day vDay=null;
165  vTodayDate=new java.util.Date();
166  vTimeSeriesCollection=new TimeSeriesCollection();
167  vTimeSeriesCollection.setDomainIsPointsInTime(false);
168  
169  for(int mIndex=0;mIndex<strMultipleDataArray.length;mIndex++)
170  {
171   String[] strElementArray=strMultipleDataArray[mIndex].split(",");
172   if(strTimeAxis.equals("Hour"))
173   {
174    vTimeSeries=new TimeSeries(strCategoryArray[mIndex],Hour.class);
175   }
176   else
177   {
178    vTimeSeries=new TimeSeries(strCategoryArray[mIndex],Day.class);
179   }
180   //vTimeSeries.clear();
181   for(int nIndex=0;nIndex<strElementArray.length;nIndex++)
182   {
183    iMultipleDataArray[mIndex][nIndex]=Double.parseDouble(strElementArray[nIndex]);
184    try
185    {
186     //vItemDate=vSimpleDateFormat.parse(vSimpleDateFormatToday.format(vTodayDate)+String.valueOf(nIndex+1));
187     vItemDate=vSimpleDateFormat.parse(strMultipleItemArray[nIndex]);
188     if(strTimeAxis.equals("Hour"))
189     {
190      vDay=new Day(vItemDate.getDate(),1+vItemDate.getMonth(),1900+vItemDate.getYear());
191      vTimeSeries.add(new Hour(vItemDate.getHours(),vDay),Double.parseDouble(strElementArray[nIndex]));
192     }
193     else
194     {
195      vTimeSeries.add(new Day(vItemDate.getDate(),1+vItemDate.getMonth(),1900+vItemDate.getYear()),Double.parseDouble(strElementArray[nIndex]));
196     }
197     
198    }
199    catch(Exception e)
200    {System.out.println(e.getMessage());}
201   }   
202   vTimeSeriesCollection.addSeries(vTimeSeries);  
203  }
204  try
205  {
206   vCategoryDataset=DatasetUtilities.createCategoryDataset(strCategoryArray,strMultipleItemArray,iMultipleDataArray);
207   vPieDataset=DatasetUtilities.createPieDatasetForColumn(vCategoryDataset,0);
208   vPieDataset=DatasetUtilities.createPieDatasetForRow(vCategoryDataset,0);
209   //vWaferMapDataset=(WaferMapDataset)vCategoryDataset;
210   
211   vTableXYDataset=(TableXYDataset)vTimeSeriesCollection;
212   vIntervalXYDataset=(IntervalXYDataset)vTimeSeriesCollection;   
213   
214   vXYDataset=(XYDataset)vTimeSeriesCollection;
215   /*
216   vXYZDataset=(XYZDataset)vTimeSeriesCollection;
217   //vWaferMapDataset=(WaferMapDataset)vTimeSeriesCollection;
218   vWindDataset=(WindDataset)vTimeSeriesCollection;   
219   vOHLCDataset=(OHLCDataset)vTimeSeriesCollection;
220   vSignalsDataset=(SignalsDataset)vTimeSeriesCollection;
221   vBoxAndWhiskerXYDataset=(BoxAndWhiskerXYDataset)vTimeSeriesCollection;
222   */

223  }

224  catch(Exception e)
225  {}
226 }
227 public void createDataset(String strItemList,String strDataList)
228 {
229  vDefaultCategoryDataset=new DefaultCategoryDataset();
230  vDefaultPieDataset=new DefaultPieDataset();  
231    
232  strItemArray=strItemList.split(",");
233  strDataArray=strDataList.split(",");
234  for(int kIndex=0;kIndex<strItemArray.length;kIndex++)
235  {
236   vDefaultCategoryDataset.addValue(Double.parseDouble(strDataArray[kIndex])," ",strItemArray[kIndex]);
237   vDefaultPieDataset.setValue(strItemArray[kIndex],Double.parseDouble(strDataArray[kIndex]));   
238  }
  
239 }

240 public DefaultCategoryDataset getDefaultCategoryDataset()
241 {
242  return this.vDefaultCategoryDataset;
243 }

244 public CategoryDataset getCategoryDataset()
245 {
246  return this.vCategoryDataset;
247 }

248 public DefaultPieDataset getDefaultPieDataset()
249 {
250  return this.vDefaultPieDataset;
251 }

252 public PieDataset getPieDataset()
253 {
254  return this.vPieDataset;
255 }

256 public XYDataset getXYDataset()
257 {
258  return this.vXYDataset;
259 }

260 public TableXYDataset getTableXYDataset()
261 {
262  return this.vTableXYDataset;
263 }

264 public XYZDataset getXYZDataset()
265 {
266  return this.vXYZDataset;
267 }

268 public IntervalXYDataset getIntervalXYDataset()
269 {
270  return this.vIntervalXYDataset;
271 }

272 public WindDataset getWindDataset()
273 {
274  return this.vWindDataset;
275 }

276 public WaferMapDataset getWaferMapDataset()
277 {
278  return this.vWaferMapDataset;
279 }

280 public IntervalCategoryDataset getIntervalCategoryDataset()
281 {
282  return this.vIntervalCategoryDataset;
283 }

284 public OHLCDataset getOHLCDataset()
285 {
286  return this.vOHLCDataset;
287 }

288 public SignalsDataset getSignalsDataset()
289 {
290  return this.vSignalsDataset;
291 }

292 public BoxAndWhiskerXYDataset getBoxAndWhiskerXYDataset()
293 {
294  return this.vBoxAndWhiskerXYDataset;
295 }

296 /*
297 iChartType:图表类型
298 */

299 public void createChart(int iChartType,String strFreeChartInfo,String strFreeChartXInfo,String strFreeChartYInfo)
300 {
301  switch(iChartType)
302  {
303   case 1:    
304    vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),true,false,false);
305    try
306    {
307     vPiePlot=(PiePlot)vFreeChart.getPlot();
308     if(vPiePlot!=null)
309     {
310      if(bRender)
311      {
312       if(strItemArray.length>0)
313       {
314        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
315        {
316         //指定Section色彩
317         vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
318         //指定Section轮廓线颜色
319         vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
320        }

321       }

322      }

323      //指定Section标签格式
324      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
325      vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
326      //
327      vPiePlot.setForegroundAlpha(0.5f);
328     }

329    }

330    catch(Exception e)
331    {}
332    break;
333   case 2:
334    vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),this.getPieDataset(),0,false,true,false,false,false,false);
335    try
336    {
337     vPiePlot=(PiePlot)vFreeChart.getPlot();
338     if(vPiePlot!=null)
339     {
340      if(bRender)
341      {
342       if(strItemArray.length>0)
343       {
344        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
345        {
346         //指定Section色彩
347         vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
348         //指定Section轮廓线颜色
349         vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
350        }

351        //抽取指定块
352        vPiePlot.setExplodePercent(0,1.00);
353       }

354      }

355      //指定Section标签格式
356      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
357      vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
358            
359      vPiePlot.setForegroundAlpha(0.5f);
360     }

361    }

362    catch(Exception e)
363    {}
364    break;
365   case 3:
366    vFreeChart=ChartFactory.createMultiplePieChart(strFreeChartInfo,this.getCategoryDataset(),TableOrder.BY_ROW,true,false,false);
367    try
368    {
369     vMultiplePiePlot=(MultiplePiePlot)vFreeChart.getPlot();
370     if(vMultiplePiePlot!=null)
371     {
372      vFreeChartExtend=vMultiplePiePlot.getPieChart();
373      vPiePlot=(PiePlot)vFreeChartExtend.getPlot();
374      if(bRender)
375      {
376       if(strItemArray.length>0)
377       {
378        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
379        {
380         //指定Section色彩
381         vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
382         //指定Section轮廓线颜色
383         vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
384        }

385       }

386      }

387      //指定Section标签格式
388      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
389      vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
390      vPiePlot.setForegroundAlpha(0.5f);      
391     }

392    }

393    catch(Exception e)
394    {}
395    break;
396   case 4:
397    vFreeChart=ChartFactory.createPieChart3D(strFreeChartInfo,this.getPieDataset(),true,false,false);
398    try
399    {     
400     vPiePlot3D=(PiePlot3D)vFreeChart.getPlot();
401     if(vPiePlot!=null)
402     {
403      if(bRender)
404      {
405       if(strItemArray.length>0)
406       {
407        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
408        {
409         //指定Section色彩
410         vPiePlot3D.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
411         //指定Section轮廓线颜色
412         vPiePlot3D.setSectionOutlinePaint(0,Color.BLACK);
413        }

414       }

415      }

416      //指定Section标签格式
417      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
418      vPiePlot3D.setLabelGenerator(vStandardPieItemLabelGenerator);
419      //
420      vPiePlot3D.setForegroundAlpha(0.8f);
421     }

422    }

423    catch(Exception e)
424    {}
425    break;
426   case 5:
427    vFreeChart=ChartFactory.createMultiplePieChart3D(strFreeChartInfo,this.getCategoryDataset(),TableOrder.BY_ROW,true,false,false);
428    try
429    {
430     vMultiplePiePlot=(MultiplePiePlot)vFreeChart.getPlot();
431     if(vMultiplePiePlot!=null)
432     {
433      vFreeChartExtend=vMultiplePiePlot.getPieChart();
434      vPiePlot3D=(PiePlot3D)vFreeChartExtend.getPlot();
435      if(bRender)
436      {       
437       if(strItemArray.length>0)
438       {
439        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
440        {
441         //指定Section色彩
442         vPiePlot3D.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
443         //指定Section轮廓线颜色
444         vPiePlot3D.setSectionOutlinePaint(0,Color.BLACK);
445        }

446       }
       
447      }

448      //指定Section标签格式
449      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
450      vPiePlot3D.setLabelGenerator(vStandardPieItemLabelGenerator);
451      //
452      vPiePlot3D.setForegroundAlpha(0.8f);
453     }

454    }

455    catch(Exception e)
456    {}
457    break;
458   case 6:
459    vFreeChart=ChartFactory.createBarChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
460    try
461    {
462     vCategoryPlot=vFreeChart.getCategoryPlot();
463     if(vCategoryPlot!=null)
464     {
465      if(bRender)
466      {
467       vBarRenderer=new BarRenderer();
468       vBarRenderer.setBaseOutlinePaint(Color.GRAY);
469       //设置X轴代表的柱的颜色      
470       if(strCategoryArray.length>0)
471       {
472        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
473        {
474         vBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));        
475         vBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
476        }

477       }

478       vBarRenderer.setItemMargin(0.1);   
479       vCategoryPlot.setRenderer(vBarRenderer);
480       //设置X轴、Y轴的显示位置
481       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
482       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
483      }

484      //设置柱的透明度
485      vCategoryPlot.setForegroundAlpha(0.5f);
486     }

487    }

488    catch(Exception e)
489    {}
490    break;
491   case 7:
492    vFreeChart=ChartFactory.createStackedBarChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
493    try
494    {
495     vCategoryPlot=vFreeChart.getCategoryPlot();
496     if(vCategoryPlot!=null)
497     {
498      if(bRender)
499      {
500       vStackedBarRenderer=new StackedBarRenderer();
501       vStackedBarRenderer.setBaseOutlinePaint(Color.GRAY);
502       //设置X轴代表的柱的颜色
503       if(strCategoryArray.length>0)
504       {
505        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
506        {
507         vStackedBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
508         vStackedBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
509        }

510       }
      
511   
512       vCategoryPlot.setRenderer(vStackedBarRenderer);       
513       //设置X轴、Y轴的显示位置
514       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
515       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
516      }

517      //设置柱的透明度
518      vCategoryPlot.setForegroundAlpha(0.5f);
519     }

520    }

521    catch(Exception e)
522    {}
523    break;
524   case 8:
525    vFreeChart=ChartFactory.createBarChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
526    try
527    {
528     vCategoryPlot=vFreeChart.getCategoryPlot();
529     if(vCategoryPlot!=null)
530     {
531      if(bRender)
532      {
533       vBarRenderer3D=new BarRenderer3D();
534       vBarRenderer3D.setBaseOutlinePaint(Color.GRAY);
535       //设置X轴代表的柱的颜色
536       if(strCategoryArray.length>0)
537       {
538        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
539        {
540         vBarRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));        
541         vBarRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
542        }

543       }

544       vBarRenderer3D.setItemMargin(0.1);
545   
546       vCategoryPlot.setRenderer(vBarRenderer3D);       
547       //设置X轴、Y轴的显示位置
548       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
549       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
550      }

551      //设置柱的透明度
552      vCategoryPlot.setForegroundAlpha(0.8f);
553     }

554    }

555    catch(Exception e)
556    {}
557    break;
558   case 9:
559    vFreeChart=ChartFactory.createStackedBarChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
560    try
561    {
562     vCategoryPlot=vFreeChart.getCategoryPlot();
563     if(vCategoryPlot!=null)
564     {
565      if(bRender)
566      {
567       vStackedBarRenderer3D=new StackedBarRenderer3D();
568       vStackedBarRenderer3D.setBaseOutlinePaint(Color.GRAY);
569       //设置X轴代表的柱的颜色
570       if(strCategoryArray.length>0)
571       {
572        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
573        {
574         vStackedBarRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
575         vStackedBarRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
576        }

577       }
      
578   
579       vCategoryPlot.setRenderer(vStackedBarRenderer3D);
580       
581       //设置X轴、Y轴的显示位置
582       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
583       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
584      }

585      //设置柱的透明度
586      vCategoryPlot.setForegroundAlpha(0.8f);
587     }

588    }

589    catch(Exception e)
590    {}    
591    break;
592   case 10:
593    vFreeChart=ChartFactory.createAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getDefaultCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
594    try
595    {
596     vCategoryPlot=vFreeChart.getCategoryPlot();
597     if(vCategoryPlot!=null)
598     {
599      if(bRender)
600      {
601       vStackedAreaRenderer=new StackedAreaRenderer();
602       vStackedAreaRenderer.setBaseOutlinePaint(Color.GRAY);
603       //设置区域颜色
604       if(strItemArray.length>0)
605       {
606        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
607        {
608         vStackedAreaRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
609         vStackedAreaRenderer.setSeriesOutlinePaint(0,Color.BLACK);
610        }

611       }
      
612   
613       vCategoryPlot.setRenderer(vStackedAreaRenderer);
614       
615       //设置X轴、Y轴的显示位置
616       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
617       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
618      }

619      //设置区域透明度
620      vCategoryPlot.setForegroundAlpha(0.5f);
621      //背景表格线
622      vCategoryPlot.setDomainGridlinesVisible(true);
623     }

624    }

625    catch(Exception e)
626    {}
627    break;
628   case 11:
629    vFreeChart=ChartFactory.createStackedAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
630    try
631    {
632     vCategoryPlot=vFreeChart.getCategoryPlot();
633     if(vCategoryPlot!=null)
634     {
635      if(bRender)
636      {
637       vStackedAreaRenderer=new StackedAreaRenderer();
638       vStackedAreaRenderer.setBaseOutlinePaint(Color.GRAY);
639       //设置区域颜色
640       if(strCategoryArray.length>0)
641       {
642        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
643        {
644         vStackedAreaRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
645         vStackedAreaRenderer.setSeriesOutlinePaint(0,Color.BLACK);
646        }

647       }
      
648   
649       vCategoryPlot.setRenderer(vStackedAreaRenderer);
650       
651       //设置X轴、Y轴的显示位置
652       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
653       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
654      }

655      //设置区域透明度
656      vCategoryPlot.setForegroundAlpha(0.5f);
657     }

658    }

659    catch(Exception e)
660    {}    
661    break;
662   case 12:
663    vFreeChart=ChartFactory.createLineChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
664    try
665    {
666     vCategoryPlot=vFreeChart.getCategoryPlot();
667     if(vCategoryPlot!=null)
668     {
669      vLineAndShapeRenderer=new LineAndShapeRenderer();
670      vLineAndShapeRenderer.setBaseOutlinePaint(Color.GRAY);
671      if(bRender)
672      {       
673       //设置线条颜色
674       if(strCategoryArray.length>0)
675       {
676        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
677        {
678         vLineAndShapeRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
679         vLineAndShapeRenderer.setSeriesOutlinePaint(0,Color.BLACK);
680        }

681       }
       
682       
683       //设置X轴、Y轴的显示位置
684       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
685       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
686      }

687      vCategoryPlot.setRenderer(vLineAndShapeRenderer);
688      //背景表格线
689      vCategoryPlot.setDomainGridlinesVisible(true);
690     }

691    }

692    catch(Exception e)
693    {}    
694    break;
695   case 13:
696    vFreeChart=ChartFactory.createLineChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
697    try
698    {
699     vCategoryPlot=vFreeChart.getCategoryPlot();
700     if(vCategoryPlot!=null)
701     {
702      vLineRenderer3D=new LineRenderer3D();
703      vLineRenderer3D.setBaseOutlinePaint(Color.GRAY);
704      if(bRender)
705      {       
706       //设置线面颜色
707       if(strCategoryArray.length>0)
708       {
709        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
710        {
711         vLineRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
712         vLineRenderer3D.setSeriesFillPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
713         vLineRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
714        }

715       }

716       //设置X轴、Y轴的显示位置
717       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
718       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
719      }

720      vCategoryPlot.setRenderer(vLineRenderer3D);
721      //背景表格线
722      vCategoryPlot.setDomainGridlinesVisible(true);
723     }

724    }

725    catch(Exception e)
726    {}
727    break;
728   case 14:
729    vFreeChart=ChartFactory.createGanttChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getIntervalCategoryDataset(),true,false,false);
730    break;
731   case 15:
732    vFreeChart=ChartFactory.createWaterfallChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
733    try
734    {
735     vCategoryPlot=vFreeChart.getCategoryPlot();
736     if(vCategoryPlot!=null)
737     {
738      vWaterfallBarRenderer=new WaterfallBarRenderer();
739      vWaterfallBarRenderer.setBaseOutlinePaint(Color.GRAY);
740      if(bRender)
741      {       
742       //设置柱颜色
743       if(strCategoryArray.length>0)
744       {
745        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
746        {
747         vWaterfallBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
748         vWaterfallBarRenderer.setSeriesFillPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
749         vWaterfallBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
750        }

751       }

752       //设置X轴、Y轴的显示位置
753       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
754       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
755      }

756      vCategoryPlot.setRenderer(vWaterfallBarRenderer);
757      //背景表格线
758      vCategoryPlot.setDomainGridlinesVisible(true);
759      //设置区域透明度
760      vCategoryPlot.setForegroundAlpha(0.5f);
761     }

762    }

763    catch(Exception e)
764    {}
765    break;
766   case 16:
767    vFreeChart=ChartFactory.createPolarChart(strFreeChartInfo,this.getXYDataset(),true,false,false);
768    break;
769   case 17:
770    vFreeChart=ChartFactory.createScatterPlot(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
771    break;
772   case 18:
773    vFreeChart=ChartFactory.createXYBarChart(strFreeChartInfo,strFreeChartXInfo,false,strFreeChartYInfo,this.getIntervalXYDataset(),PlotOrientation.VERTICAL,true,false,false);
774    break;
775   case 19:
776    vFreeChart=ChartFactory.createXYAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
777    break;
778   case 20:
779    vFreeChart=ChartFactory.createStackedXYAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getTableXYDataset(),PlotOrientation.VERTICAL,true,false,false); 
780    break;
781   case 21:
782    vFreeChart=ChartFactory.createXYLineChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
783    try
784    {
785     vXYPlot=(XYPlot)vFreeChart.getXYPlot();
786     if(vXYPlot!=null)
787     {
788      vXYItemRenderer=vXYPlot.getRenderer();
789      vXYItemRenderer.setBaseOutlinePaint(Color.GRAY);
790      if(bRender)
791      {       
792       //设置线面颜色
793       if(strItemArray.length>0)
794       {
795        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
796        {
797         vXYItemRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
798         vXYItemRenderer.setSeriesOutlinePaint(0,Color.BLACK);
799        }

800       }
       
801       //设置X轴、Y轴的显示位置
802       vXYPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
803       vXYPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
804      }

805      //设置线条颜色
806      vXYItemRenderer.setPaint(new Color(0,100,255));
807      vXYPlot.setRenderer(vXYItemRenderer);
808      //背景表格线
809      vXYPlot.setDomainGridlinesVisible(true);
810     }

811    }

812    catch(Exception e)
813    {}    
814    break;
815   case 22:
816    vFreeChart=ChartFactory.createXYStepChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
817    break;
818   case 23:
819    vFreeChart=ChartFactory.createXYStepAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
820    break;
821   case 24:
822    vFreeChart=ChartFactory.createTimeSeriesChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),true,false,false);
823    try
824    {
825     vXYPlot=(XYPlot)vFreeChart.getXYPlot();
826     if(vXYPlot!=null)
827     {
828      vXYItemRenderer=vXYPlot.getRenderer();
829      vXYItemRenderer.setBaseOutlinePaint(Color.GRAY);
830      if(bRender)
831      {
832       //设置线面颜色
833       if(strItemArray.length>0)
834       {
835        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
836        {
837         vXYItemRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
838         vXYItemRenderer.setSeriesOutlinePaint(0,Color.BLACK);
839        }

840       }

841       //设置X轴、Y轴的显示位置
842       vXYPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
843       vXYPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
844       
845       //设置线条颜色
846       vXYItemRenderer.setPaint(new Color(0,100,255));
847       vXYPlot.setRenderer(vXYItemRenderer);
848      }
      
849      //背景表格线
850      vXYPlot.setDomainGridlinesVisible(true);
851      
852      SimpleDateFormat vSimpleDateFormat=null;
853      if(strTimeAxis.equals("Hour"))
854      {
855       vSimpleDateFormat=new SimpleDateFormat("dd日HH点");
856      }

857      else      
858      {      
859       vSimpleDateFormat=new SimpleDateFormat("MM月dd日");
860      }

861           vDateAxis=(DateAxis)vXYPlot.getDomainAxis();
862          vDateAxis.setDateFormatOverride(vSimpleDateFormat);
863     }

864    }

865    catch(Exception e)
866    {}
867    break;
868   case 25:
869    vFreeChart=ChartFactory.createCandlestickChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
870    break;
871   case 26:
872    vFreeChart=ChartFactory.createHighLowChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
873    break;
874   case 27:
875    vFreeChart=ChartFactory.createHighLowChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
876    break;
877   case 28:
878    vFreeChart=ChartFactory.createSignalChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getSignalsDataset(),true);
879    break;
880   case 29:
881    vFreeChart=ChartFactory.createBubbleChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYZDataset(),PlotOrientation.VERTICAL,true,false,false);
882    break;
883   case 30:
884    vFreeChart=ChartFactory.createHistogram(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getIntervalXYDataset(),PlotOrientation.VERTICAL,true,false,false);
885    break;
886   case 31:
887    vFreeChart=ChartFactory.createBoxAndWhiskerChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getBoxAndWhiskerXYDataset(),true);
888    break;
889   case 32:
890    vFreeChart=ChartFactory.createWindPlot(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getWindDataset(),true,false,false);
891    break;
892   case 33:
893    vFreeChart=ChartFactory.createWaferMapChart(strFreeChartInfo,this.getWaferMapDataset(),PlotOrientation.VERTICAL,true,false,false);       
894    break;
895   default:
896    vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),true,false,false);
897    break;
898  }

899  //图表背景颜色
900  vFreeChart.setBackgroundPaint(new Color(212,234,243));
901 }

902 public JFreeChart getChart()
903 {
904  return this.vFreeChart;
905 }

906 public void createImageFile(int iWidth,int iHeight,HttpSession session)
907 {
908  try
909  {
910   java.util.Date vDate=new java.util.Date(System.currentTimeMillis());
911   java.text.SimpleDateFormat vSimpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
912   java.util.Date vFileDate=null;
913   File[] vFileList=(new File(session.getServletContext().getRealPath("/"))).listFiles();   
914   for(int kIndex=0;kIndex<vFileList.length;kIndex++)
915   {
916    if(vFileList[kIndex].isFile())
917    {     
918     if(vFileList[kIndex].getName().indexOf(".jpg")!=-1)
919     {
920      vFileDate=vSimpleDateFormat.parse(vFileList[kIndex].getName().substring(0,vFileList[kIndex].getName().length()-4));
921      //十分钟前的临时文件删除
922      if(vDate.getTime()-vFileDate.getTime()>60*10*1000)
923      {
924       vFileList[kIndex].delete();
925      }

926     }

927    }

928   }
   
929   String strTime=vSimpleDateFormat.format(vDate);
930   strFileName=session.getServletContext().getRealPath("/")+"\\"+strTime+".jpg";
931   ChartUtilities.saveChartAsJPEG(new File(strFileName),100,this.getChart(),iWidth,iHeight);
932   strFileName="/"+getApplicationName(session.getServletContext().getRealPath("/"))+"/"+strTime+".jpg";
933  }

934  catch(Exception e)
935  {}
936 }

937 public String getImageFile()
938 {
939  return this.strFileName;
940 }

941 public String getApplicationName(String strRealPath)
942 {
943  String[] strAppArray=strRealPath.split("\\\\");
944  return strAppArray[strAppArray.length-1];
945 }

946 public boolean getRender()
947 {
948  return bRender;
949 }

950 public void setRender(boolean bRender)
951 {
952  this.bRender=bRender;
953 }

954 public String getTimeAxis()
955 {
956  return this.strTimeAxis;
957 }

958 public void setTimeAxis(String strTimeAxis)
959 {
960  this.strTimeAxis=strTimeAxis;
961 }

962}
963
964
965

JSP应用示例

 1<%@ page contentType="text/html; charset=gb2312"%>
 2<%@ page import="java.io.*"%>
 3<jsp:useBean id="FreeChart" scope="session" class="com.function.FreeChart"/>
 4<head>
 5<meta http-equiv="Pragma" content="no-cache">
 6<meta http-equiv="Cache-Control" content="no-cache">
 7<meta http-equiv="Expires" content="0">
 8</head>
 9<link rel="stylesheet" type="text/css" href="/defineOA/css/default.css">
10<%
11 //初始化
12 //考虑图形中是否直接链接到统计查询表格
13 String strTimeList="";
14 String strMultipleTimeList="";
15 String strCategoryList="";
16 String strData="";
17 String strMultipleData="";
18
19 strTimeList="2005-05-01 00:00:00,2005-05-02 05:00:00,2005-05-03 10:00:00,2005-05-04 15:00:00,2005-05-05 20:00:00";
20 strMultipleTimeList="2005-05-01 00:00:00,2005-05-02 05:00:00,2005-05-03 10:00:00,2005-05-04 15:00:00,2005-05-05 20:00:00";
21 strCategoryList="JSP工作量,Servlet工作量,Bean工作量";
22 strData="1,2,3,4,5";
23 strMultipleData="1,2,3,4,5#2,3,4,5,6#3,4,5,6,7";
24 
25 FreeChart.createDataset(strMultipleTimeList,strCategoryList,strMultipleData);
26 FreeChart.createDataset(strTimeList,strData);
27 FreeChart.setRender(true);
28 FreeChart.setTimeAxis("Hour");
29 if(request.getParameter("ChartType")==null)
30 {
31  FreeChart.createChart(1,"默认统计图","时间轴","统计值轴");
32 }
33 else
34 {
35  FreeChart.createChart(Integer.parseInt(request.getParameter("ChartType")),"统计图","时间轴","统计值轴");
36 }
37 FreeChart.createImageFile(800,400,session);
38
39 response.setHeader("Pragma","No-cache");
40 response.setDateHeader("Expires",0);
41 response.setHeader("Cache-Control","no-cache");
42
%>
43<%
44 for(int iIndex=0;iIndex<FreeChart.vChartTypeArray.length;iIndex++)
45 {
46
%>
47<input type=button class="GeneralButton" value="<%=FreeChart.vChartTypeArray[iIndex]%>" onclick="window.location='/defineOA/test/chart.jsp?ChartType=<%=iIndex+1%>';">
48<%
49 }
50
%>
51<br>
52<fieldset>
53<legend>图表</legend>
54<!--class="GeneralImage"-->
55<br>
56<center>
57<table>
58  <tr>
59    <td style="border:1 solid #3399CC;"><img src="<%=FreeChart.getImageFile()%>"></td>
60  </tr>
61</table>
62</center>
63<br>
64</fieldset>
65
66也可以将应用继续用Bean封装.
67
68
posted on 2007-06-28 17:51 冰封的爱 阅读(1636) 评论(0)  编辑  收藏 所属分类: 开源项目

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(3)

随笔档案

文章分类

文章档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜