KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > ChartGraphics


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org)
7  *
8  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.graph;
26
27 import java.awt.Color JavaDoc;
28 import java.awt.Stroke JavaDoc;
29 import java.awt.Graphics2D JavaDoc;
30
31 /**
32  * <p>Represent a specific Graphics object holding all specifications of the chart area of the entire graph,
33  * including a handle to a Graphics2D context. This class is a wrapper around the graphics context, taking
34  * care of some coordinate calculations and translations automatically.</p>
35  *
36  * @author Arne Vandamme (cobralord@jrobin.org)
37  */

38 class ChartGraphics
39 {
40     // ================================================================
41
// -- Members
42
// ================================================================
43
private Graphics2D JavaDoc g;
44     
45     private int width, height;
46     private long xStart, xEnd;
47     private double yStart, yEnd;
48
49     private double widthDelta = 1.0d, heightDelta = 3.0d;
50
51     // ================================================================
52
// -- Constructors
53
// ================================================================
54
/**
55      * Creates a new <code>ChartGraphics</code> object based on a graphics handle.
56      * @param graphics Handle of a Graphics2D context to use.
57      */

58     ChartGraphics( Graphics2D JavaDoc graphics )
59     {
60         g = graphics;
61     }
62
63
64     // ================================================================
65
// -- Protected methods
66
// ================================================================
67
/**
68      * Draws a line on the graphics context. The line is specified by
69      * providing begin and end point.
70      * @param x1 X coordinate of the begin point.
71      * @param y1 Y coordinate of the begin point.
72      * @param x2 X coordinate of the end point.
73      * @param y2 Y coordinate of the end point.
74      */

75     void drawLine(int x1, int y1, int x2, int y2)
76     {
77         g.drawLine( x1, -y1, x2, -y2 );
78     }
79
80     /**
81      * Draws a filled rectangle on the graphics context. The rectangle is specified
82      * by providing two points, bottom-left corner and upper-right corner. This is contrary
83      * to the general Graphics <code>fillRect</code> method, where the rectangle is specified
84      * using a single point and a rectangle height and width.
85      * @param x1 X coordinate of the bottom-left corner.
86      * @param y1 Y coordinate of the bottom-left corner.
87      * @param x2 X coordinate of the upper-right corner.
88      * @param y2 Y coordinate of the upper-right corner.
89      */

90     // Contrary to Graphics2D fillRect, this method uses boundary points
91
void fillRect(int x1, int y1, int x2, int y2)
92     {
93         g.fillRect( x1, -y2, x2 - x1, - (y2 - y1) );
94     }
95     
96     /**
97      * Sets the color of the current brush on the graphics context.
98      * The next items drawn will be in this color.
99      * @param c Color to use.
100      */

101     void setColor( Color JavaDoc c )
102     {
103         g.setColor( c );
104     }
105
106     /**
107      * Sets the absolute (pixel) dimensions of the chart area to which this object applies.
108      * @param width Width of the chart area in pixels.
109      * @param height Height of the chart area in pixels.
110      */

111     void setDimensions( int width, int height )
112     {
113         this.width = width;
114         this.height = height;
115     }
116
117     /**
118      * Sets the timerange specified for the chart, used for scaling down timestamps to fit in the X axis pixel range.
119      * @param start Start timestamp (in seconds) of the timespan.
120      * @param end End timestamp (in seconds) of the timespan.
121      */

122     void setXRange( long start, long end )
123     {
124         xStart = start;
125         xEnd = end;
126     
127         if ( xEnd != xStart )
128             widthDelta = width * 1.0d / (( xEnd - xStart) * 1.0d);
129         else
130             widthDelta = 1.0d;
131     }
132
133     /**
134      * Sets the valuerange specified for the chart, used for scaling down values to fit in the Y axis pixel range.
135      * @param lower Lower value of the range.
136      * @param upper Upper value of the range.
137      */

138     void setYRange( double lower, double upper )
139     {
140         yStart = lower;
141         yEnd = upper;
142         
143         if ( yEnd != yStart )
144             heightDelta = height * 1.0d / (( yEnd - yStart) * 1.0d);
145         else
146             heightDelta = 1.0d;
147
148         yStart = (yStart < 0 ? 0 : Math.abs(yStart));
149     }
150
151     /**
152      * Calculates the pixel position on the X axis for a specific timestamp (in seconds).
153      * @param timestamp Timestamp for which to calculate the corresponding X coordinate.
154      * @return X coordinate on the horizontal chart axis.
155      */

156     int getX( long timestamp )
157     {
158         return (int) ((timestamp - xStart) * widthDelta);
159     }
160
161     /**
162      * Calculates the pixel position on the Y axis for a specific double value.
163      * @param value Value for which to calculate the corresponding Y coordinate.
164      * @return Y coordinate on the horizontal chart axis.
165      */

166     int getY( double value )
167     {
168         if ( Double.isNaN(value) ) return Integer.MIN_VALUE;
169
170         return (int) ((value - yStart ) * heightDelta);
171     }
172
173     double getInverseY( int value )
174     {
175         if ( value == Integer.MIN_VALUE ) return Double.NaN;
176
177         return (value * 1.0d/heightDelta) + yStart;
178     }
179
180     /**
181      * Sets the Stroke to use for graphing on the graphics context.
182      * @param s Specified <code>Stroke</code> to use.
183      */

184     void setStroke( Stroke JavaDoc s )
185     {
186         g.setStroke( s );
187     }
188
189     /**
190      * Retrieves the lowest X coordinate of the chart area.
191      * @return Lowest X coordinate of the chart area.
192      */

193     int getMinX()
194     {
195         return 0;
196     }
197     
198     /**
199      * Retrieves the highest X coordinate of the chart area.
200      * @return Highest X coordinate of the chart area.
201      */

202     int getMaxX()
203     {
204         return 0 + width;
205     }
206     
207     /**
208      * Retrieves the lowest Y coordinate of the chart area.
209      * @return Lowest Y coordinate of the chart area.
210      */

211     int getMinY()
212     {
213         return 0;
214     }
215     
216     /**
217      * Retrieves the highest Y coordinate of the chart area.
218      * @return Highest Y coordinate of the chart area.
219      */

220     int getMaxY()
221     {
222         return 0 + height;
223     }
224     
225     /**
226      * Retrieves the handle of the Graphics2D context for this object.
227      * @return Handle to the internal Graphics2D context.
228      */

229     Graphics2D JavaDoc getGraphics()
230     {
231         return g;
232     }
233 }
234
Popular Tags