001: /***********************************************************************************************
002: * File Info: $Id: ScatterPlotChartServlet.java,v 1.1 2003/08/28 01:17:36 nathaniel_auvil Exp $
003: * Copyright (C) 2002
004: * Author: Nathaniel G. Auvil
005: * Contributor(s):
006: *
007: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
008: *
009: * Redistribution and use of this software and associated documentation ("Software"), with or
010: * without modification, are permitted provided that the following conditions are met:
011: *
012: * 1. Redistributions of source code must retain copyright statements and notices.
013: * Redistributions must also contain a copy of this document.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
016: * conditions and the following disclaimer in the documentation and/or other materials
017: * provided with the distribution.
018: *
019: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
020: * products derived from this Software without prior written permission of Nathaniel G.
021: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
022: *
023: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
024: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
025: * registered trademark of Nathaniel G. Auvil.
026: *
027: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
028: *
029: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
030: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
031: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
033: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
034: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
037: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: ************************************************************************************************/package org.krysalis.jcharts.demo.simpleservlet;
039:
040: import org.krysalis.jcharts.axisChart.ScatterPlotAxisChart;
041: import org.krysalis.jcharts.chartData.ScatterPlotDataSeries;
042: import org.krysalis.jcharts.chartData.ScatterPlotDataSet;
043: import org.krysalis.jcharts.encoders.ServletEncoderHelper;
044: import org.krysalis.jcharts.properties.AxisProperties;
045: import org.krysalis.jcharts.properties.ChartProperties;
046: import org.krysalis.jcharts.properties.DataAxisProperties;
047: import org.krysalis.jcharts.properties.LegendProperties;
048: import org.krysalis.jcharts.properties.LineChartProperties;
049: import org.krysalis.jcharts.properties.PointChartProperties;
050: import org.krysalis.jcharts.properties.ScatterPlotProperties;
051: import org.krysalis.jcharts.properties.util.ChartFont;
052:
053: import javax.servlet.ServletException;
054: import javax.servlet.http.HttpServlet;
055: import javax.servlet.http.HttpServletRequest;
056: import javax.servlet.http.HttpServletResponse;
057: import java.awt.*;
058: import java.awt.geom.Point2D;
059: import java.io.IOException;
060:
061: public class ScatterPlotChartServlet extends HttpServlet {
062: //---all of my charts serviced by this Servlet will have the same properties.
063: protected LegendProperties legendProperties;
064: protected AxisProperties axisProperties;
065: protected ChartProperties chartProperties;
066:
067: protected int width = 550;
068: protected int height = 360;
069:
070: /**********************************************************************************************
071: *
072: **********************************************************************************************/
073: public void init() {
074: this .legendProperties = new LegendProperties();
075: this .chartProperties = new ChartProperties();
076: this .axisProperties = new AxisProperties(true);
077: ChartFont axisScaleFont = new ChartFont(new Font(
078: "Georgia Negreta cursiva", Font.PLAIN, 13), Color.black);
079: axisProperties.getXAxisProperties().setScaleChartFont(
080: axisScaleFont);
081: axisProperties.getYAxisProperties().setScaleChartFont(
082: axisScaleFont);
083:
084: ChartFont axisTitleFont = new ChartFont(new Font(
085: "Arial Narrow", Font.PLAIN, 14), Color.black);
086: axisProperties.getXAxisProperties().setTitleChartFont(
087: axisTitleFont);
088: axisProperties.getYAxisProperties().setTitleChartFont(
089: axisTitleFont);
090:
091: ChartFont titleFont = new ChartFont(new Font(
092: "Georgia Negreta cursiva", Font.PLAIN, 14), Color.black);
093: this .chartProperties.setTitleFont(titleFont);
094: }
095:
096: /******************************************************************************************
097: *
098: *
099: ******************************************************************************************/
100: private ScatterPlotProperties createScatterPlotProperties() {
101: Stroke[] strokes = new Stroke[] { LineChartProperties.DEFAULT_LINE_STROKE };
102: Shape[] shapes = new Shape[] { PointChartProperties.SHAPE_CIRCLE };
103:
104: return new ScatterPlotProperties(strokes, shapes);
105: }
106:
107: /*****************************************************************************************
108: * Generates a random MultiDataSet
109: *
110: * @return ScatterPlotDataSet
111: ******************************************************************************************/
112: private ScatterPlotDataSet createScatterPlotDataSet() {
113: Point2D.Double[] points = new Point2D.Double[20];
114: for (int x = 0; x < 20; x++) {
115: //--- y = x^2
116: points[x] = ScatterPlotDataSet.createPoint2DDouble();
117: points[x].setLocation(x, Math.pow(x, 2));
118: }
119:
120: ScatterPlotDataSet scatterPlotDataSet = new ScatterPlotDataSet(
121: this .createScatterPlotProperties());
122: scatterPlotDataSet.addDataPoints(points, Color.red,
123: "Proprietary");
124:
125: return scatterPlotDataSet;
126: }
127:
128: /**********************************************************************************************
129: *
130: **********************************************************************************************/
131: public void service(HttpServletRequest req,
132: HttpServletResponse httpServletResponse)
133: throws ServletException, IOException {
134: try {
135: ScatterPlotDataSet scatterPlotDataSet = this
136: .createScatterPlotDataSet();
137: ScatterPlotDataSeries scatterPlotDataSeries = new ScatterPlotDataSeries(
138: scatterPlotDataSet, "X-Axis Title", "Y-Axis Title",
139: "Chart Title");
140:
141: DataAxisProperties xAxisProperties = new DataAxisProperties();
142: xAxisProperties.setUserDefinedScale(-5, 3);
143: xAxisProperties.setNumItems(10);
144: xAxisProperties.setRoundToNearest(0);
145:
146: DataAxisProperties yAxisProperties = new DataAxisProperties();
147: yAxisProperties.setUserDefinedScale(-30, 50);
148: yAxisProperties.setNumItems(10);
149: yAxisProperties.setRoundToNearest(1);
150:
151: AxisProperties axisProperties = new AxisProperties(
152: xAxisProperties, yAxisProperties);
153: ChartProperties chartProperties = new ChartProperties();
154: LegendProperties legendProperties = new LegendProperties();
155:
156: ScatterPlotAxisChart scatterPlotAxisChart = new ScatterPlotAxisChart(
157: scatterPlotDataSeries, chartProperties,
158: axisProperties, legendProperties, 500, 400);
159:
160: ServletEncoderHelper.encodeJPEG13(scatterPlotAxisChart,
161: 1.0f, httpServletResponse);
162: } catch (Throwable throwable) {
163: //HACK do your error handling here...
164: throwable.printStackTrace();
165: }
166:
167: }
168: }
|