KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > model > DefaultChartDataModel


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     DefaultChartDataModel.java
21     Created on 28. Juni 2001, 20:41
22 */

23
24 package de.progra.charting.model;
25
26 import de.progra.charting.CoordSystem;
27 import de.progra.charting.ChartUtilities;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.HashMap JavaDoc;
33
34 /**
35  * Implements a default ChartModel. It uses a DataSet[] and the columns
36  * are numeric. It's purely read-only.
37  * @author mueller
38  * @version 1.0
39  */

40 public class DefaultChartDataModel extends AbstractChartDataModel {
41     
42     /** The sorted x-axis values used for calculating the constraints. */
43     protected TreeSet JavaDoc columnSet = new TreeSet JavaDoc();
44     
45     /** The DataSet list.*/
46     protected ArrayList JavaDoc data = new ArrayList JavaDoc();
47     
48     /** A HashMap containing the ordered data used for calculating the constraints. */
49     protected HashMap JavaDoc valuesbyaxis = new HashMap JavaDoc();
50     
51     /** The constraints for the first and second y-axes.*/
52     protected ChartDataModelConstraints constraints1, constraints2;
53     
54     /** Creates a new empty DefaultChartDataModel.
55      */

56     public DefaultChartDataModel() {
57         TreeSet JavaDoc set1 = new TreeSet JavaDoc();
58         valuesbyaxis.put(new Integer JavaDoc(CoordSystem.FIRST_YAXIS), set1);
59         TreeSet JavaDoc set2 = new TreeSet JavaDoc();
60         valuesbyaxis.put(new Integer JavaDoc(CoordSystem.SECOND_YAXIS), set2);
61         constraints1 = new DefaultChartDataModelConstraints(this, CoordSystem.FIRST_YAXIS);
62         constraints2 = new DefaultChartDataModelConstraints(this, CoordSystem.SECOND_YAXIS);
63     }
64     
65     /** Creates new DefaultChartDataModel with the default axis binding.
66      * @param data the array of values. The first index specifies the
67      * datasets, the last one is the value index.
68      * @param columns the array of x-axis values. The length of the
69      * datasets and the length of the column should be equal and the columns should
70      * be ordered.
71      * @param rows the array of DataSet titles. It has to have the same
72      * length as the number of DataSets.
73      */

74     public DefaultChartDataModel(Number JavaDoc[][] data,
75                                  double[] columns,
76                                  String JavaDoc[] rows) {
77         this();
78         
79         columnSet.addAll(Arrays.asList(ChartUtilities.transformArray(columns)));
80         
81         TreeSet JavaDoc set = (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(CoordSystem.FIRST_YAXIS));
82                 
83         ChartUtilities.addDataToSet(set, data);
84         trimSet(set);
85         
86         for(int i = 0; i < data.length; i++) {
87             this.data.add(new DefaultDataSet(data[i],
88                                               ChartUtilities.transformArray(columns),
89                                               CoordSystem.FIRST_YAXIS,
90                                               rows[i]));
91         }
92     }
93   
94     /** Creates new DefaultChartDataModel.
95      * @param data the array of values. The first index specifies the
96      * datasets, the last one is the value index.
97      * @param columns the array of x-axis values. The length of the
98      * datasets and the length of the column should be equal and
99      * the columns should be ordered.
100      * @param rows the array of DataSet titles. It has to have the same
101      * length as the number of DataSets.
102      */

103     public DefaultChartDataModel(int[][] data,
104                                  double[] columns,
105                                  String JavaDoc[] rows) {
106         this();
107         
108         Number JavaDoc[][] numdata = ChartUtilities.transformArray(data);
109         
110         columnSet.addAll(Arrays.asList(ChartUtilities.transformArray(columns)));
111         
112         TreeSet JavaDoc set =
113             (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(CoordSystem.FIRST_YAXIS));
114         
115         ChartUtilities.addDataToSet(set, numdata);
116
117         trimSet(set);
118         
119         for(int i = 0; i < data.length; i++) {
120             this.data.add(new DefaultDataSet(numdata[i],
121                                               ChartUtilities.transformArray(columns),
122                                               CoordSystem.FIRST_YAXIS,
123                                               rows[i]));
124         }
125     }
126     
127     /** Creates new DefaultChartDataModel.
128      * @param data the array of values. The first index specifies the
129      * datasets, the last one is the value index.
130      * @param columns the array of x-axis values. The length of the
131      * datasets and the length of the column should be equal and
132      * the columns should be ordered.
133      * @param rows the array of DataSet titles. It has to have the same
134      * length as the number of DataSets.
135      */

136     public DefaultChartDataModel(double[][] data,
137                                  double[] columns,
138                                  String JavaDoc[] rows) {
139         this();
140         
141         Number JavaDoc[][] numdata = ChartUtilities.transformArray(data);
142         
143         columnSet.addAll(Arrays.asList(ChartUtilities.transformArray(columns)));
144         
145         TreeSet JavaDoc set =
146             (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(CoordSystem.FIRST_YAXIS));
147         
148         ChartUtilities.addDataToSet(set, numdata);
149         trimSet(set);
150         for(int i = 0; i < data.length; i++) {
151             this.data.add(new DefaultDataSet(numdata[i],
152                                               ChartUtilities.transformArray(columns),
153                                               CoordSystem.FIRST_YAXIS,
154                                               rows[i]));
155         }
156     }
157     
158     /** Creates a new DefaultChartDataModel using the
159      * given array of DataSets, effectively enabling the creation
160      * of DataModels with differently sized DataSets.
161      * @param ds the array of DataSets to be used.
162      */

163     public DefaultChartDataModel(DataSet[] ds) {
164         this();
165         
166         TreeSet JavaDoc set;
167         for(int i = 0; i < ds.length; i++) {
168             data.add(ds[i]);
169             set = (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(ds[i].getYAxis()));
170             for(int j = 0; j < ds[i].getDataSetLength(); j++) {
171                 columnSet.add(ds[i].getColumnValueAt(j));
172                 set.add(ds[i].getValueAt(j));
173                 
174                 trimSet(set);
175             }
176         }
177     }
178
179     /** Returns the length of a certain dataset.
180      * @param set the DataSet
181      * @return the length of the DataSet
182      */

183     public int getDataSetLength(int set) {
184         return ((DataSet)data.get(set)).getDataSetLength();
185     }
186     
187     /** Returns the total amount of datasets.
188      * @return the amount of DataSets
189      */

190     public int getDataSetNumber() {
191         return data.size();
192     }
193     
194     /** Returns the title of the DataSet. This is the number of
195      * the DataSet per default.
196      * @param set the DataSet index
197      * @return the String title
198      */

199     public String JavaDoc getDataSetName(int set) {
200         return ((DataSet)data.get(set)).getTitle();
201     }
202     
203     /** Returns the axis binding for a DataSet
204      * @param set the DataSet index
205      * @return an axis binding constant
206      */

207     public int getAxisBinding(int set) {
208         return ((DataSet)data.get(set)).getYAxis();
209     }
210     
211     /** Returns true if the columns are numeric.
212      * @return <CODE>true</CODE>
213      */

214     public boolean isColumnNumeric() {
215         return true;
216     }
217     
218     /** Returns the class of the column values.
219      * @return <CODE>Double.class</CODE>
220      */

221     public Class JavaDoc getColumnClass() {
222         return Double JavaDoc.class;
223     }
224     
225     /** Returns the Value in a specific dataset at a certain index. *
226      * @param set the DataSet index
227      * @param index the value index
228      * @return the Number value at the specified position
229      */

230     public Number JavaDoc getValueAt(int set, int index) {
231         return (Number JavaDoc)((DataSet)data.get(set)).getValueAt(index);
232     }
233     
234
235     /** Use getColumnValue(int set, int col) instead, because DefaultChartDataModel
236      * can contain DataSets with different lengths and column values.
237      * @return null
238      */

239     public Object JavaDoc getColumnValueAt(int col) {
240         return null;
241     }
242     
243     /** Returns a specific column value.
244      * @return the column value or <code>null</code> if the column doesn't exist.
245      * @param col the column index
246      * @param set the DataSet of which the column value is desired
247      */

248     public Object JavaDoc getColumnValueAt(int set, int col) {
249         // PENDING: Why do we create a new Double here?
250
if(col < getDataSetLength(set))
251             return new Double JavaDoc(((Number JavaDoc)((DataSet)data.get(set)).getColumnValueAt(col)).doubleValue());
252         else
253             return null;
254     }
255     
256      
257     /** Returns a ChartDataModelConstraints Object for a given axis.
258      * This way, there are different constraints for the first and for
259      * the second y-axis. If the model is empty, the maximum values are 1 and
260      * the minimum values are 0, thus enabling proper rendering.
261      * @param axis the axis constant.
262      * @return a ChartDataModelConstraints object with the constraints
263      * for the specified y-axis.
264      */

265     public ChartDataModelConstraints getChartDataModelConstraints(final int axis) {
266         if(axis == CoordSystem.FIRST_YAXIS)
267             return constraints1;
268         else
269             return constraints2;
270     }
271     
272     /** Sets the ChartDataModelConstraints object for the given
273      * axis binding.
274      * @param axis the Axis constant
275      * @param constraints the ChartDataModelConstraints object
276      * @return a ChartDataModelConstraints object.
277      */

278     public void setChartDataModelConstraints(int axis, ChartDataModelConstraints constraints) {
279         if(axis == CoordSystem.FIRST_YAXIS)
280             constraints1 = constraints;
281         else
282             constraints2 = constraints;
283     }
284     
285     /** Removes infinite and NaN values from a TreeSet. Called with the TreeSet
286      * containing all values. If asymptotic functions are plotted, infinite values
287      * are the max / min values, resulting in bogus point-to-pixel rations. Therefore,
288      * these values are omitted from these calculations.
289      */

290     protected void trimSet(TreeSet JavaDoc s) {
291        while(((Number JavaDoc)s.first()).doubleValue() == Double.NEGATIVE_INFINITY) {
292            s.remove(s.first());
293        }
294        double last = ((Number JavaDoc)s.last()).doubleValue();
295
296        while(last == Double.POSITIVE_INFINITY
297             || last != last) {
298                 s.remove(s.last());
299                 last = ((Number JavaDoc)s.last()).doubleValue();
300        }
301     }
302     
303     /** Returns an ordered set of all data values for the specified axis.
304      * This is called by the ChartDataModelConstraints classes.
305      */

306     protected TreeSet JavaDoc getOrderedValues(int axis) {
307         return (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(axis));
308     }
309     
310     /** Returns the first ordered column value for use by the ChartDataModelConstraints. */
311     protected double getFirstColumnValue() {
312         return ((Number JavaDoc)columnSet.first()).doubleValue();
313     }
314     
315     /** Returns the last ordered column value for use by the ChartDataModelConstraints. */
316     protected double getLastColumnValue() {
317         return ((Number JavaDoc)columnSet.last()).doubleValue();
318     }
319 }
320
Popular Tags