import
java.io.FileOutputStream;
import
java.io.IOException;
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.data.category.IntervalCategoryDataset;
import
org.jfree.data.gantt.Task;
import
org.jfree.data.gantt.TaskSeries;
import
org.jfree.data.gantt.TaskSeriesCollection;
/** */
/** * @author sorunxian * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */
public
class
GanttTest
...
{ public static void main(String[] args) ...{ IntervalCategoryDataset dataset = createSampleDataset(); JFreeChart jfc = ChartFactory.createGanttChart("项目管理系统", "项目各阶段详细实施计划", "项目周期", dataset, false, false, false); FileOutputStream fop = null; try ...{ System.out.println("----->begin!!"); fop = new FileOutputStream("D:/gantt.jpg"); ChartUtilities.writeChartAsJPEG(fop,1f, jfc, 800, 600,null); System.out.println("----->successful!!"); } catch (IOException e) ...{ e.printStackTrace(); } finally ...{ try ...{ fop.close(); } catch (IOException e) ...{ e.printStackTrace(); } } } /** *//** * Utility method for creating <code>Date</code> objects. * * @param day 日 * @param month 月 * @param year 年 * * @return a date. */ private static Date date(final int day, final int month, final int year) ...{ final Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day); final Date result = calendar.getTime(); return result; } /** *//** * Creates a sample dataset for a Gantt chart, using sub-tasks. In general, you won't * hard-code the dataset in this way - it's done here so that the demo is self-contained. * * @return The dataset. */ private static IntervalCategoryDataset createSampleDataset() ...{ final TaskSeries s1 = new TaskSeries("日程表"); final Task t1 = new Task( "项目立项", date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001) ); t1.setPercentComplete(1.00); s1.add(t1); final Task t2 = new Task( "项目启动", date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001) ); t2.setPercentComplete(1.00); s1.add(t2); // 创建一个任务并插入两个子任务 final Task t3 = new Task( "需求分析", date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001) ); final Task st31 = new Task( "需求1", date(10, Calendar.APRIL, 2001), date(25, Calendar.APRIL, 2001) ); st31.setPercentComplete(1.0); final Task st32 = new Task( "需求2", date(1, Calendar.MAY, 2001), date(5, Calendar.MAY, 2001) ); st32.setPercentComplete(1.0); t3.addSubtask(st31); t3.addSubtask(st32); s1.add(t3); // 添加其他任务 final Task t4 = new Task( "基本设计", date(6, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001) ); final Task st41 = new Task( "设计1", date(6, Calendar.MAY, 2001), date(10, Calendar.MAY, 2001) ); st41.setPercentComplete(1.0); final Task st42 = new Task( "设计2", date(15, Calendar.MAY, 2001), date(20, Calendar.MAY, 2001) ); st42.setPercentComplete(1.0); final Task st43 = new Task( "设计3", date(23, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001) ); st43.setPercentComplete(0.50); t4.addSubtask(st41); t4.addSubtask(st42); t4.addSubtask(st43); s1.add(t4); final Task t5 = new Task( "设计结束", date(2, Calendar.JUNE, 2001), date(2, Calendar.JUNE, 2001) ); s1.add(t5); final Task t6 = new Task( "实现", date(3, Calendar.JUNE, 2001), date(31, Calendar.JULY, 2001) ); t6.setPercentComplete(0.60); s1.add(t6); final Task t7 = new Task( "设计Review", date(1, Calendar.AUGUST, 2001), date(8, Calendar.AUGUST, 2001) ); t7.setPercentComplete(0.0); s1.add(t7); final Task t8 = new Task( "设计结束", date(10, Calendar.AUGUST, 2001), date(10, Calendar.AUGUST, 2001) ); t8.setPercentComplete(0.0); s1.add(t8); final Task t9 = new Task( "试用", date(12, Calendar.AUGUST, 2001), date(12, Calendar.SEPTEMBER, 2001) ); t9.setPercentComplete(0.0); s1.add(t9); final Task t10 = new Task( "测试", date(13, Calendar.SEPTEMBER, 2001), date(31, Calendar.OCTOBER, 2001) ); t10.setPercentComplete(0.0); s1.add(t10); final Task t11 = new Task( "最终实现", date(1, Calendar.NOVEMBER, 2001), date(15, Calendar.NOVEMBER, 2001) ); t11.setPercentComplete(0.0); s1.add(t11); final Task t12 = new Task( "全部结束", date(28, Calendar.NOVEMBER, 2001), date(30, Calendar.NOVEMBER, 2001) ); t12.setPercentComplete(0.0); s1.add(t12); final TaskSeriesCollection collection = new TaskSeriesCollection(); collection.add(s1); return collection; } }
生成的图片:
甘特图(Gantt chart)是在20世纪初由亨利.甘特开发的。它基本上是一种线条图,横轴表示时间,纵轴表示要安排的活动,线条表示在整个期间上计划的和实际的活动完成情况。甘特图直观地表明任务计划在什么时候进行,以及实际进展与计划要求的对比。 下面我们来举一个图书出版的例子来说明甘特图。 时间以月为单位表示在图的下方,主要活动从上到下列在图的左边。计划需要确定数的出版包括哪些活动,这些活动的顺序,以及每项活动持续的时间。时间框里的线条表示计划的活动顺序,空白的现况表示活动的实际进度。甘特图作为一种控制工具,帮助管理者发现实际进度偏离计划的情况。在本例中,除了打印长条校样以外,其他活动都是按计划完成的。