KickJava   Java API By Example, From Geeks To Geeks.

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


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     StackedChartDataModelConstraints.java
21     Created on 26. Sept. 2002
22 */

23
24 package de.progra.charting.model;
25
26 import de.progra.charting.ChartUtilities;
27 import de.progra.charting.CoordSystem;
28 import java.util.TreeSet JavaDoc;
29
30 /**
31  * Implementing the ChartDataModelConstraints this class provides an implementation
32  * for the data model constraints where the maximum value is the sum of all
33  * positive values and the minimum value is the sum of all negative values.
34  * @author smueller
35  */

36 public class StackedChartDataModelConstraints implements ChartDataModelConstraints {
37     
38     /** The model for which to calculate the constraints. */
39     protected AbstractChartDataModel model;
40     
41     /** The axis to compute the constraints. */
42     protected int axis;
43     
44     /** A flag which determines if column values should be manually scalable. */
45     protected boolean allowManualColScale = true;
46     
47     /** Creates a new instance of DefaultChartDataModelConstraints */
48     public StackedChartDataModelConstraints(AbstractChartDataModel model, int axis) {
49         this.model = model;
50         this.axis = axis;
51     }
52     
53     /** Creates a new instance of DefaultChartDataModelConstraints
54      * @param model the AbstractDataModel for which constraints will be computed
55      * @param axis the y-axis which will be considered
56      * @param allowManualScale a flag which triggers if column values should
57      * be allowed to be scaled manually (default is yes)
58      */

59     public StackedChartDataModelConstraints(AbstractChartDataModel model, int axis, boolean allowManualColScale) {
60         this(model, axis);
61         this.allowManualColScale = allowManualColScale;
62     }
63     
64     /** Returns the maximum value of all datasets. */
65     public Number JavaDoc getMaximumValue() {
66         int minimumDataSetLength = Integer.MAX_VALUE;
67         double maxvalue = 0.0;
68         double minvalue = 0.0;
69         
70         double columnminvalue = Double.MAX_VALUE;
71         double columnmaxvalue = Double.MIN_VALUE;
72         
73         for(int i = 0; i < model.getDataSetNumber(); i++) {
74             minimumDataSetLength = Math.min(minimumDataSetLength, model.getDataSetLength(i));
75         }
76         
77         double value = 0.0;
78         
79         for(int i = 0; i < minimumDataSetLength; i++) {
80             for(int j = 0; j < model.getDataSetNumber(); j++) {
81                 value = model.getValueAt(j, i).doubleValue();
82                 if(value < 0)
83                     columnminvalue += value;
84                 else
85                     columnmaxvalue += value;
86             }
87             minvalue = Math.min(columnminvalue, minvalue);
88             columnminvalue = 0.0;
89             maxvalue = Math.max(columnmaxvalue, maxvalue);
90             columnmaxvalue = 0.0;
91         }
92         
93         if(model.getOrderedValues(CoordSystem.FIRST_YAXIS).size() == 0
94         || (maxvalue == 0.0 && minvalue == 0.0))
95             return new Integer JavaDoc(1);
96         else if(model.isManualScale()) {
97             return new Double JavaDoc(Math.max(model.getManualMaximumValue().doubleValue(), maxvalue));
98         }
99         else if(model.isAutoScale()) {
100             if(minvalue / maxvalue > 0.95) {
101                 //System.out.println("** ChartUtilities.performAutoScale(min/2, 2 * max)[1]"+ChartUtilities.performAutoScale(min/2, 2 * max)[1]);
102
return new Double JavaDoc(ChartUtilities.performAutoScale(minvalue/2,
103                                                                   2 * maxvalue)[1]);
104              }
105             else {
106                 //System.out.println("** ChartUtilities.performAutoScale(min, max)[1]"+ChartUtilities.performAutoScale(min, max)[1]);
107
return new Double JavaDoc(ChartUtilities.performAutoScale(minvalue,
108                                                               maxvalue)[1]);
109              }
110         } else
111             return new Double JavaDoc(maxvalue);
112     }
113
114     /** Returns the minimum value of all datasets. */
115     public Number JavaDoc getMinimumValue() {
116         int minimumDataSetLength = Integer.MAX_VALUE;
117         double maxvalue = 0.0;
118         double minvalue = 0.0;
119         double columnmaxvalue = 0.0;
120         double columnminvalue = 0.0;
121         
122         for(int i = 0; i < model.getDataSetNumber(); i++) {
123             minimumDataSetLength = Math.min(minimumDataSetLength, model.getDataSetLength(i));
124         }
125         
126         double value = 0.0;
127         
128         for(int i = 0; i < minimumDataSetLength; i++) {
129             for(int j = 0; j < model.getDataSetNumber(); j++) {
130                 value = model.getValueAt(j, i).doubleValue();
131                 if(value < 0)
132                     columnminvalue += value;
133                 else
134                     columnmaxvalue += value;
135             }
136             minvalue = Math.min(columnminvalue, minvalue);
137             columnminvalue = 0.0;
138             maxvalue = Math.max(columnmaxvalue, maxvalue);
139             columnmaxvalue = 0.0;
140         }
141
142         if(model.getOrderedValues(CoordSystem.FIRST_YAXIS).size() == 0
143         || (maxvalue == 0.0 && minvalue == 0.0))
144             return new Integer JavaDoc(0);
145         else if(model.isManualScale()) {
146             //System.out.println("** model.getManualMinimumValue() = "+model.getManualMinimumValue());
147
return new Double JavaDoc(Math.min(model.getManualMinimumValue().doubleValue(), minvalue));
148         }
149         else if(model.isAutoScale()) {
150             //System.out.println("** min = "+min+" max = "+max);
151

152             if(minvalue / maxvalue > 0.95) {
153                 //System.out.println("** ChartUtilities.performAutoScale(min/2, 2 * max)[0]"+ChartUtilities.performAutoScale(min/2, 2 * max)[0]);
154
return new Double JavaDoc(ChartUtilities.performAutoScale(minvalue/2,
155                                                                   2 * maxvalue)[0]);
156              }
157             else {
158                 //System.out.println("** ChartUtilities.performAutoScale(min, max)[0]"+ChartUtilities.performAutoScale(min, max)[0]);
159
return new Double JavaDoc(ChartUtilities.performAutoScale(minvalue,
160                                                               maxvalue)[0]);
161              }
162         } else
163            return new Double JavaDoc(minvalue);
164     }
165
166     /** Returns the minimum column value.
167      * @throws ArrayIndexOutOfBoundsException if the Model is empty
168      */

169     public double getMinimumColumnValue() {
170         if(model.isManualScale() && allowManualColScale) {
171             return model.getManualMinimumColumnValue();
172         }
173         if(model.isAutoScale())
174             return ChartUtilities.performAutoScale(model.getFirstColumnValue(),
175                                                    model.getLastColumnValue())[0];
176         else
177             return model.getFirstColumnValue();
178     }
179
180     /** Returns the maximum column value.
181      * @throws ArrayIndexOutOfBoundsException if the model is empty
182      */

183     public double getMaximumColumnValue() {
184         if(model.isManualScale() && allowManualColScale) {
185             return model.getManualMaximumColumnValue();
186         }
187         if(model.isAutoScale())
188             return ChartUtilities.performAutoScale(model.getFirstColumnValue(),
189                                                    model.getLastColumnValue())[1];
190         else
191             return model.getLastColumnValue();
192     }
193 }
194
Popular Tags