fusioncharts报表

转帖地址:http://fengfeng925.javaeye.com/blog/581755

 

线简单介绍一下FusionCharts,这是一款动态的制作报表的工具,主要通过XML数据和SWF文件进行交互,实现类似于flash的非常漂亮的报表。下面就做一个Demo,简要介绍一下啊fusioncharts的功能,以及如何实现一个完整动态数据的交互。
   首先下载FusionCharts的官方API发布包,我做这个例子的时候是V3.1下载后,在MyEclipse下新建一个Web工程。然后将发布包中的Charts文件夹拷贝到WebRoot下,这里面都是我们接下来要做图的时候用到的一些swf文件。接着还需要一个JS文件,是JSClass文件夹下的FusionCharts.js文件,将它拷贝到WebRoot下的ChartsJs文件夹下。由于我这个项目是结合struts1还有spring加上JPA做的。所以大家先有个基本的认识。然后在WEB-INF下建立两个目录,一个叫fusion、一个叫common,等会我们会用到。找到发布包的/Code/JSP/Includes目录下,将FusionCharts.jsp文件拷贝到common文件夹下。
   准备环境完成了,那么我们下面就开始做了。
   首先说我想做什么内容。主要是展示2004、2005、2006、2007四年的一个油品的销售统计,用柱状图来表示,然后点击每一个柱子,会在右边显示出饼状的上半年的各个月份的销售比例。
   首先建立实体Bean,代码如下

Java代码 复制代码
  1. import java.util.HashSet;   
  2. import java.util.Set;   
  3.   
  4. import javax.persistence.CascadeType;   
  5. import javax.persistence.Column;   
  6. import javax.persistence.Entity;   
  7. import javax.persistence.GeneratedValue;   
  8. import javax.persistence.Id;   
  9. import javax.persistence.OneToMany;   
  10. import javax.persistence.Table;   
  11.   
  12. import com.zxyg.bean.orgnization.Employee;   
  13.   
  14. /**  
  15.  * 年销售统计  
  16.  * @author Lan  
  17.  *   
  18.  */  
  19. @Entity  
  20. @Table(name = "t_yearunits")   
  21. public class YearUnits {   
  22.     private Integer id;   
  23.     private String year;   
  24.     private Float units;   
  25.        
  26.     private Set<MonthUnits> months = new HashSet<MonthUnits>();   
  27.   
  28.     @OneToMany(mappedBy="year", cascade=CascadeType.REFRESH)   
  29.     public Set<MonthUnits> getMonths() {   
  30.         return months;   
  31.     }   
  32.   
  33.     public void setMonths(Set<MonthUnits> months) {   
  34.         this.months = months;   
  35.     }   
  36.   
  37.     @Id @GeneratedValue  
  38.     public Integer getId() {   
  39.         return id;   
  40.     }   
  41.   
  42.     public void setId(Integer id) {   
  43.         this.id = id;   
  44.     }   
  45.   
  46.     @Column(length=4, nullable=false)   
  47.     public String getYear() {   
  48.         return year;   
  49.     }   
  50.   
  51.     public void setYear(String year) {   
  52.         this.year = year;   
  53.     }   
  54.   
  55.     @Column(length=5, nullable=false)   
  56.     public Float getUnits() {   
  57.         return units;   
  58.     }   
  59.   
  60.     public void setUnits(Float units) {   
  61.         this.units = units;   
  62.     }   
  63.   
  64.        
  65. }  
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.zxyg.bean.orgnization.Employee;

/**
 * 年销售统计
 * @author Lan
 * 
 */
@Entity
@Table(name = "t_yearunits")
public class YearUnits {
	private Integer id;
	private String year;
	private Float units;
	
	private Set<MonthUnits> months = new HashSet<MonthUnits>();

	@OneToMany(mappedBy="year", cascade=CascadeType.REFRESH)
	public Set<MonthUnits> getMonths() {
		return months;
	}

	public void setMonths(Set<MonthUnits> months) {
		this.months = months;
	}

	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(length=4, nullable=false)
	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	@Column(length=5, nullable=false)
	public Float getUnits() {
		return units;
	}

