FusionCharts.js 13 KB

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