KickJava   Java API By Example, From Geeks To Geeks.

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


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
36 package org.krysalis.jcharts.axisChart;
37
38
39 import org.krysalis.jcharts.axisChart.axis.*;
40 import org.krysalis.jcharts.axisChart.axis.scale.*;
41 import org.krysalis.jcharts.chartData.interfaces.*;
42 import org.krysalis.jcharts.chartData.processors.AxisChartDataProcessor;
43 import org.krysalis.jcharts.chartData.processors.ScatterPlotDataProcessor;
44 import org.krysalis.jcharts.chartText.NumericTagGroup;
45 import org.krysalis.jcharts.imageMap.ImageMap;
46 import org.krysalis.jcharts.properties.*;
47 import org.krysalis.jcharts.test.HTMLChartTestable;
48 import org.krysalis.jcharts.test.HTMLGenerator;
49 import org.krysalis.jcharts.types.ChartType;
50
51 import java.awt.font.FontRenderContext JavaDoc;
52
53
54
55 /**************************************************************************************
56  * This Class is used to create all axis chart types. This class knows how to render
57  * charts based on the ChartType specified in on the iAxisChartDataSet.
58  *
59  * @author Nathaniel Auvil
60  * @version $Id: ScatterPlotAxisChart.java,v 1.2 2003/06/20 01:54:58 nathaniel_auvil Exp $
61  ************************************************************************************/

