KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 class Area extends PlotDef
37 {
38     // ================================================================
39
// -- Constructors
40
// ================================================================
41
/**
42      * Constructs a <code>Area</code> PlotDef object based on a datasource name and a graph color.
43      * @param sourceName Name of the graph definition <code>Source</code> containing the datapoints.
44      * @param color Color of the resulting area, if no color is specified, the Area will not be drawn.
45      */

46     Area( String JavaDoc sourceName, Color color )
47     {
48         super( sourceName, color );
49         this.plotType = PlotDef.PLOT_AREA;
50     }
51     
52     /**
53      * Constructs a <code>Area</code> object based on a Source containing all necessary datapoints and
54      * a color to draw the resulting graph in. The last two parameters define if the
55      * Area should be drawn, and if it is stacked onto a previous PlotDef yes or no.
56      * @param source Source containing all datapoints for this Area.
57      * @param color Color of the resulting graphed area.
58      * @param stacked True if this PlotDef is stacked on the previous one, false if not.
59      * @param visible True if this PlotDef should be graphed, false if not.
60      */

61     Area( Source source, double[] values, Color color, boolean stacked, boolean visible )
62     {
63         super( source, values, color, stacked, visible);
64     }
65     
66     
67     // ================================================================
68
// -- Protected methods
69
// ================================================================
70
/**
71      * Draws the actual Area on the chart.
72      * @param g ChartGraphics object representing the graphing area.
73      * @param xValues List of relative chart area X positions corresponding to the datapoints.
74      * @param stackValues Datapoint values of previous PlotDefs, used to stack on if necessary.
75      * @param lastPlotType Type of the previous PlotDef, used to determine PlotDef type of a stack.
76      */

77     void draw( ChartGraphics g, int[] xValues, double[] stackValues, int lastPlotType )
78     {
79         g.setColor( color );
80
81         int len = values.length;
82
83         double value;
84         int ax = 0, ay = 0, nx = 0, ny = 0, py;
85
86         for ( int i = 0; i < len; i++ )
87         {
88             py = 0;
89             nx = xValues[i];
90             value = values[i];
91
92             if ( !Double.isNaN(value) )
93             {
94                 if ( stacked )
95                 {
96                     py = g.getY( stackValues[i] );
97                     value += stackValues[i];
98                 }
99
100                 ny = g.getY( value );
101
102                 if ( visible )
103                 {
104                     if ( nx > ax + 1 ) // More than one pixel hop, draw intermediate pixels too
105
{
106                         // For each pixel between nx and ax, calculate the y, plot the line
107
int co = (ny - ay) / (nx - ax);
108                         int j = (ax > 0 ? ax : 1 ); // Skip 0
109

110                         for (j = ax; j <= nx; j++)
111                             if ( ay != Integer.MIN_VALUE && ny != Integer.MIN_VALUE )
112                                 g.drawLine( j, py, j, ( co * (j - ax) + ay) );
113                     }
114                     else if ( nx != 0 && py != Integer.MIN_VALUE && ny != Integer.MIN_VALUE )
115                         g.drawLine( nx, py, nx, ny );
116                 }
117             }
118
119             // Special case with NaN doubles
120
stackValues[i] = value;
121             ax = nx;
122             ay = ny;
123         }
124     }
125
126     void exportXmlTemplate( XmlWriter xml, String JavaDoc legend )
127     {
128         xml.startTag("area");
129         xml.writeTag("datasource", sourceName);
130         xml.writeTag("color", color);
131         xml.writeTag("legend", legend);
132         xml.closeTag(); // area
133
}
134 }
135
Popular Tags