KickJava   Java API By Example, From Geeks To Geeks.

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


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.chartData.interfaces.IDataSeries;
40 import org.krysalis.jcharts.imageMap.CircleMapArea;
41 import org.krysalis.jcharts.properties.PointChartProperties;
42 import org.krysalis.jcharts.properties.DataAxisProperties;
43
44 import java.awt.*;
45 import java.awt.geom.AffineTransform JavaDoc;
46 import java.awt.geom.Rectangle2D JavaDoc;
47
48
49 /*************************************************************************************
50  *
51  * @author Nathaniel Auvil
52  * @version $Id: PointChart.java,v 1.1 2003/05/17 16:56:57 nathaniel_auvil Exp $
53  ************************************************************************************/

54 abstract class PointChart
55 {
56
57     /********************************************************************************************
58      * Draws the chart
59      *
60      * @param axisChart
61      * @param iAxisChartDataSet
62      *********************************************************************************************/

63     static void render( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet )
64     {
65         Graphics2D g2d=axisChart.getGraphics2D();
66         PointChartProperties pointChartProperties=(PointChartProperties) iAxisChartDataSet.getChartTypeProperties();
67
68         //---Point Charts can not be horizontal so we know the y-axis is DataAxisProperties
69
DataAxisProperties dataAxisProperties= (DataAxisProperties) axisChart.getAxisProperties().getYAxisProperties();
70         IDataSeries iDataSeries= (IDataSeries) axisChart.getIAxisDataSeries();
71
72
73         float xPosition=axisChart.getXAxis().getTickStart();
74         float yPosition;
75
76         //---when centering the shapes on the points, need x and y offset to do this
77
double[] cornerXOffset=new double[ iAxisChartDataSet.getNumberOfDataSets() ];
78         double[] cornerYOffset=new double[ iAxisChartDataSet.getNumberOfDataSets() ];
79
80
81         //---get the original transform so can reset it.
82
AffineTransform JavaDoc affineTransform=g2d.getTransform();
83
84         Rectangle2D JavaDoc rectangle;
85
86         //LOOP
87
//---pre-compute the dimensions of each Shape so do not do it in loop.
88
for( int i=0; i < iAxisChartDataSet.getNumberOfDataSets(); i++ )
89         {
90             rectangle=pointChartProperties.getShape( i ).getBounds2D();
91             cornerXOffset[ i ]=rectangle.getWidth() / 2;
92             cornerYOffset[ i ]=rectangle.getHeight() / 2;
93         }
94
95         g2d.setStroke( PointChartProperties.DEFAULT_POINT_BORDER_STROKE );
96
97         //LOOP
98
//---for each item in data set...
99
for( int i=0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ )
100         {
101             //LOOP
102
//---for each data set
103
for( int dataSetIndex=0; dataSetIndex < iAxisChartDataSet.getNumberOfDataSets(); dataSetIndex++ )
104             {
105                 //---if the value is NaN, do not display a value
106
if( Double.isNaN( iAxisChartDataSet.getValue( dataSetIndex, i ) ) )
107                 {
108                     continue;
109                 }
110
111                 yPosition= axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
112                                                                                          iAxisChartDataSet.getValue( dataSetIndex, i ),
113                                                                                          axisChart.getYAxis().getScaleCalculator().getMinValue() );
114
115                 //---if we are generating an ImageMap, store the image coordinates
116
if( axisChart.getGenerateImageMapFlag() )
117                 {
118                     String JavaDoc label;
119                     if( axisChart.getXAxis().getAxisLabelsGroup() != null )
120                     {
121                         label = axisChart.getXAxis().getAxisLabelsGroup().getTextTag( i ).getText();
122                     }
123                     else
124                     {
125                         label = null;
126                     }
127
128                     axisChart.getImageMap().addImageMapArea( new CircleMapArea( xPosition,
129                                                                                                     yPosition,
130                                                                                                     iAxisChartDataSet.getValue( dataSetIndex, i ),
131                                                                                                     label,
132                                                                                                     iAxisChartDataSet.getLegendLabel( dataSetIndex ) ) );
133                 }
134
135                 //---translate the Shape into position.
136
g2d.translate( xPosition - cornerXOffset[ dataSetIndex ], yPosition - cornerYOffset[ dataSetIndex ] );
137
138                 g2d.setPaint( iAxisChartDataSet.getPaint( dataSetIndex ) );
139
140                 //---render the point
141
if( pointChartProperties.getFillPointsFlag( dataSetIndex ) )
142                 {
143                     //---fill the point
144
g2d.fill( pointChartProperties.getShape( dataSetIndex ) );
145
146                     //---if we are filling the points, see if we should outline the Shape
147
if( pointChartProperties.getPointOutlinePaints( dataSetIndex ) != null )
148                     {
149                         g2d.setPaint( pointChartProperties.getPointOutlinePaints( dataSetIndex ) );
150                         g2d.draw( pointChartProperties.getShape( dataSetIndex ) );
151                     }
152                 }
153                 else
154                 {
155                     //---if we are NOT filling the points, use the Paint specified with the DataSet to outline.
156
g2d.draw( pointChartProperties.getShape( dataSetIndex ) );
157                 }
158
159                 g2d.setTransform( affineTransform );
160             }
161
162             //---move right to the next x-axis scale item
163
xPosition+=axisChart.getXAxis().getScalePixelWidth();
164         }
165     }
166 }
167
Popular Tags