FusionChart动态报表的绘制

来源:互联网 发布:mcs51单片机引脚 编辑:程序博客网 时间:2024/04/29 10:22

此文主要记下如何根据数据库中存储的数据来绘制flash报表。

环境:eclipse+tomcat6.0+MySQL5.5.13

从官网(http://www.fusioncharts.com/goodies/fusioncharts-free/)下载FusionCharts Free,下载MySQL的JDBC驱动包——mysql-connector-java-5.1.5-bin.jar。

1.新建web Project

2.将FusionCharts Free解压后的Charts文件夹和JSClass文件夹放到新建web项目的WebContent目录下,将MySQL的JDBC驱动包——mysql-connector-java-5.1.5-bin.jar放到WebContent/WEB-INF/lib目录下

3.在WebContent目录下新建JSP目录,然后把FusionCharts Free中的Code/JSP/Includes下的FusionChartsRenderer.jsp文件拷到新建的JSP目录下

然后打开MySQL新建factorydb数据库,并建两张表即factory_master和factory_output

最后编代码,如下:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.* " %>

<HTML>
<HEAD>
<TITLE>FusionCharts Free - Database Example</TITLE>
<%
/*You need to include the following JS file, if you intend to embed the chart using JavaScript.
Embedding using JavaScripts avoids the "Click to Activate..." issue in Internet Explorer
When you make your own charts, make sure that the path to this JS file is correct. Else, you would get JavaScript errors.
*/
%>
<SCRIPT LANGUAGE="Javascript" SRC="../../JSClass/FusionCharts.js"></SCRIPT>
<style type="text/css">
<!--
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.text{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style>
</HEAD>
<BODY>
<CENTER>
<%
/*
In this example, we show how to connect FusionCharts to a database.
For the sake of ease, we've used a database which contains two tables, which are linked to each
other. 
*/

String driverClass="com.mysql.jdbc.Driver";
    Connection oConn=null;
//Database Objects - Initialization
Statement st1,st2;
ResultSet rs1,rs2;

String url="jdbc:mysql://localhost:3306/factorydb";//之前数据库名simple
String user="root";//MySQ帐号
String password="root";//MYSQL密码


String strQuery="";

//strXML will be used to store the entire XML document generated
String strXML="";


//Generate the chart element
strXML = "<graph caption='Factory Output report' subCaption='By Quantity' decimalPrecision='0' showNames='1'  numberSuffix=' Units'  pieSliceDepth='30' formatNumberScale='0'>";

//Construct the query to retrieve data
strQuery = "select * from Factory_Master";

Class.forName(driverClass).newInstance();
oConn=DriverManager.getConnection(url,user,password);

st1=oConn.createStatement();
rs1=st1.executeQuery(strQuery);

String factoryId=null;
String factoryName=null;
String totalOutput="";
//Iterate through each factory
while(rs1.next()) {

factoryId=rs1.getString("FactoryId");
factoryName=rs1.getString("FactoryName");
//Now create second recordset to get details for this factory
strQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId=" + factoryId;
st2=oConn.createStatement();
rs2 = st2.executeQuery(strQuery);
if(rs2.next()){
totalOutput=rs2.getString("TotOutput");
}
//Generate <set name='..' value='..'/>
strXML += "<set name='" + factoryName + "' value='" +totalOutput+ "'/>";
//Close resultset
try {
if(null!=rs2){
rs2.close();
rs2=null;
}
}catch(java.sql.SQLException e){
//do something
System.out.println("Could not close the resultset");
}
try{
if(null!=st2) {
st2.close();
st2=null;
}
}catch(java.sql.SQLException e){
//do something
System.out.println("Could not close the statement");
}
} //end of while
//Finally, close <graph> element
strXML += "</graph>";
//close the resulset,statement,connection
try {
if(null!=rs1){
rs1.close();
rs1=null;
}
}catch(java.sql.SQLException e){
//do something
System.out.println("Could not close the resultset");
}
try {
if(null!=st1) {
st1.close();
st1=null;
}
   }catch(java.sql.SQLException e){
//do something
System.out.println("Could not close the statement");
}
try {
if(null!=oConn) {
   oConn.close();
   oConn=null;
}
   }catch(java.sql.SQLException e){
//do something
System.out.println("Could not close the connection");
}

//Create the chart - Pie 3D Chart with data from strXML
%> 
<jsp:include page="FusionChartsRenderer.jsp" flush="true"> 
<jsp:param name="chartSWF" value="../../Charts/FCF_Line.swf" /> 
<jsp:param name="strURL" value="" /> 
<jsp:param name="strXML" value="<%=strXML %>" /> 
<jsp:param name="chartId" value="FactorySum" /> 
<jsp:param name="chartWidth" value="650" /> 
<jsp:param name="chartHeight" value="450" /> 
<jsp:param name="debugMode" value="false" />
<jsp:param name="registerWithJS" value="false" />
</jsp:include>
</CENTER>
</BODY>
</HTML>


原创粉丝点击