KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SegmentedHighLowChartDemo.java
24  * ------------------------------
25  * (C) Copyright 2003, by Bill Kelemen and Contributors.
26  *
27  * Original Author: Bill Kelemen;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: SegmentedHighLowChartDemo.java,v 1.8 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 24-May-2003 : Version 1 (BK);
35  * 12-Nov-2003 : Added roll factor to tick units to improve labelling on date axis (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.util.Calendar JavaDoc;
43
44 import org.jfree.chart.ChartFactory;
45 import org.jfree.chart.ChartPanel;
46 import org.jfree.chart.JFreeChart;
47 import org.jfree.chart.axis.DateAxis;
48 import org.jfree.chart.axis.DateTickUnit;
49 import org.jfree.chart.axis.NumberAxis;
50 import org.jfree.chart.axis.SegmentedTimeline;
51 import org.jfree.chart.axis.TickUnits;
52 import org.jfree.data.HighLowDataset;
53 import org.jfree.ui.ApplicationFrame;
54 import org.jfree.ui.RefineryUtilities;
55
56 /**
57  * A demo showing a daily and intraday segmented high-low-open-close charts.
58  *
59  * @author Bill Kelemen
60  */

61 public class SegmentedHighLowChartDemo extends ApplicationFrame {
62
63     /**
64      * A demonstration application showing a high-low-open-close chart using a
65      * segmented or non-segmented axis.
66      *
67      * @param title the frame title.
68      * @param useSegmentedAxis use a segmented axis for this demo?
69      * @param timelineType Type of timeline to use: 1=Monday through Friday, 2=Intraday
70      */

71     public SegmentedHighLowChartDemo(String JavaDoc title,
72                                      boolean useSegmentedAxis,
73                                      int timelineType) {
74
75         super(title);
76
77         System.out.println("\nMaking SegmentedHighLowChartDemo(" + title + ")");
78
79         // create a Calendar object with today's date at midnight
80
Calendar JavaDoc cal = Calendar.getInstance();
81         cal.set(Calendar.HOUR_OF_DAY, 0);
82         cal.set(Calendar.MINUTE, 0);
83         cal.set(Calendar.SECOND, 0);
84         cal.set(Calendar.MILLISECOND, 0);
85
86         // create a timeline for the demo
87
SegmentedTimeline timeline = null;
88         switch (timelineType) {
89             case 1:
90                 timeline = SegmentedTimeline.newMondayThroughFridayTimeline();
91                 break;
92
93             case 2:
94                 timeline = SegmentedTimeline.newFifteenMinuteTimeline();
95
96                 Calendar JavaDoc cal2 = (Calendar JavaDoc) cal.clone();
97                 cal2.add(Calendar.YEAR, 1);
98
99                 // add 1 year of baseTimeline's excluded segments (Saturdays and Sundays) as
100
// exceptions of the intraday timeline
101
timeline.addBaseTimelineExclusions(cal.getTime().getTime(),
102                     cal2.getTime().getTime());
103                 break;
104
105             default:
106                 System.out.println("Invalid timelineType.");
107                 System.exit(1);
108         }
109
110
111         // create a data set that has data for trading days (Monday through Friday).
112
HighLowDataset dataset =
113             DemoDatasetFactory.createSegmentedHighLowDataset(timeline, cal.getTime());
114
115         JFreeChart chart;
116         if (useSegmentedAxis) {
117             chart = ChartFactory.createHighLowChart(
118                 title,
119                 "Time", "Value",
120                 dataset, timeline, true);
121         } else {
122             chart = ChartFactory.createHighLowChart(
123                 title,
124                 "Time", "Value",
125                 dataset, true);
126         }
127
128         DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();
129         axis.setAutoRange(true);
130         TickUnits units = new TickUnits();
131         units.add(new DateTickUnit(DateTickUnit.DAY, 1, DateTickUnit.HOUR, 1,
132                                    new SimpleDateFormat JavaDoc("d-MMM")));
133         units.add(new DateTickUnit(DateTickUnit.DAY, 2, DateTickUnit.HOUR, 1,
134                                    new SimpleDateFormat JavaDoc("d-MMM")));
135         units.add(new DateTickUnit(DateTickUnit.DAY, 7, DateTickUnit.DAY, 1,
136                                    new SimpleDateFormat JavaDoc("d-MMM")));
137         units.add(new DateTickUnit(DateTickUnit.DAY, 15, DateTickUnit.DAY, 1,
138                                    new SimpleDateFormat JavaDoc("d-MMM")));
139         units.add(new DateTickUnit(DateTickUnit.DAY, 30, DateTickUnit.DAY, 1,
140                                    new SimpleDateFormat JavaDoc("d-MMM")));
141         axis.setStandardTickUnits(units);
142
143         NumberAxis vaxis = (NumberAxis) chart.getXYPlot().getRangeAxis();
144         vaxis.setAutoRangeIncludesZero(false);
145
146         ChartPanel chartPanel = new ChartPanel(chart);
147         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
148
149         setContentPane(chartPanel);
150
151     }
152
153     // ****************************************************************************
154
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
155
// * Please note that commercial support and documentation is available from: *
156
// * *
157
// * http://www.object-refinery.com/jfreechart/support.html *
158
// * *
159
// * This is not only a great service for developers, but is a VERY IMPORTANT *
160
// * source of funding for the JFreeChart project. Please support us so that *
161
// * we can continue developing free software. *
162
// ****************************************************************************
163

164     /**
165      * Starting point for the demonstration application.
166      *
167      * @param args ignored.
168      */

169     public static void main(String JavaDoc[] args) {
170
171         ApplicationFrame[][] frame = new ApplicationFrame[2][2];
172         frame[0][0] = new SegmentedHighLowChartDemo("Segmented Daily High-Low-Open-Close Demo",
173                                                     true, 1);
174         frame[1][0] = new SegmentedHighLowChartDemo("Normal Daily High-Low-Open-Close Demo",
175                                                     false, 1);
176         frame[0][1] = new SegmentedHighLowChartDemo("Segmented Intraday High-Low-Open-Close Demo",
177                                                     true, 2);
178         frame[1][1] = new SegmentedHighLowChartDemo("Normal Intraday High-Low-Open-Close Demo",
179                                                     false, 2);
180         for (int i = 0; i < 2; i++) {
181             for (int j = 0; j < 2; j++) {
182                 frame[i][j].pack();
183                 RefineryUtilities.positionFrameOnScreen(frame[i][j], .15 + .70 * j, .25 + .50 * i);
184                 frame[i][j].setVisible(true);
185             }
186         }
187
188     }
189
190 }
191
Popular Tags