1 34 35 package org.krysalis.jcharts.demo.simpleservlet; 36 37 38 import org.krysalis.jcharts.axisChart.AxisChart; 39 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition; 40 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer; 41 import org.krysalis.jcharts.chartData.AxisChartDataSet; 42 import org.krysalis.jcharts.chartData.DataSeries; 43 import org.krysalis.jcharts.chartData.interfaces.IAxisDataSeries; 44 import org.krysalis.jcharts.encoders.ServletEncoderHelper; 45 import org.krysalis.jcharts.properties.AxisProperties; 46 import org.krysalis.jcharts.properties.BarChartProperties; 47 import org.krysalis.jcharts.properties.ChartProperties; 48 import org.krysalis.jcharts.properties.DataAxisProperties; 49 import org.krysalis.jcharts.properties.LegendProperties; 50 import org.krysalis.jcharts.properties.PropertyException; 51 import org.krysalis.jcharts.properties.util.ChartFont; 52 import org.krysalis.jcharts.types.ChartType; 53 54 import javax.servlet.ServletException ; 55 import javax.servlet.http.HttpServlet ; 56 import javax.servlet.http.HttpServletRequest ; 57 import javax.servlet.http.HttpServletResponse ; 58 import java.awt.*; 59 import java.io.IOException ; 60 61 62 67 public class BarChartServlet extends HttpServlet 68 { 69 private BarChartProperties barChartProperties; 71 72 protected LegendProperties legendProperties; 74 protected AxisProperties axisProperties; 75 protected ChartProperties chartProperties; 76 77 protected int width = 550; 78 protected int height = 360; 79 80 81 84 public void init() 85 { 86 this.legendProperties = new LegendProperties(); 87 this.chartProperties = new ChartProperties(); 88 this.axisProperties = new AxisProperties( false ); 89 ChartFont axisScaleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.black ); 90 axisProperties.getXAxisProperties().setScaleChartFont( axisScaleFont ); 91 axisProperties.getYAxisProperties().setScaleChartFont( axisScaleFont ); 92 93 ChartFont axisTitleFont = new ChartFont( new Font( "Arial Narrow", Font.PLAIN, 14 ), Color.black ); 94 axisProperties.getXAxisProperties().setTitleChartFont( axisTitleFont ); 95 axisProperties.getYAxisProperties().setTitleChartFont( axisTitleFont ); 96 97 DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties(); 98 99 try 100 { 101 dataAxisProperties.setUserDefinedScale( -3000, 3000 ); 102 } 103 catch( PropertyException propertyException ) 104 { 105 propertyException.printStackTrace(); 106 } 107 108 dataAxisProperties.setRoundToNearest( 2 ); 109 110 ChartFont titleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 14 ), Color.black ); 111 this.chartProperties.setTitleFont( titleFont ); 112 113 this.barChartProperties = new BarChartProperties(); 114 115 ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer( false, false, true, -1 ); 116 valueLabelRenderer.setValueLabelPosition( ValueLabelPosition.ON_TOP ); 117 valueLabelRenderer.useVerticalLabels( false ); 118 barChartProperties.addPostRenderEventListener( valueLabelRenderer ); 119 120 } 121 122 123 126 public void service( HttpServletRequest req, HttpServletResponse httpServletResponse ) throws ServletException , IOException 127 { 128 try 129 { 130 String [] xAxisLabels = {"1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004"}; 131 String xAxisTitle = "Years"; 132 String yAxisTitle = "Problems"; 133 String title = "Micro$oft At Work"; 134 IAxisDataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 135 136 double[][] data = new double[][]{{1500, 6880, 4510, 2600, -1200, -1580, 7000, 4555, 4000, 6120}}; 137 String [] legendLabels = {"Bugs"}; 138 Paint[] paints = new Paint[]{Color.yellow}; 139 dataSeries.addIAxisPlotDataSet( new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, this.barChartProperties ) ); 140 141 AxisChart axisChart = new AxisChart( dataSeries, this.chartProperties, this.axisProperties, this.legendProperties, this.width, this.height ); 142 ServletEncoderHelper.encodeJPEG13( axisChart, 1.0f, httpServletResponse ); 143 } 144 catch( Throwable throwable ) 145 { 146 throwable.printStackTrace(); 148 } 149 } 150 } 151 | Popular Tags |