I have a jqPlot pie chart that I am loading by reading an XML file. However, when I load the array for slices dynamically the pie chart does not render right, when I hard code the array it seems to render correctly.
The code I use to get the array is:
function loadSlices(xml)
{
slices = new Array();
$(xml).find("category").each(
function()
{
$(this).find("slice").each(
function()
{
slice = new Array($(this).attr("name"));
slice.push($(this).attr("size"));
slices.push(slice);
}
);
}
);
return slices;
}
The code loading the chart is:
function pieChart(name,xmlurl)
{
$.get(xmlurl,function(xml)
{
loadSize(name,xml);
slices= new Array();
slices.push(loadSlices(xml));
$("#debug").append("\nSLICES ARR (getting ready to load):");
debug2dArray(slices);
//debug2dArray(slices)
//now load in the pie chart
var plot1 = $.jqplot(name,[slices],
{
seriesDefaults:{
renderer: $.jqplot.PieRenderer,
rendererOptions:{
showDataLables: true
},
}
});
});//end get AJAX request
}
When I hard code the slices array as [[["PB&J",2],["Ham",1]]] it works perfectly fine. Any help would be appreciated.
Updates
XML
<chart>
<size width="450px" height="200px"/>
<category>
<slice name="PB&J" size="3" />
<slice name="Ham" size="1" />
</category>
</chart>
I found the answer. When I was loading it the second time it was rendering the second argument in the embeded arrays as a string so
["PB&J","1"], which jqPlot did not like. So adding a parseInt() around the push for the integer resulted in the correct display. To see a demo of the fix see this jsfiddle.