KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > Axis


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     Axis.java
21     Created on 30. Juni 2001, 22:27
22  */

23
24 package de.progra.charting;
25
26 import de.progra.charting.model.ChartDataModelConstraints;
27
28 /**
29  * The CoordSystem contains two or possibly three Axis objects for the x-axis
30  * and the at most two y-axis.
31  * @author mueller
32  * @version 1.0
33  */

34 public class Axis {
35
36     /** Defines a horizontal x-axis. */
37     public final static int HORIZONTAL = 1;
38     
39     /** Defines a vertical y-axis. */
40     public final static int VERTICAL = 2;
41     
42     /** Defines a logarithmic scale. */
43     public final static int LOGARITHMIC = 3;
44     
45     /** Defines a linear scale. */
46     public final static int LINEAR = 4;
47     
48     /** The axis' alignment. */
49     private int align = HORIZONTAL;
50     
51     ChartDataModelConstraints constraints;
52     
53     int length = Integer.MAX_VALUE;
54     
55     /** Creates new Axis.
56      * @param align the alignment of the axis.
57      * @param c the ChartDataModelConstraints
58      */

59     public Axis(int align, ChartDataModelConstraints c) {
60                     
61         if(align == HORIZONTAL || align == VERTICAL)
62             this.align = align;
63         
64         this.constraints = c;
65     }
66     
67     /** Returns the alignment of the axis.
68      * @return the alignment constant: <CODE>Axis.VERTICAL</CODE> or <CODE>Axis.HORIZONTAL</CODE>
69      */

70     public int getAlignment() {
71         return align;
72     }
73
74     /** Sets the Pixel length of the axis.
75      * @param length the length in pixel
76      */

77     public void setLength(int length) {
78         this.length = length;
79     }
80     
81     /** Returns length of the axis in pixels.
82      * @return the length in pixels
83      */

84     public int getLength() {
85         return length;
86     }
87     
88     /** Returns the point on the axis for a specific value.
89      * If the axis is a x-axis and the column values are not numeric,
90      * this isn't needed since then the axis can be divided into
91      * equally long parts. This is a relative pixel distance to
92      * the starting pixel of the axis.
93      * @param value the double value to compute the pixel distance for
94      * @return the pixel distance for the given value relative to the start of the axis
95      */

96     public double getPixelForValue(double value) {
97         // if scale == linear
98
if(getAlignment() == Axis.VERTICAL) {
99             return (value - constraints.getMinimumValue().doubleValue()) /
100                     getPointToPixelRatio();
101         } else {
102             return (value - constraints.getMinimumColumnValue()) /
103                    getPointToPixelRatio();
104         }
105     }
106     
107     /** Returns the ratio between a value unit and the screen pixels.
108      * This is only useful for linear scales.
109      * @return the ratio points / pixel length for the axis.
110      */

111     public double getPointToPixelRatio() {
112         //System.out.println("** constraints.getMaximumColumnValue() = "+constraints.getMaximumColumnValue());
113
if(getAlignment() == Axis.VERTICAL) {
114             
115             return (constraints.getMaximumValue().doubleValue() -
116                     constraints.getMinimumValue().doubleValue()) /
117                     length;
118         }
119         else
120             return (constraints.getMaximumColumnValue() -
121                     constraints.getMinimumColumnValue()) /
122                     length;
123     }
124 }
125
Popular Tags