KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
42
43 import java.awt.*;
44 import java.awt.geom.*;
45
46
47 /*************************************************************************************
48  *
49  * @author Nathaniel Auvil
50  * @version $Id: LineChart.java,v 1.3 2003/09/26 00:44:02 nathaniel_auvil Exp $
51  ************************************************************************************/

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

62     static void render( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet ) throws PropertyException
63     {
64         Graphics2D g2d=axisChart.getGraphics2D();
65
66         LineChartProperties lineChartProperties=(LineChartProperties) iAxisChartDataSet.getChartTypeProperties();
67         lineChartProperties.validate( iAxisChartDataSet );
68
69         //DataAxisProperties dataAxisProperties= (DataAxisProperties) axisChart.getAxisProperties().getYAxisProperties();
70
IDataSeries iDataSeries= (IDataSeries) axisChart.getIAxisDataSeries();
71
72         //---cache the computed values
73
float[][] yAxisCoordinates=new float[ iAxisChartDataSet.getNumberOfDataSets() ][ iAxisChartDataSet.getNumberOfDataItems() ];
74
75         //---need this for image map calculation
76
float xMapCoordinate=axisChart.getXAxis().getTickStart();
77
78
79         //LOOP
80
for( int j=0; j < iAxisChartDataSet.getNumberOfDataItems(); j++ )
81         {
82             //LOOP
83
for( int i=0; i < yAxisCoordinates.length; i++ )
84             {
85                 if( iAxisChartDataSet.getValue( i, j ) != Double.NaN )
86                 {
87                                         // Dual Y axis changes integrated CMC 25Aug03
88
//yAxisCoordinates[ i ][ j ]= axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
89
// iAxisChartDataSet.getValue( i, j ),
90
// axisChart.getYAxis().getScaleCalculator().getMinValue() );
91

92                                         // The coordinates of the line charts are drawn with the default scale and
93
// multiplicate with the second scale for the right axis (by default equal 1)
94
// if the second scale at the right is unchanged then there will be no impact
95
yAxisCoordinates[ i ][ j ]= axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
96                                                                                     iAxisChartDataSet.getValue( i, j )*axisChart.axisProperties.getYAxisProperties().getSecondScaleRight(),
97                                                                                     axisChart.getYAxis().getScaleCalculator().getMinValue() );
98
99                     //---if we are generating an ImageMap, store the image coordinates
100
if( axisChart.getGenerateImageMapFlag() )
101                     {
102                         String JavaDoc label;
103                         if( axisChart.getXAxis().getAxisLabelsGroup() != null )
104                         {
105                             label = axisChart.getXAxis().getAxisLabelsGroup().getTextTag( j ).getText();
106                         }
107                         else
108                         {
109                             label = null;
110                         }
111
112                         axisChart.getImageMap().addImageMapArea( new CircleMapArea( xMapCoordinate,
113                                                                                                         yAxisCoordinates[ i ][ j ],
114                                                                                                         iAxisChartDataSet.getValue( i, j ),
115                                                                                                         label,
116                                                                                                         iAxisChartDataSet.getLegendLabel( i ) ) );
117                     }
118                 }
119                 else
120                 {
121                     yAxisCoordinates[ i ][ j ]=Float.NaN;
122                 }
123             }
124
125             xMapCoordinate+=axisChart.getXAxis().getScalePixelWidth();
126         }
127
128
129         AffineTransform originalTransform=null;
130         double[] cornerXOffset=null;
131         double[] cornerYOffset=null;
132
133         //---check if there are any points to display
134
if( lineChartProperties.getShapes() != null )
135         {
136             //---when centering the shapes on the points, need x and y offset to do this
137
cornerXOffset=new double[ iAxisChartDataSet.getNumberOfDataSets() ];
138             cornerYOffset=new double[ iAxisChartDataSet.getNumberOfDataSets() ];
139
140             //---get the original transform so can reset it.
141
originalTransform=g2d.getTransform();
142
143             Rectangle2D rectangle;
144
145             //LOOP
146
//---pre-compute the dimensions of each Shape so do not do it in loop.
147
for( int i=0; i < iAxisChartDataSet.getNumberOfDataSets(); i++ )
148             {
149                 if( lineChartProperties.getShapes()[ i ] != null )
150                 {
151                     rectangle=lineChartProperties.getShapes()[ i ].getBounds2D();
152                     cornerXOffset[ i ]=rectangle.getWidth() / 2;
153                     cornerYOffset[ i ]=rectangle.getHeight() / 2;
154                 }
155             }
156         }
157
158
159         //---init for first segment
160
Line2D.Float line=new Line2D.Float( axisChart.getXAxis().getTickStart(),
161                                                         yAxisCoordinates[ 0 ][ 0 ],
162                                                         axisChart.getXAxis().getTickStart(),
163                                                         yAxisCoordinates[ 0 ][ 0 ] );
164         //---make sure not plotting a chart with only one data point.
165
if( yAxisCoordinates[ 0 ].length > 1 )
166         {
167             line.y2= yAxisCoordinates[ 0 ][ 1 ];
168         }
169
170
171         //LOOP
172
//---draw each line to the image
173
for( int i=0; i < yAxisCoordinates.length; i++ )
174         {
175             line.x1=axisChart.getXAxis().getTickStart();
176             line.y1=yAxisCoordinates[ i ][ 0 ];
177             line.x2=line.x1;
178
179             //LOOP
180
for( int j=1; j < yAxisCoordinates[ 0 ].length; j++ )
181             {
182                 //---if current point on line should be drawn
183
if( !Float.isNaN( yAxisCoordinates[ i ][ j ] ) )
184                 {
185                     //---if the previous point was not drawn, no line
186
if( Float.isNaN( yAxisCoordinates[ i ][ j - 1 ] ) )
187                     {
188                         line.x2+=axisChart.getXAxis().getScalePixelWidth();
189                         line.x1=line.x2;
190                         line.y1=yAxisCoordinates[ i ][ j ];
191                         line.y2=yAxisCoordinates[ i ][ j ];
192
193                         continue;
194                     }
195
196
197                     line.x2+=axisChart.getXAxis().getScalePixelWidth();
198                     line.y2=yAxisCoordinates[ i ][ j ];
199
200                     g2d.setPaint( iAxisChartDataSet.getPaint( i ) );
201                     g2d.setStroke( lineChartProperties.getLineStrokes()[ i ] );
202                     g2d.draw( line );
203
204                     //---plot the Point
205
if( lineChartProperties.getShapes()[ i ] != null )
206                     {
207                         //---translate the Shape into position.
208
g2d.translate( line.x1 - cornerXOffset[ i ], line.y1 - cornerYOffset[ i ] );
209
210                         g2d.setPaint( iAxisChartDataSet.getPaint( i ) );
211                         g2d.fill( lineChartProperties.getShapes()[ i ] );
212
213                         //---translate back to the original position
214
g2d.setTransform( originalTransform );
215                     }
216
217                     line.x1=line.x2;
218                     line.y1=line.y2;
219                 }
220                 else
221                 {
222                     if( ( !Float.isNaN( yAxisCoordinates[ i ][ j - 1 ] ) ) && ( lineChartProperties.getShapes()[ i ] != null ) )
223                     {
224                         //---translate the Shape into position.
225
g2d.translate( line.x1 - cornerXOffset[ i ], line.y1 - cornerYOffset[ i ] );
226
227                         g2d.setPaint( iAxisChartDataSet.getPaint( i ) );
228                         g2d.fill( lineChartProperties.getShapes()[ i ] );
229
230                         //---translate back to the original position
231
g2d.setTransform( originalTransform );
232                     }
233
234                     line.x2+=axisChart.getXAxis().getScalePixelWidth();
235                     line.x1=line.x2;
236                 }
237             }
238
239
240             //---put the last shape on the line
241
if( ( !Float.isNaN( yAxisCoordinates[ i ][ yAxisCoordinates[ i ].length - 1 ] ) ) && ( lineChartProperties.getShapes()[ i ] != null ) )
242             {
243                 //---translate the Shape into position.
244
g2d.translate( line.x2 - cornerXOffset[ i ], line.y2 - cornerYOffset[ i ] );
245
246                 g2d.setPaint( iAxisChartDataSet.getPaint( i ) );
247                 g2d.fill( lineChartProperties.getShapes()[ i ] );
248
249                 //---translate back to the original position
250
g2d.setTransform( originalTransform );
251             }
252         }
253     }
254
255
256 }
257
Popular Tags