KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import org.jrobin.core.RrdException;
30 import org.jrobin.core.XmlWriter;
31 import org.jrobin.core.Util;
32
33 /**
34  * <p>Class used to represent a datasource plotted as a line in a graph.</p>
35  *
36  * @author Arne Vandamme (cobralord@jrobin.org)
37  */

38 class Line extends PlotDef
39 {
40     // ================================================================
41
// -- Members
42
// ================================================================
43
protected static BasicStroke DEF_LINE_STROKE = new BasicStroke(1.0f);
44     protected int lineWidth = 1; // Default line width of 1 pixel
45

46     
47     // ================================================================
48
// -- Constructors
49
// ================================================================
50
Line() {
51         super();
52     }
53
54     /**
55      * Constructs a <code>Line</code> PlotDef object based on a datasource name and a graph color.
56      * The resulting line will have a width of 1 pixel.
57      * @param sourceName Name of the graph definition <code>Source</code> containing the datapoints.
58      * @param color Color of the resulting line, if no color is specified, the Line will not be drawn.
59      */

60     Line( String JavaDoc sourceName, Color color )
61     {
62         super( sourceName, color );
63     }
64     
65     /**
66      * Constructs a <code>Line</code> PlotDef object based on a datasource name, a graph color and a line width.
67      * @param sourceName Name of the graph definition <code>Source</code> containing the datapoints.
68      * @param color Color of the resulting line, if no color is specified, the Line will not be drawn.
69      * @param lineWidth Width in pixels of the line to draw.
70      */

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

86     Line( Source source, double[] values, Color color, boolean stacked, boolean visible )
87     {
88         super( source, values, color, stacked, visible);
89     }
90     
91     
92     // ================================================================
93
// -- Protected methods
94
// ================================================================
95
/**
96      * Draws the actual Line on the chart.
97      * @param g ChartGraphics object representing the graphing area.
98      * @param xValues List of relative chart area X positions corresponding to the datapoints.
99      * @param stackValues Datapoint values of previous PlotDefs, used to stack on if necessary.
100      * @param lastPlotType Type of the previous PlotDef, used to determine PlotDef type of a stack.
101      */

102     void draw( ChartGraphics g, int[] xValues, double[] stackValues, int lastPlotType ) throws RrdException
103     {
104         g.setColor( color );
105         g.setStroke( lineWidth != 1 ? new BasicStroke(lineWidth) : DEF_LINE_STROKE );
106
107         Graphics2D gd = g.getGraphics();
108         int len = values.length;
109
110         double value;
111         int ax = 0, ay = 0, nx = 0, ny = 0;
112
113         for ( int i = 0; i < len; i++ )
114         {
115             value = values[i];
116             nx = xValues[i];
117
118             if ( stacked )
119                 value += stackValues[i];
120
121             ny = g.getY( value );
122
123             if ( visible && nx != 0 && ay != Integer.MIN_VALUE && ny != Integer.MIN_VALUE )
124                 gd.drawLine(ax, -ay, nx, -ny);
125
126             stackValues[i] = value;
127             ax = nx;
128             ay = ny;
129         }
130
131         g.setStroke( STROKE );
132     }
133     
134     int getLineWidth() {
135         return lineWidth;
136     }
137     
138     void exportXmlTemplate( XmlWriter xml, String JavaDoc legend )
139     {
140         xml.startTag("line");
141         xml.writeTag("datasource", sourceName);
142         xml.writeTag("color", color);
143         xml.writeTag("legend", legend);
144         xml.writeTag("width", lineWidth);
145         xml.closeTag(); // area
146
}
147 }
148
Popular Tags