	public void setUnits(Float units) {
		this.units = units;
	}

	
}

 

Java代码 复制代码
  1. import javax.persistence.CascadeType;   
  2. import javax.persistence.Column;   
  3. import javax.persistence.Entity;   
  4. import javax.persistence.GeneratedValue;   
  5. import javax.persistence.Id;   
  6. import javax.persistence.JoinColumn;   
  7. import javax.persistence.ManyToOne;   
  8. import javax.persistence.Table;   
  9.   
  10. /**  
  11.  * 月销售统计  
  12.  * @author Lan  
  13.  *   
  14.  */  
  15. @Entity  
  16. @Table(name = "t_monthunits")   
  17. public class MonthUnits {   
  18.     private Integer id;   
  19.     private String month;   
  20.     private Float units;   
  21.        
  22.     private YearUnits year;   
  23.   
  24.     @ManyToOne(cascade=CascadeType.REFRESH)   
  25.     @JoinColumn(name="year_id")   
  26.     public YearUnits getYear() {   
  27.         return year;   
  28.     }   
  29.   
  30.     public void setYear(YearUnits year) {   
  31.         this.year = year;   
  32.     }   
  33.   
  34.     @Id @GeneratedValue  
  35.     public Integer getId() {   
  36.         return id;   
  37.     }   
  38.   
  39.     public void setId(Integer id) {   
  40.         this.id = id;   
  41.     }   
  42.   
  43.     @Column(length=4, nullable=false)   
  44.     public String getMonth() {   
  45.         return month;   
  46.     }   
  47.   
  48.     public void setMonth(String month) {   
  49.         this.month = month;   
  50.     }   
  51.   
  52.     @Column(length=5, nullable=false)   
  53.     public Float getUnits() {   
  54.         return units;   
  55.     }   
  56.   
  57.     public void setUnits(Float units) {   
  58.         this.units = units;   
  59.     }   
  60. }  
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * 月销售统计
 * @author Lan
 * 
 */
@Entity
@Table(name = "t_monthunits")
public class MonthUnits {
	private Integer id;
	private String month;
	private Float units;
	
	private YearUnits year;

	@ManyToOne(cascade=CascadeType.REFRESH)
	@JoinColumn(name="year_id")
	public YearUnits getYear() {
		return year;
	}

	public void setYear(YearUnits year) {
		this.year = year;
	}

	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(length=4, nullable=false)
	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	@Column(length=5, nullable=false)
	public Float getUnits() {
		return units;
	}

	public void setUnits(Float units) {
		this.units = units;
	}
}


这分别是年销售统计和月销售统计的实体Bean
由于Service层的东西都在底层封装好了,所以建立好表后,手动添加一些数据就好了。
接下来是Action层的代码,首先是针对年销售突击的Action的代码

Java代码 复制代码
  1. import java.io.PrintWriter;   
  2. import java.util.List;   
  3.   
  4. import javax.annotation.Resource;   
  5. import javax.servlet.http.HttpServletRequest;   
  6. import javax.servlet.http.HttpServletResponse;   
  7.   
  8. import org.apache.struts.action.Action;   
  9. import org.apache.struts.action.ActionForm;   
  10. import org.apache.struts.action.ActionForward;   
  11. import org.apache.struts.action.ActionMapping;   
  12. import org.apache.struts.actions.DispatchAction;   
  13. import org.springframework.stereotype.Controller;   
  14.   
  15. import com.zxyg.bean.report.MonthUnits;   
  16. import com.zxyg.bean.report.YearUnits;   
  17. import com.zxyg.service.report.MonthUnitsService;   
  18. import com.zxyg.service.report.YearUnitsService;   
  19.   
  20. /**  
  21.  * 年销售统计图表  
  22.  * @author Lan  
  23.  *  
  24.  */  
  25. @Controller("/year/units/report")   
  26. public class YearUnitsAction extends Action {   
  27.     @Resource private YearUnitsService yearUnitsService;   
  28.        
  29.        
  30.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  31.             HttpServletRequest request, HttpServletResponse response)   
  32.             throws Exception {   
  33.         //年销售统计   
  34.         StringBuilder strXML1 = new StringBuilder("");   
  35.         strXML1.append("<?xml version='1.0' encoding='GBK'?><chart palette='2' maxColWidth='70' rotateYAxisName='0' caption='油品年销售统计' "  
  36.                  + "startingAngle='125' shownames='1' showvalues='0' numberPrefix='$'>");   
  37.            
  38.         List<YearUnits> yearunits = yearUnitsService.getScrollData().getResultlist();   
  39.         for(YearUnits units : yearunits) {   
  40.             strXML1.append("<set label='" + units.getYear() + "' value='" + units.getUnits()+ "'link='JavaScript:myJS(" + units.getYear().substring(04) + ")'/>");   
  41.         }   
  42.         strXML1.append("</chart>");   
  43.         request.setAttribute("strXML1", strXML1.toString());   
  44.         return mapping.findForward("report");   
  45.     }   
  46.        
  47. }  
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.apache.struts.actions.DispatchAction;
import org.springframework.stereotype.Controller;

