package com.mis.tools; /** * @author Dahai He * @date 2010-07-30 */ import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.DateTickUnit; import org.jfree.chart.axis.DateTickUnitType; import org.jfree.chart.plot.CategoryPlot; import org.jfree.data.category.IntervalCategoryDataset; import org.jfree.data.gantt.Task; import org.jfree.data.gantt.TaskSeries; import org.jfree.data.gantt.TaskSeriesCollection; import org.jfree.data.time.SimpleTimePeriod; import com.mis.vo.TaskForGantt; public class GanttChartUtil { /** * Creates a sample dataSet for a Gantt chart. * @return The dataSet. */ public static IntervalCategoryDataset createDataset(List<TaskForGantt> tasks) { TaskSeries s1 = new TaskSeries("Scheduled"); TaskSeries s2 = new TaskSeries("Actual"); for(int i = 0; i < tasks.size(); i++){ // a task TaskForGantt tfg = tasks.get(i); //create a task with scheduled time, time can not be null SimpleTimePeriod stp1 = new SimpleTimePeriod(tfg.getStart(),tfg.getEnd()); s1.add(new Task(tfg.getDescription(),stp1)); //create a task with actual time, time can be null SimpleTimePeriod stp2 = null; if(tfg.getActualStart() == null || tfg.getActualEnd() == null){ s2.add(new Task(tfg.getDescription(),stp2)); }else{ stp2 = new SimpleTimePeriod(tfg.getActualStart(),tfg.getActualEnd()); s2.add(new Task(tfg.getDescription(),stp2)); } } TaskSeriesCollection collection = new TaskSeriesCollection(); collection.add(s1); collection.add(s2); return collection; } /** * Utility method for creating Date objects. * @param day the date. * @param month the month. * @param year the year. * @return a date. */ private static Date date(int day,int month,int year) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day); Date result = calendar.getTime(); return result; } /** * Creates a chart. * @param dataset the dataset. * @return The chart. */ private static JFreeChart createChart(IntervalCategoryDataset dataset,String title) { JFreeChart chart = ChartFactory.createGanttChart( title, // chart title "Task", // domain axis label "Date", // range axis label dataset, // data true, // include legend true, // tooltips false // urls ); return chart; } /** * create a jpeg image by the tasks * @param title, the title for the image * @param out, a OutPutStream * @param tasks, a list */ public static void makeGanntChart(String title,OutputStream out,List<TaskForGantt> tasks){ IntervalCategoryDataset dataset = createDataset(tasks); JFreeChart chart = createChart(dataset,title); CategoryPlot plot = chart.getCategoryPlot(); DateAxis da = (DateAxis)plot.getRangeAxis(); da.setDateFormatOverride(new SimpleDateFormat("dd/MM/yy")); //da.setAutoRange(false); //da.setTickUnit(new DateTickUnit(DateTickUnitType.DAY,10)); //CategoryAxis domainAxis = (CategoryAxis)plot.getDomainAxis(); int size = tasks.size(); try { int height = 300; if(size > 10 && size <= 20){ height = 500; }else if(size > 20 && size <= 30){ height = 700; }else if(size > 30 && size <= 40){ height = 900; } ChartUtilities.writeChartAsJPEG(out,1.0f,chart,800,height,null); } catch (IOException e) { e.printStackTrace(); } } }