`
5aijava
  • 浏览: 417531 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JFreeChart完全攻略示例各种方法和属性

阅读更多
封装FreeChart的Bean

FreeChart.java

package com.function;
/*基于JFreeChart 1.0.0 Pre2.jar*/
import java.io.*;
import java.awt.Font;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.Calendar;
import javax.servlet.http.HttpSession;

import org.jfree.data.*;
import org.jfree.chart.*;
//数据集
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.TableXYDataset;
import org.jfree.data.xy.XYZDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.WindDataset;
import org.jfree.data.general.WaferMapDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.xy.SignalsDataset;
import org.jfree.data.statistics.BoxAndWhiskerXYDataset;

//数据集辅助
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

//图集
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.XYPlot;


//表现形式集
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer3D;
import org.jfree.chart.renderer.category.StackedAreaRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.LineRenderer3D;
import org.jfree.chart.renderer.category.WaterfallBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;

//标签
import org.jfree.chart.labels.StandardPieItemLabelGenerator;


//刻度
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.SegmentedTimeline;

import org.jfree.chart.axis.TickUnit;
import org.jfree.chart.axis.TickUnits;
import org.jfree.chart.axis.TickUnitSource;

//通用
import org.jfree.data.general.DatasetUtilities;

//常量
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.util.TableOrder;


public class FreeChart
{
/*定义各类图表所需的Dataset*/
private DefaultCategoryDataset vDefaultCategoryDataset=null;
private CategoryDataset vCategoryDataset=null;
private DefaultPieDataset vDefaultPieDataset=null;
private PieDataset vPieDataset=null;
private XYDataset vXYDataset=null;
private TableXYDataset vTableXYDataset=null;
private XYZDataset vXYZDataset=null;
private IntervalXYDataset vIntervalXYDataset=null;
private WindDataset vWindDataset=null;
private WaferMapDataset vWaferMapDataset=null;
private IntervalCategoryDataset vIntervalCategoryDataset=null;
private OHLCDataset vOHLCDataset=null;
private SignalsDataset vSignalsDataset=null;
private BoxAndWhiskerXYDataset vBoxAndWhiskerXYDataset=null;
/*定义各类图表所需的Dataset辅助*/
TimeSeries vTimeSeries=null;
TimeSeriesCollection vTimeSeriesCollection=null;
/*定义图表*/
private JFreeChart vFreeChart=null;
private JFreeChart vFreeChartExtend=null;
/*映射图片的路径名称*/
private String strFileName=null;

/*定义效果*/
private CategoryPlot vCategoryPlot=null;
private PiePlot vPiePlot=null;
private MultiplePiePlot vMultiplePiePlot=null;
private PiePlot3D vPiePlot3D=null;
private XYPlot vXYPlot=null;

private StandardPieItemLabelGenerator vStandardPieItemLabelGenerator=null;

/*定义刻度*/
private NumberAxis vNumberAxis=null;
private DateAxis vDateAxis=null;
private CategoryAxis vCategoryAxis=null;
private ValueAxis vValueAxis=null;
private SegmentedTimeline vSegmentedTimeline=null;

private BarRenderer vBarRenderer=null;
private BarRenderer3D vBarRenderer3D=null;
private StackedBarRenderer vStackedBarRenderer=null;
private StackedBarRenderer3D vStackedBarRenderer3D=null;
private StackedAreaRenderer vStackedAreaRenderer=null;
private LineAndShapeRenderer vLineAndShapeRenderer=null;
private LineRenderer3D vLineRenderer3D=null;
private WaterfallBarRenderer vWaterfallBarRenderer=null;
private XYItemRenderer vXYItemRenderer=null;

//是否启用效果模式
private boolean bRender=false;

/*定义静态数据*/
private String[] strItemArray=null;
private String[] strMultipleItemArray=null;
private String[] strCategoryArray=null;
private String[] strDataArray=null;
private String[] strMultipleDataArray=null;
private double[][] iMultipleDataArray=null;

/**/

public static String[] vChartTypeArray={"饼图","分离型饼图","三维饼图","复合饼图","三维分离型饼图","簇状柱形图","三维簇状柱形图","堆积柱形图","三维堆积柱形图","面积图","三维面积图","折线图","三维折线图","三维曲面图","三维柱形图","雷达图","散点图","百分比堆积面积图","三维百分比堆积面积图","折线散点图","面积散点图","时间序列图","三维柱形圆锥图","盘高-盘低-收盘图","开盘-盘高-盘低-收盘图","曲面图(俯视框架图)","气泡图","簇状条形图","数据点折线图","无数据点折线散点图","无数据点平滑线散点图"};
public String strTimeAxis="";
public void FreeChart()
{
 
}
public void createDataset(String strMultipleItemList,String strCategoryList,String strMultipleDataList)
{
  strMultipleItemArray=strMultipleItemList.split(",");
  strCategoryArray=strCategoryList.split(",");
  strMultipleDataArray=strMultipleDataList.split("#");
  iMultipleDataArray=new double[strCategoryArray.length][strMultipleItemArray.length];
 
  java.text.SimpleDateFormat vSimpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
  java.text.SimpleDateFormat vSimpleDateFormatToday=new SimpleDateFormat("yyyy-MM-");
  java.util.Date vItemDate=null;
  java.util.Date vTodayDate=null;
  Day vDay=null;
  vTodayDate=new java.util.Date();
  vTimeSeriesCollection=new TimeSeriesCollection();
  vTimeSeriesCollection.setDomainIsPointsInTime(false);
 
  for(int mIndex=0;mIndex<strMultipleDataArray.length;mIndex++)
  {
   String[] strElementArray=strMultipleDataArray[mIndex].split(",");
   if(strTimeAxis.equals("Hour"))
   {
    vTimeSeries=new TimeSeries(strCategoryArray[mIndex],Hour.class);
   }
   else
   {
    vTimeSeries=new TimeSeries(strCategoryArray[mIndex],Day.class);
   }
   //vTimeSeries.clear();
   for(int nIndex=0;nIndex<strElementArray.length;nIndex++)
   {
    iMultipleDataArray[mIndex][nIndex]=Double.parseDouble(strElementArray[nIndex]);
    try
    {
     //vItemDate=vSimpleDateFormat.parse(vSimpleDateFormatToday.format(vTodayDate)+String.valueOf(nIndex+1));
     vItemDate=vSimpleDateFormat.parse(strMultipleItemArray[nIndex]);
     if(strTimeAxis.equals("Hour"))
     {
      vDay=new Day(vItemDate.getDate(),1+vItemDate.getMonth(),1900+vItemDate.getYear());
      vTimeSeries.add(new Hour(vItemDate.getHours(),vDay),Double.parseDouble(strElementArray[nIndex]));
     }
     else
     {
      vTimeSeries.add(new Day(vItemDate.getDate(),1+vItemDate.getMonth(),1900+vItemDate.getYear()),Double.parseDouble(strElementArray[nIndex]));
     }
    
    }
    catch(Exception e)
    {System.out.println(e.getMessage());}
   }  
   vTimeSeriesCollection.addSeries(vTimeSeries); 
  }
  try
  {
   vCategoryDataset=DatasetUtilities.createCategoryDataset(strCategoryArray,strMultipleItemArray,iMultipleDataArray);
   vPieDataset=DatasetUtilities.createPieDatasetForColumn(vCategoryDataset,0);
   vPieDataset=DatasetUtilities.createPieDatasetForRow(vCategoryDataset,0);
   //vWaferMapDataset=(WaferMapDataset)vCategoryDataset;
  
   vTableXYDataset=(TableXYDataset)vTimeSeriesCollection;
   vIntervalXYDataset=(IntervalXYDataset)vTimeSeriesCollection;  
  
   vXYDataset=(XYDataset)vTimeSeriesCollection;
   /*
   vXYZDataset=(XYZDataset)vTimeSeriesCollection;
   //vWaferMapDataset=(WaferMapDataset)vTimeSeriesCollection;
   vWindDataset=(WindDataset)vTimeSeriesCollection;  
   vOHLCDataset=(OHLCDataset)vTimeSeriesCollection;
   vSignalsDataset=(SignalsDataset)vTimeSeriesCollection;
   vBoxAndWhiskerXYDataset=(BoxAndWhiskerXYDataset)vTimeSeriesCollection;
   */
  }
  catch(Exception e)
  {}
}
public void createDataset(String strItemList,String strDataList)
{
  vDefaultCategoryDataset=new DefaultCategoryDataset();
  vDefaultPieDataset=new DefaultPieDataset(); 
   
  strItemArray=strItemList.split(",");
  strDataArray=strDataList.split(",");
  for(int kIndex=0;kIndex<strItemArray.length;kIndex++)
  {
   vDefaultCategoryDataset.addValue(Double.parseDouble(strDataArray[kIndex])," ",strItemArray[kIndex]);
   vDefaultPieDataset.setValue(strItemArray[kIndex],Double.parseDouble(strDataArray[kIndex]));  
  } 
}
public DefaultCategoryDataset getDefaultCategoryDataset()
{
  return this.vDefaultCategoryDataset;
}
public CategoryDataset getCategoryDataset()
{
  return this.vCategoryDataset;
}
public DefaultPieDataset getDefaultPieDataset()
{
  return this.vDefaultPieDataset;
}
public PieDataset getPieDataset()
{
  return this.vPieDataset;
}
public XYDataset getXYDataset()
{
  return this.vXYDataset;
}
public TableXYDataset getTableXYDataset()
{
  return this.vTableXYDataset;
}
public XYZDataset getXYZDataset()
{
  return this.vXYZDataset;
}
public IntervalXYDataset getIntervalXYDataset()
{
  return this.vIntervalXYDataset;
}
public WindDataset getWindDataset()
{
  return this.vWindDataset;
}
public WaferMapDataset getWaferMapDataset()
{
  return this.vWaferMapDataset;
}
public IntervalCategoryDataset getIntervalCategoryDataset()
{
  return this.vIntervalCategoryDataset;
}
public OHLCDataset getOHLCDataset()
{
  return this.vOHLCDataset;
}
public SignalsDataset getSignalsDataset()
{
  return this.vSignalsDataset;
}
public BoxAndWhiskerXYDataset getBoxAndWhiskerXYDataset()
{
  return this.vBoxAndWhiskerXYDataset;
}
/*
iChartType:图表类型
*/
public void createChart(int iChartType,String strFreeChartInfo,String strFreeChartXInfo,String strFreeChartYInfo)
{
  switch(iChartType)
  {
   case 1:   
    vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),true,false,false);
    try
    {
     vPiePlot=(PiePlot)vFreeChart.getPlot();
     if(vPiePlot!=null)
     {
      if(bRender)
      {
       if(strItemArray.length>0)
       {
        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
        {
         //指定Section色彩
         vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
         //指定Section轮廓线颜色
         vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
        }
       }
      }
      //指定Section标签格式
      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
      vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
      //
      vPiePlot.setForegroundAlpha(0.5f);
     }
    }
    catch(Exception e)
    {}
    break;
   case 2:
    vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),this.getPieDataset(),0,false,true,false,false,false,false);
    try
    {
     vPiePlot=(PiePlot)vFreeChart.getPlot();
     if(vPiePlot!=null)
     {
      if(bRender)
      {
       if(strItemArray.length>0)
       {
        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
        {
         //指定Section色彩
         vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
         //指定Section轮廓线颜色
         vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
        }
        //抽取指定块
        vPiePlot.setExplodePercent(0,1.00);
       }
      }
      //指定Section标签格式
      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
      vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
           
      vPiePlot.setForegroundAlpha(0.5f);
     }
    }
    catch(Exception e)
    {}
    break;
   case 3:
    vFreeChart=ChartFactory.createMultiplePieChart(strFreeChartInfo,this.getCategoryDataset(),TableOrder.BY_ROW,true,false,false);
    try
    {
     vMultiplePiePlot=(MultiplePiePlot)vFreeChart.getPlot();
     if(vMultiplePiePlot!=null)
     {
      vFreeChartExtend=vMultiplePiePlot.getPieChart();
      vPiePlot=(PiePlot)vFreeChartExtend.getPlot();
      if(bRender)
      {
       if(strItemArray.length>0)
       {
        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
        {
         //指定Section色彩
         vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
         //指定Section轮廓线颜色
         vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
        }
       }
      }
      //指定Section标签格式
      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
      vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
      vPiePlot.setForegroundAlpha(0.5f);     
     }
    }
    catch(Exception e)
    {}
    break;
   case 4:
    vFreeChart=ChartFactory.createPieChart3D(strFreeChartInfo,this.getPieDataset(),true,false,false);
    try
    {    
     vPiePlot3D=(PiePlot3D)vFreeChart.getPlot();
     if(vPiePlot!=null)
     {
      if(bRender)
      {
       if(strItemArray.length>0)
       {
        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
        {
         //指定Section色彩
         vPiePlot3D.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
         //指定Section轮廓线颜色
         vPiePlot3D.setSectionOutlinePaint(0,Color.BLACK);
        }
       }
      }
      //指定Section标签格式
      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
      vPiePlot3D.setLabelGenerator(vStandardPieItemLabelGenerator);
      //
      vPiePlot3D.setForegroundAlpha(0.8f);
     }
    }
    catch(Exception e)
    {}
    break;
   case 5:
    vFreeChart=ChartFactory.createMultiplePieChart3D(strFreeChartInfo,this.getCategoryDataset(),TableOrder.BY_ROW,true,false,false);
    try
    {
     vMultiplePiePlot=(MultiplePiePlot)vFreeChart.getPlot();
     if(vMultiplePiePlot!=null)
     {
      vFreeChartExtend=vMultiplePiePlot.getPieChart();
      vPiePlot3D=(PiePlot3D)vFreeChartExtend.getPlot();
      if(bRender)
      {      
       if(strItemArray.length>0)
       {
        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
        {
         //指定Section色彩
         vPiePlot3D.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         //指定Section轮廓线颜色
         vPiePlot3D.setSectionOutlinePaint(0,Color.BLACK);
        }
       }      
      }
      //指定Section标签格式
      vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
      vPiePlot3D.setLabelGenerator(vStandardPieItemLabelGenerator);
      //
      vPiePlot3D.setForegroundAlpha(0.8f);
     }
    }
    catch(Exception e)
    {}
    break;
   case 6:
    vFreeChart=ChartFactory.createBarChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      if(bRender)
      {
       vBarRenderer=new BarRenderer();
       vBarRenderer.setBaseOutlinePaint(Color.GRAY);
       //设置X轴代表的柱的颜色     
       if(strCategoryArray.length>0)
       {
        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
        {
         vBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));       
         vBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }
       vBarRenderer.setItemMargin(0.1);  
       vCategoryPlot.setRenderer(vBarRenderer);
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      //设置柱的透明度
      vCategoryPlot.setForegroundAlpha(0.5f);
     }
    }
    catch(Exception e)
    {}
    break;
   case 7:
    vFreeChart=ChartFactory.createStackedBarChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      if(bRender)
      {
       vStackedBarRenderer=new StackedBarRenderer();
       vStackedBarRenderer.setBaseOutlinePaint(Color.GRAY);
       //设置X轴代表的柱的颜色
       if(strCategoryArray.length>0)
       {
        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
        {
         vStackedBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         vStackedBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }     
  
       vCategoryPlot.setRenderer(vStackedBarRenderer);      
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      //设置柱的透明度
      vCategoryPlot.setForegroundAlpha(0.5f);
     }
    }
    catch(Exception e)
    {}
    break;
   case 8:
    vFreeChart=ChartFactory.createBarChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      if(bRender)
      {
       vBarRenderer3D=new BarRenderer3D();
       vBarRenderer3D.setBaseOutlinePaint(Color.GRAY);
       //设置X轴代表的柱的颜色
       if(strCategoryArray.length>0)
       {
        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
        {
         vBarRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));       
         vBarRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }
       vBarRenderer3D.setItemMargin(0.1);
  
       vCategoryPlot.setRenderer(vBarRenderer3D);      
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      //设置柱的透明度
      vCategoryPlot.setForegroundAlpha(0.8f);
     }
    }
    catch(Exception e)
    {}
    break;
   case 9:
    vFreeChart=ChartFactory.createStackedBarChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      if(bRender)
      {
       vStackedBarRenderer3D=new StackedBarRenderer3D();
       vStackedBarRenderer3D.setBaseOutlinePaint(Color.GRAY);
       //设置X轴代表的柱的颜色
       if(strCategoryArray.length>0)
       {
        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
        {
         vStackedBarRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         vStackedBarRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }     
  
       vCategoryPlot.setRenderer(vStackedBarRenderer3D);
      
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      //设置柱的透明度
      vCategoryPlot.setForegroundAlpha(0.8f);
     }
    }
    catch(Exception e)
    {}   
    break;
   case 10:
    vFreeChart=ChartFactory.createAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getDefaultCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      if(bRender)
      {
       vStackedAreaRenderer=new StackedAreaRenderer();
       vStackedAreaRenderer.setBaseOutlinePaint(Color.GRAY);
       //设置区域颜色
       if(strItemArray.length>0)
       {
        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
        {
         vStackedAreaRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
         vStackedAreaRenderer.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }     
  
       vCategoryPlot.setRenderer(vStackedAreaRenderer);
      
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      //设置区域透明度
      vCategoryPlot.setForegroundAlpha(0.5f);
      //背景表格线
      vCategoryPlot.setDomainGridlinesVisible(true);
     }
    }
    catch(Exception e)
    {}
    break;
   case 11:
    vFreeChart=ChartFactory.createStackedAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      if(bRender)
      {
       vStackedAreaRenderer=new StackedAreaRenderer();
       vStackedAreaRenderer.setBaseOutlinePaint(Color.GRAY);
       //设置区域颜色
       if(strCategoryArray.length>0)
       {
        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
        {
         vStackedAreaRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         vStackedAreaRenderer.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }     
  
       vCategoryPlot.setRenderer(vStackedAreaRenderer);
      
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      //设置区域透明度
      vCategoryPlot.setForegroundAlpha(0.5f);
     }
    }
    catch(Exception e)
    {}   
    break;
   case 12:
    vFreeChart=ChartFactory.createLineChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      vLineAndShapeRenderer=new LineAndShapeRenderer();
      vLineAndShapeRenderer.setBaseOutlinePaint(Color.GRAY);
      if(bRender)
      {      
       //设置线条颜色
       if(strCategoryArray.length>0)
       {
        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
        {
         vLineAndShapeRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         vLineAndShapeRenderer.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }      
      
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      vCategoryPlot.setRenderer(vLineAndShapeRenderer);
      //背景表格线
      vCategoryPlot.setDomainGridlinesVisible(true);
     }
    }
    catch(Exception e)
    {}   
    break;
   case 13:
    vFreeChart=ChartFactory.createLineChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      vLineRenderer3D=new LineRenderer3D();
      vLineRenderer3D.setBaseOutlinePaint(Color.GRAY);
      if(bRender)
      {      
       //设置线面颜色
       if(strCategoryArray.length>0)
       {
        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
        {
         vLineRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         vLineRenderer3D.setSeriesFillPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         vLineRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      vCategoryPlot.setRenderer(vLineRenderer3D);
      //背景表格线
      vCategoryPlot.setDomainGridlinesVisible(true);
     }
    }
    catch(Exception e)
    {}
    break;
   case 14:
    vFreeChart=ChartFactory.createGanttChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getIntervalCategoryDataset(),true,false,false);
    break;
   case 15:
    vFreeChart=ChartFactory.createWaterfallChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vCategoryPlot=vFreeChart.getCategoryPlot();
     if(vCategoryPlot!=null)
     {
      vWaterfallBarRenderer=new WaterfallBarRenderer();
      vWaterfallBarRenderer.setBaseOutlinePaint(Color.GRAY);
      if(bRender)
      {      
       //设置柱颜色
       if(strCategoryArray.length>0)
       {
        for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
        {
         vWaterfallBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         vWaterfallBarRenderer.setSeriesFillPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
         vWaterfallBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }
       //设置X轴、Y轴的显示位置
       vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      vCategoryPlot.setRenderer(vWaterfallBarRenderer);
      //背景表格线
      vCategoryPlot.setDomainGridlinesVisible(true);
      //设置区域透明度
      vCategoryPlot.setForegroundAlpha(0.5f);
     }
    }
    catch(Exception e)
    {}
    break;
   case 16:
    vFreeChart=ChartFactory.createPolarChart(strFreeChartInfo,this.getXYDataset(),true,false,false);
    break;
   case 17:
    vFreeChart=ChartFactory.createScatterPlot(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
    break;
   case 18:
    vFreeChart=ChartFactory.createXYBarChart(strFreeChartInfo,strFreeChartXInfo,false,strFreeChartYInfo,this.getIntervalXYDataset(),PlotOrientation.VERTICAL,true,false,false);
    break;
   case 19:
    vFreeChart=ChartFactory.createXYAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
    break;
   case 20:
    vFreeChart=ChartFactory.createStackedXYAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getTableXYDataset(),PlotOrientation.VERTICAL,true,false,false);
    break;
   case 21:
    vFreeChart=ChartFactory.createXYLineChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
    try
    {
     vXYPlot=(XYPlot)vFreeChart.getXYPlot();
     if(vXYPlot!=null)
     {
      vXYItemRenderer=vXYPlot.getRenderer();
      vXYItemRenderer.setBaseOutlinePaint(Color.GRAY);
      if(bRender)
      {      
       //设置线面颜色
       if(strItemArray.length>0)
       {
        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
        {
         vXYItemRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
         vXYItemRenderer.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }      
       //设置X轴、Y轴的显示位置
       vXYPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vXYPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      }
      //设置线条颜色
      vXYItemRenderer.setPaint(new Color(0,100,255));
      vXYPlot.setRenderer(vXYItemRenderer);
      //背景表格线
      vXYPlot.setDomainGridlinesVisible(true);
     }
    }
    catch(Exception e)
    {}   
    break;
   case 22:
    vFreeChart=ChartFactory.createXYStepChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
    break;
   case 23:
    vFreeChart=ChartFactory.createXYStepAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
    break;
   case 24:
    vFreeChart=ChartFactory.createTimeSeriesChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),true,false,false);
    try
    {
     vXYPlot=(XYPlot)vFreeChart.getXYPlot();
     if(vXYPlot!=null)
     {
      vXYItemRenderer=vXYPlot.getRenderer();
      vXYItemRenderer.setBaseOutlinePaint(Color.GRAY);
      if(bRender)
      {
       //设置线面颜色
       if(strItemArray.length>0)
       {
        for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
        {
         vXYItemRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
         vXYItemRenderer.setSeriesOutlinePaint(0,Color.BLACK);
        }
       }
       //设置X轴、Y轴的显示位置
       vXYPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
       vXYPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
      
       //设置线条颜色
       vXYItemRenderer.setPaint(new Color(0,100,255));
       vXYPlot.setRenderer(vXYItemRenderer);
      }     
      //背景表格线
      vXYPlot.setDomainGridlinesVisible(true);
     
      SimpleDateFormat vSimpleDateFormat=null;
      if(strTimeAxis.equals("Hour"))
      {
       vSimpleDateFormat=new SimpleDateFormat("dd日HH点");
      }
      else     
      {     
       vSimpleDateFormat=new SimpleDateFormat("MM月dd日");
      }
           vDateAxis=(DateAxis)vXYPlot.getDomainAxis();
          vDateAxis.setDateFormatOverride(vSimpleDateFormat);
     }
    }
    catch(Exception e)
    {}
    break;
   case 25:
    vFreeChart=ChartFactory.createCandlestickChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
    break;
   case 26:
    vFreeChart=ChartFactory.createHighLowChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
    break;
   case 27:
    vFreeChart=ChartFactory.createHighLowChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
    break;
   case 28:
    vFreeChart=ChartFactory.createSignalChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getSignalsDataset(),true);
    break;
   case 29:
    vFreeChart=ChartFactory.createBubbleChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYZDataset(),PlotOrientation.VERTICAL,true,false,false);
    break;
   case 30:
    vFreeChart=ChartFactory.createHistogram(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getIntervalXYDataset(),PlotOrientation.VERTICAL,true,false,false);
    break;
   case 31:
    vFreeChart=ChartFactory.createBoxAndWhiskerChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getBoxAndWhiskerXYDataset(),true);
    break;
   case 32:
    vFreeChart=ChartFactory.createWindPlot(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getWindDataset(),true,false,false);
    break;
   case 33:
    vFreeChart=ChartFactory.createWaferMapChart(strFreeChartInfo,this.getWaferMapDataset(),PlotOrientation.VERTICAL,true,false,false);      
    break;
   default:
    vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),true,false,false);
    break;
  }
  //图表背景颜色
  vFreeChart.setBackgroundPaint(new Color(212,234,243));
}
public JFreeChart getChart()
{
  return this.vFreeChart;
}
public void createImageFile(int iWidth,int iHeight,HttpSession session)
{
  try
  {
   java.util.Date vDate=new java.util.Date(System.currentTimeMillis());
   java.text.SimpleDateFormat vSimpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
   java.util.Date vFileDate=null;
   File[] vFileList=(new File(session.getServletContext().getRealPath("/"))).listFiles();  
   for(int kIndex=0;kIndex<vFileList.length;kIndex++)
   {
    if(vFileList[kIndex].isFile())
    {    
     if(vFileList[kIndex].getName().indexOf(".jpg")!=-1)
     {
      vFileDate=vSimpleDateFormat.parse(vFileList[kIndex].getName().substring(0,vFileList[kIndex].getName().length()-4));
      //十分钟前的临时文件删除
      if(vDate.getTime()-vFileDate.getTime()>60*10*1000)
      {
       vFileList[kIndex].delete();
      }
     }
    }
   }  
   String strTime=vSimpleDateFormat.format(vDate);
   strFileName=session.getServletContext().getRealPath("/")+"http://www.java-asp.net/java/200601/\\"+strTime+".jpg";
   ChartUtilities.saveChartAsJPEG(new File(strFileName),100,this.getChart(),iWidth,iHeight);
   strFileName="/"+getApplicationName(session.getServletContext().getRealPath("/"))+"/"+strTime+".jpg";
  }
  catch(Exception e)
  {}
}
public String getImageFile()
{
  return this.strFileName;
}
public String getApplicationName(String strRealPath)
{
  String[] strAppArray=strRealPath.split("\\\\");
  return strAppArray[strAppArray.length-1];
}
public boolean getRender()
{
  return bRender;
}
public void setRender(boolean bRender)
{
  this.bRender=bRender;
}
public String getTimeAxis()
{
  return this.strTimeAxis;
}
public void setTimeAxis(String strTimeAxis)
{
  this.strTimeAxis=strTimeAxis;
}
}


JSP应用示例

<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="FreeChart" scope="session" class="com.function.FreeChart"/>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
</head>
<link rel="stylesheet" type="text/css" href="/defineOA/css/default.css">
<%
//初始化
//考虑图形中是否直接链接到统计查询表格
String strTimeList="";
String strMultipleTimeList="";
String strCategoryList="";
String strData="";
String strMultipleData="";

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";
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";
strCategoryList="JSP工作量,Servlet工作量,Bean工作量";
strData="1,2,3,4,5";
strMultipleData="1,2,3,4,5#2,3,4,5,6#3,4,5,6,7";

FreeChart.createDataset(strMultipleTimeList,strCategoryList,strMultipleData);
FreeChart.createDataset(strTimeList,strData);
FreeChart.setRender(true);
FreeChart.setTimeAxis("Hour");
if(request.getParameter("ChartType")==null)
{
  FreeChart.createChart(1,"默认统计图","时间轴","统计值轴");
}
else
{
  FreeChart.createChart(Integer.parseInt(request.getParameter("ChartType")),"统计图","时间轴","统计值轴");
}
FreeChart.createImageFile(800,400,session);

response.setHeader("Pragma","No-cache");
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control","no-cache");
%>
<%
for(int iIndex=0;iIndex<FreeChart.vChartTypeArray.length;iIndex++)
{
%>
<input type=button class="GeneralButton" value="<%=FreeChart.vChartTypeArray[iIndex]%>" onclick="window.location=´/defineOA/test/chart.jsp?ChartType=<%=iIndex+1%>´;">
<%
}
%>
<br>
<fieldset>
<legend>图表</legend>
<!--class="GeneralImage"-->
<br>
<center>
<table>
  <tr>
    <td style="border:1 solid #3399CC;"><img src="<%=FreeChart.getImageFile()%>"></td>
  </tr>
</table>
</center>
<br>
</fieldset>

也可以将应用继续用Bean封装.

分享到:
评论

相关推荐

    JFreeChart示例

    在描述中提到的"集中了大部分的JFreeChart示例",这通常意味着包含了一个示例代码库,展示了JFreeChart的各种功能和用法。这些示例可以帮助初学者快速上手,同时也为有经验的开发者提供了参考和灵感。为了运行这些...

    JfreeChart 示例

    这个示例资源提供了JFreeChart的实践应用,帮助开发者了解如何在Java程序中创建和展示各种图表。 在运行JfreeChart示例之前,我们需要准备以下依赖库: 1. **jcommon-1.0.9.jar**:这是JFreeChart的一个基础库,...

    jfreechart-1.0.13 示例

    这个压缩包“jfreechart-1.0.13 示例”提供了一些示例代码,帮助开发者更好地理解和使用 JFreeChart 库。博文链接(已不可用)可能包含了更详细的使用教程和实践案例。 在 JFreeChart 1.0.13 版本中,以下是一些...

    jfreechart几个示例

    在这个压缩包中,可能包含了一些JFreeChart的示例代码,用于展示如何使用该库来生成各种图表。下面,我们将深入探讨JFreeChart的主要功能和使用方法。 1. **JFreeChart概述** JFreeChart是一个开源项目,广泛应用...

    JFreeChart 指南 & JFreeChart 示例

    JFreeChart 指南 JFreeChart Guide JFreeChart 示例 JFreeChart demo

    JFreeChart 官方例子 JFReeChart Dome

    "JFreeChart 官方例子 JFreeChart Dome" 提供了一系列官方示例,帮助开发者更好地理解和运用JFreeChart库的各种功能。 "ChangeLog" 文件通常记录了软件的更新历史,其中包括了每次版本升级中添加的新特性、修复的...

    jfreechart 1.0.13 示例源码

    这个压缩包包含了名为"study.JFreeChart.demo"的文件,这很可能是一个包含多个示例类的包,展示了JFreeChart的各种功能。 JFreeChart 1.0.13是该库的一个稳定版本,提供了丰富的API和定制选项,使得开发者可以轻松...

    Jfreechart中文示例(含注解说明,个人认为是比较全面的示例了)

    这个示例集提供了详细的中文注解,使得理解和使用JFreeChart变得更加容易。以下是对这些示例的深入解析。 ### 一、柱状图(Bar Chart) 柱状图是数据可视化中最常见的图表之一,常用于比较不同类别的数值。在...

    JFreeChart代码示例及jar包

    JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联

    Jfreechart封装示例

    JFreeChart官方文档和社区论坛提供了丰富的教程和示例代码,帮助开发者理解和使用该库。此外,还可以参考GitHub上的开源项目,了解实际项目中的应用。 总之,"JFreeChart封装示例"是一个很好的起点,帮助开发者...

    JFreeChart 典型图表生成及属性设置示例

    通过本文介绍的`CategoryDataset`示例,读者不仅可以了解如何生成常见的图表,还可以学习到如何设置图表的各种属性,从而提高图表的美观性和可读性。 总之,掌握JFreeChart的使用对于任何需要在Java项目中集成数据...

    jfreechart示例demo与源码

    `jfreechart-1.0.13-demo.src.zip` 文件包含了许多示例代码,覆盖了各种图表类型和自定义选项。这些示例提供了很好的学习资源,你可以逐步分析和运行它们来理解 JFreeChart 的工作原理。 5. **使用 JFreeChart 的...

    jfreechart示例图片

    jfreechart示例图片dfsdfdsfdsfffffffffffffffffffffffdsfsdf

    JfreeChart Demo示例包括所需架包

    通过阅读和运行这些示例,开发者能够快速掌握JFreeChart的基本用法,并且能为自己的项目找到合适的图表类型和定制方法。 在提供的压缩包中,"JFreeChart"很可能包含了JFreeChart库的源码、示例代码和必要的依赖。...

    jFreeChart示例代码,封装成jsp标签

    JFreeChart 是一个强大的 Java 图表库,它提供了各种类型的图表,如柱状图、饼图、线形图、散点图等,适用于报告、数据分析和可视化应用。在这个项目中,开发者已经将 JFreeChart 的功能封装成了 JSP 标签,使得在 ...

    JfreeChart各种图形案例

    在"JfreeChart各种图形案例"这个资源中,包含了多种JFreeChart图形的示例,旨在帮助开发者直观地理解如何在实际项目中应用这些图表。** **一、JFreeChart简介** JFreeChart是Java的一个开源项目,它提供了丰富的...

    jfreechart示例

    - **API参考**:详述了JFreeChart的各个类和方法,帮助开发者深入理解其功能。 - **示例代码解释**:对示例代码进行详细解读,便于学习和复制。 - **常见问题解答**:解答在使用过程中可能会遇到的问题。 总之,这...

    JFreeChart的一个简单例子

    - **自定义样式:** 通过`ChartCustomizer`或直接修改`JFreeChart`和`CategoryPlot`的属性,可以实现更复杂的样式定制。 - **交互功能:** JFreeChart支持用户交互,如点击事件、工具提示等,可以通过监听器来实现...

Global site tag (gtag.js) - Google Analytics