KickJava   Java API By Example, From Geeks To Geeks.

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


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     ObjectColumnChartDataModel.java
21     Created on 7. August 2001, 17:27
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.TreeSet JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32 /**
33  * The ObjectChartDataModel implements a ChartDataModel for Charts,
34  * which have e.g. String values on the x-axis. This is especially useful
35  * for Barcharts and also for Piecharts, although those don't exactly have
36  * an x-axis.
37  * @author mueller
38  * @version 1.0
39  */

40 public class ObjectChartDataModel extends AbstractChartDataModel {
41     
42     /** The x-axis values. */
43     protected Object JavaDoc[] columns;
44         
45     /** The data values.*/
46     protected DataSet[] data;
47     
48     /** A HashMap containing the ordered data per axis. */
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      * Initializes all Objects and Arrays as empty ones.
56      */

57     public ObjectChartDataModel() {
58         data = new DefaultDataSet[0];
59         columns = new Object JavaDoc[0];
60         
61         TreeSet JavaDoc set1 = new TreeSet JavaDoc();
62         valuesbyaxis.put(new Integer JavaDoc(CoordSystem.FIRST_YAXIS), set1);
63         
64         TreeSet JavaDoc set2 = new TreeSet JavaDoc();
65         valuesbyaxis.put(new Integer JavaDoc(CoordSystem.SECOND_YAXIS), set2);
66         
67         constraints1 = new DefaultChartDataModelConstraints(this, CoordSystem.FIRST_YAXIS, false);
68         constraints2 = new DefaultChartDataModelConstraints(this, CoordSystem.SECOND_YAXIS, false);
69     }
70     
71     /** Creates new ObjectChartDataModel with the default axis binding.
72      * @param data the array of values. The first index specifies the
73      * datasets, the last one is the value index.
74      * @param columns the array of x-axis values. The length of the
75      * datasets and the length of the column should be equal and the columns should
76      * be ordered.
77      * @param rows the DataSet titles
78      */

79     public ObjectChartDataModel(Number JavaDoc[][] data, Object JavaDoc[] columns, String JavaDoc rows[]) {
80         this();
81         
82         this.columns = columns;
83         
84         this.data = new DefaultDataSet[data.length];
85         
86         TreeSet JavaDoc set =
87             (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(CoordSystem.FIRST_YAXIS));
88                 
89         ChartUtilities.addDataToSet(set, data);
90         
91         for(int i = 0; i < data.length; i++) {
92             this.data[i] = new DefaultDataSet(data[i],
93                                               columns,
94                                               CoordSystem.FIRST_YAXIS,
95                                               rows[i]);
96         }
97     }
98   
99     /** Creates new ObjectChartDataModel.
100      * @param data the array of values. The first index specifies the
101      * datasets, the last one is the value index.
102      * @param columns the array of x-axis values. The length of the
103      * datasets and the length of the column should be equal and
104      * the columns should be ordered.
105      * @param rows the DataSet titles
106      */

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

138     public ObjectChartDataModel(double[][] data, Object JavaDoc[] columns, String JavaDoc[] rows) {
139         this();
140         
141         Number JavaDoc[][] numdata = ChartUtilities.transformArray(data);
142         
143         this.columns = columns;
144         
145         this.data = new DefaultDataSet[data.length];
146         
147         TreeSet JavaDoc set =
148             (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(CoordSystem.FIRST_YAXIS));
149         
150         ChartUtilities.addDataToSet(set, numdata);
151         
152         for(int i = 0; i < data.length; i++) {
153             this.data[i] = new DefaultDataSet(numdata[i],
154                                               columns,
155                                               CoordSystem.FIRST_YAXIS,
156                                               rows[i]);
157         }
158     }
159     
160     /** Creates a new ObjectChartDataModel using the
161      * given array of DataSets, effectively enabling the creation
162      * of DataModels with differently sized DataSets. Internally, the DataSets
163      * are transformed into equally sized DataSets, where the missing data fields
164      * are filled with Double.NaN.
165      * @param ds the array of DataSets to be used.
166      * @param columns the array of column values. This needs to be supplied,
167      * because using Objects as x-axis values you need to have an ordered
168      * superset of all column values especially if different DataSets only
169      * contain some column values
170      */

171     public ObjectChartDataModel(DataSet[] ds, Object JavaDoc[] columns) {
172         this();
173         data = ds;
174         this.columns = columns;
175         
176         TreeSet JavaDoc set;
177             
178         HashMap JavaDoc map = new HashMap JavaDoc();
179         
180         for(int i = 0; i < ds.length; i++) {
181             map.clear();
182             Number JavaDoc[] numdata = new Number JavaDoc[columns.length];
183             
184             for(int j = 0; j < columns.length; j++)
185                 map.put(columns[j], new Double JavaDoc(Double.NaN));
186             
187             set = (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(ds[i].getYAxis()));
188             
189             for(int j = 0; j < ds[i].getDataSetLength(); j++) {
190                 map.put(ds[i].getColumnValueAt(j), ds[i].getValueAt(j));
191                 set.add(ds[i].getValueAt(j));
192             }
193                     
194             for(int j = 0; j < columns.length; j++) {
195                 numdata[j] = (Number JavaDoc)map.get(columns[j]);
196             }
197             
198             data[i] = new DefaultDataSet(numdata,
199                                          columns,
200                                          CoordSystem.FIRST_YAXIS,
201                                          ds[i].getTitle());
202         }
203     }
204
205     /** Returns the length of a certain dataset.
206      * @param set the DataSet index
207      * @return the DataSet length
208      */

209     public int getDataSetLength(int set) {
210         return data[set].getDataSetLength();
211     }
212     
213     /** Returns the total amount of datasets.
214      * @return the amount of DataSet
215      */

216     public int getDataSetNumber() {
217         return data.length;
218     }
219     
220     
221     /** Returns the title of the DataSet. This is the number of
222      * the DataSet per default.
223      * @param set the DataSet index
224      * @return the String title
225      */

226     public String JavaDoc getDataSetName(int set) {
227         return data[set].getTitle();
228     }
229     
230     /** Returns the axis to which a DataSet is attached
231      * @param set the DataSet index
232      * @return the axis constant
233      */

234     public int getAxisBinding(int set) {
235         return data[set].getYAxis();
236     }
237     
238     /** Returns the Value in a specific dataset at a certain index.
239      * @param set the DataSet index
240      * @param index the value index
241      * @return the Number value
242      */

243     public Number JavaDoc getValueAt(int set, int index) {
244         return (Number JavaDoc)data[set].getValueAt(index);
245     }
246     
247     /** Returns a ChartDataModelConstraints Object for a given axis.
248      * This way, there are different constraints for the first and for
249      * the second y-axis. If the model is empty, the maximum values are 1 and
250      * the minimum values are 0, thus enabling proper rendering.
251      * @param axis the axis constant
252      * @return the ChartDataModelConstraints for the defined y-axis
253      */

254     public ChartDataModelConstraints getChartDataModelConstraints(final int axis) {
255         if(axis == CoordSystem.FIRST_YAXIS)
256             return constraints1;
257         else
258             return constraints2;
259     }
260     
261     /** Sets the ChartDataModelConstraints object for the given
262      * axis binding.
263      * @param axis the Axis constant
264      * @param constraints the ChartDataModelConstraints object
265      * @return a ChartDataModelConstraints object.
266      */

267     public void setChartDataModelConstraints(int axis, ChartDataModelConstraints constraints) {
268         if(axis == CoordSystem.FIRST_YAXIS)
269             constraints1 = constraints;
270         else
271             constraints2 = constraints;
272     }
273
274     /** Returns a specific column value.
275      * @return the column value or <code>null</code> if the column doesn't exist.
276      * @param col the column index
277      */

278     public Object JavaDoc getColumnValueAt(int col) {
279         if(col < columns.length)
280             return columns[col];
281         else
282             return null;
283     }
284     
285     /** Calls getColumnValueAt(int col).
286      * @return the column value or <code>null</code> if the column doesn't exist.
287      * @param col the column index
288      */

289     public Object JavaDoc getColumnValueAt(int set, int col) {
290        return getColumnValueAt(col);
291     }
292     
293     /** Returns an ordered set of all data values for the specified axis.
294      * This is called by the ChartDataModelConstraints classes.
295      */

296     protected TreeSet JavaDoc getOrderedValues(int axis) {
297         return (TreeSet JavaDoc)valuesbyaxis.get(new Integer JavaDoc(axis));
298     }
299     
300     /** Is called by the ChartDataModelConstraints Object to compute the minimum column value.
301      * @return Returns 0.0.
302      */

303     protected double getFirstColumnValue() {
304         return 0.0;
305     }
306     
307     /** Is called by the ChartDataModelConstraints Object to compute the maximum column value.
308      * @return Returns <code>columns.length</code>.
309      */

310     protected double getLastColumnValue() {
311         return Math.max((double)columns.length, 1.0);
312     }
313 }
314
Popular Tags