首页>代码>Java开源绘制图形组件jfreechart>/JfreechartTest/src/smart/jrsoft/chart/DrawGant.java
package smart.jrsoft.chart;
import java.awt.Color;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.gantt.TaskSeriesCollection;
import javax.servlet.http.*;
/**
 * @author sychallenger
 *
 */
public class DrawGant
{

	private static DrawGant instance = null;	//画干特图的实例
	private String title="干特图实例";			//干特图标题
	private String XTitle="任务进度";				//干特图横坐标题目
	private String YTitle="任务";				//干特图纵坐标题目
	private int width=800;						//图片宽度
	private int height=450;						//图片高度
	private String fileName="d:\\temp.jpg";		//暂时保存的图片名称
	private Color bgColor=Color.white;			//图片背景颜色
	private Color foreColor=Color.lightGray;	//图片背景颜色
	private Color spaceLineColor=Color.white;	//图片背景颜色

	private TaskList taskList=null;				//任务序列集
	private TaskObject taskObject=null;			//具体任务对象
	private TaskObject taskChildObject=null;	//添加子任务
	FileOutputStream fosJpg = null;				//生成干特图的输出流

	TaskSeriesCollection tc = new TaskSeriesCollection(); //任务序列集合


	/**
	 * 设置初始化参数
	 */
	public void init(){
		setTitle("干特图演示");
		setXTitle("任务进度");
		setYTitle("任务");
		setWidth(800);
		setHeight(450);
		setFileName("d:\\temp.jpg");
		setBgColor(Color.white);
		setForeColor(Color.lightGray);
		setSpaceLineColor(Color.white);
	}


	/**
	 * 恢复默认值
	 */
	public void reset(){
		init();
	}


	/**
	 * 设置干特图标题
	 * @param 生成的干特图的标题
	 */
	public void setTitle(String title){
		this.title=title;
	}


	/**
	 * 生成干特图的横坐标标题
	 * @param xTitle 横坐标标题
	 */
	public void setXTitle(String xTitle){
		this.XTitle=xTitle;
	}


	/**
	 * 生成干特图的纵坐标标题
	 * @param yTitle 纵坐标标题
	 */
	public void setYTitle(String yTitle){
		this.YTitle=yTitle;
	}


	/**
	 * 生成干特图的宽度
	 * @param width	干特图的宽度
	 */
	public void setWidth(int width){
		this.width=width;
	}


	/**
	 * 生成干特图的高度
	 * @param height干特图的高度
	 */
	public void setHeight(int height){
		this.height=height;
	}


	/**
	 * 生成干特图的文件名称
	 * @param fileName 干特图的文件名称
	 */
	private void setFileName(String fileName){
		this.fileName=fileName;
	}


	/**
	 * 单态模式生成类对象
	 * @return 该类的一个对象
	 */

	public static synchronized DrawGant getInstance() {
		if (instance==null)
		instance=new DrawGant();
		return instance;
	}


	/**
	 * 设置背景颜色
	 * @param color 背景颜色
	 */
	public void setBgColor(Color color){
		this.bgColor=color;
	}


	/**
	 * 设置背景颜色
	 * @param red   red色素值
	 * @param green green色素值
	 * @param blue  blue色素值
	 */
	public void setBgColor(int red,int green,int blue){
		this.bgColor=new Color(red,green,blue);
	}


	/**
	 * 改变背景颜色
	 * @param str  背景颜色描述 比如:BLACK black blue Blue 等
	 */
	public void setBgcolor(String str){
		this.bgColor=ChangeColor.getColor(str);
	}


	/**
	 * 设置绘干特图区域的背景颜色
	 * @param color 绘干特图区域的背景颜色
	 */
	public void setForeColor(Color color){
		this.foreColor=color;
	}


	/**
	 * 设置绘干特图区域的背景颜色
	 * @param red   red色素值
	 * @param green green色素值
	 * @param blue  blue色素值
	 */
	public void setForeColor(int red,int green,int blue){
		this.foreColor=new Color(red,green,blue);
	}

	/**
	 * 设置绘干特图区域的背景颜色
	 * @param str  绘干特图区域的背景颜色 比如:BLACK black blue Blue 等
	 */
	public void setForeColor(String str){
		this.foreColor=ChangeColor.getColor(str);
	}


	/**
	 * 设置绘干特图间隔线的颜色
	 * @param red   red色素值
	 * @param green green色素值
	 * @param blue  blue色素值
	 */
	public void setSpaceLineColor(int red,int green,int blue){
		this.spaceLineColor=new Color(red,green,blue);
	}


	/**
	 * 设置间隔线的颜色
	 * @param color 间隔线的颜色
	 */
	public void setSpaceLineColor(Color color){
		this.spaceLineColor=color;
	}

	/**
	 * 设置间隔线的颜色
	 * @param str  间隔线的颜色 比如:BLACK black blue Blue 等
	 */
	public void setSpaceLineColor(String str){
		this.spaceLineColor=ChangeColor.getColor(str);
	}


