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.
<
pre lang=”javascript”>/**
* 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
var d = sourceXML.match(/<dataset[sS]*?</dataset>/ig);
if(!forceSingleSeries && d && d.length) {
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;
};
Download the above script: FusionCharts.XML2Array.js
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 set
elements.
Sample Implementation Snippets
getValuesFromFusionChartsXML("<chart><set value="100" /><set value="50" /> </chart>");
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.
Can you also retrieve the title of the series somehow?
so for multi series charts i want to create arrays that look like this :
new Array(“Product D”,436500,765700,453900,326400);