KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > GanttDemo2


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ---------------
23  * GanttDemo2.java
24  * ---------------
25  * (C) Copyright 2003 by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: GanttDemo2.java,v 1.6 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 10-Jan-2003 : Version 1 (based on GanttDemo1) (DG);
35  * 16-Sep-2003 : Transferred dataset creation from DemoDatasetFactory to this class (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Color JavaDoc;
42 import java.util.Calendar JavaDoc;
43 import java.util.Date JavaDoc;
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 /**
58  * A simple demonstration application showing how to create a Gantt chart with multiple bars per
59  * task.
60  *
61  * @author David Gilbert
62  *
63  */

64 public class GanttDemo2 extends ApplicationFrame {
65
66     /**
67      * Creates a new demo.
68      *
69      * @param title the frame title.
70      */

71     public GanttDemo2(String JavaDoc title) {
72
73         super(title);
74
75         IntervalCategoryDataset dataset = createSampleDataset();
76
77         // create the chart...
78
JFreeChart chart = ChartFactory.createGanttChart(
79             "Gantt Chart Demo", // chart title
80
"Task", // domain axis label
81
"Date", // range axis label
82
dataset, // data
83
true, // include legend
84
true, // tooltips
85
false // urls
86
);
87         CategoryPlot plot = (CategoryPlot) chart.getPlot();
88         CategoryItemRenderer renderer = plot.getRenderer();
89         renderer.setSeriesPaint(0, Color.blue);
90
91         // add the chart to a panel...
92
ChartPanel chartPanel = new ChartPanel(chart);
93         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
94         setContentPane(chartPanel);
95
96     }
97
98     // ****************************************************************************
99
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
100
// * Please note that commercial support and documentation is available from: *
101
// * *
102
// * http://www.object-refinery.com/jfreechart/support.html *
103
// * *
104
// * This is not only a great service for developers, but is a VERY IMPORTANT *
105
// * source of funding for the JFreeChart project. Please support us so that *
106
// * we can continue developing free software. *
107
// ****************************************************************************
108

109     /**
110      * Creates a sample dataset for a Gantt chart, using sub-tasks. In general, you won't
111      * hard-code the dataset in this way - it's done here so that the demo is self-contained.
112      *
113      * @return The dataset.
114      */

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         // here is a task split into two subtasks...
132
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         // and another...
151
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     /**
233      * Utility method for creating <code>Date</code> objects.
234      *
235      * @param day the date.
236      * @param month the month.
237      * @param year the year.
238      *
239      * @return a date.
240      */

241     private static Date JavaDoc date(int day, int month, int year) {
242
243         Calendar JavaDoc calendar = Calendar.getInstance();
244         calendar.set(year, month, day);
245         Date JavaDoc result = calendar.getTime();
246         return result;
247
248     }
249
250     /**
251      * Starting point for the demonstration application.
252      *
253      * @param args ignored.
254      */

255     public static void main(String JavaDoc[] 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