KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MarkerDemo1.java
24  * ----------------
25  * (C) Copyright 2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: MarkerDemo1.java,v 1.8 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 21-May-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.BasicStroke JavaDoc;
41 import java.awt.Color JavaDoc;
42 import java.awt.Font 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.Legend;
48 import org.jfree.chart.Marker;
49 import org.jfree.chart.MarkerLabelPosition;
50 import org.jfree.chart.annotations.XYAnnotation;
51 import org.jfree.chart.annotations.XYDrawableAnnotation;
52 import org.jfree.chart.annotations.XYPointerAnnotation;
53 import org.jfree.chart.axis.DateAxis;
54 import org.jfree.chart.axis.ValueAxis;
55 import org.jfree.chart.labels.TimeSeriesToolTipGenerator;
56 import org.jfree.chart.plot.PlotOrientation;
57 import org.jfree.chart.plot.XYPlot;
58 import org.jfree.data.XYDataset;
59 import org.jfree.data.time.Day;
60 import org.jfree.data.time.Hour;
61 import org.jfree.data.time.Minute;
62 import org.jfree.data.time.TimeSeries;
63 import org.jfree.data.time.TimeSeriesCollection;
64 import org.jfree.ui.ApplicationFrame;
65 import org.jfree.ui.RefineryUtilities;
66 import org.jfree.ui.TextAnchor;
67
68 /**
69  * A demo application.
70  *
71  * @author David Gilbert
72  */

73 public class MarkerDemo1 extends ApplicationFrame {
74
75     /**
76      * Creates a new instance.
77      *
78      * @param title the frame title.
79      */

80     public MarkerDemo1(String JavaDoc title) {
81
82         super(title);
83         XYDataset data = createDataset();
84         JFreeChart chart = createChart(data);
85         ChartPanel chartPanel = new ChartPanel(chart);
86         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
87         chartPanel.setVerticalZoom(true);
88         chartPanel.setHorizontalZoom(true);
89         setContentPane(chartPanel);
90
91     }
92
93     /**
94      * Creates a sample chart.
95      *
96      * @param data the sample data.
97      *
98      * @return A configured chart.
99      */

100     private JFreeChart createChart(XYDataset data) {
101
102         JFreeChart chart = ChartFactory.createScatterPlot(
103             "Marker Demo 1",
104             "X",
105             "Y",
106             data,
107             PlotOrientation.VERTICAL,
108             true,
109             true,
110             false
111         );
112         chart.getLegend().setAnchor(Legend.EAST);
113
114         // customise...
115
XYPlot plot = chart.getXYPlot();
116
117         // need a time series tool tip generator...
118
plot.getRenderer().setToolTipGenerator(new TimeSeriesToolTipGenerator());
119
120         // set axis margins to allow space for marker labels...
121
DateAxis domainAxis = new DateAxis("Time");
122         domainAxis.setUpperMargin(0.50);
123         plot.setDomainAxis(domainAxis);
124
125         ValueAxis rangeAxis = plot.getRangeAxis();
126         rangeAxis.setUpperMargin(0.30);
127         rangeAxis.setLowerMargin(0.50);
128
129         // add a labelled marker for the bid start price...
130
Marker start = new Marker(200.0, Color.green);
131         start.setLabel("Bid Start Price");
132         start.setLabelPosition(MarkerLabelPosition.TOP_RIGHT);
133         plot.addRangeMarker(start);
134
135         // add a labelled marker for the target price...
136
Marker target = new Marker(175.0, Color.red);
137         target.setLabel("Target Price");
138         target.setLabelPosition(MarkerLabelPosition.TOP_RIGHT);
139         plot.addRangeMarker(target);
140
141         // add a labelled marker for the original closing time...
142
Hour hour = new Hour(2, new Day(22, 5, 2003));
143         double millis = hour.getFirstMillisecond();
144         Marker originalEnd = new Marker(millis, Color.orange);
145         originalEnd.setLabel("Original Close (02:00)");
146         plot.addDomainMarker(originalEnd);
147
148         // add a labelled marker for the current closing time...
149
Minute min = new Minute(15, hour);
150         millis = min.getFirstMillisecond();
151         Marker currentEnd = new Marker(millis, Color.red);
152         currentEnd.setLabel("Close Date (02:15)");
153         currentEnd.setLabelPosition(MarkerLabelPosition.TOP_RIGHT);
154         plot.addDomainMarker(currentEnd);
155
156         // ****************************************************************************
157
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
158
// * Please note that commercial support and documentation is available from: *
159
// * *
160
// * http://www.object-refinery.com/jfreechart/support.html *
161
// * *
162
// * This is not only a great service for developers, but is a VERY IMPORTANT *
163
// * source of funding for the JFreeChart project. Please support us so that *
164
// * we can continue developing free software. *
165
// ****************************************************************************
166

167         // label the best bid with an arrow and label...
168
Hour h = new Hour(2, new Day(22, 5, 2003));
169         Minute m = new Minute(10, h);
170         millis = m.getFirstMillisecond();
171         CircleDrawer cd = new CircleDrawer(Color.red, new BasicStroke JavaDoc(1.0f), null);
172         XYAnnotation bestBid = new XYDrawableAnnotation(millis, 163.0, 11, 11, cd);
173         plot.addAnnotation(bestBid);
174         XYPointerAnnotation pointer = new XYPointerAnnotation("Best Bid", millis, 163.0,
175                                                               3.0 * Math.PI / 4.0);
176         pointer.setBaseRadius(35.0);
177         pointer.setTipRadius(10.0);
178         pointer.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 9));
179         pointer.setPaint(Color.blue);
180         pointer.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT);
181         plot.addAnnotation(pointer);
182
183         return chart;
184
185     }
186
187     /**
188      * Returns a sample dataset.
189      *
190      * @return A sample dataset.
191      */

