1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.Color ; 42 import java.util.Calendar ; 43 import java.util.Date ; 44 45 import org.jfree.chart.ChartFactory; 46 import org.jfree.chart.ChartPanel; 47 import org.jfree.chart.JFreeChart; 48 import org.jfree.chart.plot.CategoryPlot; 49 import org.jfree.chart.renderer.CategoryItemRenderer; 50 import org.jfree.data.IntervalCategoryDataset; 51 import org.jfree.data.gantt.Task; 52 import org.jfree.data.gantt.TaskSeries; 53 import org.jfree.data.gantt.TaskSeriesCollection; 54 import org.jfree.ui.ApplicationFrame; 55 import org.jfree.ui.RefineryUtilities; 56 57 64 public class GanttDemo2 extends ApplicationFrame { 65 66 71 public GanttDemo2(String title) { 72 73 super(title); 74 75 IntervalCategoryDataset dataset = createSampleDataset(); 76 77 JFreeChart chart = ChartFactory.createGanttChart( 79 "Gantt Chart Demo", "Task", "Date", dataset, true, true, false ); 87 CategoryPlot plot = (CategoryPlot) chart.getPlot(); 88 CategoryItemRenderer renderer = plot.getRenderer(); 89 renderer.setSeriesPaint(0, Color.blue); 90 91 ChartPanel chartPanel = new ChartPanel(chart); 93 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 94 setContentPane(chartPanel); 95 96 } 97 98 109 115 private IntervalCategoryDataset createSampleDataset() { 116 117 TaskSeries s1 = new TaskSeries("Scheduled"); 118 119 Task t1 = new Task( 120 "Write Proposal", date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001) 121 ); 122 t1.setPercentComplete(1.00); 123 s1.add(t1); 124 125 Task t2 = new Task( 126 "Obtain Approval", date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001) 127 ); 128 t2.setPercentComplete(1.00); 129 s1.add(t2); 130 131 Task t3 = new Task( 133 "Requirements Analysis", 134 date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001) 135 ); 136 Task st31 = new Task( 137 "Requirements 1", 138 date(10, Calendar.APRIL, 2001), date(25, Calendar.APRIL, 2001) 139 ); 140 st31.setPercentComplete(1.0); 141 Task st32 = new Task( 142 "Requirements 2", 143 date(1, Calendar.MAY, 2001), date(5, Calendar.MAY, 2001) 144 ); 145 st32.setPercentComplete(1.0); 146 t3.addSubtask(st31); 147 t3.addSubtask(st32); 148 s1.add(t3); 149 150 Task t4 = new Task( 152 "Design Phase", 153 date(6, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001) 154 ); 155 Task st41 = new Task( 156 "Design 1", 157 date(6, Calendar.MAY, 2001), date(10, Calendar.MAY, 2001) 158 ); 159 st41.setPercentComplete(1.0); 160 Task st42 = new Task( 161 "Design 2", 162 date(15, Calendar.MAY, 2001), date(20, Calendar.MAY, 2001) 163 ); 164 st42.setPercentComplete(1.0); 165 Task st43 = new Task( 166 "Design 3", 167 date(23, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001) 168 ); 169 st43.setPercentComplete(0.50); 170 t4.addSubtask(st41); 171 t4.addSubtask(st42); 172 t4.addSubtask(st43); 173 s1.add(t4); 174 175 Task t5 = new Task( 176 "Design Signoff", date(2, Calendar.JUNE, 2001), date(2, Calendar.JUNE, 2001) 177 ); 178 s1.add(t5); 179 180 Task t6 = new Task( 181 "Alpha Implementation", date(3, Calendar.JUNE, 2001), date(31, Calendar.JULY, 2001) 182 ); 183 t6.setPercentComplete(0.60); 184 185 s1.add(t6); 186 187 Task t7 = new Task( 188 "Design Review", date(1, Calendar.AUGUST, 2001), date(8, Calendar.AUGUST, 2001) 189 ); 190 t7.setPercentComplete(0.0); 191 s1.add(t7); 192 193 Task t8 = new Task( 194 "Revised Design Signoff", 195 date(10, Calendar.AUGUST, 2001), date(10, Calendar.AUGUST, 2001) 196 ); 197 t8.setPercentComplete(0.0); 198 s1.add(t8); 199 200 Task t9 = new Task( 201 "Beta Implementation", 202 date(12, Calendar.AUGUST, 2001), date(12, Calendar.SEPTEMBER, 2001) 203 ); 204 t9.setPercentComplete(0.0); 205 s1.add(t9); 206 207 Task t10 = new Task( 208 "Testing", date(13, Calendar.SEPTEMBER, 2001), date(31, Calendar.OCTOBER, 2001) 209 ); 210 t10.setPercentComplete(0.0); 211 s1.add(t10); 212 213 Task t11 = new Task( 214 "Final Implementation", 215 date(1, Calendar.NOVEMBER, 2001), date(15, Calendar.NOVEMBER, 2001) 216 ); 217 t11.setPercentComplete(0.0); 218 s1.add(t11); 219 220 Task t12 = new Task( 221 "Signoff", date(28, Calendar.NOVEMBER, 2001), date(30, Calendar.NOVEMBER, 2001) 222 ); 223 t12.setPercentComplete(0.0); 224 s1.add(t12); 225 226 TaskSeriesCollection collection = new TaskSeriesCollection(); 227 collection.add(s1); 228 229 return collection; 230 } 231 232 241 private static Date date(int day, int month, int year) { 242 243 Calendar calendar = Calendar.getInstance(); 244 calendar.set(year, month, day); 245 Date result = calendar.getTime(); 246 return result; 247 248 } 249 250 255 public static void main(String [] args) { 256 257 GanttDemo2 demo = new GanttDemo2("Gantt Chart Demo 2"); 258 demo.pack(); 259 RefineryUtilities.centerFrameOnScreen(demo); 260 demo.setVisible(true); 261 262 } 263 264 } 265 | Popular Tags |