package com.gaojingmin;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultKeyedValuesDataset;
public class ChartServlet extends HttpServlet {
	/**
	 * Constructor of the object.
	 */
	public ChartServlet() {
		super();
	}
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        Map<String,Integer> ht=new Hashtable<String,Integer>();
        ht.put("美国", 139800);
        ht.put("日本", 52900);
        ht.put("德国", 32800);
        ht.put("中国", 30100);
        ht.put("英国", 25700);
        ht.put("法国", 25200);
        ht.put("意大利", 20900);
        ht.put("西班牙", 14100);
        ht.put("加拿大", 13600);
        ht.put("俄罗斯", 11400);
        
        float sum=0;
        for(Integer i:ht.values()){
            sum+=i;
        }
        
        // 设定数据源
        DefaultKeyedValuesDataset piedata = new DefaultKeyedValuesDataset();
        // 向数据源中插值,第一个参数为名称,第二个参数是double数
        for(String nation:ht.keySet()){
            piedata.setValue(nation, ((float)ht.get(nation))/sum*100);
        }
        
        // 使用ChartFactory来创建JFreeChart
        JFreeChart chart = ChartFactory.createPieChart("2007世界总GDP排名", piedata, true,
                true, true);
        
        // 设定图片标题
        chart.setTitle(new TextTitle("2007世界总GDP排名", new Font("隶书", Font.ITALIC, 15)));
        // 设定背景
        chart.setBackgroundPaint(Color.white);
        
        // 创建文件并将图片地址传递到页面
        String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
                null, request.getSession());
        String graphURL = request.getContextPath()
                + "/displayChart?filename=" + filename;
        request.setAttribute("graphURL", graphURL);
        
        // 标识
        request.setAttribute("msg", "饼图效果");
        // 页面转向
        RequestDispatcher dispatcher = request
                .getRequestDispatcher("/PieChart.jsp");
        dispatcher.forward(request, response);
        return;
	}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
}
 最近下载更多
最近下载更多
                
                1358849392     LV21
                2023年7月5日
            
            
        
                ok1821     LV1
                2022年5月10日
            
            
        
                shmtuec9     LV1
                2021年12月28日
            
            
        
                ericxu1116     LV24
                2021年6月5日
            
            
        
                fangzhicheng     LV1
                2021年4月21日
            
            
        
                infoboy     LV1
                2021年3月5日
            
            
        
                zjjhzjb     LV15
                2019年12月7日
            
            
        
                Yann12138     LV1
                2019年12月5日
            
            
        
                空心菜4     LV9
                2019年5月30日
            
            
        
                听雨楼     LV9
                2019年5月22日
            
            
         最近浏览更多
最近浏览更多
                
                Solowen    
                4月16日
            
            
                    暂无贡献等级
            
        
                fesfefe     LV13
                2023年11月1日
            
            
        
                qq1357574774     LV2
                2023年8月14日
            
            
        
                微信网友_5992582549164032     LV6
                2023年2月21日
            
            
        
                1358849392     LV21
                2022年11月23日
            
            
        
                ok1821     LV1
                2022年5月10日
            
            
        
                huzxiong    
                2022年2月24日
            
            
                    暂无贡献等级
            
        
                shmtuec9     LV1
                2021年12月28日
            
            
        
                James_hong     LV1
                2021年10月14日
            
            
        
                17558420274     LV16
                2021年7月27日
            
            
        
 
                 
                 
    