KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > axisChart > StackedAreaChart


1 /***********************************************************************************************
2  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
3  *
4  * Redistribution and use of this software and associated documentation ("Software"), with or
5  * without modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain copyright statements and notices.
8  * Redistributions must also contain a copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
15  * products derived from this Software without prior written permission of Nathaniel G.
16  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
17  *
18  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
19  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
20  * registered trademark of Nathaniel G. Auvil.
21  *
22  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
23  *
24  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
25  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
33  ************************************************************************************************/

34
35 package org.krysalis.jcharts.axisChart;
36
37
38 import org.krysalis.jcharts.chartData.interfaces.IAxisChartDataSet;
39 import org.krysalis.jcharts.imageMap.ImageMapNotSupportedException;
40 import org.krysalis.jcharts.properties.DataAxisProperties;
41
42 import java.awt.*;
43 import java.awt.geom.Area JavaDoc;
44 import java.awt.geom.GeneralPath JavaDoc;
45
46
47 /*************************************************************************************
48  *
49  * @author Nathaniel Auvil
50  * @version $Id: StackedAreaChart.java,v 1.1 2003/05/17 16:56:57 nathaniel_auvil Exp $
51  ************************************************************************************/

52 abstract class StackedAreaChart
53 {
54
55     /********************************************************************************************
56      * Draws the chart
57      *
58      * @param axisChart
59      * @param iAxisChartDataSet
60      *********************************************************************************************/

61     static void render( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet )
62     {
63         //---hopefully eliminate support requests asking about this...
64
if( axisChart.getImageMap() != null )
65         {
66             throw new ImageMapNotSupportedException( "HTML client-side image maps are not supported on Area Charts." );
67         }
68
69         //AreaChartProperties areaChartProperties=(AreaChartProperties) iAxisChartDataSet.getChartTypeProperties();
70

71         float xPosition=axisChart.getXAxis().getTickStart();
72
73         GeneralPath JavaDoc generalPaths[]=new GeneralPath JavaDoc[ iAxisChartDataSet.getNumberOfDataSets() ];
74
75
76         //---cache the computed values
77
//float[][] yAxisCoordinates= new float[ iAxisChartDataSet.getNumberOfDataSets() ][ iAxisChartDataSet.getNumberOfDataItems() ];
78

79         //---StackedAreaCharts can not be drawn on a horizontal axis so y-axis will always be the data axis
80
DataAxisProperties dataAxisProperties= (DataAxisProperties) axisChart.getAxisProperties().getYAxisProperties();
81
82
83         float stackedValue=0f;
84
85
86         //LOOP
87
//---initial postion of each line must be set with call to moveTo()
88
//---Do this here so every point does not have to check....if( i == 0 )... in loop
89
for( int i=0; i < generalPaths.length; i++ )
90         {
91             generalPaths[ i ]=new GeneralPath JavaDoc();
92
93             generalPaths[ i ].moveTo( xPosition, axisChart.getYAxis().getZeroLineCoordinate() );
94
95             stackedValue+=iAxisChartDataSet.getValue( i, 0 );
96             generalPaths[ i ].lineTo( xPosition, axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
97                                                                                                                         stackedValue,
98                                                                                                                         axisChart.getYAxis().getScaleCalculator().getMinValue() ) );
99         }
100
101         //LOOP
102
for( int j=1; j < iAxisChartDataSet.getNumberOfDataItems(); j++ )
103         {
104             xPosition+=axisChart.getXAxis().getScalePixelWidth();
105
106             stackedValue=0f;
107
108             //LOOP
109
for( int i=0; i < generalPaths.length; i++ )
110             {
111                 stackedValue+=iAxisChartDataSet.getValue( i, j );
112                 generalPaths[ i ].lineTo( xPosition, axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
113                                                                                                                             stackedValue,
114                                                                                                                             axisChart.getYAxis().getScaleCalculator().getMinValue() ) );
115             }
116         }
117
118
119         Area JavaDoc[] areas=new Area JavaDoc[ generalPaths.length ];
120
121         Area JavaDoc totalArea=null;
122         Area JavaDoc newArea;
123
124
125         //LOOP
126
//---close the path and create an Area so can stack them
127
for( int i=0; i < generalPaths.length; i++ )
128         {
129             generalPaths[ i ].lineTo( xPosition, axisChart.getYAxis().getZeroLineCoordinate() );
130
131             generalPaths[ i ].closePath();
132             //generalPaths[ i ].lineTo( axisChart.getXAxisProperties().getTickStartX(), axisChart.getYAxisProperties().getZeroLineCoordinate() );
133

134             newArea=new Area JavaDoc( generalPaths[ i ] );
135
136             //---if this is not the first area, subtract cumulative area
137
if( i > 0 )
138             {
139                 areas[ i ]=(Area JavaDoc) newArea.clone();
140                 areas[ i ].subtract( totalArea );
141
142                 totalArea=newArea;
143             }
144             else
145             {
146                 areas[ 0 ]=newArea;
147                 totalArea=areas[ 0 ];
148             }
149         }
150
151
152         Graphics2D g2d=axisChart.getGraphics2D();
153
154         //LOOP
155
//---draw each path to the image
156
for( int i=0; i < areas.length; i++ )
157         {
158             g2d.setPaint( iAxisChartDataSet.getPaint( i ) );
159             //g2d.setStroke( lineChartProperties.getLineStrokes()[ i ] );
160
g2d.fill( areas[ i ] );
161         }
162     }
163
164 }
165
Popular Tags