⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jfreechartaction.java

📁 人力资源管理系统
💻 JAVA
字号:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.accphr.web.action;

import java.awt.Font;
import java.io.IOException;
import java.util.List;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.accphr.biz.IStandardsBiz;
import com.accphr.entity.Standards;


public class JFreeChartAction extends DispatchAction {

	private IStandardsBiz standardsBiz;
	public void setStandardsBiz(IStandardsBiz standardsBiz) {
		this.standardsBiz = standardsBiz;
	}
	
	//到显示页面
	public ActionForward toShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List list=this.standardsBiz.findAll();
		request.setAttribute("standlist", list);
		return mapping.findForward("toshow");
	}
	
	//到显示薪酬页面
	public ActionForward toShowStandard(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List list=this.standardsBiz.findAll();
		request.setAttribute("standlist", list);
		HttpSession session=request.getSession();
		//创建柱状图形报表的数据集对象
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		//添加报表统计数据
		//参数1:统计数值(此处代表就业人数)
		//参数2:数据分组信息,代表数据都为就业的相关信息
		//参数3:数据分类信息,代表数据是哪一类的数据信息,比如南京的就业人数为110人
		for (int i = 0; i < list.size(); i++) {
			Standards standards=(Standards)list.get(i);
			dataset.addValue(standards.getSalarySum(), "工资",standards.getStandardName());
		}

		//在内存中创建报表对象(此处使用的是工厂方法)
		//工厂方法参数介绍
		//参数1:报表的标题信息
		//参数2:报表X轴的汇总标题
		//参数3:报表Y轴的汇总标题
		//参数4:数据集对象
		//参数5:设置柱状报表的显示形式(VERTICAL纵向柱状图 | HORIZONTAL横向柱状图)
		//参数6:是否生成图例
		//参数7:是否显示工具提示
		//参数8:是否生成URL
		JFreeChart chart = ChartFactory.createBarChart3D("薪酬标准查看图",
				"薪酬标准", "金额", dataset, PlotOrientation.VERTICAL, false,false, false);
		//使用ServletUtilities将报表转换为图片
		//注意:保存的图片如果希望在浏览器中直接显示,必须使用saveChartAsPNG方法,而不能用saveChartAsJPEG
		String fileName = "";
		try {
			fileName=ServletUtilities.saveChartAsPNG(chart, 500, 300,null, session);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//通过JFreeChart提供的控制器,生成图片的绝对路径
		String graphURL = request.getContextPath() + "/DisplayChart?filename=" + fileName;
		System.out.println(fileName);
		System.out.println(graphURL);
		request.setAttribute("graphURL", graphURL);
		request.setAttribute("filename", fileName);
		return mapping.findForward("toshowstandard");
	}
	
	//到显示页面
	public ActionForward toShowStandard2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		List list=this.standardsBiz.findAll();
		request.setAttribute("standlist", list);
		HttpSession session=request.getSession();
		//创建饼状图形报表的数据集对象
		DefaultPieDataset dataset = new DefaultPieDataset();
		//添加报表统计数据
		//参数1:数据分类信息,代表数据是哪一类的数据信息,比如初级程序员人数为10000人
		//参数2:该类的数值
		for (int i = 0; i < list.size(); i++) {
			Standards standards=(Standards)list.get(i);
			dataset.setValue(standards.getStandardName(),standards.getSalarySum());
		}
//		dataset.setValue("初级程序员", 10000);
//		dataset.setValue("中级程序员", 23000);
//		dataset.setValue("高级程序员", 10000);
//		dataset.setValue("项目经理", 6000);
//		dataset.setValue("系统分析师", 6000);
//		dataset.setValue("软件架构师", 3000);
//		dataset.setValue("其他", 10000);

		//在内存中创建报表对象(此处使用的是工厂方法)
		//工厂方法参数介绍
		//参数1:报表的标题信息
		//参数2:数据集对象
		//参数6:是否生成图例
		//参数7:是否显示工具提示
		//参数8:是否生成URL
		JFreeChart chart = ChartFactory.createPieChart3D("薪酬标准对比图",dataset, true, false, false);

		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setLabelFont(new Font("隶书", 0, 10));

		//没有数据的时候显示的内容
		plot.setNoDataMessage("无数据显示");

		//在饼图的各块中显示百分比
		plot.setCircular(true);
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}={2}"));

		//使用ServletUtilities将报表转换为图片
		//注意:保存的图片如果希望在浏览器中直接显示,必须使用saveChartAsPNG方法,而不能用saveChartAsJPEG
		String fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300,
				null, session);
		//通过JFreeChart提供的控制器,生成图片的绝对路径
		String graphURL = request.getContextPath() + "/DisplayChart?filename=" + fileName;
		
		request.setAttribute("graphurl", graphURL);
		request.setAttribute("filenames", fileName);
		
		return mapping.findForward("toshowstandard2");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -