Convert FusionCharts Data-XML To JavaScript Array

November 14, 2009 · 0 comments   70 views

in Technical Publications

This is a snippet of JavaScript that would allow you to easily retrieve all “set” values from a FusionCharts data-XML in form of an Array.

One can use it to perform various mathematical or visual operations by retrieving the data-XML from FusionCharts object using the getXMLData() method and then passing on the same to this function.

  1. /**
  2.  * Returns all the values from FusionCharts data-xml in form of Array
  3.  * @id FusionChartsGetValues
  4.  *
  5.  * @return Array
  6.  * @type Array
  7.  *
  8.  * @code
  9.  * getValuesFromFusionChartsXML(sourceXML);
  10.  *
  11.  * @param sourceXML {String}
  12.  * @param forceSingleSeries {Boolean}
  13.  *
  14.  * @note
  15.  * The function returns double-dimension array for multi-series XML,
  16.  * but can be forced to return a single-dimension array
  17.  * forceSingleSeries=true
  18.  */
  19. getValuesFromFusionChartsXML = function(sourceXML, forceSingleSeries) {
  20.     // validate innput argument type
  21.     if(typeof sourceXML != ’string’) {
  22.         throw "ArgumentException() :: sourceXML is not string.";
  23.     }
  24.  
  25.     var r = []; // array to store results
  26.  
  27.     // in case of multi-series, recurse
  28.     if(!forceSingleSeries) {
  29.         var d = sourceXML.match(/<dataset [\s\S]*?<\/dataset>/ig);
  30.         while(d && d.length){
  31.             r.push(getValuesFromFusionChartsXML(d.pop(), true));
  32.         }
  33.         return r;
  34.     }
  35.  
  36.     // parse set elements to retrieve values
  37.     var a = sourceXML.replace(/<set .*?value=\‘(.*?)\’|<set .*?value=\”(.*?)\”/ig,
  38.        function($1, $2, $3) { r.push(parseInt($2 || $3)); });
  39.    return r;
  40. };

How to use this script?

One needs to copy the above script within the section of a page or include it as a separate .js file. In either case, a JavaScript function will be created that accepts a valid FusionCharts data-XML as parameter.

Depending upon the type of the source XML (single-series or multi-series,) the function will return a single-dimension or two-dimensional Array containing the values of all elements.

Sample Implementation Snippets

getValuesFromFusionChartsXML(''); will return an array of two integers in an Array [100, 50]

Retrieving the sum of all values from within a FusionCharts object:

  1. function getSum(chartId) {
  2.     var s = 0,
  3.         v = getValuesFromFusionChartsXML(getChartFromId(chartId).getXMLData());
  4.     while(v && v.length) {
  5.         s += v.pop();
  6.     }
  7.     return s;
  8. }
  9.  

Profiling Results

The above code primarily uses regular-expressions to parse the data-XML instead of using any dedicated XML parser and consequently is extremely fast. It takes approximately 1.9ms to convert 2kb of data on Mozilla FireFox.


Related posts:

  1. Generating Pareto Chart Data for FusionCharts
  2. Transpose An Array In JavaScript and jQuery
  3. Fast Algorithm To Find Unique Items in JavaScript Array
  4. String Reversing Algorithm Performance In JavaScript
  5. Executing jQuery Within FusionCharts Link Attribute

Leave a Comment