KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SampleXYDataset.java
24  * --------------------
25  * (C) Copyright 2000-2003, by Object Refinery Limited;
26  *
27  * Original Author: David Gilbert;
28  * Contributor(s): -;
29  *
30  * $Id: SampleXYDataset.java,v 1.3 2003/06/12 16:54:10 mungady Exp $
31  *
32  * Changes (from 24-Aug-2001)
33  * --------------------------
34  * 24-Aug-2001 : Added standard source header. Fixed DOS encoding problem (DG);
35  * 15-Oct-2001 : Parent class has changed package (DG);
36  * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
37  * Added translate factor, used for demonstrating dynamic chart (DG);
38  * 07-Nov-2001 : Updated source header (DG);
39  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  *
41  */

42
43 package org.jfree.chart.demo;
44
45 import org.jfree.data.AbstractSeriesDataset;
46 import org.jfree.data.DatasetChangeEvent;
47 import org.jfree.data.XYDataset;
48
49 /**
50  * A dummy dataset for an XY plot.
51  * <P>
52  * Note that the aim of this class is to create a self-contained data source for demo purposes -
53  * it is NOT intended to show how you should go about writing your own datasets.
54  *
55  * @author David Gilbert
56  */

57 public class SampleXYDataset extends AbstractSeriesDataset implements XYDataset {
58
59     /** Use the translate to change the data and demonstrate dynamic data changes. */
60     private double translate;
61
62     /**
63      * Default constructor.
64      */

65     public SampleXYDataset() {
66         this.translate = 0.0;
67     }
68
69     /**
70      * Returns the translation factor.
71      *
72      * @return the translation factor.
73      */

74     public double getTranslate() {
75         return this.translate;
76     }
77
78     /**
79      * Sets the translation constant for the x-axis.
80      *
81      * @param translate the translation factor.
82      */

83     public void setTranslate(double translate) {
84         this.translate = translate;
85         notifyListeners(new DatasetChangeEvent(this, this));
86     }
87
88     /**
89      * Returns the x-value for the specified series and item. Series are numbered 0, 1, ...
90      *
91      * @param series the index (zero-based) of the series.
92      * @param item the index (zero-based) of the required item.
93      *
94      * @return the x-value for the specified series and item.
95      */

96     public Number JavaDoc getXValue(int series, int item) {
97         return new Double JavaDoc(-10.0 + translate + (item / 10.0));
98     }
99
100     /**
101      * Returns the y-value for the specified series and item. Series are numbered 0, 1, ...
102      *
103      * @param series the index (zero-based) of the series.
104      * @param item the index (zero-based) of the required item.
105      *
106      * @return the y-value for the specified series and item.
107      */

108     public Number JavaDoc getYValue(int series, int item) {
109         if (series == 0) {
110             return new Double JavaDoc(Math.cos(-10.0 + translate + (item / 10.0)));
111         }
112         else {
113             return new Double JavaDoc(2 * (Math.sin(-10.0 + translate + (item / 10.0))));
114         }
115     }
116
117     /**
118      * Returns the number of series in the dataset.
119      *
120      * @return the number of series in the dataset.
121      */

122     public int getSeriesCount() {
123         return 2;
124     }
125
126     /**
127      * Returns the name of the series.
128      *
129      * @param series the index (zero-based) of the series.
130      *
131      * @return the name of the series.
132      */

133     public String JavaDoc getSeriesName(int series) {
134         if (series == 0) {
135             return "y = cosine(x)";
136         }
137         else if (series == 1) {
138             return "y = 2*sine(x)";
139         }
140         else {
141             return "Error";
142         }
143     }
144
145     /**
146      * Returns the number of items in the specified series.
147      *
148      * @param series the index (zero-based) of the series.
149      * @return the number of items in the specified series.
150      *
151      */

152     public int getItemCount(int series) {
153         return 200;
154     }
155
156 }
157
158
159
160
161
162
163
164
Popular Tags