62 public class ScatterPlotAxisChart extends AxisChart implements HTMLChartTestable
63 {
64
65     /**************************************************************************************************
66      * Constructor
67      *
68      * @param iScatterPlotDataSeries
69      * @param chartProperties
70      * @param axisProperties
71      * @param legendProperties if no legend is desired, pass NULL
72      * @param pixelWidth
73      * @param pixelHeight
74      ***************************************************************************************************/

75     public ScatterPlotAxisChart( IScatterPlotDataSeries iScatterPlotDataSeries,
76                                           ChartProperties chartProperties,
77                                           AxisProperties axisProperties,
78                                           LegendProperties legendProperties,
79                                           int pixelWidth,
80                                           int pixelHeight )
81     {
82         super( iScatterPlotDataSeries, chartProperties, axisProperties, legendProperties, pixelWidth, pixelHeight );
83     }
84
85
86     /********************************************************************************************
87      * ScatterPlots create a subclass of AxisChartDataProcessor so we need this method so we can
88      * overload it.
89      *
90      * @return AxisChartDataProcessor
91      ********************************************************************************************/

92     public AxisChartDataProcessor createAxisChartDataProcessor()
93     {
94         return new ScatterPlotDataProcessor();
95     }
96
97
98     /************************************************************************************************
99      * Once we determine which axis is the data axis, the logic to set it up is the same whether it
100      * is a horizontal or vertical plot.
101      *
102      * @param dataAxisProperties
103      * @param axisChartDataProcessor
104      * @param fontRenderContext
105      * @return NumericTagGroup need to set this on the right axis
106      ************************************************************************************************/

107     protected NumericTagGroup setupDataAxisProperties( Axis axis,
108                                                                         DataAxisProperties dataAxisProperties,
109                                                                         AxisChartDataProcessor axisChartDataProcessor,
110                                                                         FontRenderContext JavaDoc fontRenderContext )
111     {
112         //---we know this is of this type
113
ScatterPlotDataProcessor scatterPlotDataProcessor= (ScatterPlotDataProcessor) axisChartDataProcessor;
114
115         if( dataAxisProperties.getScaleCalculator() == null )
116         {
117             ScaleCalculator s;
118
119             if( dataAxisProperties.hasUserDefinedScale() )
120             {
121                 s = new UserDefinedScaleCalculator( dataAxisProperties.getUserDefinedMinimumValue(), dataAxisProperties.getUserDefinedIncrement() );
122             }
123             else
124             {
125                 s = new AutomaticScaleCalculator();
126                 if( axis instanceof XAxis )
127                 {
128                     s.setMaxValue( scatterPlotDataProcessor.getMaxValue() );
129                     s.setMinValue( scatterPlotDataProcessor.getMinValue() );
130                 }
131                 else
132                 {
133                     s.setMaxValue( scatterPlotDataProcessor.getyMax() );
134                     s.setMinValue( scatterPlotDataProcessor.getyMin() );
135                 }
136             }
137
138             axis.setScaleCalculator( s );
139         }
140         else
141         {
142             axis.setScaleCalculator( dataAxisProperties.getScaleCalculator() );
143
144             if( axis instanceof XAxis )
145             {
146                 axis.getScaleCalculator().setMaxValue( scatterPlotDataProcessor.getMaxValue() );
147                 axis.getScaleCalculator().setMinValue( scatterPlotDataProcessor.getMinValue() );
148             }
149             else
150             {
151                 axis.getScaleCalculator().setMaxValue( scatterPlotDataProcessor.getyMax() );
152                 axis.getScaleCalculator().setMinValue( scatterPlotDataProcessor.getyMin() );
153             }
154         }
155
156         axis.getScaleCalculator().setRoundingPowerOfTen( dataAxisProperties.getRoundToNearest() );
157         axis.getScaleCalculator().setNumberOfScaleItems( dataAxisProperties.getNumItems() );
158         axis.getScaleCalculator().computeScaleValues();
159
160
161 //TODO what if they do not want to display axis labels?
162
NumericTagGroup numericTagGroup = new NumericTagGroup( dataAxisProperties.getScaleChartFont(),
163                                                                                  fontRenderContext,
164                                                                                  dataAxisProperties.useDollarSigns(),
165                                                                                  dataAxisProperties.usePercentSigns(),
166                                                                                  dataAxisProperties.useCommas(),
167                                                                                  dataAxisProperties.getRoundToNearest() );
168
169         numericTagGroup.createAxisScaleLabels( axis.getScaleCalculator() );
170
171         return numericTagGroup;
172     }
173
174
175     /***************************************************************************************
176      *
177      *
178      * @param axisChartDataProcessor
179      * @param fontRenderContext
180      **************************************************************************************/

181     protected void setupAxis( AxisChartDataProcessor axisChartDataProcessor, FontRenderContext JavaDoc fontRenderContext )
182     {
183         //---X AXIS---------------------------------------------------------------------------
184
DataAxisProperties dataAxisProperties = (DataAxisProperties) this.getAxisProperties().getXAxisProperties();
185         this.xAxis = new XAxis( this, dataAxisProperties.getNumItems() );
186         NumericTagGroup numericTagGroup = this.setupDataAxisProperties( this.xAxis, dataAxisProperties, axisChartDataProcessor, fontRenderContext );
187         this.xAxis.setAxisLabelsGroup( numericTagGroup );
188
189         //---Y AXIS---------------------------------------------------------------------------
190
dataAxisProperties = (DataAxisProperties) this.getAxisProperties().getYAxisProperties();
191         this.yAxis = new YAxis( this, dataAxisProperties.getNumItems() );
192         numericTagGroup = this.setupDataAxisProperties( this.yAxis, dataAxisProperties, axisChartDataProcessor, fontRenderContext );
193         this.yAxis.setAxisLabelsGroup( numericTagGroup );
194
195
196         //---if yAxisTitle is null, do not show title
197
this.yAxis.computeMinimumWidthNeeded( super.getIAxisDataSeries().getYAxisTitle() );
198         this.xAxis.computeMinimumHeightNeeded( super.getIAxisDataSeries().getXAxisTitle() );
199     }
200
201
202     /******************************************************************************************
203      *
204      *****************************************************************************************/

205     protected void deriveAxisValues()
206     {
207         //---Determine how many labels will fit on the x-axis
208
//TODO should we do this also for the YAxis?
209
//todo what if they do not want labels on the x-axis?
210
this.xAxis.computeLabelFilter();
211
212         this.xAxis.computeShouldTickStartAtYAxis( super.getIAxisDataSeries(), this.axisProperties.getXAxisProperties() );
213
214
215         //---X Axis--------------------
216
//DataAxisProperties dataAxisProperties = (DataAxisProperties) this.axisProperties.getXAxisProperties();
217
this.xAxis.computeScalePixelWidthDataAxis( this.axisProperties.getXAxisProperties() );
218         this.xAxis.computeOneUnitPixelSize( this.xAxis.getScalePixelWidth(), this.xAxis.getScaleCalculator().getIncrement() );
219
220         //---we ADD to the origin position when doing x-axis
221
float zeroLineCoordinate = (float) (this.xAxis.getOrigin() + (this.xAxis.getScalePixelWidth() * (-this.xAxis.getScaleCalculator().getMinValue())) / this.xAxis.getScaleCalculator().getIncrement());
222         this.xAxis.setZeroLineCoordinate( zeroLineCoordinate );
223
224
225         //---Y Axis--------------------
226
//dataAxisProperties = (DataAxisProperties) this.axisProperties.getYAxisProperties();
227
this.yAxis.computeScalePixelWidthDataAxis( this.axisProperties.getYAxisProperties() );
228         this.yAxis.computeOneUnitPixelSize( this.yAxis.getScalePixelWidth(), this.yAxis.getScaleCalculator().getIncrement() );
229
230         //---we SUBTRACT to the origin position when doing y-axis
231
zeroLineCoordinate = (float) (this.yAxis.getOrigin() - (this.yAxis.getScalePixelWidth() * (-this.yAxis.getScaleCalculator().getMinValue())) / this.yAxis.getScaleCalculator().getIncrement());
232         this.yAxis.setZeroLineCoordinate( zeroLineCoordinate );
233
234         this.xAxis.computeTickStart();
235     }
236
237
238     /********************************************************************************************
239      * Draws the charts over the axis. We have to render in a specific order so combo charts
240      * get drawn correctly
241      *
242      * @throws PropertyException
243      ******************************************************************************************/

244     protected void overlayCharts() throws PropertyException
245     {
246         IAxisPlotDataSet iAxisPlotDataSet = super.getIAxisDataSeries().getIAxisPlotDataSet( ChartType.SCATTER_PLOT );
247         ScatterPlotChart.render( this, (IScatterPlotDataSet) iAxisPlotDataSet );
248     }
249
250
251     /**********************************************************************************************
252      * Enables the testing routines to display the contents of this Object. Override Chart
253      * implementation as PieCharts use AreaProperties directly rather than a child.
254      *
255      * @param htmlGenerator
256      * @param imageFileName
257      * @param imageMap if this is NULL we are not creating image map data in html
258      **********************************************************************************************/

259     public void toHTML( HTMLGenerator htmlGenerator, String JavaDoc imageFileName, ImageMap imageMap )
260     {
261         htmlGenerator.chartTableStart( this.getClass().getName(), imageFileName, imageMap );
262
263 /*
264         if( iDataSeries instanceof HTMLTestable )
265         {
266             ( ( HTMLTestable ) this.iDataSeries ).toHTML( htmlGenerator );
267         }
268 */

269
270         //---AxisProperties
271
htmlGenerator.chartTableRowStart();
272         this.axisProperties.toHTML( htmlGenerator );
273         htmlGenerator.chartTableRowEnd();
274
275
276         //---XAxis
277
htmlGenerator.chartTableRowStart();
278         this.xAxis.toHTML( htmlGenerator );
279         htmlGenerator.chartTableRowEnd();
280
281         //---YAxis
282
htmlGenerator.chartTableRowStart();
283         this.yAxis.toHTML( htmlGenerator );
284         htmlGenerator.chartTableRowEnd();
285
286
287         //---ChartProperties
288
htmlGenerator.chartTableRowStart();
289         super.getChartProperties().toHTML( htmlGenerator );
290         htmlGenerator.chartTableRowEnd();
291
292
293         if( super.getLegend() != null )
294         {
295             htmlGenerator.chartTableRowStart();
296             this.getLegend().toHTML( htmlGenerator );
297             htmlGenerator.chartTableRowEnd();
298         }
299
300         htmlGenerator.chartTableEnd();
301     }
302
303 }
304
Popular Tags