struts整合jfreechart

阅读更多

Struts2与JFreeChart的整合

1、添加相应的jar包(除struts2所必需的包以外的包):

  1. struts2-jfreechart-plugin-2.3.12.jar
  2. jcommon-1.0.17.jar
  3. jfreechart-1.0.14.jar

2、添加相应的Action

 

package cn.hpu.action;

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

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import cn.hpu.po.Complain;
import cn.hpu.po.DDInfo;
import cn.hpu.service.ComplainService;
import cn.hpu.service.DDInfoService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ComplainAction extends ActionSupport {

	private ComplainService complainService;
	private DDInfoService ddInfoService;
	
	private List complains;
	
	private List satisfications;
	
	// 生成图形
	private JFreeChart chart;

	/**获取用户满意度饼状报表
	 * @author qinrui
	 * @see cn.hpu.service.ComplainService complainService
	 * @see cn.hpu.service.DDInfoService ddInfoService
	 * @return java.lang.String
	 */
	public String pieChart() {
		
		DefaultPieDataset dpd = new DefaultPieDataset();
	
		complains = complainService.loadComplains();
		satisfications = ddInfoService.loadCustomerSatisfications();
		
		for(DDInfo satisfication : satisfications) {
			
			if(satisfication.getName() != null && !"".equals(satisfication.getName().trim())) {
				
				int count = 0;
				
				for(Complain complain : complains) {
					
					if(complain.getSatisfication().getId().equals(satisfication.getId())) {
						count ++;
					}
				}
				dpd.setValue(satisfication.getName(), count);
			}
		}
		
		StandardChartTheme standardChartTheme=new StandardChartTheme("CN");    
        //设置标题字体    
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));    
        //设置图例的字体    
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));    
        //设置轴向的字体    
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));    
        //应用主题样式    
        ChartFactory.setChartTheme(standardChartTheme);
		
		/**生成图形
		 * 1.title java.lang.String 标题
		 * 2.dataset org.jfree.data.general.DefaultPieDataset 数据源
		 * 3.legend java.lang.Boolean 图例
		 * 4.tooltips java.lang.Boolean 工具提示信息
		 * 5.locale java.lang.Boolean 本地化
		 */
		chart = ChartFactory.createPieChart3D("客户满意度", dpd, true, true, false);
		
		return SUCCESS;
	}
	
	/**生成柱状报表
	 * @author qinrui
	 * @see cn.hpu.service.ComplainService complainService
	 * @see cn.hpu.service.DDInfoService ddInfoService
	 * @return java.lang.String
	 */
	public String barChart() {

		DefaultCategoryDataset dcd = new DefaultCategoryDataset();
	
		complains = complainService.loadComplains();
		satisfications = ddInfoService.loadCustomerSatisfications();
		
		for(DDInfo satisfication : satisfications) {
			
			if(satisfication.getName() != null && !"".equals(satisfication.getName().trim())) {
				
				int count = 0;
				
				for(Complain complain : complains) {
					
					if(complain.getSatisfication().getId().equals(satisfication.getId())) {
						count ++;
					}
				}
				dcd.setValue(count , "", satisfication.getName());
			}
		}
		
		StandardChartTheme standardChartTheme=new StandardChartTheme("CN");    
        //设置标题字体    
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));    
        //设置图例的字体    
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));    
        //设置轴向的字体    
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));    
        //应用主题样式    
        ChartFactory.setChartTheme(standardChartTheme);
		
		/**生成图形
		 * 1.title java.lang.String 标题
		 * 2.dataset org.jfree.data.general.DefaultPieDataset 数据源
		 * 3.legend java.lang.Boolean 图例
		 * 4.tooltips java.lang.Boolean 工具提示信息
		 * 5.locale java.lang.Boolean 本地化
		 */
		chart = ChartFactory.createBarChart("客户满意度", "满意程度", "投诉数量", dcd, PlotOrientation.VERTICAL, true, true, false);
		
		return SUCCESS;
	}
	
	public ComplainService getComplainService() {
		return complainService;
	}

	public void setComplainService(ComplainService complainService) {
		this.complainService = complainService;
	}

	public List getComplains() {
		return complains;
	}

	public List getSatisfications() {
		return satisfications;
	}

	public void setSatisfications(List satisfications) {
		this.satisfications = satisfications;
	}

	public DDInfoService getDdInfoService() {
		return ddInfoService;
	}

	public void setDdInfoService(DDInfoService ddInfoService) {
		this.ddInfoService = ddInfoService;
	}

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}
}

 3、配置struts.xml文件(注意要继承jfreechart-default,返回值type为chart)

 

 





	
	
		
		
		
			
				300
				400
			
		
		
		
		
			
				300
				400
			
		
	

 

 

你可能感兴趣的:(struts整合jfreechart)