KickJava   Java API By Example, From Geeks To Geeks.

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


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.BasicStroke JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 import org.jrobin.core.RrdException;
32 import org.jrobin.core.XmlWriter;
33
34 /**
35  * <p>Class used to represent a line defined by two points in a graph. The line is drawn between those two points.</p>
36  *
37  * @author Arne Vandamme (cobralord@jrobin.org)
38  */

39 class CustomLine extends Line
40 {
41     // ================================================================
42
// -- Members
43
// ================================================================
44
private long xVal1;
45     private long xVal2;
46     
47     private double yVal1;
48     private double yVal2;
49     
50     private double dc;
51     
52     
53     // ================================================================
54
// -- Constructors
55
// ================================================================
56
/**
57      * Constructs a <code>CustomLine</code> PlotDef object based on a startpoint, endpoint and a graph color.
58      * The resulting line will have a width of 1 pixel.
59      * @param startTime Timestamp of the first datapoint (startpoint).
60      * @param startValue Value of the first datapoint (startpoint).
61      * @param endTime Timestamp of the second datapoint (endpoint).
62      * @param endValue Value of the second datapoint (endpoint).
63      * @param color Color of the resulting line, if no color is specified, the CustomLine will not be drawn.
64      */

65     CustomLine( long startTime, double startValue, long endTime, double endValue, Color JavaDoc color )
66     {
67         this.color = color;
68         if ( color == null )
69             visible = false;
70         
71         this.xVal1 = startTime;
72         this.xVal2 = endTime;
73         this.yVal1 = startValue;
74         this.yVal2 = endValue;
75
76         try
77         {
78             long xc = xVal2 - xVal1;
79             if ( xc != 0 )
80                 this.dc = ( yVal2 - yVal1 ) / xc;
81             else
82                 this.dc = 0;
83         }
84         catch (Exception JavaDoc e) {
85             this.dc = 0;
86         }
87     }
88     
89     /**
90      * Constructs a <code>CustomLine</code> PlotDef object based on a startpoint, a endpoint, a graph color and a line width.
91      * @param startTime Timestamp of the first datapoint (startpoint).
92      * @param startValue Value of the first datapoint (startpoint).
93      * @param endTime Timestamp of the second datapoint (endpoint).
94      * @param endValue Value of the second datapoint (endpoint).
95      * @param color Color of the resulting line, if no color is specified, the CustomLine will not be drawn.
96      * @param lineWidth Width in pixels of the line to draw.
97      */

98     CustomLine( long startTime, double startValue, long endTime, double endValue, Color JavaDoc color, int lineWidth )
99     {
100         this( startTime, startValue, endTime, endValue, color );
101         this.lineWidth = lineWidth;
102     }
103     
104     
105     // ================================================================
106
// -- Protected methods
107
// ================================================================
108
/**
109      * Draws the actual CustomLine on the chart.
110      * @param g ChartGraphics object representing the graphing area.
111      * @param xValues List of relative chart area X positions corresponding to the datapoints, obsolete with CustomLine.
112      * @param stackValues Datapoint values of previous PlotDefs, used to stack on if necessary.
113      * @param lastPlotType Type of the previous PlotDef, used to determine PlotDef type of a stack.
114      */

115     void draw( ChartGraphics g, int[] xValues, double[] stackValues, int lastPlotType ) throws RrdException
116     {
117         g.setColor( color );
118         g.setStroke( lineWidth != 1 ? new BasicStroke JavaDoc(lineWidth) : DEF_LINE_STROKE );
119         
120         int ax, ay, nx, ny;
121
122         // Get X positions
123
if ( xVal1 == Long.MIN_VALUE )
124             ax = g.getMinX();
125         else if ( xVal1 == Long.MAX_VALUE )
126             ax = g.getMaxX();
127         else
128             ax = g.getX( xVal1 );
129         
130         if ( xVal2 == Long.MIN_VALUE )
131             nx = g.getMinX();
132         else if ( xVal2 == Long.MAX_VALUE )
133             nx = g.getMaxX();
134         else
135             nx = g.getX( xVal2 );
136         
137         // Get Y positions
138
if ( yVal1 == Double.MIN_VALUE )
139             ay = g.getMinY();
140         else if ( yVal1 == Double.MAX_VALUE )
141             ay = g.getMaxY();
142         else
143             ay = g.getY( yVal1 );
144         
145         if ( yVal2 == Double.MIN_VALUE )
146             ny = g.getMinY();
147         else if ( yVal2 == Double.MAX_VALUE )
148             ny = g.getMaxY();
149         else
150             ny = g.getY( yVal2 );
151
152         // Draw the line
153
if ( visible )
154             g.drawLine( ax, ay, nx, ny );
155          
156         // Set the stackvalues
157
int rx = nx - ax;
158         if ( rx != 0 )
159         {
160             double rc = ((ny - ay) * 1.0d) / rx;
161             for (int i = 0; i < xValues.length; i++) {
162                 if ( xValues[i] < ax || xValues[i] > nx )
163                     stackValues[i] = g.getInverseY(0);
164                 else if ( ay == ny )
165                     stackValues[i] = g.getInverseY(ay);
166                 else
167                     stackValues[i] = g.getInverseY( (int) (rc * (xValues[i] - ax) + ay) );
168             }
169         }
170
171         g.setStroke( STROKE );
172     }
173
174     /**
175      * Retrieves the value for a specific point of the CustomLine. The corresponding value is calculated based
176      * on the mathematical line function with the timestamp as a X value.
177      * @param tblPos Table index of the datapoint to be retrieved.
178      * @param timestamps Table containing the timestamps corresponding to all datapoints.
179      * @return Y value of the point as a double.
180      */

181     double getValue( int tblPos, long[] timestamps )
182     {
183         long time = timestamps[tblPos];
184         
185         // Out of range
186
if ( time > xVal2 || time < xVal1 )
187             return Double.NaN;
188         
189         // Hrule
190
if ( yVal1 == yVal2 )
191             return yVal1;
192         
193         // Vrule
194
if ( yVal1 == Double.MIN_VALUE && yVal2 == Double.MAX_VALUE )
195             return Double.NaN;
196         
197         // No line, very rare, will usually be 'out of range' first
198
if ( xVal1 == xVal2 )
199             return Double.NaN;
200                 
201         // Custom line
202
return ( dc * ( time - xVal1 ) + yVal1 );
203     }
204     
205     // Stubbed method, irrelevant for this PlotDef
206
void setSource( Source[] sources, HashMap JavaDoc sourceIndex ) throws RrdException {
207     }
208
209     // Stubbed, we don't need to set value for a Custom plotdef
210
void setValue( int tableRow, long preciseTime, long[] reducedTimestamps ) {
211     }
212
213     void exportXmlTemplate( XmlWriter xml, String JavaDoc legend ) {
214         if(yVal1 == yVal2 && xVal1 != xVal2) {
215             // hrule
216
xml.startTag("hrule");
217             xml.writeTag("value", yVal1);
218             xml.writeTag("color", color);
219             xml.writeTag("legend", legend);
220             xml.writeTag("width", lineWidth);
221             xml.closeTag(); // hrule
222
}
223         else if(yVal1 != yVal2 && xVal1 == xVal2) {
224             // vrule
225
xml.startTag("vrule");
226             xml.writeTag("time", xVal1);
227             xml.writeTag("color", color);
228             xml.writeTag("legend", legend);
229             xml.writeTag("width", lineWidth);
230             xml.closeTag(); // vrule
231
}
232         else if(yVal1 != yVal2 && xVal1 != xVal2) {
233             // general line
234
xml.startTag("line");
235             xml.writeTag("time1", xVal1);
236             xml.writeTag("value1", yVal1);
237             xml.writeTag("time2", xVal2);
238             xml.writeTag("value2", yVal2);
239             xml.writeTag("color", color);
240             xml.writeTag("legend", legend);
241             xml.writeTag("width", lineWidth);
242             xml.closeTag(); //line
243
}
244     }
245 }
246
Popular Tags