KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > test > Pie3DTestDriver


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.test;
36
37
38 import org.krysalis.jcharts.chartData.ChartDataException;
39 import org.krysalis.jcharts.chartData.PieChartDataSet;
40 import org.krysalis.jcharts.imageMap.ImageMap;
41 import org.krysalis.jcharts.nonAxisChart.PieChart2D;
42 import org.krysalis.jcharts.nonAxisChart.PieChart3D;
43 import org.krysalis.jcharts.properties.ChartProperties;
44 import org.krysalis.jcharts.properties.LegendAreaProperties;
45 import org.krysalis.jcharts.properties.LegendProperties;
46 import org.krysalis.jcharts.properties.PieChart2DProperties;
47 import org.krysalis.jcharts.properties.PieChart3DProperties;
48 import org.krysalis.jcharts.properties.PropertyException;
49 import org.krysalis.jcharts.properties.util.ChartStroke;
50 import org.krysalis.jcharts.types.PieLabelType;
51
52 import java.awt.*;
53
54
55 /*************************************************************************************
56  *
57  * @author Nathaniel Auvil
58  * @version $Id: Pie3DTestDriver.java,v 1.5 2003/06/25 01:39:48 nathaniel_auvil Exp $
59  ************************************************************************************/

60 public class Pie3DTestDriver
61 {
62
63     /******************************************************************************************
64      * Test for PieChart2D
65      *
66      * @throws ChartDataException
67      ******************************************************************************************/

68     static void test() throws ChartDataException, PropertyException
69     {
70         PieChart2D pieChart2D;
71         PieChartDataSet pieChartDataSet;
72         LegendProperties legendProperties;
73         ChartProperties chartProperties;
74
75         int dataSize;
76         int width;
77         int height;
78         int numTestsToRun = 15;
79         String JavaDoc fileName;
80
81         HTMLGenerator htmlGenerator = new HTMLGenerator( ChartTestDriver.OUTPUT_PATH + "pieChart2dTest.html" );
82
83         for( int i = 0; i < numTestsToRun; i++ )
84         {
85             boolean createImageMap = true; //( TestDataGenerator.getRandomNumber( 1 ) > 0.5d );
86

87             dataSize = (int) TestDataGenerator.getRandomNumber( 1, 10 );
88             pieChartDataSet = Pie3DTestDriver.getPieChartDataSet( dataSize, 1, 7 );
89
90             width = (int) TestDataGenerator.getRandomNumber( 100, 600 );
91             height = (int) TestDataGenerator.getRandomNumber( 100, 600 );
92
93             legendProperties = new LegendProperties();
94             TestDataGenerator.randomizeLegend( legendProperties );
95             //legendProperties.setBorderStroke( new BasicStroke( 2.0f ) );
96

97             chartProperties = new ChartProperties();
98             //areaProperties.setEdgePadding( (int) TestDataGenerator.getRandomNumber( 0, 50 ) );
99
chartProperties.setBackgroundPaint( TestDataGenerator.getRandomPaint() );
100             //chartProperties.setBorderStroke( new BasicStroke( 1f ) );
101

102             pieChart2D = new PieChart2D( pieChartDataSet, legendProperties, chartProperties, width, height );
103
104             fileName = ChartTestDriver.OUTPUT_PATH + "pieChart2d" + i + ChartTestDriver.EXTENSION;
105
106             ImageMap imageMap;
107             if( createImageMap )
108             {
109                 pieChart2D.renderWithImageMap();
110                 imageMap = pieChart2D.getImageMap();
111             }
112             else
113             {
114                 imageMap = null;
115             }
116
117
118             ChartTestDriver.exportImage( pieChart2D, fileName );
119
120
121             htmlGenerator.chartTableStart( "PieChart2D", fileName, imageMap );
122             htmlGenerator.propertiesTableRowStart();
123             pieChartDataSet.toHTML( htmlGenerator );
124             htmlGenerator.propertiesTableRowStart();
125             pieChart2D.toHTML( htmlGenerator, fileName );
126
127             htmlGenerator.addLineBreak();
128         }
129
130         htmlGenerator.saveFile();
131     }
132
133
134     /*****************************************************************************************
135      * Generates a random NonAxisChartDataSet
136      *
137      * @param numToCreate the number of doubles to generate
138      * @param minValue
139      * @param maxValue
140      * @return PieChartDataSet
141      ******************************************************************************************/

142     private static PieChartDataSet getPieChartDataSet( int numToCreate, int minValue, int maxValue ) throws ChartDataException
143     {
144         PieChart2DProperties properties = new PieChart2DProperties();
145         //properties.setZeroDegreeOffset( (float) TestDataGenerator.getRandomNumber( 0, 500 ) );
146
properties.setBorderChartStroke( new ChartStroke( new BasicStroke( 1.0f ), TestDataGenerator.getRandomPaint() ) );
147
148         String JavaDoc[] labels = TestDataGenerator.getRandomStrings( numToCreate, (int) TestDataGenerator.getRandomNumber( 3, 20 ), false );
149         Paint[] paints = TestDataGenerator.getRandomPaints( numToCreate );
150
151         return new PieChartDataSet( "This is a test title", TestDataGenerator.getRandomNumbers( numToCreate, minValue, maxValue ), labels, paints, properties );
152     }
153
154
155     /***********************************************************************************************
156      *
157      * @param args
158      * @throws ChartDataException
159      * @throws PropertyException
160      ************************************************************************************************/

161     public static void main( String JavaDoc[] args ) throws ChartDataException, PropertyException
162     {
163
164
165         double[] data = {45.00d, 90.00d, 45.00d, 180d};
166         String JavaDoc[] labels = {"Equities", "Bonds", "Money Market", "Alternative Investments"};
167         //Paint[] paints = TestDataGenerator.getRandomPaints( data.length );
168

169         //Paint[] paints = { new Color( 200, 0, 0 ), new Color( 0, 200, 0 ), new Color( 0, 0, 200 ), new Color( 200, 200, 0 ) };
170
Paint[] paints = { new Color( 200, 0, 0, 20 ),
171                                  new Color( 0, 200, 0, 220 ),
172                                  new Color( 0, 0, 200, 20 ),
173                                  new Color( 200, 200, 0, 20 ) };
174
175
176 /*
177         double[] data = { 73.6d };
178         String[] labels = { "Alternative Investments"};
179         Paint[] paints = { Color.blue };
180 */

181
182
183
184         PieChart3DProperties pieChart3DProperties = new PieChart3DProperties();
185         pieChart3DProperties.setPieLabelType( PieLabelType.LEGEND_LABELS );
186         pieChart3DProperties.setTickLength( 5 );
187
188         pieChart3DProperties.setZeroDegreeOffset( 60 );
189
190         pieChart3DProperties.setDepth( 15 );
191
192         LegendProperties legendProperties = new LegendProperties();
193         legendProperties.setPlacement( LegendAreaProperties.RIGHT );
194         legendProperties.setNumColumns( 1 );
195         //legendProperties.setBorderStroke( null );
196

197         PieChartDataSet pieChartDataSet = new PieChartDataSet( "Investment Categories", data, labels, paints, pieChart3DProperties );
198
199         ChartProperties chartProperties = new ChartProperties();
200         chartProperties.setBorderStroke( ChartStroke.DEFAULT_CHART_OUTLINE );
201
202         PieChart3D pieChart = new PieChart3D( pieChartDataSet, legendProperties, chartProperties, 600, 200 );
203         //PieChart2D pieChart = new PieChart2D( pieChartDataSet, legendProperties, chartProperties, 400, 300 );
204

205         ChartTestDriver.exportImage( pieChart, "pie3d.png" );
206     }
207
208
209 }
210
Popular Tags