KickJava   Java API By Example, From Geeks To Geeks.

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


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

48
49 package org.krysalis.jcharts.demo.simpleservlet;
50
51
52 import org.krysalis.jcharts.properties.LineChartProperties;
53 import org.krysalis.jcharts.properties.BarChartProperties;
54 import org.krysalis.jcharts.properties.LegendProperties;
55 import org.krysalis.jcharts.properties.AxisProperties;
56 import org.krysalis.jcharts.properties.ChartProperties;
57 import org.krysalis.jcharts.properties.PointChartProperties;
58 import org.krysalis.jcharts.properties.util.ChartFont;
59 import org.krysalis.jcharts.chartData.interfaces.IAxisDataSeries;
60 import org.krysalis.jcharts.chartData.DataSeries;
61 import org.krysalis.jcharts.chartData.AxisChartDataSet;
62 import org.krysalis.jcharts.types.ChartType;
63 import org.krysalis.jcharts.axisChart.AxisChart;
64 import org.krysalis.jcharts.encoders.ServletEncoderHelper;
65
66 import javax.servlet.ServletException JavaDoc;
67 import javax.servlet.http.*;
68 import java.awt.*;
69 import java.io.IOException JavaDoc;
70
71
72 public class ComboChartServlet extends HttpServlet
73 {
74     //---all of my charts serviced by this Servlet will have the same properties.
75
private LineChartProperties lineChartProperties;
76
77     //---all of my charts serviced by this Servlet will have the same properties.
78
private BarChartProperties barChartProperties;
79
80     //---all of my charts serviced by this Servlet will have the same properties.
81
protected LegendProperties legendProperties;
82     protected AxisProperties axisProperties;
83     protected ChartProperties chartProperties;
84
85     protected int width = 550;
86     protected int height = 360;
87
88
89     /**********************************************************************************************
90      *
91      **********************************************************************************************/

92     public void init()
93     {
94         this.legendProperties = new LegendProperties();
95         this.chartProperties = new ChartProperties();
96         this.axisProperties = new AxisProperties( false );
97         ChartFont axisScaleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.black );
98         axisProperties.getXAxisProperties().setScaleChartFont( axisScaleFont );
99         axisProperties.getYAxisProperties().setScaleChartFont( axisScaleFont );
100
101         ChartFont axisTitleFont = new ChartFont( new Font( "Arial Narrow", Font.PLAIN, 14 ), Color.black );
102         axisProperties.getXAxisProperties().setTitleChartFont( axisTitleFont );
103         axisProperties.getYAxisProperties().setTitleChartFont( axisTitleFont );
104
105         ChartFont titleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 14 ), Color.black );
106         this.chartProperties.setTitleFont( titleFont );
107
108
109         Stroke[] strokes = {LineChartProperties.DEFAULT_LINE_STROKE};
110         Shape[] shapes = {PointChartProperties.SHAPE_DIAMOND};
111         this.lineChartProperties = new LineChartProperties( strokes, shapes );
112
113         this.barChartProperties = new BarChartProperties();
114     }
115
116
117     /**********************************************************************************************
118      *
119      **********************************************************************************************/

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