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: DefaultChartDataModelConstraints.java
021: Created on 16. Sept. 2002
022: */
023:
024: package de.progra.charting.model;
025:
026: import de.progra.charting.ChartUtilities;
027: import java.util.TreeSet;
028:
029: /**
030: * Implementing the ChartDataModelConstraints this class provides the default implementation
031: * for the data model constraints. Alternative implementations could return the sum of all
032: * column values to implement stacked bar charts e.g.
033: * @author smueller
034: */
035: public class DefaultChartDataModelConstraints implements
036: ChartDataModelConstraints {
037:
038: /** The model for which to calculate the constraints. */
039: protected AbstractChartDataModel model;
040:
041: /** The axis to compute the constraints. */
042: protected int axis;
043:
044: /** A flag which determines if column values should be manually scalable. */
045: protected boolean allowManualColScale = true;
046:
047: /** Creates a new instance of DefaultChartDataModelConstraints */
048: public DefaultChartDataModelConstraints(
049: AbstractChartDataModel model, int axis) {
050: this .model = model;
051: this .axis = axis;
052: }
053:
054: /** Creates a new instance of DefaultChartDataModelConstraints
055: * @param model the AbstractDataModel for which constraints will be computed
056: * @param axis the y-axis which will be considered
057: * @param allowManualScale a flag which triggers if column values should
058: * be allowed to be scaled manually (default is yes)
059: */
060: public DefaultChartDataModelConstraints(
061: AbstractChartDataModel model, int axis,
062: boolean allowManualColScale) {
063: this (model, axis);
064: this .allowManualColScale = allowManualColScale;
065: }
066:
067: /** Returns the maximum value of all datasets. */
068: public Number getMaximumValue() {
069: TreeSet ordered_values = (TreeSet) model.getOrderedValues(axis);
070:
071: if (ordered_values.size() == 0)
072: return new Integer(1);
073: else if (model.isManualScale()) {
074: //System.out.println("** model.getManualMaximumValue() = "+model.getManualMaximumValue());
075: return model.getManualMaximumValue();
076: } else if (model.isAutoScale()) {
077: double min = ((Number) ordered_values.first())
078: .doubleValue();
079: double max = ((Number) ordered_values.last()).doubleValue();
080:
081: //System.out.println("** min = "+min+" max = "+max);
082:
083: if (min / max > 0.95) {
084: //System.out.println("** ChartUtilities.performAutoScale(min/2, 2 * max)[1]"+ChartUtilities.performAutoScale(min/2, 2 * max)[1]);
085: return new Double(ChartUtilities.performAutoScale(
086: min / 2, 2 * max)[1]);
087: } else {
088: //System.out.println("** ChartUtilities.performAutoScale(min, max)[1]"+ChartUtilities.performAutoScale(min, max)[1]);
089: return new Double(ChartUtilities.performAutoScale(min,
090: max)[1]);
091: }
092: } else
093: return (Number) ordered_values.last();
094: }
095:
096: /** Returns the minimum value of all datasets. */
097: public Number getMinimumValue() {
098: TreeSet ordered_values = (TreeSet) model.getOrderedValues(axis);
099:
100: if (ordered_values.size() == 0)
101: return new Integer(0);
102: else if (model.isManualScale()) {
103: //System.out.println("** model.getManualMinimumValue() = "+model.getManualMinimumValue());
104: return model.getManualMinimumValue();
105: } else if (model.isAutoScale()) {
106: double min = ((Number) ordered_values.first())
107: .doubleValue();
108: double max = ((Number) ordered_values.last()).doubleValue();
109:
110: //System.out.println("** min = "+min+" max = "+max);
111:
112: if (min / max > 0.95) {
113: //System.out.println("** ChartUtilities.performAutoScale(min/2, 2 * max)[0]"+ChartUtilities.performAutoScale(min/2, 2 * max)[0]);
114: return new Double(ChartUtilities.performAutoScale(
115: min / 2, 2 * max)[0]);
116: } else {
117: //System.out.println("** ChartUtilities.performAutoScale(min, max)[0]"+ChartUtilities.performAutoScale(min, max)[0]);
118: return new Double(ChartUtilities.performAutoScale(min,
119: max)[0]);
120: }
121: } else
122: return (Number) ordered_values.first();
123: }
124:
125: /** Returns the minimum column value.
126: * @throws ArrayIndexOutOfBoundsException if the Model is empty
127: */
128: public double getMinimumColumnValue() {
129: if (model.isManualScale() && allowManualColScale) {
130: return model.getManualMinimumColumnValue();
131: }
132: if (model.isAutoScale())
133: return ChartUtilities.performAutoScale(model
134: .getFirstColumnValue(), model.getLastColumnValue())[0];
135: else
136: return model.getFirstColumnValue();
137: }
138:
139: /** Returns the maximum column value.
140: * @throws ArrayIndexOutOfBoundsException if the model is empty
141: */
142: public double getMaximumColumnValue() {
143: if (model.isManualScale() && allowManualColScale) {
144: return model.getManualMaximumColumnValue();
145: }
146: if (model.isAutoScale())
147: return ChartUtilities.performAutoScale(model
148: .getFirstColumnValue(), model.getLastColumnValue())[1];
149: else
150: return model.getLastColumnValue();
151: }
152: }
|