FusionCharts生成报表应用

1.需要组装要展示的数据,至于如何怎样去设计数据模型,看你要展示的图形和需要的数据就行了。来个简单的。

实体类,只有两个属性,也可以使用Bean里面的实体类,无所谓了。

Java代码  收藏代码
  1. package com.golden.entity;  
  2.   
  3. public class Doughnut {  
  4.   
  5.     public Doughnut() {  
  6.     }  
  7.   
  8.     private String label;  
  9.   
  10.     private int value;  
  11.   
  12.     public String getLabel() {  
  13.         return label;  
  14.     }  
  15.   
  16.     public void setLabel(String label) {  
  17.         this.label = label;  
  18.     }  
  19.   
  20.     public int getValue() {  
  21.         return value;  
  22.     }  
  23.   
  24.     public void setValue(int value) {  
  25.         this.value = value;  
  26.     }  
  27.   
  28.     public Doughnut(String label, int value) {  
  29.         super();  
  30.         this.label = label;  
  31.         this.value = value;  
  32.     }  
  33.   
  34. }  

2.做一个请求到Servlet,简单使用,也可以请求到Action,无所谓。该Servlet从后来得到数据,然后设置到该请求环境中。

Java代码  收藏代码
  1. package com.golden.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import com.golden.entity.Doughnut;  
  13.   
  14. @SuppressWarnings("serial")  
  15. public class FirstServlet extends HttpServlet {  
  16.       
  17.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  18.             throws ServletException, IOException {  
  19.         List<Doughnut> list = new ArrayList<Doughnut>();  
  20.         Doughnut d1 = new Doughnut("France", 17);  
  21.         Doughnut d2 = new Doughnut("India", 12);  
  22.         Doughnut d3 = new Doughnut("Brazil", 18);  
  23.         Doughnut d4 = new Doughnut("USA", 8);  
  24.         Doughnut d5 = new Doughnut("Australia", 10);  
  25.         Doughnut d6 = new Doughnut("Japan", 7);  
  26.         Doughnut d7 = new Doughnut("England", 5);  
  27.         Doughnut d8 = new Doughnut("Nigeria", 12);  
  28.         Doughnut d9 = new Doughnut("Italy", 8);  
  29.         Doughnut d10 = new Doughnut("China", 10);  
  30.         Doughnut d11 = new Doughnut("Canada", 19);  
  31.         Doughnut d12 = new Doughnut("Germany", 15);  
  32.         list.add(d1);  
  33.         list.add(d2);  
  34.         list.add(d3);  
  35.         list.add(d4);  
  36.         list.add(d5);  
  37.         list.add(d6);  
  38.         list.add(d7);  
  39.         list.add(d8);  
  40.         list.add(d9);  
  41.         list.add(d10);  
  42.         list.add(d11);  
  43.         list.add(d12);  
  44.         request.getSession().setAttribute("list", list);  
  45.         request.getRequestDispatcher("/show.jsp").forward(request, response);  
  46.     }  
  47.   
  48.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  49.             throws ServletException, IOException {  
  50.         doGet(request, response);  
  51.     }  
  52.   
  53. }  

3.配置,例如需要的Swf文件和JS文件,因为需要JSTL,所以要引入包,页面引进标签,一些低级的工作,赶紧搞定。

4.页面加载时初始化方法解析数据生成XML文件的字符串,然后设置给SWF,他就显示数据了,搞定。

Html代码  收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <%@page import="com.golden.entity.Doughnut"%>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.     <head>  
  12.         <base href="<%=basePath%>">  
  13.         <title>FusionCharts报表生成页面</title>  
  14.         <script type="text/javascript" src="<%=request.getContextPath() %>/js/FusionCharts.js"></script>  
  15.         <script type="text/javascript">  
  16.         var majorXml;  
  17.         //var list;  
  18.         function init(){  
  19.             initXml();  
  20.         }  
  21.         function initXml(){  
  22.             majorXml="<chart palette='2' showBorder='1'>";  
  23.             majorXml += "<c:forEach var ='item' items='${list}'><set label='${item.label}' value='${item.value}'/></c:forEach>";  
  24.             majorXml+="</chart>";  
  25.             showDou3D();  
  26.         }  
  27.         function showDou3D(){  
  28.            var myChart=new FusionCharts("<%=request.getContextPath()%>/FusionCharts/Doughnut3D.swf", "ChartId", "600", "300", "0", "0");  
  29.            myChart.setDataXML(majorXml);  
  30.            myChart.render("majorbus");  
  31.         }  
  32.         </script>  
  33.     </head>  
  34.   
  35.     <body onload="init()">  
  36.         <center>  
  37.             <div style="" id="majorbus">  
  38.             </div>  
  39.         </center>  
  40.     </body>  
  41. </html>  

5.不知道文件在哪里不要紧,在Webroot下建立js和FusionCharts文件夹,分别把附近弄进去,没有JSTL的LIB里有。

  • js.rar (4.5 KB)
  • 下载次数: 485
  • lib.rar (390.2 KB)
  • 下载次数: 785
原文地址:https://www.cnblogs.com/henuyuxiang/p/3948968.html