1.需要组装要展示的数据,至于如何怎样去设计数据模型,看你要展示的图形和需要的数据就行了。来个简单的。
实体类,只有两个属性,也可以使用Bean里面的实体类,无所谓了。
- package com.golden.entity;
- public class Doughnut {
- public Doughnut() {
- }
- private String label;
- private int value;
- public String getLabel() {
- return label;
- }
- public void setLabel(String label) {
- this.label = label;
- }
- public int getValue() {
- return value;
- }
- public void setValue(int value) {
- this.value = value;
- }
- public Doughnut(String label, int value) {
- super();
- this.label = label;
- this.value = value;
- }
- }
2.做一个请求到Servlet,简单使用,也可以请求到Action,无所谓。该Servlet从后来得到数据,然后设置到该请求环境中。
- package com.golden.servlet;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.golden.entity.Doughnut;
- @SuppressWarnings("serial")
- public class FirstServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- List<Doughnut> list = new ArrayList<Doughnut>();
- Doughnut d1 = new Doughnut("France", 17);
- Doughnut d2 = new Doughnut("India", 12);
- Doughnut d3 = new Doughnut("Brazil", 18);
- Doughnut d4 = new Doughnut("USA", 8);
- Doughnut d5 = new Doughnut("Australia", 10);
- Doughnut d6 = new Doughnut("Japan", 7);
- Doughnut d7 = new Doughnut("England", 5);
- Doughnut d8 = new Doughnut("Nigeria", 12);
- Doughnut d9 = new Doughnut("Italy", 8);
- Doughnut d10 = new Doughnut("China", 10);
- Doughnut d11 = new Doughnut("Canada", 19);
- Doughnut d12 = new Doughnut("Germany", 15);
- list.add(d1);
- list.add(d2);
- list.add(d3);
- list.add(d4);
- list.add(d5);
- list.add(d6);
- list.add(d7);
- list.add(d8);
- list.add(d9);
- list.add(d10);
- list.add(d11);
- list.add(d12);
- request.getSession().setAttribute("list", list);
- request.getRequestDispatcher("/show.jsp").forward(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
3.配置,例如需要的Swf文件和JS文件,因为需要JSTL,所以要引入包,页面引进标签,一些低级的工作,赶紧搞定。
4.页面加载时初始化方法解析数据生成XML文件的字符串,然后设置给SWF,他就显示数据了,搞定。
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@page import="com.golden.entity.Doughnut"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>FusionCharts报表生成页面</title>
- <script type="text/javascript" src="<%=request.getContextPath() %>/js/FusionCharts.js"></script>
- <script type="text/javascript">
- var majorXml;
- //var list;
- function init(){
- initXml();
- }
- function initXml(){
- majorXml="<chart palette=‘2‘ showBorder=‘1‘>";
- majorXml += "<c:forEach var =‘item‘ items=‘${list}‘><set label=‘${item.label}‘ value=‘${item.value}‘/></c:forEach>";
- majorXml+="</chart>";
- showDou3D();
- }
- function showDou3D(){
- var myChart=new FusionCharts("<%=request.getContextPath()%>/FusionCharts/Doughnut3D.swf", "ChartId", "600", "300", "0", "0");
- myChart.setDataXML(majorXml);
- myChart.render("majorbus");
- }
- </script>
- </head>
- <body onload="init()">
- <center>
- <div style="" id="majorbus">
- </div>
- </center>
- </body>
- </html>
5.不知道文件在哪里不要紧,在Webroot下建立js和FusionCharts文件夹,分别把附近弄进去,没有JSTL的LIB里有。