KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package de.progra.charting.model;
25
26 import de.progra.charting.ChartUtilities;
27 import java.util.TreeSet JavaDoc;
28
29 /**
30  * Implementing the ChartDataModelConstraints this class provides the default implementation
31  * for the data model constraints. Alternative implementations could return the sum of all
32  * column values to implement stacked bar charts e.g.
33  * @author smueller
34  */

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

58     public DefaultChartDataModelConstraints(AbstractChartDataModel model, int axis, boolean allowManualColScale) {
59         this(model, axis);
60         this.allowManualColScale = allowManualColScale;
61     }
62     
63     /** Returns the maximum value of all datasets. */
64     public Number JavaDoc getMaximumValue() {
65         TreeSet JavaDoc ordered_values = (TreeSet JavaDoc)model.getOrderedValues(axis);
66         
67         if(ordered_values.size() == 0)
68             return new Integer JavaDoc(1);
69         else if(model.isManualScale()) {
70             //System.out.println("** model.getManualMaximumValue() = "+model.getManualMaximumValue());
71
return model.getManualMaximumValue();
72         }
73         else if(model.isAutoScale()) {
74             double min = ((Number JavaDoc)ordered_values.first()).doubleValue();
75             double max = ((Number JavaDoc)ordered_values.last()).doubleValue();
76             
77             //System.out.println("** min = "+min+" max = "+max);
78

79             if(min / max > 0.95) {
80                 //System.out.println("** ChartUtilities.performAutoScale(min/2, 2 * max)[1]"+ChartUtilities.performAutoScale(min/2, 2 * max)[1]);
81
return new Double JavaDoc(ChartUtilities.performAutoScale(min/2,
82                                                                   2 * max)[1]);
83              }
84             else {
85                 //System.out.println("** ChartUtilities.performAutoScale(min, max)[1]"+ChartUtilities.performAutoScale(min, max)[1]);
86
return new Double JavaDoc(ChartUtilities.performAutoScale(min,
87                                                               max)[1]);
88              }
89         } else
90             return (Number JavaDoc)ordered_values.last();
91     }
92
93     /** Returns the minimum value of all datasets. */
94     public Number JavaDoc getMinimumValue() {
95         TreeSet JavaDoc ordered_values = (TreeSet JavaDoc)model.getOrderedValues(axis);
96
97         if(ordered_values.size() == 0)
98             return new Integer JavaDoc(0);
99         else if(model.isManualScale()) {
100             //System.out.println("** model.getManualMinimumValue() = "+model.getManualMinimumValue());
101
return model.getManualMinimumValue();
102         }
103         else if(model.isAutoScale()) {
104             double min = ((Number JavaDoc)ordered_values.first()).doubleValue();
105             double max = ((Number JavaDoc)ordered_values.last()).doubleValue();
106
107             //System.out.println("** min = "+min+" max = "+max);
108

109             if(min / max > 0.95) {
110                 //System.out.println("** ChartUtilities.performAutoScale(min/2, 2 * max)[0]"+ChartUtilities.performAutoScale(min/2, 2 * max)[0]);
111
return new Double JavaDoc(ChartUtilities.performAutoScale(min/2,
112                                                                   2 * max)[0]);
113              }
114             else {
115                 //System.out.println("** ChartUtilities.performAutoScale(min, max)[0]"+ChartUtilities.performAutoScale(min, max)[0]);
116
return new Double JavaDoc(ChartUtilities.performAutoScale(min,
117                                                               max)[0]);
118              }
119         } else
120            return (Number JavaDoc)ordered_values.first();
121     }
122
123     /** Returns the minimum column value.
124      * @throws ArrayIndexOutOfBoundsException if the Model is empty
125      */

126     public double getMinimumColumnValue() {
127         if(model.isManualScale() && allowManualColScale) {
128             return model.getManualMinimumColumnValue();
129         }
130         if(model.isAutoScale())
131             return ChartUtilities.performAutoScale(model.getFirstColumnValue(),
132                                                    model.getLastColumnValue())[0];
133         else
134             return model.getFirstColumnValue();
135     }
136
137     /** Returns the maximum column value.
138      * @throws ArrayIndexOutOfBoundsException if the model is empty
139      */

140     public double getMaximumColumnValue() {
141         if(model.isManualScale() && allowManualColScale) {
142             return model.getManualMaximumColumnValue();
143         }
144         if(model.isAutoScale())
145             return ChartUtilities.performAutoScale(model.getFirstColumnValue(),
146                                                    model.getLastColumnValue())[1];
147         else
148             return model.getLastColumnValue();
149     }
150 }
Popular Tags