1 39 40 package org.krysalis.jcharts.demo.simpleservlet; 41 42 43 import org.krysalis.jcharts.chartData.ChartDataException; 44 import org.krysalis.jcharts.chartData.PieChartDataSet; 45 import org.krysalis.jcharts.encoders.ServletEncoderHelper; 46 import org.krysalis.jcharts.nonAxisChart.PieChart2D; 47 import org.krysalis.jcharts.properties.ChartProperties; 48 import org.krysalis.jcharts.properties.LegendProperties; 49 import org.krysalis.jcharts.properties.PieChart2DProperties; 50 51 import javax.servlet.ServletException ; 52 import javax.servlet.http.HttpServlet ; 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletResponse ; 55 import java.awt.*; 56 import java.io.IOException ; 57 58 59 public class PieChart2DServlet extends HttpServlet 60 { 61 private PieChart2DProperties properties; 63 private LegendProperties legendProperties; 64 private ChartProperties chartProperties; 65 66 private int width = 550; 67 private int height = 350; 68 69 70 73 public void init() 74 { 75 this.properties = new PieChart2DProperties(); 77 78 this.legendProperties = new LegendProperties(); 79 this.legendProperties.setNumColumns( 2 ); 80 this.legendProperties.setPlacement( LegendProperties.RIGHT ); 81 82 this.chartProperties = new ChartProperties(); 83 } 84 85 86 89 public void service( HttpServletRequest req, HttpServletResponse response ) throws ServletException , IOException 90 { 91 try 92 { 93 PieChart2D pieChart2D = new PieChart2D( this.getData(), this.legendProperties, this.chartProperties, this.width, this.height ); 94 ServletEncoderHelper.encodeJPEG13( pieChart2D, 1.0f, response ); 95 } 96 catch( Throwable throwable ) 97 { 98 throwable.printStackTrace(); 100 } 101 } 102 103 104 109 private PieChartDataSet getData() throws ChartDataException 110 { 111 double[] data = new double[]{40, 15, 35, 65, 59}; 112 Paint[] paints = new Paint[]{Color.blue, Color.red, Color.green, Color.yellow, Color.white}; 113 String [] labels = {"BMW", "Honda", "Lexus", "Audi", "Acura"}; 114 return new PieChartDataSet( "Cars That Own!", data, labels, paints, this.properties ); 115 } 116 } 117 | Popular Tags |