KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
29
30 import org.jrobin.core.RrdException;
31 import org.jrobin.core.XmlWriter;
32
33 /**
34  * <p>Class used to represent an area defined by two points in a graph. The area is drawn as a rectangle
35  * that has the line startpoint-endpoint as a diagonal. Direction of plotting the CustomArea can be seen as
36  * drawing a rectangle from the bottom-left corner to the upper-right corner.
37  * It is possible to stack another PlotDef on top of the CustomArea, in that case the stacked PlotDef will
38  * always be stacked on top of the (Y) value of the second defined datapoint of the CustomArea.</p>
39  *
40  * @author Arne Vandamme (cobralord@jrobin.org)
41  */

42 class CustomArea extends PlotDef
43 {
44     // ================================================================
45
// -- Members
46
// ================================================================
47
private long xVal1;
48     private long xVal2;
49     
50     private double yVal1;
51     private double yVal2;
52
53
54     // ================================================================
55
// -- Constructors
56
// ================================================================
57
/**
58      * Constructs a <code>CustomArea</code> PlotDef object based on a startpoint, endpoint and a graph color.
59      * The resulting area will be graphed as a rectangle from the bottom-left corner (startpoint) to the
60      * upper-right coner (endpoint).
61      * @param startTime Timestamp of the first datapoint (startpoint).
62      * @param startValue Value of the first datapoint (startpoint).
63      * @param endTime Timestamp of the second datapoint (endpoint).
64      * @param endValue Value of the second datapoint (endpoint).
65      * @param color Color of the resulting line, if no color is specified, the CustomLine will not be drawn.
66      */

67     CustomArea( long startTime, double startValue, long endTime, double endValue, Color JavaDoc color )
68     {
69         this.color = color;
70         if ( color == null )
71             visible = false;
72         
73         this.xVal1 = startTime;
74         this.xVal2 = endTime;
75         this.yVal1 = startValue;
76         this.yVal2 = endValue;
77     }
78
79
80     // ================================================================
81
// -- Protected methods
82
// ================================================================
83
/**
84      * Draws the actual CustomArea on the chart.
85      * @param g ChartGraphics object representing the graphing area.
86      * @param xValues List of relative chart area X positions corresponding to the datapoints, obsolete with CustomArea.
87      * @param stackValues Datapoint values of previous PlotDefs, used to stack on if necessary.
88      * @param lastPlotType Type of the previous PlotDef, used to determine PlotDef type of a stack.
89      */

90     void draw( ChartGraphics g, int[] xValues, double[] stackValues, int lastPlotType ) throws RrdException
91     {
92         g.setColor( color );
93     
94         int ax, ay, nx, ny;
95     
96         // Get X positions
97
if ( xVal1 == Long.MIN_VALUE )
98             ax = g.getMinX();
99         else if ( xVal1 == Long.MAX_VALUE )
100             ax = g.getMaxX();
101         else
102             ax = g.getX( xVal1 );
103     
104         if ( xVal2 == Long.MIN_VALUE )
105             nx = g.getMinX();
106         else if ( xVal2 == Long.MAX_VALUE )
107             nx = g.getMaxX();
108         else
109             nx = g.getX( xVal2 );
110     
111         // Get Y positions
112
if ( yVal1 == Double.MIN_VALUE )
113             ay = g.getMinY();
114         else if ( yVal1 == Double.MAX_VALUE )
115             ay = g.getMaxY();
116         else
117             ay = g.getY( yVal1 );
118     
119         if ( yVal2 == Double.MIN_VALUE )
120             ny = g.getMinY();
121         else if ( yVal2 == Double.MAX_VALUE )
122             ny = g.getMaxY();
123         else
124             ny = g.getY( yVal2 );
125
126         // Draw the area
127
if ( visible )
128         {
129             if ( ny > ay )
130                 g.fillRect( ax, ay, nx, ny );
131             else
132                 g.fillRect( ax, ny, nx, ay );
133         }
134         
135         // Set the stackvalues
136
// Always use the y value of the second specified point to stack on
137
if ( yVal2 != Double.MAX_VALUE )
138             for (int i = 0; i < stackValues.length; i++)
139                 if ( xValues[i] < ax || xValues[i] > nx )
140                     stackValues[i] = g.getInverseY(0);
141                 else
142                     stackValues[i] = g.getInverseY(ny);
143     }
144
145     /**
146      * Retrieves the value for a specific point of the CustomArea. The CustomArea is always a rectangle,
147      * this means the returned double value will always be equal to the (Y) value of the second datapoint.
148      * In case of an unlimited CustomArea (second datapoint Y value is <code>Double.MAX_VALUE</code>)
149      * the returned value is <code>Double.NaN</code>.
150      * @param tblPos Table index of the datapoint to be retrieved.
151      * @param timestamps Table containing the timestamps corresponding to all datapoints.
152      * @return Y value of the point as a double.
153      */

154     double getValue( int tblPos, long[] timestamps )
155     {
156         long time = timestamps[tblPos];
157     
158         // Out of range
159
if ( time > xVal2 || time < xVal1 )
160             return Double.NaN;
161     
162         if ( yVal2 == Double.MAX_VALUE )
163             return Double.NaN;
164         
165         return yVal2;
166     }
167
168     // Stubbed method, irrelevant for this PlotDef
169
void setSource( Source[] sources, HashMap JavaDoc sourceIndex ) throws RrdException {
170     }
171
172     // Stubbed, we don't need to set value for a Custom plotdef
173
void setValue( int tableRow, long preciseTime, long[] reducedTimestamps ) {
174     }
175
176     void exportXmlTemplate( XmlWriter xml, String JavaDoc legend ) {
177         xml.startTag("area");
178         xml.writeTag("time1", xVal1);
179         xml.writeTag("value1", yVal1);
180         xml.writeTag("time2", xVal2);
181         xml.writeTag("value2", yVal2);
182         xml.writeTag("color", color);
183         xml.writeTag("legend", legend);
184         xml.closeTag(); //area
185
}
186 }
187
Popular Tags