1 39 40 package org.krysalis.jcharts.demo.simpleservlet; 41 42 43 import org.krysalis.jcharts.axisChart.ScatterPlotAxisChart; 44 import org.krysalis.jcharts.chartData.ScatterPlotDataSeries; 45 import org.krysalis.jcharts.chartData.ScatterPlotDataSet; 46 import org.krysalis.jcharts.encoders.ServletEncoderHelper; 47 import org.krysalis.jcharts.properties.AxisProperties; 48 import org.krysalis.jcharts.properties.ChartProperties; 49 import org.krysalis.jcharts.properties.DataAxisProperties; 50 import org.krysalis.jcharts.properties.LegendProperties; 51 import org.krysalis.jcharts.properties.LineChartProperties; 52 import org.krysalis.jcharts.properties.PointChartProperties; 53 import org.krysalis.jcharts.properties.ScatterPlotProperties; 54 import org.krysalis.jcharts.properties.util.ChartFont; 55 56 import javax.servlet.ServletException ; 57 import javax.servlet.http.HttpServlet ; 58 import javax.servlet.http.HttpServletRequest ; 59 import javax.servlet.http.HttpServletResponse ; 60 import java.awt.*; 61 import java.awt.geom.Point2D ; 62 import java.io.IOException ; 63 64 65 public class ScatterPlotChartServlet extends HttpServlet 66 { 67 protected LegendProperties legendProperties; 69 protected AxisProperties axisProperties; 70 protected ChartProperties chartProperties; 71 72 protected int width = 550; 73 protected int height = 360; 74 75 76 79 public void init() 80 { 81 this.legendProperties = new LegendProperties(); 82 this.chartProperties = new ChartProperties(); 83 this.axisProperties = new AxisProperties( true ); 84 ChartFont axisScaleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.black ); 85 axisProperties.getXAxisProperties().setScaleChartFont( axisScaleFont ); 86 axisProperties.getYAxisProperties().setScaleChartFont( axisScaleFont ); 87 88 ChartFont axisTitleFont = new ChartFont( new Font( "Arial Narrow", Font.PLAIN, 14 ), Color.black ); 89 axisProperties.getXAxisProperties().setTitleChartFont( axisTitleFont ); 90 axisProperties.getYAxisProperties().setTitleChartFont( axisTitleFont ); 91 92 ChartFont titleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 14 ), Color.black ); 93 this.chartProperties.setTitleFont( titleFont ); 94 } 95 96 97 101 private ScatterPlotProperties createScatterPlotProperties() 102 { 103 Stroke[] strokes = new Stroke[]{LineChartProperties.DEFAULT_LINE_STROKE}; 104 Shape[] shapes = new Shape[]{PointChartProperties.SHAPE_CIRCLE}; 105 106 return new ScatterPlotProperties( strokes, shapes ); 107 } 108 109 110 115 private ScatterPlotDataSet createScatterPlotDataSet() 116 { 117 Point2D.Double [] points = new Point2D.Double [ 20 ]; 118 for( int x = 0; x < 20; x++ ) 119 { 120 points[ x ] = ScatterPlotDataSet.createPoint2DDouble(); 122 points[ x ].setLocation( x, Math.pow( x, 2 ) ); 123 } 124 125 ScatterPlotDataSet scatterPlotDataSet = new ScatterPlotDataSet( this.createScatterPlotProperties() ); 126 scatterPlotDataSet.addDataPoints( points, Color.red, "Proprietary" ); 127 128 return scatterPlotDataSet; 129 } 130 131 132 135 public void service( HttpServletRequest req, HttpServletResponse httpServletResponse ) throws ServletException , IOException 136 { 137 try 138 { 139 ScatterPlotDataSet scatterPlotDataSet = this.createScatterPlotDataSet(); 140 ScatterPlotDataSeries scatterPlotDataSeries = new ScatterPlotDataSeries( scatterPlotDataSet, 141 "X-Axis Title", 142 "Y-Axis Title", 143 "Chart Title" ); 144 145 DataAxisProperties xAxisProperties = new DataAxisProperties(); 146 xAxisProperties.setUserDefinedScale( -5, 3 ); 147 xAxisProperties.setNumItems( 10 ); 148 xAxisProperties.setRoundToNearest( 0 ); 149 150 DataAxisProperties yAxisProperties = new DataAxisProperties(); 151 yAxisProperties.setUserDefinedScale( -30, 50 ); 152 yAxisProperties.setNumItems( 10 ); 153 yAxisProperties.setRoundToNearest( 1 ); 154 155 AxisProperties axisProperties = new AxisProperties( xAxisProperties, yAxisProperties ); 156 ChartProperties chartProperties = new ChartProperties(); 157 LegendProperties legendProperties = new LegendProperties(); 158 159 ScatterPlotAxisChart scatterPlotAxisChart = new ScatterPlotAxisChart( scatterPlotDataSeries, 160 chartProperties, 161 axisProperties, 162 legendProperties, 163 500, 164 400 ); 165 166 ServletEncoderHelper.encodeJPEG13( scatterPlotAxisChart, 1.0f, httpServletResponse ); 167 } 168 catch( Throwable throwable ) 169 { 170 throwable.printStackTrace(); 172 } 173 174 } 175 } 176 | Popular Tags |