/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yuntong.action;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.yuntong.business.vo.J_FactoryInfoVO;
import com.yuntong.service.J_FactoryInfoViewService;

/**
 * MyEclipse Struts Creation date: 01-15-2006
 * 
 * XDoclet definition:
 * 
 * @struts.action validate="true"
 */
public class J_RigHotLineActionAction extends Action {
	private J_FactoryInfoViewService factoryInfoViewService;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		HttpSession session = request.getSession();
		/** ****************柱图******************** */
		List list = factoryInfoViewService.FactoryLine();
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();// 设置数据源
		Iterator iter = list.iterator();
		while (iter.hasNext()) {
			J_FactoryInfoVO info = (J_FactoryInfoVO) iter.next();
			dataset.setValue(info.getMoney(), "图解", info.getName());
		}

		JFreeChart chart1 = ChartFactory.createBarChart3D("厂商业务量Top10", "厂商名称",
				"消费量", dataset, PlotOrientation.VERTICAL, false, false, false);
		// double temp=0.5;
		chart1.setBackgroundImageAlpha(Float.parseFloat("0.2"));
		CategoryPlot plot = chart1.getCategoryPlot();

		// BarRenderer3D renderer1=new BarRenderer3D();

		final BarRenderer renderer1 = (BarRenderer) plot.getRenderer();

		renderer1
				.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());

		renderer1.setItemLabelsVisible(true);// 显示每个柱子上的数据

		renderer1.setItemMargin(2.0);// 设置每个柱子之间的距离
		plot.setRenderer(renderer1);// 让上面对柱子的设置生效
		String imageFileName = null;

		imageFileName = ServletUtilities.saveChartAsJPEG(chart1, 750, 400,
				session);

		// 生成图片，包括图片的大小

		session.setAttribute("image_name3", imageFileName);
		String URL = request.getContextPath() + "/display?filename="
				+ imageFileName;

		session.setAttribute("image_url3", URL);
		/** ****************3d饼（等级分布）******************** */
		List li = factoryInfoViewService.findConsumption();
		DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
		defaultpiedataset.setValue("初级会员", Double.parseDouble(li.get(0)
				.toString()));
		defaultpiedataset.setValue("高级会员", Double.parseDouble(li.get(1)
				.toString()));
		defaultpiedataset.setValue("银卡会员", Double.parseDouble(li.get(2)
				.toString()));
		defaultpiedataset.setValue("金卡会员", Double.parseDouble(li.get(3)
				.toString()));
		defaultpiedataset.setValue("钻石会员", Double.parseDouble(li.get(4)
				.toString()));

		JFreeChart jfreechart = ChartFactory.createPieChart3D("厂商等级分布",
				defaultpiedataset, true, false, false);
		PiePlot3D pieplot3d = (PiePlot3D) jfreechart.getPlot();

		String imageFileName2 = ServletUtilities.saveChartAsJPEG(jfreechart,
				750, 400, session);
		session.setAttribute("image_name2", imageFileName2);
		String URL2 = request.getContextPath() + "/display?filename="
				+ imageFileName2;

		session.setAttribute("image_url2", URL2);
		/** ****************3d主（运输线路）******************** */
		List list2 = factoryInfoViewService.findXianlu();
		DefaultCategoryDataset d2 = new DefaultCategoryDataset();// 设置数据源

		for (int k = 0; k < 10; k++) {
			Object[] results = (Object[]) list2.get(k);
			if (results[0].toString().equals("")
					|| results[0].toString() == null) {
				results[0] = "0";
			}
			d2.setValue(Double.parseDouble(results[0].toString()), "图解",
					results[1].toString());
		}

		JFreeChart jfc = ChartFactory.createBarChart3D("热点线路Top10", "线路名称",
				"货票数量", d2, PlotOrientation.VERTICAL, false, false, false);
		// double temp=0.5;
		jfc.setBackgroundImageAlpha(Float.parseFloat("0.2"));
		CategoryPlot plot3 = jfc.getCategoryPlot();

		// BarRenderer3D renderer1=new BarRenderer3D();

		final BarRenderer renderer3 = (BarRenderer) plot3.getRenderer();

		renderer3
				.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());

		renderer3.setItemLabelsVisible(true);// 显示每个柱子上的数据

		renderer3.setItemMargin(1.1);// 设置每个柱子之间的距离

		plot.setRenderer(renderer3);// 让上面对柱子的设置生效
		String imageFileName3 = null;

		imageFileName3 = ServletUtilities.saveChartAsJPEG(jfc, 800, 400,
				session);

		// 生成图片，包括图片的大小

		// session.setAttribute("image_name4", imageFileName3);
		String URL4 = request.getContextPath() + "/display?filename="
				+ imageFileName3;

		session.setAttribute("image_url4", URL4);
		/** ****************3d饼（结算方式分布）******************** */
		List bl = factoryInfoViewService.findBili();
		DefaultPieDataset blDataset = new DefaultPieDataset();
		blDataset.setValue("收讫", Double.parseDouble(bl.get(0).toString()));
		blDataset.setValue("提付", Double.parseDouble(bl.get(1).toString()));		
		JFreeChart bljf = ChartFactory.createPieChart3D("结算方式分布图",
				blDataset, true, false, false);
		PiePlot3D bl3d = (PiePlot3D) bljf.getPlot();

		String ifn = ServletUtilities.saveChartAsJPEG(bljf,
				750, 400, session);
		
		String URL5 = request.getContextPath() + "/display?filename="
				+ ifn;

		session.setAttribute("image_url5", URL5);
		return mapping.findForward("view");
	}

	public J_FactoryInfoViewService getFactoryInfoViewService() {
		return factoryInfoViewService;
	}

	public void setFactoryInfoViewService(
			J_FactoryInfoViewService factoryInfoViewService) {
		this.factoryInfoViewService = factoryInfoViewService;
	}
}