001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: StackedChartDataModelConstraints.java
021: Created on 26. Sept. 2002
022: */
023:
024: package de.progra.charting.model;
025:
026: import de.progra.charting.ChartUtilities;
027: import de.progra.charting.CoordSystem;
028: import java.util.TreeSet;
029:
030: /**
031: * Implementing the ChartDataModelConstraints this class provides an implementation
032: * for the data model constraints where the maximum value is the sum of all
033: * positive values and the minimum value is the sum of all negative values.
034: * @author smueller
035: */
036: public class StackedChartDataModelConstraints implements
037: ChartDataModelConstraints {
038:
039: /** The model for which to calculate the constraints. */
040: protected AbstractChartDataModel model;
041:
042: /** The axis to compute the constraints. */
043: protected int axis;
044:
045: /** A flag which determines if column values should be manually scalable. */
046: protected boolean allowManualColScale = true;
047:
048: /** Creates a new instance of DefaultChartDataModelConstraints */
049: public StackedChartDataModelConstraints(
050: AbstractChartDataModel model, int axis) {
051: this .model = model;
052: this .axis = axis;
053: }
054:
055: /** Creates a new instance of DefaultChartDataModelConstraints
056: * @param model the AbstractDataModel for which constraints will be computed
057: * @param axis the y-axis which will be considered
058: * @param allowManualScale a flag which triggers if column values should
059: * be allowed to be scaled manually (default is yes)
060: */
061: public StackedChartDataModelConstraints(
062: AbstractChartDataModel model, int axis,
063: boolean allowManualColScale) {
064: this (model, axis);
065: this .allowManualColScale = allowManualColScale;
066: }
067:
068: /** Returns the maximum value of all datasets. */
069: public Number getMaximumValue() {
070: int minimumDataSetLength = Integer.MAX_VALUE;
071: double maxvalue = 0.0;
072: double minvalue = 0.0;
073:
074: double columnminvalue = Double.MAX_VALUE;
075: double columnmaxvalue = Double.MIN_VALUE;
076:
077: for (int i = 0; i < model.getDataSetNumber(); i++) {
078: minimumDataSetLength = Math.min(minimumDataSetLength, model
079: .getDataSetLength(i));
080: }
081:
082: double value = 0.0;
083:
084: for (int i = 0; i < minimumDataSetLength; i++) {
085: for (int j = 0; j < model.getDataSetNumber(); j++) {
086: value = model.getValueAt(j, i).doubleValue();
087: if (value < 0)
088: columnminvalue += value;
089: else
090: columnmaxvalue += value;
091: }
092: minvalue = Math.min(columnminvalue, minvalue);
093: columnminvalue = 0.0;
094: maxvalue = Math.max(columnmaxvalue, maxvalue);
095: columnmaxvalue = 0.0;
096: }
097:
098: if (model.getOrderedValues(CoordSystem.FIRST_YAXIS).size() == 0
099: || (maxvalue == 0.0 && minvalue == 0.0))
100: return new Integer(1);
101: else if (model.isManualScale()) {
102: return new Double(Math.max(model.getManualMaximumValue()
103: .doubleValue(), maxvalue));
104: } else if (model.isAutoScale()) {
105: if (minvalue / maxvalue > 0.95) {
106: //System.out.println("** ChartUtilities.performAutoScale(min/2, 2 * max)[1]"+ChartUtilities.performAutoScale(min/2, 2 * max)[1]);
107: return new Double(ChartUtilities.performAutoScale(
108: minvalue / 2, 2 * maxvalue)[1]);
109: } else {
110: //System.out.println("** ChartUtilities.performAutoScale(min, max)[1]"+ChartUtilities.performAutoScale(min, max)[1]);
111: return new Double(ChartUtilities.performAutoScale(
112: minvalue, maxvalue)[1]);
113: }
114: } else
115: return new Double(maxvalue);
116: }
117:
118: /** Returns the minimum value of all datasets. */
119: public Number getMinimumValue() {
120: int minimumDataSetLength = Integer.MAX_VALUE;
121: double maxvalue = 0.0;
122: double minvalue = 0.0;
123: double columnmaxvalue = 0.0;
124: double columnminvalue = 0.0;
125:
126: for (int i = 0; i < model.getDataSetNumber(); i++) {
127: minimumDataSetLength = Math.min(minimumDataSetLength, model
128: .getDataSetLength(i));
129: }
130:
131: double value = 0.0;
132:
133: for (int i = 0; i < minimumDataSetLength; i++) {
134: for (int j = 0; j < model.getDataSetNumber(); j++) {
135: value = model.getValueAt(j, i).doubleValue();
136: if (value < 0)
137: columnminvalue += value;
138: else
139: columnmaxvalue += value;
140: }
141: minvalue = Math.min(columnminvalue, minvalue);
142: columnminvalue = 0.0;
143: maxvalue = Math.max(columnmaxvalue, maxvalue);
144: columnmaxvalue = 0.0;
145: }
146:
147: if (model.getOrderedValues(CoordSystem.FIRST_YAXIS).size() == 0
148: || (maxvalue == 0.0 && minvalue == 0.0))
149: return new Integer(0);
150: else if (model.isManualScale()) {
151: //System.out.println("** model.getManualMinimumValue() = "+model.getManualMinimumValue());
152: return new Double(Math.min(model.getManualMinimumValue()
153: .doubleValue(), minvalue));
154: } else if (model.isAutoScale()) {
155: //System.out.println("** min = "+min+" max = "+max);
156:
157: if (minvalue / maxvalue > 0.95) {
158: //System.out.println("** ChartUtilities.performAutoScale(min/2, 2 * max)[0]"+ChartUtilities.performAutoScale(min/2, 2 * max)[0]);
159: return new Double(ChartUtilities.performAutoScale(
160: minvalue / 2, 2 * maxvalue)[0]);
161: } else {
162: //System.out.println("** ChartUtilities.performAutoScale(min, max)[0]"+ChartUtilities.performAutoScale(min, max)[0]);
163: return new Double(ChartUtilities.performAutoScale(
164: minvalue, maxvalue)[0]);
165: }
166: } else
167: return new Double(minvalue);
168: }
169:
170: /** Returns the minimum column value.
171: * @throws ArrayIndexOutOfBoundsException if the Model is empty
172: */
173: public double getMinimumColumnValue() {
174: if (model.isManualScale() && allowManualColScale) {
175: return model.getManualMinimumColumnValue();
176: }
177: if (model.isAutoScale())
178: return ChartUtilities.performAutoScale(model
179: .getFirstColumnValue(), model.getLastColumnValue())[0];
180: else
181: return model.getFirstColumnValue();
182: }
183:
184: /** Returns the maximum column value.
185: * @throws ArrayIndexOutOfBoundsException if the model is empty
186: */
187: public double getMaximumColumnValue() {
188: if (model.isManualScale() && allowManualColScale) {
189: return model.getManualMaximumColumnValue();
190: }
191: if (model.isAutoScale())
192: return ChartUtilities.performAutoScale(model
193: .getFirstColumnValue(), model.getLastColumnValue())[1];
194: else
195: return model.getLastColumnValue();
196: }
197: }
|