FusionCharts.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * FusionCharts: Flash Player detection and Chart embedding.
  3. * Version: 1.2 (1st November, 2007) - Added Player detection, New conditional fixes for IE, New FORM fixes for IE
  4. * Version: 1.1 (29th June, 2007) - Added Player detection, New conditional fixes for IE
  5. *
  6. * Morphed from SWFObject (http://blog.deconcept.com/swfobject/) under MIT License:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. *
  9. */
  10. if(typeof infosoftglobal == "undefined") var infosoftglobal = new Object();
  11. if(typeof infosoftglobal.FusionChartsUtil == "undefined") infosoftglobal.FusionChartsUtil = new Object();
  12. infosoftglobal.FusionCharts = function(swf, id, w, h, debugMode, registerWithJS, c, scaleMode, lang, detectFlashVersion, autoInstallRedirect){
  13. if (!document.getElementById) { return; }
  14. //Flag to see whether data has been set initially
  15. this.initialDataSet = false;
  16. //Create container objects
  17. this.params = new Object();
  18. this.variables = new Object();
  19. this.attributes = new Array();
  20. //Set attributes for the SWF
  21. if(swf) { this.setAttribute('swf', swf); }
  22. if(id) { this.setAttribute('id', id); }
  23. if(w) { this.setAttribute('width', w); }
  24. if(h) { this.setAttribute('height', h); }
  25. //Set background color
  26. if(c) { this.addParam('bgcolor', c); }
  27. //Set Quality
  28. this.addParam('quality', 'high');
  29. //Add scripting access parameter
  30. this.addParam('allowScriptAccess', 'always');
  31. //Pass width and height to be appended as chartWidth and chartHeight
  32. this.addVariable('chartWidth', w);
  33. this.addVariable('chartHeight', h);
  34. //Whether in debug mode
  35. debugMode = debugMode ? debugMode : 0;
  36. this.addVariable('debugMode', debugMode);
  37. //Pass DOM ID to Chart
  38. this.addVariable('DOMId', id);
  39. //Whether to registed with JavaScript
  40. registerWithJS = registerWithJS ? registerWithJS : 0;
  41. this.addVariable('registerWithJS', registerWithJS);
  42. //Scale Mode of chart
  43. scaleMode = scaleMode ? scaleMode : 'noScale';
  44. this.addVariable('scaleMode', scaleMode);
  45. //Application Message Language
  46. lang = lang ? lang : 'EN';
  47. this.addVariable('lang', lang);
  48. //Whether to auto detect and re-direct to Flash Player installation
  49. this.detectFlashVersion = detectFlashVersion?detectFlashVersion:1;
  50. this.autoInstallRedirect = autoInstallRedirect?autoInstallRedirect:1;
  51. //Ger Flash Player version
  52. this.installedVer = infosoftglobal.FusionChartsUtil.getPlayerVersion();
  53. if (!window.opera && document.all && this.installedVer.major > 7) {
  54. // Only add the onunload cleanup if the Flash Player version supports External Interface and we are in IE
  55. infosoftglobal.FusionCharts.doPrepUnload = true;
  56. }
  57. }
  58. infosoftglobal.FusionCharts.prototype = {
  59. setAttribute: function(name, value){
  60. this.attributes[name] = value;
  61. },
  62. getAttribute: function(name){
  63. return this.attributes[name];
  64. },
  65. addParam: function(name, value){
  66. this.params[name] = value;
  67. },
  68. getParams: function(){
  69. return this.params;
  70. },
  71. addVariable: function(name, value){
  72. this.variables[name] = value;
  73. },
  74. getVariable: function(name){
  75. return this.variables[name];
  76. },
  77. getVariables: function(){
  78. return this.variables;
  79. },
  80. getVariablePairs: function(){
  81. var variablePairs = new Array();
  82. var key;
  83. var variables = this.getVariables();
  84. for(key in variables){
  85. variablePairs.push(key +"="+ variables[key]);
  86. }
  87. return variablePairs;
  88. },
  89. getSWFHTML: function() {
  90. var swfNode = "";
  91. if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
  92. // netscape plugin architecture
  93. swfNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swf') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'" ';
  94. swfNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
  95. var params = this.getParams();
  96. for(var key in params){ swfNode += [key] +'="'+ params[key] +'" '; }
  97. var pairs = this.getVariablePairs().join("&");
  98. if (pairs.length > 0){ swfNode += 'flashvars="'+ pairs +'"'; }
  99. swfNode += '/>';
  100. } else { // PC IE
  101. swfNode = '<object id="'+ this.getAttribute('id') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
  102. swfNode += '<param name="movie" value="'+ this.getAttribute('swf') +'" />';
  103. swfNode += '<param name="wmode" value="opaque" />'; // 将 flash 置为最底层
  104. var params = this.getParams();
  105. for(var key in params) {
  106. swfNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
  107. }
  108. var pairs = this.getVariablePairs().join("&");
  109. if(pairs.length > 0) {swfNode += '<param name="flashvars" value="'+ pairs +'" />';}
  110. swfNode += "</object>";
  111. }
  112. return swfNode;
  113. },
  114. setDataURL: function(strDataURL){
  115. //This method sets the data URL for the chart.
  116. //If being set initially
  117. if (this.initialDataSet==false){
  118. this.addVariable('dataURL',strDataURL);
  119. //Update flag
  120. this.initialDataSet = true;
  121. }else{
  122. //Else, we update the chart data using External Interface
  123. //Get reference to chart object
  124. var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
  125. chartObj.setDataURL(strDataURL);
  126. }
  127. },
  128. setDataXML: function(strDataXML){
  129. //If being set initially
  130. if (this.initialDataSet==false){
  131. //This method sets the data XML for the chart INITIALLY.
  132. this.addVariable('dataXML',strDataXML);
  133. //Update flag
  134. this.initialDataSet = true;
  135. }else{
  136. //Else, we update the chart data using External Interface
  137. //Get reference to chart object
  138. var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
  139. chartObj.setDataXML(strDataXML);
  140. }
  141. },
  142. setTransparent: function(isTransparent){
  143. //Sets chart to transparent mode when isTransparent is true (default)
  144. //When no parameter is passed, we assume transparent to be true.
  145. if(typeof isTransparent=="undefined") {
  146. isTransparent=true;
  147. }
  148. //Set the property
  149. if(isTransparent)
  150. this.addParam('WMode', 'transparent');
  151. else
  152. this.addParam('WMode', 'Opaque');
  153. },
  154. render: function(elementId){
  155. //First check for installed version of Flash Player - we need a minimum of 8
  156. if((this.detectFlashVersion==1) && (this.installedVer.major < 8)){
  157. if (this.autoInstallRedirect==1){
  158. //If we can auto redirect to install the player?
  159. var installationConfirm = window.confirm("You need Adobe Flash Player 8 (or above) to view the charts. It is a free and lightweight installation from Adobe.com. Please click on Ok to install the same.");
  160. if (installationConfirm){
  161. window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
  162. }else{
  163. return false;
  164. }
  165. }else{
  166. //Else, do not take an action. It means the developer has specified a message in the DIV (and probably a link).
  167. //So, expect the developers to provide a course of way to their end users.
  168. //window.alert("You need Adobe Flash Player 8 (or above) to view the charts. It is a free and lightweight installation from Adobe.com. ");
  169. return false;
  170. }
  171. }else{
  172. //Render the chart
  173. var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
  174. n.innerHTML = this.getSWFHTML();
  175. //Added <FORM> compatibility
  176. //Check if it's added in Mozilla embed array or if already exits
  177. if(!document.embeds[this.getAttribute('id')] && !window[this.getAttribute('id')])
  178. window[this.getAttribute('id')]=document.getElementById(this.getAttribute('id'));
  179. //or else document.forms[formName/formIndex][chartId]
  180. return true;
  181. }
  182. }
  183. }
  184. /* ---- detection functions ---- */
  185. infosoftglobal.FusionChartsUtil.getPlayerVersion = function(){
  186. var PlayerVersion = new infosoftglobal.PlayerVersion([0,0,0]);
  187. if(navigator.plugins && navigator.mimeTypes.length){
  188. var x = navigator.plugins["Shockwave Flash"];
  189. if(x && x.description) {
  190. PlayerVersion = new infosoftglobal.PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
  191. }
  192. }else if (navigator.userAgent && navigator.userAgent.indexOf("Windows CE") >= 0){
  193. //If Windows CE
  194. var axo = 1;
  195. var counter = 3;
  196. while(axo) {
  197. try {
  198. counter++;
  199. axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+ counter);
  200. PlayerVersion = new infosoftglobal.PlayerVersion([counter,0,0]);
  201. } catch (e) {
  202. axo = null;
  203. }
  204. }
  205. } else {
  206. // Win IE (non mobile)
  207. // Do minor version lookup in IE, but avoid Flash Player 6 crashing issues
  208. try{
  209. var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
  210. }catch(e){
  211. try {
  212. var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
  213. PlayerVersion = new infosoftglobal.PlayerVersion([6,0,21]);
  214. axo.AllowScriptAccess = "always"; // error if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
  215. } catch(e) {
  216. if (PlayerVersion.major == 6) {
  217. return PlayerVersion;
  218. }
  219. }
  220. try {
  221. axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
  222. } catch(e) {}
  223. }
  224. if (axo != null) {
  225. PlayerVersion = new infosoftglobal.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
  226. }
  227. }
  228. return PlayerVersion;
  229. }
  230. infosoftglobal.PlayerVersion = function(arrVersion){
  231. this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
  232. this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
  233. this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
  234. }
  235. // ------------ Fix for Out of Memory Bug in IE in FP9 ---------------//
  236. /* Fix for video streaming bug */
  237. infosoftglobal.FusionChartsUtil.cleanupSWFs = function() {
  238. var objects = document.getElementsByTagName("OBJECT");
  239. for (var i = objects.length - 1; i >= 0; i--) {
  240. objects[i].style.display = 'none';
  241. for (var x in objects[i]) {
  242. if (typeof objects[i][x] == 'function') {
  243. objects[i][x] = function(){};
  244. }
  245. }
  246. }
  247. }
  248. // Fixes bug in fp9
  249. if (infosoftglobal.FusionCharts.doPrepUnload) {
  250. if (!infosoftglobal.unloadSet) {
  251. infosoftglobal.FusionChartsUtil.prepUnload = function() {
  252. __flash_unloadHandler = function(){};
  253. __flash_savedUnloadHandler = function(){};
  254. window.attachEvent("onunload", infosoftglobal.FusionChartsUtil.cleanupSWFs);
  255. }
  256. window.attachEvent("onbeforeunload", infosoftglobal.FusionChartsUtil.prepUnload);
  257. infosoftglobal.unloadSet = true;
  258. }
  259. }
  260. /* Add document.getElementById if needed (mobile IE < 5) */
  261. if (!document.getElementById && document.all) { document.getElementById = function(id) { return document.all[id]; }}
  262. /* Add Array.push if needed (ie5) */
  263. if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}
  264. /* Function to return Flash Object from ID */
  265. infosoftglobal.FusionChartsUtil.getChartObject = function(id)
  266. {
  267. var chartRef=null;
  268. if (navigator.appName.indexOf("Microsoft Internet")==-1) {
  269. if (document.embeds && document.embeds[id])
  270. chartRef = document.embeds[id];
  271. else
  272. chartRef = window.document[id];
  273. }
  274. else {
  275. chartRef = window[id];
  276. }
  277. if (!chartRef)
  278. chartRef = document.getElementById(id);
  279. return chartRef;
  280. }
  281. /* Aliases for easy usage */
  282. var getChartFromId = infosoftglobal.FusionChartsUtil.getChartObject;
  283. var FusionCharts = infosoftglobal.FusionCharts;