KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
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 a drawn datasource in the graph.
35  * This class is abstract, it can only be used by child classes with a specific
36  * implementation of the <code>draw()</code> method.</p>
37  *
38  * @author Arne Vandamme (cobralord@jrobin.org)
39  */

40 abstract class PlotDef
41 {
42     // ================================================================
43
// -- Members
44
// ================================================================
45
protected static final int PLOT_LINE = 0;
46     protected static final int PLOT_AREA = 1;
47     protected static final int PLOT_STACK = 2;
48     protected static final BasicStroke STROKE = new BasicStroke();
49
50     protected boolean visible = true;
51     protected boolean stacked = false;
52     protected int plotType = PLOT_LINE; // Unknown plotdef is a line
53

54     protected String JavaDoc sourceName = "";
55     protected Source source = null;
56     protected Color color = Color.BLACK; // Default color is black
57

58     protected double[] values = null;
59
60     // ================================================================
61
// -- Constructors
62
// ================================================================
63
PlotDef() {
64     }
65     
66     /**
67      * Constructs a default <code>PlotDef</code> object based on a datasource name and a graph color.
68      * @param sourceName Name of the graph definition <code>Source</code> containing the datapoints.
69      * @param color Color of the resulting graph for this PlotDef, if no color is specified, the PlotDef will not be drawn.
70      */

71     PlotDef( String JavaDoc sourceName, Color color )
72     {
73         this.sourceName = sourceName;
74         this.color = color;
75         
76         // If no color is given, we should not plot this source
77
if ( color == null )
78             visible = false;
79     }
80     
81     /**
82      * Constructs a default <code>PlotDef</code> object based on a Source containing all necessary datapoints and
83      * a color to draw the resulting graph in. The last two parameters define if this
84      * PlotDef should be drawn, and if it is a stack yes or no.
85      * @param source Source containing all datapoints for this PlotDef.
86      * @param color Color of the resulting graph for this PlotDef.
87      * @param stacked True if this PlotDef is stacked on the previous one, false if not.
88      * @param visible True if this PlotDef should be graphed, false if not.
89      */

90     PlotDef( Source source, double[] values, Color color, boolean stacked, boolean visible )
91     {
92         this.source = source;
93         this.values = values;
94         this.color = color;
95         this.stacked = stacked;
96         this.visible = visible;
97     }
98
99     // ================================================================
100
// -- Protected methods
101
// ================================================================
102
/**
103      * Sets the Source for this PlotDef, based on the internal datasource name.
104      * @param sources Source table containing all datasources necessary to create the final graph.
105      * @param sourceIndex HashMap containing the sourcename - index keypairs, to retrieve the index
106      * in the Source table based on the sourcename.
107      */

108     void setSource( Source[] sources, HashMap JavaDoc sourceIndex ) throws RrdException
109     {
110         if ( sourceIndex.containsKey(sourceName) ) {
111             source = sources[ ((Integer JavaDoc) sourceIndex.get(sourceName)).intValue() ];
112         }
113         else
114             throw new RrdException( "Invalid DEF or CDEF: " + sourceName );
115     }
116
117     void prepareValues( int arraySize )
118     {
119         values = new double[ arraySize ];
120     }
121
122     void setValue( int tableRow, long preciseTime, long[] reducedTimestamps )
123     {
124         values[ tableRow ] = source.get( preciseTime, reducedTimestamps );
125     }
126
127     /**
128      * Retrieves the value for a specific datapoint of the PlotDef.
129      * @param tblPos Table index of the datapoint to be retrieved.
130      * @param timestamps Table containing the timestamps corresponding to all datapoints.
131      * @return Double value of the datapoint.
132      */

133     double getValue( int tblPos, long[] timestamps )
134     {
135         return source.get( tblPos );
136     }
137     
138     /**
139      * Abstract draw method, must be implemented in all child classes.
140      * This method is responsible for the actual drawing of the PlotDef.
141      */

142     abstract void draw( ChartGraphics g, int[] xValues, double[] stackValues, int lastPlotType ) throws RrdException;
143     
144     Source getSource() {
145         return source;
146     }
147     
148     String JavaDoc getSourceName() {
149         return sourceName;
150     }
151     
152     int getType() {
153         return plotType;
154     }
155     
156     Color getColor() {
157         return color;
158     }
159
160     void exportXmlTemplate(XmlWriter xml, String JavaDoc legend) {
161
162     }
163 }
164
Popular Tags