kingpub

海内存知己,博客若比邻

 

FusionCharts

  

1.   概述

FusionCharts是一个能帮你创建其引人注目的动态图像效果的Macromedia Flash控件。充分利用Macromedia Flash所具有的流畅功能来创建简洁的、交互式的和引人注目的动态图像

 

2.   功能描述

FusionChartsInfoSoft Global的一个产品,InfoSoft Global 公司是专业的Flash图形方案提供商,他们还有几款其他的,基于Flash技术的产品,都非常的漂亮。

FusionCharts是一个跨平台,跨浏览器的flash图表组件解决方案,能够被 ASP.NET, ASP, PHP, JSP, ColdFusion, Ruby on Rails, 简单 HTML 页面甚至PPT调用。你不需要知道任何关于flash编程的知识,你只需要知道你所用的编程语言就可以了。

FusionCharts是一个闪光的图表组件,它可以用来制作数据动画图表,其中动画效果用的是Adobe Flash 8 (原Macromedia Flash的)制作的flash FusionCharts可用于任何网页的脚本语言类似于HTML .NETASP JSP技术的, PHP ColdFusion等,提供互动性和强大的图表。使用XML作为其数据接口, FusionCharts充分利用流体美丽的Flash创建紧凑,互动性和视觉逮捕图表

3.   技术细节

3.1. 不需要任何Active-X或扩展控件

不需要安装任何扩展Active-X控件或组件来渲染效果。对于服务器不需要安装组件,FusionCharts能够运行的非常流畅。你只需上载核心的SWF文件到服务器中并将为其配置XML数据文件。

3.2. 减少服务器负载

FusionCharts能很大程度地减轻服务器的负荷,相当在客户端处理工作。由于该控件文件非常小,因此也非常适合窄频带传输。

3.3. 兼容多种脚本语言

FusionCharts兼容多种脚本语言:如ASPASP.NET,JSP, PHP, Perl, CGI, CF等,如同在客户端进行处理时,无论是哪一种脚本语言通过数据访问组件都没太大关系。

3.4. 改变客户端动态数据库

你可以用客户端中很少的几句脚本来控制FusionCharts,也能够在客户端中建立全新的图形而不需要重新加载页面和图形

3.5. 可在图形中追加其它特征

FusionCharts提供了自制的很多选项:比如透明度、色彩主题、热点选区、盘旋标题等。其最大的好处在于你能够利用XML数据文件进行自定义

 