	/**
	 * 生成日期
	 * @param i 日
	 * @param j 月
	 * @param k 年
	 * @return 日期 Date类型
	 */
	public static Date date(int i, int j, int k)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set(k, j, i);
        Date date1 = calendar.getTime();
        return date1;
    }
	/**
	 * 设置任务集
	 * @param str 任务集名称
	 */
	public void setTaskList(String str){
		if(this.taskList!=null){
			this.taskList=null;
		}
		this.taskList=new TaskList(str);
	}
	/**
	 * 设置任务对象
	 * @param str       任务名称
	 * @param startDate 任务开始时间
	 * @param endDate   任务结束时间
	 * @param value     任务完成百分比 (如0.8表示完成了80%)
	 */
	public void setTaskObject(String str,Date startDate,Date endDate,double value){
		if(this.taskObject!=null){
			this.taskObject=null;
		}
		this.taskObject=new TaskObject(str,startDate, endDate);
		this.taskObject.setPercentComplete(value);
	}
	/**
	 * 给当前任务对象添加子任务
	 * @param str		子任务名称
	 * @param startDate 子任务开始时间
	 * @param endDate   子任务结束时间
	 * @param value		子任务完成百分比 (如0.8表示完成了80%)
	 */
	public void setChildTask(String str ,Date startDate ,Date endDate, double value){
		if(this.taskChildObject!=null){
			this.taskChildObject=null;
		}
		this.taskChildObject=new TaskObject(str,startDate,endDate);
		this.taskChildObject.setPercentComplete(value);
	}
	/**
	 * 设置任务对象
	 * @param str       任务名称
	 * @param startDate 任务开始时间
	 * @param endDate   任务结束时间
	 */
	public void setTaskObject(String str,Date startDate,Date endDate){
		if(this.taskObject!=null){
			this.taskObject=null;
		}
		this.taskObject=new TaskObject(str,startDate, endDate);

	}
	/**
	 * 给当前任务对象添加子任务
	 * @param str		子任务名称
	 * @param startDate 子任务开始时间
	 * @param endDate   子任务结束时间
	 */
	public void setChildTask(String str ,Date startDate ,Date endDate){
		if(this.taskChildObject!=null){
			this.taskChildObject=null;
		}
		this.taskChildObject=new TaskObject(str,startDate,endDate);
	}
	/**
	 * 添加要绘干特图的任务列表
	 */
	public void addTaskList(){
		tc.add(taskList.getTaskSeries());
	}

	/**
	 * 添加任务对象
	 * @param str       任务名称
	 * @param startDate 任务开始时间
	 * @param endDate   任务结束时间
	 * @param value     任务完成百分比 (如0.8表示完成了80%)
	 * @return 成功 true 失败 false
	 */

	public boolean addTask(String str,Date startDate,Date endDate,double value){
		if(this.taskList==null){
			return false;
		}
		this.setTaskObject(str,startDate,endDate,value);
		this.taskList.add(this.taskObject);
		return true;
	}
	/**
	 * 添加任务对象
	 * @param str       任务名称
	 * @param startDate 任务开始时间
	 * @param endDate   任务结束时间
	 * @return 成功 true 失败 false
	 */
	public boolean addTask(String str,Date startDate,Date endDate){
		if(this.taskList==null){
			return false;
		}
		this.setTaskObject(str,startDate,endDate);
		this.taskList.add(this.taskObject);
		return true;
	}
	/**
	 * 给当前任务对象添加子任务
	 * @param str		子任务名称
	 * @param startDate 子任务开始时间
	 * @param endDate   子任务结束时间
	 * @param value		子任务完成百分比 (如0.8表示完成了80%)
	 */
	public boolean addChildTask(String str,Date startDate,Date endDate,double value){
		if(this.taskObject==null){
			return false;
		}
		this.setChildTask(str,startDate,endDate,value);
		this.taskObject.addSubtask(this.taskChildObject);
		return true;
	}
	/**
	 * 给当前任务对象添加子任务
	 * @param str		子任务名称
	 * @param startDate 子任务开始时间
	 * @param endDate   子任务结束时间
	 */
	public boolean addChildTask(String str,Date startDate,Date endDate){
		if(this.taskObject==null){
			return false;
		}
		this.setChildTask(str,startDate,endDate);
		this.taskObject.addSubtask(this.taskChildObject);
		return true;
	}
	public boolean remove(TaskList taskList){
		try{
			tc.remove(taskList.getTaskSeries());
		}catch(Exception e){
			return false;
		}
		return true;
	}


	/**
	 * 生成干特图
	 * @param fileName  保存干特图的文件名称  文件名称为(使用路径为): d:\\web\test.jpg
	 * @return 成功:true 失败:false
	 */
        public boolean saveAbs(String fileName) {
          if (fileName != null) {
            this.setFileName(fileName);
          }

          JFreeChart chart = ChartFactory.createGanttChart(this.title, this.YTitle,
              this.XTitle, tc, true, true, false);
          chart.setBackgroundPaint(this.bgColor); //设置背景颜色
          CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
          categoryplot.setBackgroundPaint(this.foreColor);
          categoryplot.setRangeGridlinePaint(this.spaceLineColor);
          categoryplot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10F);
          categoryplot.getDomainAxis().setMaximumCategoryLabelLines(1);
          categoryplot.getDomainAxis().setCategoryMargin(0.1);

          CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
          categoryitemrenderer.setSeriesPaint(0, Color.blue);
          try {
            fosJpg = new FileOutputStream(fileName);
            ChartUtilities.writeChartAsJPEG(fosJpg, 100, chart, this.width,
                                            this.height, null);

          }
          catch (Exception e) {}
          finally {
            this.tc.removeAll();
            try {
              fosJpg.close();
            }
            catch (Exception e) {}
          }
          return true;
        }


        /**
        * 保存生成的干特图
        * @param request   在jsp页面中的request对象 用于获取文件路径
        * @param fileName   保存文件名称 文件名称必须使用站点的绝对路径 如 : “/admin/test.jpg"
        * @return true 成功  false 失败
        */
        public boolean saveWebFile(HttpServletRequest request, String fileName) {
          if (fileName != null) {
            this.setFileName(fileName);
          }
           fileName = request.getRealPath("") + fileName;
          JFreeChart chart = ChartFactory.createGanttChart(this.title, this.YTitle,
              this.XTitle, tc, true, true, false);
          chart.setBackgroundPaint(this.bgColor); //设置背景颜色
          CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
          categoryplot.setBackgroundPaint(this.foreColor);
          categoryplot.setRangeGridlinePaint(this.spaceLineColor);
          categoryplot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10F);
          categoryplot.getDomainAxis().setMaximumCategoryLabelLines(1);
          categoryplot.getDomainAxis().setCategoryMargin(0.1);

          CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
          categoryitemrenderer.setSeriesPaint(0, Color.blue);
          try {
            fosJpg = new FileOutputStream(fileName);
            ChartUtilities.writeChartAsJPEG(fosJpg, 100, chart, this.width,
                                            this.height, null);

          }
          catch (Exception e) {}
          finally {
            this.tc.removeAll();
            try {
              fosJpg.close();
            }
            catch (Exception e) {}
          }
          return true;

        }

    public static void main(String args[])
    {
    	//1.生成绘干特图的类对象
    	DrawGant gant=DrawGant.getInstance();
    	//2.添加任务集
    	gant.setTaskList("任务序列1");
    	gant.addTask("任务1",DrawGant.date(1,4,2005),DrawGant.date(3,4,2005),0.8);
    	gant.addTask("任务2",DrawGant.date(3,4,2005),DrawGant.date(5,4,2005),1.0);
    	gant.addTask("任务3",DrawGant.date(5,4,2005),DrawGant.date(8,4,2005),0.8);
    	gant.addChildTask("子任务1",DrawGant.date(5,4,2005),DrawGant.date(6,4,2005),0.8);
    	gant.addChildTask("子任务2",DrawGant.date(7,4,2005),DrawGant.date(8,4,2005),0.8);

    	gant.addTaskList();

    	gant.setTaskList("任务序列2");
    	gant.addTask("任务11",DrawGant.date(1,4,2005),DrawGant.date(3,4,2005),0.8);
    	gant.addTask("任务22",DrawGant.date(3,4,2005),DrawGant.date(5,4,2005),1.0);
    	gant.addTask("任务33",DrawGant.date(5,4,2005),DrawGant.date(8,4,2005),0.8);
    	gant.addChildTask("子任务11",DrawGant.date(5,4,2005),DrawGant.date(6,4,2005),0.8);
    	gant.addChildTask("子任务22",DrawGant.date(7,4,2005),DrawGant.date(8,4,2005),0.8);
      	gant.addTaskList();
      	//3.设置图片属性
    	gant.init();
    	gant.setBgColor(Color.white);
    	gant.setForeColor(Color.lightGray);
    	gant.setSpaceLineColor(Color.white);
    	gant.setHeight(800);
    	gant.setHeight(450);
    	gant.setTitle("干特图演示");
    	gant.setXTitle("干特图横坐标标题");
    	gant.setYTitle("干特图纵坐标标题");
    	//4.保存图片
    	gant.saveAbs("e:\\gant.jpg");

	}
}


最近下载更多
lironggang  LV38 2023年3月20日
杠上炮  LV6 2022年6月28日
xcj456  LV8 2020年9月12日
chaoy_nx  LV8 2020年2月24日
s222222  LV4 2019年5月7日
zjjhzjb  LV14 2019年2月12日
弹簧木偶  LV10 2018年9月27日
蓝色鸢尾  LV2 2018年8月23日
kuizhaoyi  LV1 2018年6月13日
mxl165856  LV12 2017年12月11日
最近浏览更多
fesfefe  LV13 2023年11月1日
dsadasdwf  LV12 2023年7月7日
lironggang  LV38 2023年3月20日
东北人  LV12 2022年9月20日
杠上炮  LV6 2022年6月28日
微信网友_5957378031800320  LV3 2022年5月18日
小爷葛优躺  LV2 2021年6月22日
Ciaoss  LV1 2021年6月7日
andy xiao2222222  LV9 2021年6月2日
fangzhicheng  LV1 2021年4月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友