001: /***********************************************************************************************
002: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
003: *
004: * Redistribution and use of this software and associated documentation ("Software"), with or
005: * without modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain copyright statements and notices.
008: * Redistributions must also contain a copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
011: * conditions and the following disclaimer in the documentation and/or other materials
012: * provided with the distribution.
013: *
014: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
015: * products derived from this Software without prior written permission of Nathaniel G.
016: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
017: *
018: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
019: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
020: * registered trademark of Nathaniel G. Auvil.
021: *
022: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
023: *
024: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
025: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
026: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
029: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
030: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
032: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
033: ************************************************************************************************/package org.krysalis.jcharts.demo.simpleservlet;
034:
035: import org.krysalis.jcharts.axisChart.AxisChart;
036: import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition;
037: import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer;
038: import org.krysalis.jcharts.chartData.AxisChartDataSet;
039: import org.krysalis.jcharts.chartData.DataSeries;
040: import org.krysalis.jcharts.chartData.interfaces.IAxisDataSeries;
041: import org.krysalis.jcharts.encoders.ServletEncoderHelper;
042: import org.krysalis.jcharts.properties.AxisProperties;
043: import org.krysalis.jcharts.properties.BarChartProperties;
044: import org.krysalis.jcharts.properties.ChartProperties;
045: import org.krysalis.jcharts.properties.DataAxisProperties;
046: import org.krysalis.jcharts.properties.LegendProperties;
047: import org.krysalis.jcharts.properties.PropertyException;
048: import org.krysalis.jcharts.properties.util.ChartFont;
049: import org.krysalis.jcharts.types.ChartType;
050:
051: import javax.servlet.ServletException;
052: import javax.servlet.http.HttpServlet;
053: import javax.servlet.http.HttpServletRequest;
054: import javax.servlet.http.HttpServletResponse;
055: import java.awt.*;
056: import java.io.IOException;
057:
058: /*************************************************************************************
059: *
060: * @author Nathaniel Auvil
061: * @version $Id: BarChartServlet.java,v 1.1 2003/08/28 01:17:36 nathaniel_auvil Exp $
062: ************************************************************************************/
063: public class BarChartServlet extends HttpServlet {
064: //---all of my charts serviced by this Servlet will have the same properties.
065: private BarChartProperties barChartProperties;
066:
067: //---all of my charts serviced by this Servlet will have the same properties.
068: protected LegendProperties legendProperties;
069: protected AxisProperties axisProperties;
070: protected ChartProperties chartProperties;
071:
072: protected int width = 550;
073: protected int height = 360;
074:
075: /**********************************************************************************************
076: *
077: **********************************************************************************************/
078: public void init() {
079: this .legendProperties = new LegendProperties();
080: this .chartProperties = new ChartProperties();
081: this .axisProperties = new AxisProperties(false);
082: ChartFont axisScaleFont = new ChartFont(new Font(
083: "Georgia Negreta cursiva", Font.PLAIN, 13), Color.black);
084: axisProperties.getXAxisProperties().setScaleChartFont(
085: axisScaleFont);
086: axisProperties.getYAxisProperties().setScaleChartFont(
087: axisScaleFont);
088:
089: ChartFont axisTitleFont = new ChartFont(new Font(
090: "Arial Narrow", Font.PLAIN, 14), Color.black);
091: axisProperties.getXAxisProperties().setTitleChartFont(
092: axisTitleFont);
093: axisProperties.getYAxisProperties().setTitleChartFont(
094: axisTitleFont);
095:
096: DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties
097: .getYAxisProperties();
098:
099: try {
100: dataAxisProperties.setUserDefinedScale(-3000, 3000);
101: } catch (PropertyException propertyException) {
102: propertyException.printStackTrace();
103: }
104:
105: dataAxisProperties.setRoundToNearest(2);
106:
107: ChartFont titleFont = new ChartFont(new Font(
108: "Georgia Negreta cursiva", Font.PLAIN, 14), Color.black);
109: this .chartProperties.setTitleFont(titleFont);
110:
111: this .barChartProperties = new BarChartProperties();
112:
113: ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer(
114: false, false, true, -1);
115: valueLabelRenderer
116: .setValueLabelPosition(ValueLabelPosition.ON_TOP);
117: valueLabelRenderer.useVerticalLabels(false);
118: barChartProperties
119: .addPostRenderEventListener(valueLabelRenderer);
120:
121: }
122:
123: /**********************************************************************************************
124: *
125: **********************************************************************************************/
126: public void service(HttpServletRequest req,
127: HttpServletResponse httpServletResponse)
128: throws ServletException, IOException {
129: try {
130: String[] xAxisLabels = { "1995", "1996", "1997", "1998",
131: "1999", "2000", "2001", "2002", "2003", "2004" };
132: String xAxisTitle = "Years";
133: String yAxisTitle = "Problems";
134: String title = "Micro$oft At Work";
135: IAxisDataSeries dataSeries = new DataSeries(xAxisLabels,
136: xAxisTitle, yAxisTitle, title);
137:
138: double[][] data = new double[][] { { 1500, 6880, 4510,
139: 2600, -1200, -1580, 7000, 4555, 4000, 6120 } };
140: String[] legendLabels = { "Bugs" };
141: Paint[] paints = new Paint[] { Color.yellow };
142: dataSeries.addIAxisPlotDataSet(new AxisChartDataSet(data,
143: legendLabels, paints, ChartType.BAR,
144: this .barChartProperties));
145:
146: AxisChart axisChart = new AxisChart(dataSeries,
147: this .chartProperties, this .axisProperties,
148: this .legendProperties, this .width, this .height);
149: ServletEncoderHelper.encodeJPEG13(axisChart, 1.0f,
150: httpServletResponse);
151: } catch (Throwable throwable) {
152: //HACK do your error handling here...
153: throwable.printStackTrace();
154: }
155: }
156: }
|