FusionCharts生成报表应用

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

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

  1. package com.golden.entity;
  2. public class Doughnut {
  3. public Doughnut() {
  4. }
  5. private String label;
  6. private int value;
  7. public String getLabel() {
  8. return label;
  9. }
  10. public void setLabel(String label) {
  11. this.label = label;
  12. }
  13. public int getValue() {
  14. return value;
  15. }
  16. public void setValue(int value) {
  17. this.value = value;
  18. }
  19. public Doughnut(String label, int value) {
  20. super();
  21. this.label = label;
  22. this.value = value;
  23. }
  24. }

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

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

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

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

  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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>FusionCharts报表生成页面</title>
  13. <script type="text/javascript" src="<%=request.getContextPath() %>/js/FusionCharts.js"></script>
  14. <script type="text/javascript">
  15. var majorXml;
  16. //var list;
  17. function init(){
  18. initXml();
  19. }
  20. function initXml(){
  21. majorXml="<chart palette='2' showBorder='1'>";
  22. majorXml += "<c:forEach var ='item' items='${list}'><set label='${item.label}' value='${item.value}'/></c:forEach>";
  23. majorXml+="</chart>";
  24. showDou3D();
  25. }
  26. function showDou3D(){
  27. var myChart=new FusionCharts("<%=request.getContextPath()%>/FusionCharts/Doughnut3D.swf", "ChartId", "600", "300", "0", "0");
  28. myChart.setDataXML(majorXml);
  29. myChart.render("majorbus");
  30. }
  31. </script>
  32. </head>
  33. <body onload="init()">
  34. <center>
  35. <div style="" id="majorbus">
  36. </div>
  37. </center>
  38. </body>
  39. </html>

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

  • js.rar (4.5 KB)
  • 下载次数: 485
  • lib.rar (390.2 KB)
  • 下载次数: 785
上一篇:实验吧简单的sql注入3


下一篇:WordPress一键部署网站