|
项目中需要用到报表,发现这个Flash做的工具很好用~
效果漂亮,且支持多达40多种报表。
看图:
简单说下这个东东在jsp环境下的应用.
java代码:
package com.eline.epicc.utils;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
/** *//**
* 报表生成公共类(创建FusionCharts<flash cool>)
*
* @author zdw
*
*/
public class ChartUtils
{
/** *//**
* 对Url数据转码的方法
* @param strDataURL - chart的数据url
* @param addNoCacheStr - 非缓存字符串
* @return
*/
public String encodeDataURL(String strDataURL, String addNoCacheStr,
HttpServletResponse response)
{
String encodedURL = strDataURL;
if (addNoCacheStr.equals("true"))
{
java.util.Calendar nowCal = java.util.Calendar.getInstance();
java.util.Date now = nowCal.getTime();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"MM/dd/yyyy HH_mm_ss a");
String strNow = sdf.format(now);
if (strDataURL.indexOf("?") > 0)
{
encodedURL = strDataURL + "&FCCurrTime=" + strNow;
}
else
{
strDataURL = strDataURL + "?FCCurrTime=" + strNow;
}
encodedURL = response.encodeURL(strDataURL);
}
return encodedURL;
}
/** *//**
* 用HTML+JavaScript创建FusionCharts对象(用此方式需要导入FusionCharts.js文件)
* @param chartSWF -
* flash文件的位置,即chart图表类型
* @param strURL - xml数据源
* @param strXML - 字符流
* @param chartId - chart对象在HTML中的唯一标识
* @param chartWidth - flash chart 的宽度(单位px)
* @param chartHeight - flash chart 的高度(单位px)
* @param debugMode - 是否开启chart 调试模式
* @param registerWithJS - 是否注册自己
*/
public static String createChart(String chartSWF, String strURL,
String strXML, String chartId, int chartWidth, int chartHeight,
boolean debugMode, boolean registerWithJS)
{
StringBuffer strBuf = new StringBuffer();
strBuf.append("<!--START Script Block for Chart -->\n");
strBuf.append("\t\t<div id='" + chartId + "Div' align='center'>\n");
strBuf.append("\t\t\t\tChart.\n");
strBuf.append("\t\t</div>\n");
strBuf.append("\t\t<script type='text/javascript'>\n");
Boolean registerWithJSBool = new Boolean(registerWithJS);
Boolean debugModeBool = new Boolean(debugMode);
int regWithJSInt = boolToNum(registerWithJSBool);
int debugModeInt = boolToNum(debugModeBool);
strBuf.append("\t\t\t\tvar chart_" + chartId + " = new FusionCharts('"
+ chartSWF + "', '" + chartId + "', '" + chartWidth + "', '"
+ chartHeight + "', '" + debugModeInt + "', '" + regWithJSInt
+ "');\n");
if (strXML.equals(""))
{
strBuf.append("\t\t\t\t//Set the dataURL of the chart\n");
strBuf.append("\t\t\t\tchart_" + chartId + ".setDataURL(\""
+ strURL + "\");\n");
}
else
{
strBuf
.append("\t\t\t\t//Provide entire XML data using dataXML method\n");
strBuf.append("\t\t\t\tchart_" + chartId + ".setDataXML(\""
+ strXML + "\");\n");
}
strBuf.append("\t\t\t\t//Finally, render the chart.\n");
strBuf.append("\t\t\t\tchart_" + chartId + ".render(\"" + chartId
+ "Div\");\n");
strBuf.append("\t\t</script>\n");
strBuf.append("\t\t<!--END Script Block for Chart-->\n");
return strBuf.substring(0);
}
/** *//**
* 创建swf charts对象(HTML)
*
* @param chartSWF -
* flash文件的位置,即chart图表类型
*
* @param strURL -
* xml数据源
* @param strXML -
* If you intend to use dataXML method for this chart, pass the
* XML data as this parameter. Else, set it to "" (in case of
* dataURL method)
* @param chartId -
* chart对象在HTML中的唯一标识
* @param chartWidth -
* flash chart 的宽度(单位px)
* @param chartHeight -
* flash chart 的高度(单位px)
* @param debugMode -
* 是否开启chart 调试模式
*/
public static String createChartHTML(String chartSWF, String strURL,
String strXML, String chartId, int chartWidth, int chartHeight,
boolean debugMode)
{
String strFlashVars = "";
Boolean debugModeBool = new Boolean(debugMode);
if (strXML.equals(""))
{
strFlashVars = "chartWidth=" + chartWidth + "&chartHeight="
+ chartHeight + "&debugMode=" + boolToNum(debugModeBool)
+ "&dataURL=" + strURL + "";
}
else
{
strFlashVars = "chartWidth=" + chartWidth + "&chartHeight="
+ chartHeight + "&debugMode=" + boolToNum(debugModeBool)
+ "&dataXML=" + strXML + "";
}
StringBuffer strBuf = new StringBuffer();
// 开始输出Object chart
strBuf.append("\t\t<!--START Code Block for Chart-->\n");
strBuf
.append("\t\t\t\t<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"
+ chartWidth
+ "' height='"
+ chartHeight
+ "' id='"
+ chartId + "'>\n");
strBuf
.append("\t\t\t\t <param name='allowScriptAccess' value='always' />\n");
strBuf.append("\t\t\t\t <param name='movie' value='" + chartSWF
+ "'/>\n");
strBuf.append("\t\t\t\t<param name='FlashVars' value=\"" + strFlashVars
+ "\" />\n");
strBuf.append("\t\t\t\t <param name='quality' value='high' />\n");
strBuf
.append("\t\t\t\t<embed src='"
+ chartSWF
+ "' FlashVars=\""
+ strFlashVars
+ "\" quality='high' width='"
+ chartWidth
+ "' height='"
+ chartHeight
+ "' name='"
+ chartId
+ "' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />\n");
strBuf.append("\t\t</object>\n");
strBuf.append("\t\t<!--END Code Block for Chart-->\n");
return strBuf.substring(0);
}
/** *//**
* 提供快速输出Chart的便捷方法(基于字符流的方式)
*
* @param chartSWF :
* flash文件的位置,即chart图表类型
* @param strXml :
* xml文件流(string格式)
* @param chartId :
* chart对象在HTML中的唯一标识
* @param chartWidth :
* flash chart 的宽度(单位px)
* @param chartHeight:
* flash chart 的高度(单位px)
* @param response :
* reponse对象
*/
public static void outChartHTML(String chartSWF, String strXml,
String chartId, int chartWidth, int chartHeight,
HttpServletResponse response)
{
String str = createChartHTML(chartSWF, "", strXml, chartId, chartWidth,
chartHeight, false);
try
{
response.getWriter().write(str);
} catch (IOException e)
{
e.printStackTrace();
}
}
/** *//**
* 提供快速输出Chart的便捷方法(基于xml文件的方式)
*
* @param chartSWF :
* flash文件的位置,即chart图表类型
* @param strURL :
* xml数据源(路径格式)
* @param chartId :
* chart对象在HTML中的唯一标识
* @param chartWidth :
* flash chart 的宽度(单位px)
* @param chartHeight:
* flash chart 的高度(单位px)
* @param response :
* reponse对象
*/
public static void outChartSourceHTML(String chartSWF, String strURL,
String chartId, int chartWidth, int chartHeight,
HttpServletResponse response)
{
String str = createChartHTML(chartSWF, strURL, "", chartId, chartWidth,
chartHeight, false);
try
{
response.getWriter().write(str);
} catch (IOException e)
{
e.printStackTrace();
}
}
/** *//**
* bollean转换为int
*/
private static int boolToNum(Boolean bool)
{
int num = 0;
if (bool.booleanValue())
{
num = 1;
}
return num;
}
}
jsp:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.eline.epicc.utils.ChartUtils"%>
<%@page import="com.eline.epicc.utils.Constants"%>
<HTML>
<HEAD>
<TITLE>FusionCharts - Simple Column 3D Chart</TITLE>
</HEAD>
<BODY>
<%
//文件源示例
ChartUtils.outChartSourceHTML("common/charts/Column3D.swf",
"data.xml", "myFirst", 600, 300, response);
String[][] arrData = new String[6][2];
//产品名字
arrData[0][0] = "Product A";
arrData[1][0] = "Product B";
arrData[2][0] = "Product C";
arrData[3][0] = "Product D";
arrData[4][0] = "Product E";
arrData[5][0] = "Product F";
arrData[0][1] = "567500";
arrData[1][1] = "815300";
arrData[2][1] = "556800";
arrData[3][1] = "734500";
arrData[4][1] = "676800";
arrData[5][1] = "648500";
//Now, we need to convert this data into XML. We convert using string concatenation.
String strXML;
int i = 0;
strXML = "<chart caption='Sales by Product' numberPrefix='$' formatNumberScale='0'>";
for (i = 0; i < arrData.length; i++)
{
strXML = strXML + "<set label='" + arrData[i][0]
+ "' value='" + arrData[i][1] + "' />";
}
strXML = strXML + "</chart>";
//String字符串流示例
ChartUtils.outChartHTML(Constants.COLUMN3D, strXML,
"myFirst", 600, 300, response);
%>
</BODY>
</HTML>
xml数据文件:
<?xml version="1.0" encoding="UTF-8"?>
<chart caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' showValues='0' formatNumberScale='0' showBorder='1'>
<set label='Jan' value='462' />
<set label='Feb' value='857' />
<set label='Mar' value='671' />
<set label='Apr' value='494' />
<set label='May' value='761' />
<set label='Jun' value='960' />
<set label='Jul' value='629' />
<set label='Aug' value='622' />
<set label='Sep' value='376' />
<set label='Oct' value='494' />
<set label='Nov' value='761' />
<set label='Dec' value='960' />
</chart>
当然FusionCharts支持很多种编程语言,具体你可以看附件的CHM.
附上破解版的下载和我做的Chm帮助文档。
下载
|