KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > demo > simpleservlet > ScatterPlotChartServlet


1 /***********************************************************************************************
2  * File Info: $Id: ScatterPlotChartServlet.java,v 1.1 2003/08/28 01:17:36 nathaniel_auvil Exp $
3  * Copyright (C) 2002
4  * Author: Nathaniel G. Auvil
5  * Contributor(s):
6  *
7  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
8  *
9  * Redistribution and use of this software and associated documentation ("Software"), with or
10  * without modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain copyright statements and notices.
13  * Redistributions must also contain a copy of this document.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
16  * conditions and the following disclaimer in the documentation and/or other materials
17  * provided with the distribution.
18  *
19  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
20  * products derived from this Software without prior written permission of Nathaniel G.
21  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
22  *
23  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
24  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
25  * registered trademark of Nathaniel G. Auvil.
26  *
27  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
28  *
29  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
30  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  ************************************************************************************************/

39
40 package org.krysalis.jcharts.demo.simpleservlet;
41
42
43 import org.krysalis.jcharts.axisChart.ScatterPlotAxisChart;
44 import org.krysalis.jcharts.chartData.ScatterPlotDataSeries;
45 import org.krysalis.jcharts.chartData.ScatterPlotDataSet;
46 import org.krysalis.jcharts.encoders.ServletEncoderHelper;
47 import org.krysalis.jcharts.properties.AxisProperties;
48 import org.krysalis.jcharts.properties.ChartProperties;
49 import org.krysalis.jcharts.properties.DataAxisProperties;
50 import org.krysalis.jcharts.properties.LegendProperties;
51 import org.krysalis.jcharts.properties.LineChartProperties;
52 import org.krysalis.jcharts.properties.PointChartProperties;
53 import org.krysalis.jcharts.properties.ScatterPlotProperties;
54 import org.krysalis.jcharts.properties.util.ChartFont;
55
56 import javax.servlet.ServletException JavaDoc;
57 import javax.servlet.http.HttpServlet JavaDoc;
58 import javax.servlet.http.HttpServletRequest JavaDoc;
59 import javax.servlet.http.HttpServletResponse JavaDoc;
60 import java.awt.*;
61 import java.awt.geom.Point2D JavaDoc;
62 import java.io.IOException JavaDoc;
63
64
65 public class ScatterPlotChartServlet extends HttpServlet JavaDoc
66 {
67     //---all of my charts serviced by this Servlet will have the same properties.
68
protected LegendProperties legendProperties;
69     protected AxisProperties axisProperties;
70     protected ChartProperties chartProperties;
71
72     protected int width = 550;
73     protected int height = 360;
74
75
76     /**********************************************************************************************
77      *
78      **********************************************************************************************/

79     public void init()
80     {
81         this.legendProperties = new LegendProperties();
82         this.chartProperties = new ChartProperties();
83         this.axisProperties = new AxisProperties( true );
84         ChartFont axisScaleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.black );
85         axisProperties.getXAxisProperties().setScaleChartFont( axisScaleFont );
86         axisProperties.getYAxisProperties().setScaleChartFont( axisScaleFont );
87
88         ChartFont axisTitleFont = new ChartFont( new Font( "Arial Narrow", Font.PLAIN, 14 ), Color.black );
89         axisProperties.getXAxisProperties().setTitleChartFont( axisTitleFont );
90         axisProperties.getYAxisProperties().setTitleChartFont( axisTitleFont );
91
92         ChartFont titleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 14 ), Color.black );
93         this.chartProperties.setTitleFont( titleFont );
94     }
95
96
97     /******************************************************************************************
98      *
99      *
100      ******************************************************************************************/

101     private ScatterPlotProperties createScatterPlotProperties()
102     {
103         Stroke[] strokes = new Stroke[]{LineChartProperties.DEFAULT_LINE_STROKE};
104         Shape[] shapes = new Shape[]{PointChartProperties.SHAPE_CIRCLE};
105
106         return new ScatterPlotProperties( strokes, shapes );
107     }
108
109
110     /*****************************************************************************************
111      * Generates a random MultiDataSet
112      *
113      * @return ScatterPlotDataSet
114      ******************************************************************************************/

115     private ScatterPlotDataSet createScatterPlotDataSet()
116     {
117         Point2D.Double JavaDoc[] points = new Point2D.Double JavaDoc[ 20 ];
118         for( int x = 0; x < 20; x++ )
119         {
120             //--- y = x^2
121
points[ x ] = ScatterPlotDataSet.createPoint2DDouble();
122             points[ x ].setLocation( x, Math.pow( x, 2 ) );
123         }
124
125         ScatterPlotDataSet scatterPlotDataSet = new ScatterPlotDataSet( this.createScatterPlotProperties() );
126         scatterPlotDataSet.addDataPoints( points, Color.red, "Proprietary" );
127
128         return scatterPlotDataSet;
129     }
130
131
132     /**********************************************************************************************
133      *
134      **********************************************************************************************/

135     public void service( HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc httpServletResponse ) throws ServletException JavaDoc, IOException JavaDoc
136     {
137         try
138         {
139             ScatterPlotDataSet scatterPlotDataSet = this.createScatterPlotDataSet();
140             ScatterPlotDataSeries scatterPlotDataSeries = new ScatterPlotDataSeries( scatterPlotDataSet,
141                                                                                                              "X-Axis Title",
142                                                                                                              "Y-Axis Title",
143                                                                                                              "Chart Title" );
144
145             DataAxisProperties xAxisProperties = new DataAxisProperties();
146             xAxisProperties.setUserDefinedScale( -5, 3 );
147             xAxisProperties.setNumItems( 10 );
148             xAxisProperties.setRoundToNearest( 0 );
149
150             DataAxisProperties yAxisProperties = new DataAxisProperties();
151             yAxisProperties.setUserDefinedScale( -30, 50 );
152             yAxisProperties.setNumItems( 10 );
153             yAxisProperties.setRoundToNearest( 1 );
154
155             AxisProperties axisProperties = new AxisProperties( xAxisProperties, yAxisProperties );
156             ChartProperties chartProperties = new ChartProperties();
157             LegendProperties legendProperties = new LegendProperties();
158
159             ScatterPlotAxisChart scatterPlotAxisChart = new ScatterPlotAxisChart( scatterPlotDataSeries,
160                                                                                                          chartProperties,
161                                                                                                          axisProperties,
162                                                                                                          legendProperties,
163                                                                                                          500,
164                                                                                                          400 );
165
166             ServletEncoderHelper.encodeJPEG13( scatterPlotAxisChart, 1.0f, httpServletResponse );
167         }
168         catch( Throwable JavaDoc throwable )
169         {
170             //HACK do your error handling here...
171
throwable.printStackTrace();
172         }
173
174     }
175 }
176
Popular Tags