import com.zxyg.bean.report.MonthUnits;
import com.zxyg.bean.report.YearUnits;
import com.zxyg.service.report.MonthUnitsService;
import com.zxyg.service.report.YearUnitsService;

/**
 * 年销售统计图表
 * @author Lan
 *
 */
@Controller("/year/units/report")
public class YearUnitsAction extends Action {
	@Resource private YearUnitsService yearUnitsService;
	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//年销售统计
		StringBuilder strXML1 = new StringBuilder("");
		strXML1.append("<?xml version='1.0' encoding='GBK'?><chart palette='2' maxColWidth='70' rotateYAxisName='0' caption='油品年销售统计' "
				 + "startingAngle='125' shownames='1' showvalues='0' numberPrefix='$'>");
		
		List<YearUnits> yearunits = yearUnitsService.getScrollData().getResultlist();
		for(YearUnits units : yearunits) {
			strXML1.append("<set label='" + units.getYear() + "' value='" + units.getUnits()+ "'link='JavaScript:myJS(" + units.getYear().substring(0, 4) + ")'/>");
		}
		strXML1.append("</chart>");
		request.setAttribute("strXML1", strXML1.toString());
		return mapping.findForward("report");
	}
	
}


这里的钻取要用到ajax技术,所以接下来是月销售统计的数据钻取

Java代码 复制代码
  1. import java.io.PrintWriter;   
  2. import java.util.List;   
  3.   
  4. import javax.annotation.Resource;   
  5. import javax.servlet.http.HttpServletRequest;   
  6. import javax.servlet.http.HttpServletResponse;   
  7.   
  8. import org.apache.struts.action.Action;   
  9. import org.apache.struts.action.ActionForm;   
  10. import org.apache.struts.action.ActionForward;   
  11. import org.apache.struts.action.ActionMapping;   
  12. import org.springframework.stereotype.Controller;   
  13.   
  14. import com.zxyg.bean.report.MonthUnits;   
  15. import com.zxyg.service.report.MonthUnitsService;   
  16.   
  17. /**  
  18.  * 月销售统计图表  
  19.  * @author Lan  
  20.  *  
  21.  */  
  22. @Controller("/month/units/report")   
  23. public class MonthUnitsAction extends Action {   
  24.     @Resource private MonthUnitsService monthUnitsService;   
  25.   
  26.     @Override  
  27.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  28.             HttpServletRequest request, HttpServletResponse response)   
  29.             throws Exception {   
  30.         response.setContentType("text/html;charset=utf-8");   
  31.         response.setHeader("Cache-Control""no-cache");   
  32.         PrintWriter out = response.getWriter();   
  33.         String year = request.getParameter("year");   
  34.         if(year != null && !"".equals(year)) {   
  35.             out.print("<chart palette='2' rotateYAxisName='0' caption='上半年销售历史' xAxisName='月份' refreshInterval='60'"    
  36.                           +" yAxisName='Units' showValues='0'>");   
  37.             List<MonthUnits> monthunits = monthUnitsService.getMonthUnitsByYear(year.trim());   
  38.             for(int i=0; i<monthunits.size(); i++) {   
  39.                 MonthUnits monthUnits = monthunits.get(i);   
  40.                 out.print("<set label='" + monthUnits.getMonth()+ "' value='" + monthUnits.getUnits()+ "' />");   
  41.             }   
  42.             out.print("</chart>");   
  43.         }   
  44.         return null;   
  45.     }   
  46. }  
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.springframework.stereotype.Controller;

