package shuju;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import database.DBConn;

public class Image extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ServletContext application = getServletContext();
		HttpSession session = request.getSession();
		request.setCharacterEncoding("gb2312");
		CategoryDataset dataset;
		String name[] = request.getParameterValues("time");
		//int time = Integer.parseInt(request.getParameter("time"));
		
			dataset = getDataSet(name);
		
		String title = "煤矿数据统计情况";
		JFreeChart chart = ChartFactory.createBarChart3D(title, "年份", "数值",
				dataset, PlotOrientation.VERTICAL, true, false, false);
		
		String filename = application.getRealPath("/") + File.separator + "1.jpg";
		session.setAttribute("filename", filename);
		ChartUtilities.saveChartAsJPEG(new File(filename), 1, chart, 500, 300);

		response.sendRedirect("tubiao.jsp");
	}

	private static CategoryDataset getDataSet(String time[]) {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		DBConn db = new DBConn();
		ArrayList a = new ArrayList();
		for(int j=0;j<time.length;j++)
		{
			ShuJu shuju1 = db.time(Integer.parseInt(time[j]));
			a.add(shuju1);
		}
		
		
		for(int i = 0;i<a.size();i++)
		{
			ShuJu shuju = (ShuJu)a.get(i);
			dataset.addValue(shuju.getWasi(), "瓦斯", shuju.getTime());
			dataset.addValue(shuju.getFangchen(), "防尘", shuju.getTime());
			dataset.addValue(shuju.getFengsu(), "风速", shuju.getTime());
			dataset.addValue(shuju.getShidu(), "湿度", shuju.getTime());
		}
		
	
		return dataset;
	}

	

}