4.   Chart XML 元素定义说明(API

5.   fusionCharts 工作原理

There are 4 ways using which you can provide XML data to FusionCharts:

5.1. dataURL method

In this method, you provide the URL of XML Data Document to FusionCharts. The chart now sends a request for XML data to the specified URL, reads it, parses it and then renders the charts accordingly. The following diagram would help you understand better:

(Click on the diagram below to see the animated feature tour on this process)

 

As you can see above, the following steps are involved in this process:

1.     You first send the HTML content and SWF file to the end viewer's browser. Along with the SWF, you also tell where the data is to be pulled from. This URL points to your server side scripts which would output the XML data document for the chart.

2.     Once the SWF is loaded on the end viewer's machine, it sends a request for XML data document to the specified URL.

3.     Your server side scripts at this URL now

o    Take in the passed parameters (if any)

o    Process them

o    Interact with the database

o    Get data from data base in native objects like Recordsets, Data Readers etc.

o    Convert this data into XML format using simple string concatenations or using XML DOM.

o    Finally write this data to output stream (NOT physically on server).

4.     FusionCharts now accepts this XML data, parses it and finally renders the chart.

If your dataURL contains special characters like ?, & etc., you need to URL Encode. Also, if you want any parameters as a part of dataURL to your server side script, you'll need to URL Encode the entire dataURL. Example: if you want to provide data URL as Data.asp?id=1&subId=2, it should be provided as Data%2Easp%3Fid%3D1%26subId%3D2

 

Effectively, in this process you need the following to build a chart:

1.     Chart Container Page - The page which contains the HTML code to embed and show the chart. This page contains the URL for XML Data Document also.

2.     Chart SWF File

3.     Data Provider Page - The page which will provide XML Data to FusionCharts. This page outputs ONLY XML and no HTML tags.

Sample Usage of dataURL method using direct HTML Embedding Method

<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="500" height="300" id="Column2D" align="center">
   <param name="movie" value="Column2D.swf" />
   <param name="FlashVars" value="&dataURL=Data.asp" />
   <param name="quality" value="high" />
   <embed src="Column2D.swf" FlashVars="&dataURL=Data.asp" quality="high" bgcolor="#ffffff" width="400" height="300" name="Column2D" align="center" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

 

Sample Usage of dataURL method using FusionCharts JavaScript Class

<div id="chart1div">
   This text is replaced by the chart.
</div>
<script type="text/javascript">
   var chart1 = new FusionCharts("Column2D.swf", "ChId1", "600", "400", "0", "0");
   chart1.setDataURL("Data.asp");

   chart1.render("chart1div");
</script>

 

dataURL method is the recommended method as it does not place any limits on the size of XML Data. Also, if you're using special characters (UTF-8 or double byte characters) in your XML, you need to necessarily use dataURL method.

 

5.2. dataXML method

In this method, you send the XML data along with the HTML Content and chart SWF file to the browser. The SWF loads, reads this data (present in same page) and then renders the chart. The following diagram would help you understand better:

(Click on the diagram below to see the animated feature tour on this process)

 

As you can see above, the following steps are involved in this process:

1.     You send the HTML content, chart SWF file and XML data all at once to the end viewer's browser.

2.     Once the SWF is loaded on the end viewer's machine, it reads the XML data present on the same page

3.     It finally parses it and then renders the chart.

If your XML Data Document contains special characters in dataXML mode, you'll need to XML/URL Encode them.

 

Effectively, in this process you need the following to build a chart:

1.     Chart Container Page - The page which contains the HTML code to embed and show the chart. It also contains the XML data to be used by the chart.

2.     Chart SWF File

Sample Usage of dataXML method using direct HTML Embedding Method

<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="500" height="300" id="Column2D" align="center">
   <param name="movie" value="Column2D.swf" />
   <param name="FlashVars" value="&dataXML=<chart><set name='Data1' value='1' /></chart>" />
   <param name="quality" value="high" />
   <embed src="Column2D.swf" FlashVars="&dataXML=<chart><set name='Data1' value='1' /></chart>" quality="high" bgcolor="#ffffff" width="400" height="300" name="Column2D" align="center" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

 

Sample Usage of dataXML method using FusionCharts JavaScript Class

<div id="chart1div">
   This text is replaced by the chart.
</div>
<script type="text/javascript">
   var chart1 = new FusionCharts("Column2D.swf", "ChId1", "600", "400", "0", "0");
   chart1.setDataXML("<chart><set name='Data1' value='1' /></chart>");

   chart1.render("chart1div");
</script>

 

dataXML method is not recommended if you've higher amounts of data. In that case, use dataURL method.

 

 

 

5.3. JavaScript + setDataXML Method

In this method, you provide the XML data to FusionCharts using JavaScript functions (present on the same page in which chart is present). The following diagram would help you understand better:

(Click on the diagram below to see the animated feature tour on this process)

 

As you can see above, the following steps involved in this:

1.     Your server first sends the HTML content of the page and the chart SWF. Now, along with this, it also sends data for chart either as JavaScript arrays or XML stored in JavaScript string variables.

2.     Once the SWF is loaded, it registers itself with pre-defined JavaScript function FC_Rendered.

3.     Now, if the data was sent as JavaScript arrays, your JavaScript functions convert it into XML data document using string concatenation. Or, if you're using AJAX, you can also request ready-made XML data from your server.

4.     This XML data is conveyed to the chart using the pre-build setDataXML method (defined in FusionCharts class).

5.     The chart reads the data, parses it and then renders.

 

Sample Usage of JavaScript + setDataXML method

<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js">
//You need to include the above JS file, if you intend to embed the chart using JavaScript.
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
//All our data is stored in the "data" JavaScript array. This array was written using a server side script

function FC_Rendered(DOMId){
   
//This method is called whenever a FusionCharts chart is loaded.
   //Check if it's the required chart using ID

   if (DOMId=="ChId1"){
       //Invoke updateChart() method to update chart with new data
       updateChart();
   }
}

function updateChart(){
   
//Storage for XML data document
   var strXML = "<chart palette='2' caption='Factory Output ' >";
  
 //Here, we build the XML data document from data array using string concatenation
   //.... Build ....
   //Closing Chart Element

   strXML = strXML + "</chart>";

   //Get reference to chart object using Dom ID "ChId1"
   var chartObj = getChartFromId("ChId1");
   
//Update its XML
   chartObj.setDataXML(strXML);
}
</SCRIPT>

<BODY>
<div id="chart1div">
   This text is replaced by the chart.
</div>
<script type="text/javascript">
   var chart1 = new FusionCharts("Column2D.swf", "ChId1", "600", "400", "0", "1");
   //Start Chart with empty data as we'll later update using JavaScript
   chart1.setDataXML("<chart></chart>");
   chart1.render("chart1div");
</script>
</BODY>

 

 

5.4.  JavaScript + setDataURL Method

In this method, you provide the URL of XML data to FusionCharts using JavaScript functions (present on the same page in which chart is present). The chart then sends a request for XML data to the specified URL, reads it, parses it and then renders the charts accordingly. The following diagram would help you understand better:

(Click on the diagram below to see the animated feature tour on this process)

 

As you can see above, the following steps involved in this:

1.     Your server first sends the HTML content of the page and the chart SWF. Now, along with this, it also sends pre-defined JavaScript functions to update the chart.

2.     Once the SWF is loaded, it registers itself with pre-defined JavaScript function FC_Rendered.

3.     Your JavaScript functions now update the chart using setDataURL method, by passing the URL of XML Data Document.

4.     The chart sends a request for the XML data, reads it, parses it and then renders.

 

Sample Usage of JavaScript + setDataURL method

<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js">
//You need to include the above JS file, if you intend to embed the chart using JavaScript.
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">

function FC_Rendered(DOMId){
   
//This method is called whenever a FusionCharts chart is loaded.
   //Check if it's the required chart using ID

   if (DOMId=="ChId1"){
       //Invoke updateChart() method to update chart with new data
       updateChart();
   }
}

function updateChart(){
   //Get reference to chart object using Dom ID "ChId1"
   var chartObj = getChartFromId("ChId1");
   
//Update its XML Data URL
   chartObj.setDataURL("Data.asp");
}
</SCRIPT>

<BODY>
<div id="chart1div">
   This text is replaced by the chart.
</div>
<script type="text/javascript">
   var chart1 = new FusionCharts("Column2D.swf", "ChId1", "600", "400", "0", "1");
   //Start Chart with empty data as we'll later update using JavaScript
   chart1.setDataXML("<chart></chart>");
   chart1.render("chart1div");
</script>
</BODY>

 

 

6.   FusionCharts JSP实例

6.1. FusionCharts 目录结构说明

6.1.1.      Charts

           存放swf文件。

6.1.2.      JSClass

           存放js文件,主要是一些函数的方法调用,能够帮你用一种友好的方式把图形嵌入到html 页面

6.1.3.      FusionCharts.jsp

          调用的函数使用参数的说明:

6.1.3.1.       函数原型:

/**

     * Encodes the dataURL before it's served to FusionCharts.

     * If you have parameters in your dataURL, you necessarily need to encode it.

     * @param strDataURL - dataURL to be fed to chart

     * @param addNoCacheStr - Whether to add aditional string to URL to disable caching of data

     * @return

     */

 String encodeDataURL(String strDataURL, String addNoCacheStr,

           HttpServletResponse response);

/**

     * Creates the Chart HTML+Javascript to create the FusionCharts object with the given parameters.

     * This method uses JavaScript to overcome the IE browser problem with SWF wherein you have to 'Click to activate' the control

     * @param chartSWF - SWF File Name (and Path) of the chart which you intend to plot

     * @param strURL - If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method)

     * @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 - Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id.

     * @param chartWidth - Intended width for the chart (in pixels)

     * @param chartHeight - Intended height for the chart (in pixels)

     * @param debugMode - Whether to start the chart in debug mode

     * @param registerWithJS - Whether to ask chart to register itself with JavaScript

     */

String createChart(String chartSWF, String strURL, String strXML,

           String chartId, int chartWidth, int chartHeight, boolean debugMode,

           boolean registerWithJS);

 

/**

     * Creates the Chart HTML to embed the swf object with the given parameters

     * @param chartSWF - SWF File Name (and Path) of the chart which you intend to plot

     * @param strURL - If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method)

     * @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 - Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id.

     * @param chartWidth - Intended width for the chart (in pixels)

     * @param chartHeight - Intended height for the chart (in pixels)

     * @param debugMode - Whether to start the chart in debug mode

     */

String createChartHTML(String chartSWF, String strURL,

           String strXML, String chartId, int chartWidth, int chartHeight,

           boolean debugMode);

7.   实例代码

<?xml version='1.0' encoding='UTF-8'?>

<%@ page language="java" import="java.util.*"

       contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page import="org.springframework.context.ApplicationContext"%>

<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>

<%@page import="com.nxt.portal.framework.persistence.hibernateimpl.cdbimlog.CdbimLogDaoImpl;"%>

<%@ include file="../../FusionCharts.jsp"%> 1引入FusionCharts.jsp文件,创建chart 调用函数createChartHTML

<%

       request.setCharacterEncoding("UTF-8");   2设置字符集,防止中文乱码

       ApplicationContext ctx = WebApplicationContextUtils

                     .getWebApplicationContext(request.getSession()

                                   .getServletContext());

       CdbimLogDaoImpl impl = (CdbimLogDaoImpl) ctx

                     .getBean("cdbimLogDao");

       Map<String, String> map = impl.countLogByName();

%>

<HTML>

       <HEAD>

              <META http-equiv="Content-Type" content="text/html;charset=utf-8" />

              <TITLE>接口数据统计</TITLE>

              <script language="Javascript" SRC="../../JSClass/FusionCharts.js"></script> 3引入脚本方式生成chart图形。

              <style type="text/css">

<!--

body {

       font-family: Arial, Helvetica, sans-serif;

       font-size: 12px;

}

-->

</style>

       </HEAD>

       <BODY>

              <CENTER>

 

                     <h4>

                            接口调用成功统计——饼图

                     </h4>

                     <%

                            //Create an XML data document in a string variable  4:创建XML数据文件字符串

                            String strXML = "";

                            strXML += "<chart caption='统计项' xAxisName='接口名称' yAxisName='成功总数' showValues='0' formatNumberScale='0' showBorder='1'>"; 5chart 元素项说明详见4.ChartXMl元素定义说明

                            for (Iterator itr = map.keySet().iterator(); itr.hasNext();) {

                                   String strkey = (String) itr.next();

                                   String strvalue = (String) map.get(strkey);

                                   strXML += "<set label='" + strkey + "' value=' " + strvalue        + "' />"; 6图形数据输出项

                            }

                            strXML += "</chart>";

                7使用dataXML方法创建chart图形

                            //Create the chart - Column 3D Chart with data from strXML variable using dataXML method

                            String chartHTMLCode=createChartHTML("../../Charts/Pie3D.swf", "", strXML, "myFirst", 600, 300, false) ;

                            String chartHTMLCode2=createChartHTML("../../Charts/Column3D.swf", "", strXML, "myFirst", 600, 300, false) ;

                     %>

                     <%= chartHTMLCode%>

                     <h4>

                            接口调用成功统计——柱形图

                     </h4>

                     <%= chartHTMLCode2%>

              </CENTER>

       </BODY>

</HTML>

posted on 2010-03-05 17:06 xiaofeng 阅读(1351) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