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.
-
/**
-
* Returns all the values from FusionCharts data-xml in form of Array
-
* @id FusionChartsGetValues
-
*
-
* @return Array
-
* @type Array
-
*
-
* @code
-
* getValuesFromFusionChartsXML(sourceXML);
-
*
-
* @param sourceXML {String}
-
* @param forceSingleSeries {Boolean}
-
*
-
* @note
-
* The function returns double-dimension array for multi-series XML,
-
* but can be forced to return a single-dimension array
-
* forceSingleSeries=true
-
*/
-
getValuesFromFusionChartsXML = function(sourceXML, forceSingleSeries) {
-
// validate innput argument type
-
if(typeof sourceXML != ’string’) {
-
throw "ArgumentException() :: sourceXML is not string.";
-
}
-
-
var r = []; // array to store results
-
-
// in case of multi-series, recurse
-
if(!forceSingleSeries) {
-
var d = sourceXML.match(/<dataset [\s\S]*?<\/dataset>/ig);
-
while(d && d.length){
-
r.push(getValuesFromFusionChartsXML(d.pop(), true));
-
}
-
return r;
-
}
-
-
// parse set elements to retrieve values
-
var a = sourceXML.replace(/<set .*?value=\‘(.*?)\’|<set .*?value=\”(.*?)\”/ig,
-
function($1, $2, $3) { r.push(parseInt($2 || $3)); });
-
return r;
-
};
-
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:
-
function getSum(chartId) {
-
var s = 0,
-
v = getValuesFromFusionChartsXML(getChartFromId(chartId).getXMLData());
-
while(v && v.length) {
-
s += v.pop();
-
}
-
return s;
-
}
-
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:

