使用open flash chart制作報表
http://sarin.iteye.com/blog/685354這篇文章對open flash chart與struts2的整合使用做了詳細的介紹,但是按照文章里面的內容進行實際操作的時候,卻報了open flash char io error Loading test data Error #2032 的錯,經過分析,原因我忘記了引入jofc2其所依賴的xstream包 ......在這里,需要注意的是這兩個JAR包版本號的問題,我使用的是jofc2-1.0-0.jar和xstream-1.3.1.jar。
項目按照Struts2的開發標準搭建,然后把OFC開發所需的flash文件,頁面顯示Flash的支持文件swfobject.js放到發布目錄的相應位置,再將jofc2和其依賴的xstream的jar包放到WEB-INF/lib下并加入編譯路徑即可。

1---- 首先是Action的定義,代碼如下:
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import jofc2.model.Chart;
import jofc2.model.axis.Label;
import jofc2.model.axis.XAxis;
import jofc2.model.axis.YAxis;
import jofc2.model.elements.BarChart;
import jofc2.model.elements.LineChart;
/**
*
* @Project gun
* @Package action.chart
* @Class TestAction
*/
public class TestAction extends ActionSupport{
private Chart ofcChart;
public Chart getOfcChart() {
return ofcChart;
}
public String showChart() throws Exception{
//y軸數據集合-使用數量
List<Number> dataSet = new ArrayList<Number>();
for(int i=1; i<=12;i++) {
dataSet.add((Integer)i);
}
//x軸數據集合-瀏覽器類型
List<Label> xLabel = new ArrayList<Label>();
//獲取需要顯示的數據集
List browserList = new TestDAO().get();
for (int i = 0; i < browserList.size(); i++) {
Test map = (Test) browserList.get(i);
//填充x軸
dataSet.add((Integer) map.getStatCount());
//填充y軸
xLabel.add(new Label((String) map.getStatVar()));
}
//設置X軸內容
XAxis labels = new XAxis();
labels.addLabels(xLabel);
//設置Y軸顯示值域:Range的三個參數含義為:坐標最小值,最大值和步進值
YAxis range = new YAxis();
range.setRange(0, 200, 10);
//OFC折線圖設置
// LineChart lineChart = new LineChart(LineChart.Style.NORMAL);
//柱狀圖
BarChart lineChart=new BarChart(BarChart.Style.GLASS);
lineChart.addValues(dataSet);
lineChart.setColour("#6666FF");
lineChart.setText("使用者數量");
//圖表設置
ofcChart = new Chart("用戶瀏覽器使用量分布");
ofcChart.setXAxis(labels);
ofcChart.setYAxis(range);
ofcChart.addElements(lineChart);
return SUCCESS;
}
}
2 DAO類,從數據庫中獲取數據,另外還定義了一個Bean類Test
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.gun.base.db.DBConnection;
/**
* 從數據庫中取得所需的數據
*
* @Project gun
* @Package action.chart
* @Class TestDAO
*/
public class TestDAO {
public List get() throws Exception {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String querySQL = "select statCount, statVar from test where statType='broeser'";
List list = new ArrayList();
try {
conn = DBConnection.getConnection();
stmt = conn.prepareStatement(querySQL);
rs = stmt.executeQuery();
System.out.println("OK");
while (rs.next()) {
Test test = new Test();
test.setStatCount(rs.getInt("statCount"));
test.setStatVar(rs.getString("statVar"));
list.add(test);
}
return list;
} catch (SQLException e) {
throw new Exception("根據主鍵查詢信息失敗,錯誤信息:" + e.toString());
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
DBConnection.releaseConnection(conn);
} catch (SQLException e) {
throw new Exception("關閉數據庫失敗,錯誤信息:" + e.toString());
}
}
}
}
/**
* 請在此處填寫類的描述
*
* @Project gun
* @Package action.chart
* @Class Test
*/
public class Test {
private String statVar;
private int statCount;
public String getStatVar() {
return statVar;
}
public void setStatVar(String statVar) {
this.statVar = statVar;
}
public int getStatCount() {
return statCount;
}
public void setStatCount(int statCount) {
this.statCount = statCount;
}
}
3 最后定義了TestResult類,繼承于StrutsResultSupport,用于生成json格式的數據。
import jofc2.model.Chart;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionInvocation;
public class TestResult extends StrutsResultSupport {
private static final String ENCODING = "GBK";
private static final long serialVersionUID = 4702848904993212381L;
@Override
protected void doExecute(String arg0, ActionInvocation inv)
throws Exception {
Chart chart = (Chart) inv.getStack().findValue("ofcChart");
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json-rpc;charset=" + ENCODING);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "0");
response.setHeader("Pragma", "No-cache");
String json = chart.toString();
System.out.println(json.toString());
response.setContentLength(json.getBytes(ENCODING).length);
PrintWriter out = response.getWriter();
out.print(json);
out.flush();
out.close();
}
}
4 現在需要在struts.xml配置文件中配置Action,如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="gun" extends="json-default">
<result-types>
<result-type name="ofc" class="action.chart.TestResult" />
</result-types>
<action name="test" class="action.chart.TestAction" method="showChart">
<result type="ofc" name="success">
</result>
</action>
</package>
</struts>
5 JSP頁面很簡單,只需要寫一些簡單的JS代碼即可:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>My JSP 'cbar.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="openflashchart/js/swfobject.js"></script>
<script type="text/javascript">
var flashvars = {"data-file":"test.action"};
var params = {menu: "false",scale: "noScale",wmode:"opaque"};
swfobject.embedSWF("openflashchart/open-flash-chart.swf", "my_chart", "900", "500", "9.0.0",
"expressInstall.swf",flashvars,params);
</script>
</head>
<body>
<table align="center">
<tr>
<td><div id="my_chart"></div></td>
</tr>
</table>
</body>
</html>
最后運行程序,結果如下:

本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!