KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > chartData > ScatterPlotDataSet


1 /***********************************************************************************************
2  * File Info: $Id: ScatterPlotDataSet.java,v 1.2 2004/05/31 16:24:34 nathaniel_auvil Exp $
3  * Copyright (C) 2002
4  * Author: Nathaniel G. Auvil
5  * Contributor(s):
6  *
7  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
8  *
9  * Redistribution and use of this software and associated documentation ("Software"), with or
10  * without modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain copyright statements and notices.
13  * Redistributions must also contain a copy of this document.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
16  * conditions and the following disclaimer in the documentation and/or other materials
17  * provided with the distribution.
18  *
19  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
20  * products derived from this Software without prior written permission of Nathaniel G.
21  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
22  *
23  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
24  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
25  * registered trademark of Nathaniel G. Auvil.
26  *
27  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
28  *
29  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
30  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  ************************************************************************************************/

39
40 package org.krysalis.jcharts.chartData;
41
42
43 import org.krysalis.jcharts.chartData.interfaces.IScatterPlotDataSet;
44 import org.krysalis.jcharts.properties.ChartTypeProperties;
45 import org.krysalis.jcharts.properties.ScatterPlotProperties;
46 import org.krysalis.jcharts.test.HTMLGenerator;
47 import org.krysalis.jcharts.test.HTMLTestable;
48 import org.krysalis.jcharts.types.ChartType;
49
50 import java.awt.*;
51 import java.awt.geom.Point2D JavaDoc;
52 import java.util.ArrayList JavaDoc;
53
54
55 public class ScatterPlotDataSet implements IScatterPlotDataSet, HTMLTestable
56 {
57     //---use this to clone for better performance than creating new
58
private static Point2D.Double JavaDoc POINT= new Point2D.Double JavaDoc();
59
60     private ArrayList JavaDoc data;
61     private ArrayList JavaDoc legendLabels;
62     private ArrayList JavaDoc paints;
63     private ScatterPlotProperties scatterPlotProperties;
64
65     private int numDataItems= -1;
66
67
68     /******************************************************************************************
69      * Constructor
70      *
71      * @param scatterPlotProperties
72      *******************************************************************************************/

73     public ScatterPlotDataSet( ScatterPlotProperties scatterPlotProperties )
74     {
75         this.data = new ArrayList JavaDoc();
76         this.legendLabels = new ArrayList JavaDoc();
77         this.paints = new ArrayList JavaDoc();
78         this.scatterPlotProperties = scatterPlotProperties;
79     }
80
81
82     /******************************************************************************************
83      * Returns the type constant that this data set should be plotted as.
84      *
85      * @return ChartType
86      *******************************************************************************************/

87     public final ChartType getChartType()
88     {
89         return ChartType.SCATTER_PLOT;
90     }
91
92
93    /****************************************************************************************
94      *
95      * @param points
96      * @param paint
97      * @param legendLabel
98      ***************************************************************************************/

99     public void addDataPoints( Point2D.Double JavaDoc[] points, Paint paint, String JavaDoc legendLabel )
100     {
101       this.data.add( points );
102         this.paints.add( paint );
103         this.legendLabels.add( legendLabel );
104
105         this.numDataItems= points.length;
106     }
107
108
109     /************************************************************************************************
110      * Performs a limited validation of data.
111      *
112      * @throws ChartDataException
113      *************************************************************************************************/

114     public void validate() throws ChartDataException
115     {
116         Point2D.Double JavaDoc[] points;
117         for( int i=0; i < this.data.size(); i++ )
118         {
119             points= (Point2D.Double JavaDoc[]) this.data.get( i );
120             if( points.length != this.numDataItems )
121             {
122                 throw new ChartDataException( "All Arrays of Point Objects must have the same length." );
123             }
124
125             if( this.paints.get( i ) == null )
126             {
127                 throw new ChartDataException( "The 'Paint' implementation can not be NULL." );
128             }
129         }
130
131         if( this.scatterPlotProperties == null )
132         {
133             throw new ChartDataException( "ScatterPlotProperties can not be NULL." );
134         }
135     }
136
137
138     /******************************************************************************************
139      * Returns the value in the data set at the specified position.
140      *
141      * @param dataset
142      * @param index
143      * @return Point.Double
144      *******************************************************************************************/

145     public Point2D.Double JavaDoc getValue( int dataset, int index )
146     {
147         return ( (Point2D.Double JavaDoc[]) this.data.get( dataset ) )[ index ];
148     }
149
150
151     /******************************************************************************************
152      * Returns the legend label for the passed index. This index corresponds to the DataSet
153      * for which label you want.
154      *
155      * @param index
156      * @return String
157      *******************************************************************************************/

158     public final String JavaDoc getLegendLabel( int index )
159     {
160         if( this.legendLabels == null )
161         {
162             return null;
163         }
164         else
165         {
166             return (String JavaDoc) this.legendLabels.get( index );
167         }
168     }
169
170
171     /*********************************************************************************************
172      * Returns the number of Legend Labels to display. This may not be the same as the number of
173      * Data Items, as in AxisCharts, or Data Sets, as in Pie Charts.
174      *
175      * @return int
176      **********************************************************************************************/

177     public int getNumberOfLegendLabels()
178     {
179         if( this.legendLabels == null )
180         {
181             return 0;
182         }
183         else
184         {
185             return this.legendLabels.size();
186         }
187     }
188
189
190     /******************************************************************************************
191      * Returns the legend label for the passed index. This index corresponds to the DataSet
192      * for which label you want.
193      *
194      * @param index
195      * @return Paint
196      *******************************************************************************************/

197     public Paint getPaint( int index )
198     {
199         return (Paint) this.paints.get( index );
200     }
201
202
203     /******************************************************************************************
204      *
205      * @return ChartTypeProperties
206      *******************************************************************************************/

207     public ChartTypeProperties getChartTypeProperties()
208     {
209         return this.scatterPlotProperties;
210     }
211
212
213     /******************************************************************************************
214      * Returns the number of elements in the data set.
215      *
216      * @return int
217      *******************************************************************************************/

218     public int getNumberOfDataSets()
219     {
220         return this.data.size();
221     }
222
223
224     /******************************************************************************************
225      * Returns the number of elements in the data set.
226      *
227      * @return int
228      *******************************************************************************************/

229     public int getNumberOfDataItems()
230     {
231         return this.numDataItems;
232     }
233
234
235     /*****************************************************************************************
236      * Take advantage of the face Cloning performs better than creating new for highly used
237      * Objects.
238      *
239      * @return Point2D.Double
240      ****************************************************************************************/

241     public static final Point2D.Double JavaDoc createPoint2DDouble()
242     {
243         return (Point2D.Double JavaDoc) POINT.clone();
244     }
245
246
247     /*********************************************************************************************
248      * Enables the testing routines to display the contents of this Object.
249      *
250      * @param htmlGenerator
251      **********************************************************************************************/

252     public void toHTML( HTMLGenerator htmlGenerator )
253     {
254         htmlGenerator.propertiesTableStart( this.getClass().getName() );
255         htmlGenerator.addTableRow( "data", HTMLGenerator.arrayToString( this.data.toArray( new Point2D.Double JavaDoc[ this.legendLabels.size() ] ) ) );
256
257         if( this.legendLabels != null )
258         {
259             htmlGenerator.addTableRow( "legendLabels", HTMLGenerator.arrayToString( this.legendLabels.toArray( new String JavaDoc[ this.legendLabels.size() ] ) ) );
260         }
261         htmlGenerator.addTableRow( "paints", HTMLGenerator.arrayToString( this.paints.toArray( new Paint[ this.paints.size() ] ) ) );
262         htmlGenerator.propertiesTableEnd();
263
264         htmlGenerator.chartTableRowStart();
265         this.scatterPlotProperties.toHTML( htmlGenerator );
266         htmlGenerator.chartTableRowEnd();
267     }
268
269 }
270
Popular Tags