KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

63     static final void render( AxisChart axisChart, IStockChartDataSet iStockChartDataSet )
64     {
65         StockChartProperties stockChartProperties=(StockChartProperties) iStockChartDataSet.getChartTypeProperties();
66         Graphics2D g2d=axisChart.getGraphics2D();
67
68         DataAxisProperties dataAxisProperties= (DataAxisProperties) axisChart.getAxisProperties().getYAxisProperties();
69       IDataSeries iDataSeries= (IDataSeries) axisChart.getIAxisDataSeries();
70
71
72         //---cache the computed values
73
//float[][] yAxisCoordinates=new float[ iStockChartDataSet.getNumberOfDataSets() ][ iStockChartDataSet.getNumberOfDataItems() ];
74

75
76         //---init for first segment
77
Line2D.Float JavaDoc line=new Line2D.Float JavaDoc( axisChart.getXAxis().getTickStart(),
78                                                         0,
79                                                         axisChart.getXAxis().getTickStart(),
80                                                         0 );
81
82
83         Line2D.Float JavaDoc openLine=null;
84         if( iStockChartDataSet.hasOpenValues() )
85         {
86             openLine=new Line2D.Float JavaDoc( axisChart.getXAxis().getTickStart() - stockChartProperties.getOpenPixelLength() - 1,
87                                                 0,
88                                                 axisChart.getXAxis().getTickStart() - 1,
89                                                 0 );
90         }
91
92
93         Line2D.Float JavaDoc closeLine=null;
94         if( iStockChartDataSet.hasCloseValues() )
95         {
96             closeLine=new Line2D.Float JavaDoc( axisChart.getXAxis().getTickStart() + 1,
97                                                  0,
98                                                  axisChart.getXAxis().getTickStart() + stockChartProperties.getClosePixelLength() + 1,
99                                                  0 );
100         }
101
102
103         //LOOP
104
//---draw each line to the image
105
for( int i=0; i < iStockChartDataSet.getNumberOfDataItems(); i++ )
106         {
107             line.y1= axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
108                                                                                   iStockChartDataSet.getLowValue( i ),
109                                                                                   axisChart.getYAxis().getScaleCalculator().getMinValue() );
110             line.y2= axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
111                                                                                   iStockChartDataSet.getHighValue( i ),
112                                                                                   axisChart.getYAxis().getScaleCalculator().getMinValue() );
113
114             String JavaDoc label;
115             if( axisChart.getXAxis().getAxisLabelsGroup() != null )
116             {
117                 label = axisChart.getXAxis().getAxisLabelsGroup().getTextTag( i ).getText();
118             }
119             else
120             {
121                 label = null;
122             }
123
124             //---if we are generating an ImageMap, store the image coordinates
125
if( axisChart.getGenerateImageMapFlag() )
126             {
127                 axisChart.getImageMap().addImageMapArea( new CircleMapArea( line.x1,
128                                                                                                 line.y1,
129                                                                                                 iStockChartDataSet.getLowValue( i ),
130                                                                                                 label,
131                                                                                                 iStockChartDataSet.getLegendLabel( StockChartDataType.LOW.getInt() ) ) );
132
133                 axisChart.getImageMap().addImageMapArea( new CircleMapArea( line.x2,
134                                                                                                 line.y2,
135                                                                                                 iStockChartDataSet.getHighValue( i ),
136                                                                                                 label,
137                                                                                                 iStockChartDataSet.getLegendLabel( StockChartDataType.HIGH.getInt() ) ) );
138             }
139
140             g2d.setPaint( iStockChartDataSet.getPaint( StockChartDataType.HIGH.getInt() ) );
141             g2d.setStroke( stockChartProperties.getHiLowStroke() );
142             g2d.draw( line );
143
144             line.x1+=axisChart.getXAxis().getScalePixelWidth();
145             line.x2=line.x1;
146
147             if( openLine != null )
148             {
149                 if( iStockChartDataSet.getOpenValue( i ) != Double.NaN )
150                 {
151                     openLine.y1= axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
152                                                                                                 iStockChartDataSet.getOpenValue( i ),
153                                                                                                 axisChart.getYAxis().getScaleCalculator().getMinValue() );
154                     openLine.y2=openLine.y1;
155
156                     g2d.setPaint( iStockChartDataSet.getPaint( StockChartDataType.OPEN.getInt() ) );
157                     g2d.setStroke( stockChartProperties.getOpenStroke() );
158                     g2d.draw( openLine );
159
160                     //---if we are generating an ImageMap, store the image coordinates
161
if( axisChart.getGenerateImageMapFlag() )
162                     {
163                         axisChart.getImageMap().addImageMapArea( new CircleMapArea( openLine.x1,
164                                                                                                         openLine.y1,
165                                                                                                         iStockChartDataSet.getOpenValue( i ),
166                                                                                                         label,
167                                                                                                         iStockChartDataSet.getLegendLabel( StockChartDataType.OPEN.getInt() ) ) );
168                     }
169
170                     openLine.x1+=axisChart.getXAxis().getScalePixelWidth();
171                     openLine.x2+=axisChart.getXAxis().getScalePixelWidth();
172                 }
173             }
174
175             if( closeLine != null )
176             {
177                 if( iStockChartDataSet.getOpenValue( i ) != Double.NaN )
178                 {
179                     closeLine.y1= axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(),
180                                                                                                  iStockChartDataSet.getCloseValue( i ),
181                                                                                                  axisChart.getYAxis().getScaleCalculator().getMinValue() );
182                     closeLine.y2=closeLine.y1;
183
184                     g2d.setPaint( iStockChartDataSet.getPaint( StockChartDataType.CLOSE.getInt() ) );
185                     g2d.setStroke( stockChartProperties.getCloseStroke() );
186                     g2d.draw( closeLine );
187
188                     //---if we are generating an ImageMap, store the image coordinates
189
if( axisChart.getGenerateImageMapFlag() )
190                     {
191                         axisChart.getImageMap().addImageMapArea( new CircleMapArea( closeLine.x2,
192                                                                                                         closeLine.y2,
193                                                                                                         iStockChartDataSet.getCloseValue( i ),
194                                                                                                         label,
195                                                                                                         iStockChartDataSet.getLegendLabel( StockChartDataType.CLOSE.getInt() ) ) );
196                     }
197
198                     closeLine.x1+=axisChart.getXAxis().getScalePixelWidth();
199                     closeLine.x2+=axisChart.getXAxis().getScalePixelWidth();
200                 }
201             }
202         }
203     }
204
205 }
206
Popular Tags