KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
39 import org.krysalis.jcharts.imageMap.CircleMapArea;
40 import org.krysalis.jcharts.properties.*;
41
42 import java.awt.*;
43 import java.awt.geom.*;
44
45
46 /*************************************************************************************
47  *
48  * @author Nathaniel Auvil
49  * @version $Id: ScatterPlotChart.java,v 1.2 2003/11/02 13:34:17 nathaniel_auvil Exp $
50  ************************************************************************************/

51 abstract class ScatterPlotChart
52 {
53
54     /********************************************************************************************
55      * Draws the chart
56      *
57      * @param scatterPlotAxisChart
58      * @param iScatterPlotDataSet
59      *********************************************************************************************/

60     static void render( ScatterPlotAxisChart scatterPlotAxisChart, IScatterPlotDataSet iScatterPlotDataSet )
61     {
62         //---cache the computed values
63
float[][] xAxisCoordinates = new float[ iScatterPlotDataSet.getNumberOfDataSets() ][ iScatterPlotDataSet.getNumberOfDataItems() ];
64         float[][] yAxisCoordinates = new float[ iScatterPlotDataSet.getNumberOfDataSets() ][ iScatterPlotDataSet.getNumberOfDataItems() ];
65
66
67 //System.out.println( "x origin= " + scatterPlotAxisChart.getXAxis().getOrigin() + " y origin= " + scatterPlotAxisChart.getYAxis().getOrigin() );
68

69         //LOOP
70
for( int index = 0; index < iScatterPlotDataSet.getNumberOfDataItems(); index++ )
71         {
72             //LOOP
73
for( int dataSet = 0; dataSet < yAxisCoordinates.length; dataSet++ )
74             {
75                 if( iScatterPlotDataSet.getValue( dataSet, index ) != null )
76                 {
77                     xAxisCoordinates[ dataSet ][ index ] = scatterPlotAxisChart.getXAxis().computeAxisCoordinate( scatterPlotAxisChart.getXAxis().getOrigin(),
78                                                                                                                                                  iScatterPlotDataSet.getValue( dataSet, index ).getX(),
79                                                                                                                                                  scatterPlotAxisChart.getXAxis().getScaleCalculator().getMinValue() );
80
81                     yAxisCoordinates[ dataSet ][ index ] = scatterPlotAxisChart.getYAxis().computeAxisCoordinate( scatterPlotAxisChart.getYAxis().getOrigin(),
82                                                                                                                                                  iScatterPlotDataSet.getValue( dataSet, index ).getY(),
83                                                                                                                                                  scatterPlotAxisChart.getYAxis().getScaleCalculator().getMinValue() );
84
85 //System.out.println( "x= " + xAxisCoordinates[ dataSet ][ index ] + " y= " + yAxisCoordinates[ dataSet ][ index ] );
86

87                     //---if we are generating an ImageMap, store the image coordinates
88
if( scatterPlotAxisChart.getGenerateImageMapFlag() )
89                     {
90                         scatterPlotAxisChart.getImageMap().addImageMapArea( new CircleMapArea( xAxisCoordinates[ dataSet ][ index ],
91                                                                                                                       yAxisCoordinates[ dataSet ][ index ],
92                                                                                                                       iScatterPlotDataSet.getValue( dataSet, index ),
93                                                                                                                       iScatterPlotDataSet.getLegendLabel( dataSet ) ) );
94                     }
95                 }
96                 else
97                 {
98                     xAxisCoordinates[ dataSet ][ index ] = Float.NaN;
99                     yAxisCoordinates[ dataSet ][ index ] = Float.NaN;
100                 }
101             }
102         }
103
104
105         ScatterPlotProperties scatterPlotProperties = (ScatterPlotProperties) iScatterPlotDataSet.getChartTypeProperties();
106         //DataAxisProperties xAxisProperties = (DataAxisProperties) scatterPlotAxisChart.getAxisProperties().getXAxisProperties();
107
//DataAxisProperties yAxisProperties = (DataAxisProperties) scatterPlotAxisChart.getAxisProperties().getYAxisProperties();
108

109         Graphics2D g2d = scatterPlotAxisChart.getGraphics2D();
110         AffineTransform originalTransform = null;
111         double[] cornerXOffset = null;
112         double[] cornerYOffset = null;
113
114         //---check if there are any points to display
115
if( scatterPlotProperties.getShapes() != null )
116         {
117             //---when centering the shapes on the points, need x and y offset to do this
118
cornerXOffset = new double[ iScatterPlotDataSet.getNumberOfDataSets() ];
119             cornerYOffset = new double[ iScatterPlotDataSet.getNumberOfDataSets() ];
120
121             //---get the original transform so can reset it.
122
originalTransform = g2d.getTransform();
123
124             Rectangle2D rectangle;
125
126             //LOOP
127
//---pre-compute the dimensions of each Shape so do not do it in loop.
128
for( int i = 0; i < iScatterPlotDataSet.getNumberOfDataSets(); i++ )
129             {
130                 if( scatterPlotProperties.getShapes()[ i ] != null )
131                 {
132                     rectangle = scatterPlotProperties.getShapes()[ i ].getBounds2D();
133                     cornerXOffset[ i ] = rectangle.getWidth() / 2;
134                     cornerYOffset[ i ] = rectangle.getHeight() / 2;
135                 }
136             }
137         }
138
139
140         //---init for first segment
141
Line2D.Float line = new Line2D.Float( xAxisCoordinates[ 0 ][ 0 ],
142                                                           yAxisCoordinates[ 0 ][ 0 ],
143                                                           xAxisCoordinates[ 0 ][ 1 ],
144                                                           yAxisCoordinates[ 0 ][ 1 ] );
145         //---make sure not plotting a chart with only one data point.
146
if( yAxisCoordinates[ 0 ].length > 1 )
147         {
148 //todo what is this for?
149
line.y2 = yAxisCoordinates[ 0 ][ 1 ];
150         }
151
152
153         //LOOP
154
//---draw each line to the image
155
for( int i = 0; i < yAxisCoordinates.length; i++ )
156         {
157             line.x1 = xAxisCoordinates[ i ][ 0 ];
158             line.y1 = yAxisCoordinates[ i ][ 0 ];
159             line.x2 = line.x1;
160
161             //LOOP
162
for( int j = 1; j < yAxisCoordinates[ 0 ].length; j++ )
163             {
164                 //---if current point on line should be drawn
165
if( !Float.isNaN( yAxisCoordinates[ i ][ j ] ) )
166                 {
167                     //---if the previous point was not drawn, no line
168
if( Float.isNaN( yAxisCoordinates[ i ][ j - 1 ] ) )
169                     {
170                         line.x1 = xAxisCoordinates[ i ][ j ];
171                         line.y1 = yAxisCoordinates[ i ][ j ];
172                         line.x2 = xAxisCoordinates[ i ][ j ];
173                         line.y2 = yAxisCoordinates[ i ][ j ];
174                         continue;
175                     }
176
177
178                     line.x2 = xAxisCoordinates[ i ][ j ];
179                     line.y2 = yAxisCoordinates[ i ][ j ];
180
181                     g2d.setPaint( iScatterPlotDataSet.getPaint( i ) );
182                     g2d.setStroke( scatterPlotProperties.getLineStrokes()[ i ] );
183                     g2d.draw( line );
184
185                     //---plot the Point
186
if( scatterPlotProperties.getShapes()[ i ] != null )
187                     {
188                         //---translate the Shape into position.
189
g2d.translate( line.x1 - cornerXOffset[ i ], line.y1 - cornerYOffset[ i ] );
190
191                         g2d.setPaint( iScatterPlotDataSet.getPaint( i ) );
192                         g2d.fill( scatterPlotProperties.getShapes()[ i ] );
193
194                         //---translate back to the original position
195
g2d.setTransform( originalTransform );
196                     }
197
198                     line.x1 = line.x2;
199                     line.y1 = line.y2;
200                 }
201                 else
202                 {
203                     if( (!Float.isNaN( yAxisCoordinates[ i ][ j - 1 ] )) && ( scatterPlotProperties.getShapes()[ i ] != null ) )
204                     {
205                         //---translate the Shape into position.
206
g2d.translate( line.x1 - cornerXOffset[ i ], line.y1 - cornerYOffset[ i ] );
207
208                         g2d.setPaint( iScatterPlotDataSet.getPaint( i ) );
209                         g2d.fill( scatterPlotProperties.getShapes()[ i ] );
210
211                         //---translate back to the original position
212
g2d.setTransform( originalTransform );
213                     }
214
215                     line.x2 = scatterPlotAxisChart.getXAxis().getScalePixelWidth();
216                     line.x1 = line.x2;
217                 }
218             }
219
220
221             //---put the last shape on the line
222
if( (!Float.isNaN( yAxisCoordinates[ i ][ yAxisCoordinates[ i ].length - 1 ] )) && ( scatterPlotProperties.getShapes()[ i ] != null ) )
223             {
224                 //---translate the Shape into position.
225
g2d.translate( line.x2 - cornerXOffset[ i ], line.y2 - cornerYOffset[ i ] );
226
227                 g2d.setPaint( iScatterPlotDataSet.getPaint( i ) );
228                 g2d.fill( scatterPlotProperties.getShapes()[ i ] );
229
230                 //---translate back to the original position
231
g2d.setTransform( originalTransform );
232             }
233         }
234     }
235
236
237 }
238
Popular Tags