import com.zxyg.bean.report.MonthUnits;
import com.zxyg.service.report.MonthUnitsService;

/**
 * 月销售统计图表
 * @author Lan
 *
 */
@Controller("/month/units/report")
public class MonthUnitsAction extends Action {
	@Resource private MonthUnitsService monthUnitsService;

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		String year = request.getParameter("year");
		if(year != null && !"".equals(year)) {
			out.print("<chart palette='2' rotateYAxisName='0' caption='上半年销售历史' xAxisName='月份' refreshInterval='60'" 
				          +" yAxisName='Units' showValues='0'>");
			List<MonthUnits> monthunits = monthUnitsService.getMonthUnitsByYear(year.trim());
			for(int i=0; i<monthunits.size(); i++) {
				MonthUnits monthUnits = monthunits.get(i);
				out.print("<set label='" + monthUnits.getMonth()+ "' value='" + monthUnits.getUnits()+ "' />");
			}
			out.print("</chart>");
		}
		return null;
	}
}


最后是页面的代码,struts1的配置我就不写了,反正是定向到WEB-INF下的fusion下的
year_units_report.jsp下
页面代码如下

Java代码 复制代码
  1. <%@ page contentType="text/html;charset=UTF-8" %>   
  2. <%@ include file="/WEB-INF/page/share/taglib.jsp" %>   
  3. <%@ include file="/WEB-INF/page/share/FusionCharts.jsp" %>   
  4. <html>   
  5. <head>   
  6. <title>报表显示</title>   
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  8. <link rel="stylesheet" href="/css/vip.css" type="text/css">   
  9. <script language="JavaScript" src="/ChartsJs/FusionCharts.js"></script>   
  10. <script LANGUAGE="JavaScript">   
  11.     var xmlHttpRequest;   
  12.     function myJS(myVar) {   
  13.         initRequest(myVar);   
  14.     }   
  15.        
  16.     //初始化xmlHttpRequest对象   
  17.     function initRequest(myVar) {   
  18.         if(window.XMLHttpRequest) {   
  19.             try {   
  20.                 xmlHttpRequest = new XMLHttpRequest();   
  21.                 if(xmlHttpRequest.overrideMimeType) {   
  22.                     xmlHttpRequest.overrideMimeType("text/html;charset=UTF-8");   
  23.                 }   
  24.             }catch (e) {}   
  25.         }else if(window.ActiveXObject) {   
  26.             try {   
  27.                 xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");   
  28.             } catch (e) {   
  29.                 try {   
  30.                     xmlHttpRequest = new ActiveXObject("Msxml2.XMLHttp");   
  31.                 } catch (e) {   
  32.                     try {   
  33.                         xmlHttpRequest = new ActiveXObject("Msxml3.XMLHttp");   
  34.                     } catch (e) {}   
  35.                 }   
  36.             }   
  37.         }   
  38.         verify(myVar);   
  39.     }   
  40.        
  41.     //准备传输数据   
  42.     function verify(myVar) {   
  43.         var url = "/month/units/report.do";   
  44.         xmlHttpRequest.onreadystatechange = callback;   
  45.         xmlHttpRequest.open("POST", url, true);   
  46.         xmlHttpRequest.setRequestHeader("Content-Type""application/x-www-form-urlencoded");   
  47.         xmlHttpRequest.send("year=" + myVar);   
  48.         //xmlHttpRequest.send(null);   
  49.     }   
  50.        
  51.     //定义回调函数   
  52.     function callback() {   
  53.         if(xmlHttpRequest.readyState == 4) {   
  54.             if(xmlHttpRequest.status == 200) {   
  55.                 var responseText = xmlHttpRequest.responseText;   
  56.                 createHTML3D(responseText);   
  57.             }   
  58.         }   
  59.     }   
  60.        
  61.     //创建子数据图   
  62.     function createHTML3D(data) {   
  63.         var myChart = new FusionCharts("/Charts/Pie3D.swf""myChartId""500""300""0""0");   
  64.         myChart.setDataXML(data);   
  65.         myChart.render("chartdiv");   
  66.     }   
  67. </script>   
  68. </head>   
  69.   
  70.   
  71.   
  72. <body bgcolor="#FFFFFF" text="#000000" marginwidth="0" marginheight="0">   
  73.         <%                     
  74.             String strXML1 = (String)request.getAttribute("strXML1");   
  75.             if(strXML1 != null && !"".equals(strXML1)) {   
  76.                 String chartHTML1 = createChartHTML("/Charts/Column3D.swf",    
  77.                     "", strXML1, "YearUnits"500300false);   
  78.         %>   
  79.             <span>   
  80.                 <%=chartHTML1%>   
  81.             </span>   
  82.         <%    
  83.             }   
  84.         %>   
  85.            
  86.         <span id="chartdiv">   
  87.         </span>   
  88. </body>   
  89. </html>  
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/page/share/taglib.jsp" %>
<%@ include file="/WEB-INF/page/share/FusionCharts.jsp" %>
<html>
<head>
<title>报表显示</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/css/vip.css" type="text/css">
<script language="JavaScript" src="/ChartsJs/FusionCharts.js"></script>
<script LANGUAGE="JavaScript">
	var xmlHttpRequest;
    function myJS(myVar) {
    	initRequest(myVar);
    }
    
    //初始化xmlHttpRequest对象
    function initRequest(myVar) {
    	if(window.XMLHttpRequest) {
			try {
				xmlHttpRequest = new XMLHttpRequest();
				if(xmlHttpRequest.overrideMimeType) {
					xmlHttpRequest.overrideMimeType("text/html;charset=UTF-8");
				}
			}catch (e) {}
		}else if(window.ActiveXObject) {
			try {
				xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				try {
					xmlHttpRequest = new ActiveXObject("Msxml2.XMLHttp");
				} catch (e) {
					try {
						xmlHttpRequest = new ActiveXObject("Msxml3.XMLHttp");
					} catch (e) {}
				}
			}
		}
    	verify(myVar);
    }
    
    //准备传输数据
    function verify(myVar) {
    	var url = "/month/units/report.do";
    	xmlHttpRequest.onreadystatechange = callback;
    	xmlHttpRequest.open("POST", url, true);
    	xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	xmlHttpRequest.send("year=" + myVar);
    	//xmlHttpRequest.send(null);
    }
    
    //定义回调函数
    function callback() {
    	if(xmlHttpRequest.readyState == 4) {
    		if(xmlHttpRequest.status == 200) {
    			var responseText = xmlHttpRequest.responseText;
    			createHTML3D(responseText);
    		}
    	}
    }
    
    //创建子数据图
    function createHTML3D(data) {
    	var myChart = new FusionCharts("/Charts/Pie3D.swf", "myChartId", "500", "300", "0", "0");
    	myChart.setDataXML(data);
    	myChart.render("chartdiv");
    }
</script>
</head>



<body bgcolor="#FFFFFF" text="#000000" marginwidth="0" marginheight="0">
		<%	               
	    	String strXML1 = (String)request.getAttribute("strXML1");
	    	if(strXML1 != null && !"".equals(strXML1)) {
	    		String chartHTML1 = createChartHTML("/Charts/Column3D.swf", 
	 				"", strXML1, "YearUnits", 500, 300, false);
	 	%>
	 		<span>
	   			<%=chartHTML1%>
	   		</span>
	 	<% 
	    	}
	 	%>
	 	
	 	<span id="chartdiv">
	 	</span>
</body>
</html>


最后图的效果如下


这样就做到了动态数据交互钻取,页面不刷新。达到了预期的效果。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值