KickJava   Java API By Example, From Geeks To Geeks.

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


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.chartData.AxisChartDataSet;
40 import org.krysalis.jcharts.chartData.ChartDataException;
41 import org.krysalis.jcharts.chartData.DataSeries;
42 import org.krysalis.jcharts.chartData.interfaces.IAxisDataSeries;
43 import org.krysalis.jcharts.imageMap.ImageMap;
44 import org.krysalis.jcharts.properties.AxisProperties;
45 import org.krysalis.jcharts.properties.BarChartProperties;
46 import org.krysalis.jcharts.properties.ChartProperties;
47 import org.krysalis.jcharts.properties.LegendProperties;
48 import org.krysalis.jcharts.properties.PropertyException;
49 import org.krysalis.jcharts.properties.util.ChartFont;
50 import org.krysalis.jcharts.types.ChartType;
51
52 import javax.servlet.ServletException JavaDoc;
53 import javax.servlet.http.HttpServlet JavaDoc;
54 import javax.servlet.http.HttpServletRequest JavaDoc;
55 import javax.servlet.http.HttpServletResponse JavaDoc;
56 import java.awt.*;
57 import java.io.IOException JavaDoc;
58
59
60 /*************************************************************************************
61  *
62  * @author Nathaniel Auvil
63  * @version $Id: HorizontalBarImageMapServlet.java,v 1.1 2003/08/28 01:17:36 nathaniel_auvil Exp $
64  ************************************************************************************/

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

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

106     public void service( HttpServletRequest JavaDoc httpServletRequest,
107                                 HttpServletResponse JavaDoc httpServletResponse ) throws ServletException JavaDoc, IOException JavaDoc
108     {
109         try
110         {
111             String JavaDoc[] xAxisLabels = {"1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004"};
112             String JavaDoc xAxisTitle = "Years";
113             String JavaDoc yAxisTitle = "Problems";
114             String JavaDoc title = "Micro$oft At Work";
115             IAxisDataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
116
117             double[][] data = new double[][]{{1500, 6880, 4510, 2600, 1200, 1580, 8000, 4555, 4000, 6120}};
118             String JavaDoc[] legendLabels = {"Bugs"};
119             Paint[] paints = new Paint[]{Color.blue.darker()};
120             dataSeries.addIAxisPlotDataSet( new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, this.barChartProperties ) );
121
122             AxisChart axisChart = new AxisChart( dataSeries, this.chartProperties, this.axisProperties, this.legendProperties, this.width, this.height );
123
124             //---this call will render the chart to an internal BufferedImage, creating all the image map coordinates
125
axisChart.renderWithImageMap();
126
127             //---get the ImageMap information from the chart
128
ImageMap imageMap = axisChart.getImageMap();
129
130             //---set the ImageMap into the HttpServletRequest so can get it out in JSP
131
httpServletRequest.setAttribute( ChartServlet.IMAGE_MAP, imageMap );
132
133             //---set the Chart into the HttpSession so we can stream it in another request
134
httpServletRequest.getSession( true ).setAttribute( ChartServlet.CHART, axisChart );
135         }
136         catch( ChartDataException chartDataException )
137         {
138             chartDataException.printStackTrace();
139         }
140         catch( PropertyException propertyException )
141         {
142             propertyException.printStackTrace();
143         }
144
145
146         this.getServletContext().getRequestDispatcher( "/imageMapChart.jsp" ).forward( httpServletRequest, httpServletResponse );
147     }
148
149 }
150
Popular Tags