192     private XYDataset createDataset() {
193
194         TimeSeriesCollection result = new TimeSeriesCollection();
195         result.addSeries(createSupplier1Bids());
196         result.addSeries(createSupplier2Bids());
197         return result;
198
199     }
200
201     /**
202      * Returns a sample data series (for supplier 1).
203      *
204      * @return A sample data series.
205      */

206     private TimeSeries createSupplier1Bids() {
207
208         Hour hour = new Hour(1, new Day(22, 5, 2003));
209
210         TimeSeries series1 = new TimeSeries("Supplier 1", Minute.class);
211         series1.add(new Minute(13, hour), 200.0);
212         series1.add(new Minute(14, hour), 195.0);
213         series1.add(new Minute(45, hour), 190.0);
214         series1.add(new Minute(46, hour), 188.0);
215         series1.add(new Minute(47, hour), 185.0);
216         series1.add(new Minute(52, hour), 180.0);
217
218         return series1;
219
220     }
221
222     /**
223      * Returns a sample data series (for supplier 2).
224      *
225      * @return A sample data series.
226      */

227     private TimeSeries createSupplier2Bids() {
228
229         Hour hour1 = new Hour(1, new Day(22, 5, 2003));
230         Hour hour2 = (Hour) hour1.next();
231
232         TimeSeries series2 = new TimeSeries("Supplier 2", Minute.class);
233         series2.add(new Minute(25, hour1), 185.0);
234         series2.add(new Minute(0, hour2), 175.0);
235         series2.add(new Minute(5, hour2), 170.0);
236         series2.add(new Minute(6, hour2), 168.0);
237         series2.add(new Minute(9, hour2), 165.0);
238         series2.add(new Minute(10, hour2), 163.0);
239
240         return series2;
241
242     }
243
244     /**
245      * Starting point for the demo application.
246      *
247      * @param args ignored.
248      */

249     public static void main(String JavaDoc[] args) {
250
251         MarkerDemo1 demo = new MarkerDemo1("Marker Demo 1");
252         demo.pack();
253         RefineryUtilities.centerFrameOnScreen(demo);
254         demo.setVisible(true);
255
256     }
257
258 }
259
Popular Tags