我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          Flash圖表(FusionChartsV3)的簡單應用

          項目中需要用到報表,發現這個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"600300, 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"600300, 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幫助文檔。
          下載

          posted on 2008-11-06 16:14 々上善若水々 閱讀(6223) 評論(22)  編輯  收藏

          評論

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          下載需要密碼,密碼是多少呢?
          2008-11-06 16:50 | tp

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          我知道了,用納米盤客戶端下載就要密碼。ie下載不需要
          2008-11-06 16:52 | tp

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          謝謝。正需要呢。
          不過是否可以在項目中用破解版,這個還真不好取舍。
          2008-11-06 17:18 | IceRao

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          to:@IceRao
          哈哈,我們公司用的就是破解版~
          2008-11-06 17:21 | 々上善若水々

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          好東西 謝謝分享
          2008-11-06 17:38 | lvq810

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          有free版本的卻要用破解版,有必要嗎?我覺得free版本已經基本夠用了,連程序員自己都支持盜版,這年頭。。。
          2008-11-06 18:00 | EricFan

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          @EricFan

          破解的支持的多,還有就是效果好一些。對于外國的軟件,堅決用破解,因為在清朝的時候我們已經付過錢了。
          2008-11-06 21:24 | supercrsky

          # re: Flash圖表(FusionChartsV3)的簡單應用 [未登錄]  回復  更多評論   

          慎用盜版
          2008-11-06 21:35 | xZeus

          # re: Flash圖表(FusionChartsV3)的簡單應用 [未登錄]  回復  更多評論   

          好東西,有機會要用一下.
          2008-11-06 23:27 | 的的

          # re: Flash圖表(FusionChartsV3)的簡單應用 [未登錄]  回復  更多評論   

          謝謝。正需要呢。
          2008-11-07 08:42 | gg

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          使用FLEX,交互性更好吧
          2008-11-07 16:39 | dfjnn

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          不能下載
          2008-11-07 22:32 | 扭曲的鉛筆

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          不錯的工具,不知FREE版是不是夠用,做程式的還是要支持正版才好。
          2008-11-08 13:55 | Vincent.Yu

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          Constants 這個類在哪里啊。怎么沒有呢
          2008-11-11 10:37 | pikenlike

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          能留下你的qq或者是msn么?
          共同研究
          2008-11-11 11:37 | pikenlike

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          @pikenlike
          Constants.COLUMN3D 是我自己寫的常量類,你只需要把你的swf文件的路徑傳進去就行了。
          2008-11-12 08:47 | 々上善若水々

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          請問一下樓主,如何讓FusionCharts生成的圖表在網頁上循環播放啊,謝謝!!!
          2008-11-20 17:34 |

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          不明白你說的什么意思,不斷刷新Chart自己?
          2008-11-21 18:44 | 々上善若水々

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          就是說網頁上有多個圖表,當頁面加載時所有動畫效果已經完成,下邊的圖表無法看到動畫的效果,只是個最終的結果,如果有循環控制的話就可以看到下邊的圖表時仍然可以看到動畫效果,也相當于不斷刷新自己吧!謝謝
          2008-11-22 10:18 |

          # re:ALL  回復  更多評論   

          諸位大蝦,請問這個案例是否從數據庫中取值,然后通過XML加載到swf中?
          有人用asp或者.net做過嗎?
          2009-03-24 08:43 | 網名很土

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          @網名很土
          當然可以,chm中有例子
          2009-03-26 14:09 | 々上善若水々

          # re: Flash圖表(FusionChartsV3)的簡單應用   回復  更多評論   

          @々上善若水々
          大哥。。。貌似chm是英文版的。。。
          不過還是感謝您的解答,謝謝啦,我會常來的。
          2009-03-26 16:12 | 網名很土

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 孙吴县| 丰顺县| 延川县| 精河县| 拉孜县| 宁国市| 潞西市| 龙游县| 汉寿县| 陆良县| 永年县| 乐平市| 犍为县| 乐都县| 嵊州市| 夏津县| 惠东县| 永顺县| 汤阴县| 辽宁省| 新沂市| 东城区| 个旧市| 莎车县| 来凤县| 藁城市| 青川县| 乌兰浩特市| 宿松县| 德令哈市| 广昌县| 石渠县| 无为县| 广汉市| 平泉县| 白山市| 新安县| 汤阴县| 合水县| 喀什市| 景宁|