KickJava   Java API By Example, From Geeks To Geeks.

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


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.demo.simpleservlet;
36
37
38 import org.krysalis.jcharts.axisChart.AxisChart;
39 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition;
40 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer;
41 import org.krysalis.jcharts.chartData.AxisChartDataSet;
42 import org.krysalis.jcharts.chartData.DataSeries;
43 import org.krysalis.jcharts.chartData.interfaces.IAxisDataSeries;
44 import org.krysalis.jcharts.encoders.ServletEncoderHelper;
45 import org.krysalis.jcharts.properties.AxisProperties;
46 import org.krysalis.jcharts.properties.BarChartProperties;
47 import org.krysalis.jcharts.properties.ChartProperties;
48 import org.krysalis.jcharts.properties.DataAxisProperties;
49 import org.krysalis.jcharts.properties.LegendProperties;
50 import org.krysalis.jcharts.properties.PropertyException;
51 import org.krysalis.jcharts.properties.util.ChartFont;
52 import org.krysalis.jcharts.types.ChartType;
53
54 import javax.servlet.ServletException JavaDoc;
55 import javax.servlet.http.HttpServlet JavaDoc;
56 import javax.servlet.http.HttpServletRequest JavaDoc;
57 import javax.servlet.http.HttpServletResponse JavaDoc;
58 import java.awt.*;
59 import java.io.IOException JavaDoc;
60
61
62 /*************************************************************************************
63  *
64  * @author Nathaniel Auvil
65  * @version $Id: BarChartServlet.java,v 1.1 2003/08/28 01:17:36 nathaniel_auvil Exp $
66  ************************************************************************************/

67 public class BarChartServlet extends HttpServlet JavaDoc
68 {
69     //---all of my charts serviced by this Servlet will have the same properties.
70
private BarChartProperties barChartProperties;
71
72     //---all of my charts serviced by this Servlet will have the same properties.
73
protected LegendProperties legendProperties;
74     protected AxisProperties axisProperties;
75     protected ChartProperties chartProperties;
76
77     protected int width = 550;
78     protected int height = 360;
79
80
81     /**********************************************************************************************
82      *
83      **********************************************************************************************/

84     public void init()
85     {
86         this.legendProperties = new LegendProperties();
87         this.chartProperties = new ChartProperties();
88         this.axisProperties = new AxisProperties( false );
89         ChartFont axisScaleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.black );
90         axisProperties.getXAxisProperties().setScaleChartFont( axisScaleFont );
91         axisProperties.getYAxisProperties().setScaleChartFont( axisScaleFont );
92
93         ChartFont axisTitleFont = new ChartFont( new Font( "Arial Narrow", Font.PLAIN, 14 ), Color.black );
94         axisProperties.getXAxisProperties().setTitleChartFont( axisTitleFont );
95         axisProperties.getYAxisProperties().setTitleChartFont( axisTitleFont );
96
97         DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties();
98
99         try
100         {
101             dataAxisProperties.setUserDefinedScale( -3000, 3000 );
102         }
103         catch( PropertyException propertyException )
104         {
105             propertyException.printStackTrace();
106         }
107
108         dataAxisProperties.setRoundToNearest( 2 );
109
110         ChartFont titleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 14 ), Color.black );
111         this.chartProperties.setTitleFont( titleFont );
112
113         this.barChartProperties = new BarChartProperties();
114
115         ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer( false, false, true, -1 );
116         valueLabelRenderer.setValueLabelPosition( ValueLabelPosition.ON_TOP );
117         valueLabelRenderer.useVerticalLabels( false );
118         barChartProperties.addPostRenderEventListener( valueLabelRenderer );
119
120     }
121
122
123     /**********************************************************************************************
124      *
125      **********************************************************************************************/

126     public void service( HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc httpServletResponse ) throws ServletException JavaDoc, IOException JavaDoc
127     {
128         try
129         {
130             String JavaDoc[] xAxisLabels = {"1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004"};
131             String JavaDoc xAxisTitle = "Years";
132             String JavaDoc yAxisTitle = "Problems";
133             String JavaDoc title = "Micro$oft At Work";
134             IAxisDataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
135
136             double[][] data = new double[][]{{1500, 6880, 4510, 2600, -1200, -1580, 7000, 4555, 4000, 6120}};
137             String JavaDoc[] legendLabels = {"Bugs"};
138             Paint[] paints = new Paint[]{Color.yellow};
139             dataSeries.addIAxisPlotDataSet( new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, this.barChartProperties ) );
140
141             AxisChart axisChart = new AxisChart( dataSeries, this.chartProperties, this.axisProperties, this.legendProperties, this.width, this.height );
142             ServletEncoderHelper.encodeJPEG13( axisChart, 1.0f, httpServletResponse );
143         }
144         catch( Throwable JavaDoc throwable )
145         {
146             //HACK do your error handling here...
147
throwable.printStackTrace();
148         }
149     }
150 }
151
Popular Tags