001: /***********************************************************************************************
002: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
003: *
004: * Redistribution and use of this software and associated documentation ("Software"), with or
005: * without modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain copyright statements and notices.
008: * Redistributions must also contain a copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
011: * conditions and the following disclaimer in the documentation and/or other materials
012: * provided with the distribution.
013: *
014: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
015: * products derived from this Software without prior written permission of Nathaniel G.
016: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
017: *
018: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
019: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
020: * registered trademark of Nathaniel G. Auvil.
021: *
022: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
023: *
024: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
025: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
026: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
029: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
030: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
032: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
033: ************************************************************************************************/package org.krysalis.jcharts.axisChart.axis;
034:
035: import org.krysalis.jcharts.axisChart.AxisChart;
036: import org.krysalis.jcharts.axisChart.axis.scale.ScaleCalculator;
037: import org.krysalis.jcharts.chartText.TextTagGroup;
038: import org.krysalis.jcharts.properties.util.ChartFont;
039: import org.krysalis.jcharts.properties.AxisTypeProperties;
040: import org.krysalis.jcharts.properties.DataAxisProperties;
041: import org.krysalis.jcharts.test.HTMLGenerator;
042: import org.krysalis.jcharts.test.HTMLTestable;
043:
044: import java.awt.font.TextLayout;
045: import java.io.Serializable;
046: import java.lang.reflect.Field;
047:
048: /*************************************************************************************
049: *
050: * @author Nathaniel Auvil
051: * @version $Id: Axis.java,v 1.3 2003/08/27 04:37:44 kiwicmc Exp $
052: ************************************************************************************/
053: abstract public class Axis implements HTMLTestable, Serializable {
054: //---reference to containing chart
055: private AxisChart axisChart;
056:
057: //---number of pixels between each label
058: private float scalePixelWidth;
059:
060: //---the length of the axis
061: private float pixelLength;
062:
063: //---the pixel coordinate of the start of the axis
064: private float origin;
065:
066: private float titleHeight;
067: private float titleWidth;
068:
069: private float minimumWidthNeeded;
070: private float minimumHeightNeeded;
071:
072: //---Note, if no labels are displayed, axisLabelsGroup will be NULL!
073: private TextTagGroup axisLabelsGroup;
074:
075: // Dual Y axis changes integrated CMC 25Aug03
076: // Variable to use to display the labels of the right Y axis
077: // the corresponding methods are also implemented
078: private TextTagGroup axisLabelsGroupRight;
079:
080: //---this number of items to plot on the Axis. Note, if no labels are displayed, axisLabelsGroup will be NULL so we can not depend on that.
081: private int numberOfScaleItems;
082:
083: //---pixel location of tick start needed for reference when start drawing charts.
084: private float tickStart;
085:
086: //---multiplication value used to determine the coordinate location of values on YAxis
087: private double oneUnitPixelSize;
088:
089: private float zeroLineCoordinate;
090:
091: private ScaleCalculator scaleCalculator;
092:
093: /**************************************************************************************************
094: * Constructor
095: *
096: * @param axisChart
097: * @param numberOfScaleItems
098: ***************************************************************************************************/
099: public Axis(AxisChart axisChart, int numberOfScaleItems) {
100: this .axisChart = axisChart;
101: this .numberOfScaleItems = numberOfScaleItems;
102: }
103:
104: /**************************************************************************************************
105: * Returns reference to AxisChart Object.
106: *
107: * @return axisChart
108: ***************************************************************************************************/
109: public final AxisChart getAxisChart() {
110: return this .axisChart;
111: }
112:
113: public int getNumberOfScaleItems() {
114: return numberOfScaleItems;
115: }
116:
117: public TextTagGroup getAxisLabelsGroup() {
118: return axisLabelsGroup;
119: }
120:
121: public void setAxisLabelsGroup(TextTagGroup axisLabelsGroup) {
122: this .axisLabelsGroup = axisLabelsGroup;
123: }
124:
125: // Dual Y axis changes integrated CMC 25Aug03
126: public TextTagGroup getAxisLabelsGroupRight() {
127: return axisLabelsGroupRight;
128: }
129:
130: public void setAxisLabelsGroupRight(
131: TextTagGroup axisLabelsGroupRight) {
132: this .axisLabelsGroupRight = axisLabelsGroupRight;
133: }
134:
135: public final float getTitleWidth() {
136: return this .titleWidth;
137: }
138:
139: public final float getTitleHeight() {
140: return this .titleHeight;
141: }
142:
143: public final float getPixelLength() {
144: return this .pixelLength;
145: }
146:
147: public final void setPixelLength(float pixelLength) {
148: this .pixelLength = pixelLength;
149: }
150:
151: public final float getOrigin() {
152: return this .origin;
153: }
154:
155: public final void setOrigin(float origin) {
156: this .origin = origin;
157: }
158:
159: public final float getMinimumWidthNeeded() {
160: return this .minimumWidthNeeded;
161: }
162:
163: public final void setMinimumWidthNeeded(float minimumWidthNeeded) {
164: this .minimumWidthNeeded = minimumWidthNeeded;
165: }
166:
167: public final float getMinimumHeightNeeded() {
168: return this .minimumHeightNeeded;
169: }
170:
171: public final void setMinimumHeightNeeded(float minimumHeightNeeded) {
172: this .minimumHeightNeeded = minimumHeightNeeded;
173: }
174:
175: public final float getScalePixelWidth() {
176: return this .scalePixelWidth;
177: }
178:
179: public final void setScalePixelWidth(float scalePixelWidth) {
180: this .scalePixelWidth = scalePixelWidth;
181: }
182:
183: public float getTickStart() {
184: return tickStart;
185: }
186:
187: public void setTickStart(float tickStart) {
188: this .tickStart = tickStart;
189: }
190:
191: public ScaleCalculator getScaleCalculator() {
192: return scaleCalculator;
193: }
194:
195: public void setScaleCalculator(ScaleCalculator scaleCalculator) {
196: this .scaleCalculator = scaleCalculator;
197: }
198:
199: /*********************************************************************************************
200: *
201: *
202: * @param title
203: * @param axisTitleFont
204: **********************************************************************************************/
205: public final void computeAxisTitleDimensions(String title,
206: ChartFont axisTitleFont) {
207: TextLayout textLayout = new TextLayout(title, axisTitleFont
208: .getFont(), this .getAxisChart().getGraphics2D()
209: .getFontRenderContext());
210:
211: this .titleWidth = textLayout.getAdvance();
212: this .titleHeight = textLayout.getAscent()
213: + textLayout.getDescent();
214: }
215:
216: /*********************************************************************************************
217: * Enables the testing routines to display the contents of this Object.
218: *
219: * Can only see 'private' fields from this class.
220: *
221: * @param htmlGenerator
222: **********************************************************************************************/
223: public void toHTML(HTMLGenerator htmlGenerator) {
224: String name = this .getClass().getSuperclass().getName() + "->";
225:
226: //---calling on instance of YAxis or XAxis
227: Field[] fields = this .getClass().getSuperclass()
228: .getDeclaredFields();
229: for (int i = 0; i < fields.length; i++) {
230: try {
231: htmlGenerator.addField(name + fields[i].getName(),
232: fields[i].get(this ));
233: } catch (IllegalAccessException illegalAccessException) {
234: illegalAccessException.printStackTrace();
235: }
236: }
237: }
238:
239: /*************************************************************************************************
240: * Computes the number of pixels between each value on the axis.
241: *
242: **************************************************************************************************/
243: public void computeScalePixelWidth(
244: AxisTypeProperties axisTypeProperties) {
245: setScalePixelWidth(getPixelLength()
246: / this .getNumberOfScaleItems());
247: }
248:
249: /*************************************************************************************************
250: * Computes the number of pixels between each value on the axis.
251: *
252: **************************************************************************************************/
253: public void computeScalePixelWidthDataAxis(
254: AxisTypeProperties axisTypeProperties) {
255: //---if we are plotting data items, allow for a padding beyond the tallest value so we are not
256: //--- hitting the top of the axis
257: if (axisTypeProperties instanceof DataAxisProperties) {
258: setScalePixelWidth((getPixelLength() - 5)
259: / (this .numberOfScaleItems - 1));
260: } else {
261: setScalePixelWidth((getPixelLength())
262: / (this .numberOfScaleItems - 1));
263: }
264: }
265:
266: public void setOneUnitPixelSize(double oneUnitPixelSize) {
267: this .oneUnitPixelSize = oneUnitPixelSize;
268: }
269:
270: /**************************************************************************************************
271: * Returns the number of pixels one value unit occupies.
272: *
273: * @return double the number of pixels one value unit occupies.
274: ***************************************************************************************************/
275: public double getOneUnitPixelSize() {
276: return this .oneUnitPixelSize;
277: }
278:
279: /**************************************************************************************************
280: * Returns the screen coordinate of the zero line. This will not always be the same as the origin
281: * as not all charts start at zero. This is needed not only by the Axis, but some of the Chart
282: * implementations as well.
283: *
284: * @return float the screen pixel location of the zero line.
285: ***************************************************************************************************/
286: public float getZeroLineCoordinate() {
287: return this .zeroLineCoordinate;
288: }
289:
290: /**************************************************************************************************
291: * Sets the screen coordinate of the zero line. This will not always be the same as the origin
292: * as not all charts start at zero.
293: *
294: * @param value the screen pixel location of the zero line.
295: ***************************************************************************************************/
296: public void setZeroLineCoordinate(float value) {
297: this .zeroLineCoordinate = value;
298: }
299:
300: /*************************************************************************************************
301: * Computes the relationship of data point values to pixel values so know where along the axis
302: * a value is.
303: *
304: * @param scalePixelLength
305: * @param increment
306: **************************************************************************************************/
307: public void computeOneUnitPixelSize(float scalePixelLength,
308: double increment) {
309: this.oneUnitPixelSize = scalePixelLength / increment;
310: }
311:
312: }
|