Parsing jQuery XML object into Highcharts
I'm new here and a JavaScript beginner so please bear with me.
I'm trying to plot a highcharts chart based on some data in a jQuery xml
object. The actual data is coming from a SOAP request but for purposes of
my fiddle I just pasted it into a text variable for now.
my jsfiddle example
Just click the button "Create HTML Table and Plot" and scroll down to see
the chart.
An example of my xml data is as follows:
<rowset>
<Row>
    <Column0>0</Column0>
    <Column1>101</Column1>
    <Column2>US 1 LE 1 BU 1</Column2>
    <Column3>110</Column3>
    <Column4>CEO</Column4>
    <Column5>Ledger USA PL</Column5>
    <Column6>01-12</Column6>
    <Column7>2012</Column7>
    <Column8>20120101</Column8>
    <Column10>240481</Column10>
</Row>
<Row>
    <Column0>0</Column0>
    <Column1>101</Column1>
    <Column2>US 1 LE 1 BU 1</Column2>
    <Column3>121</Column3>
    <Column4>US Operations</Column4>
    <Column5>Ledger USA PL</Column5>
    <Column6>01-12</Column6>
    <Column7>2012</Column7>
    <Column8>20120101</Column8>
    <Column9>0</Column9>
    <Column10>2026166</Column10>
</Row>
I am able to create the x-axis categories which displays the accounting
period (Column6) in the chart x-axis as 01-12, 02-12 etc. :
var cats = [];
$($xml).find("Row").each(function () {
var column = $(this).find("Column6").text();
        if ($.inArray(column, cats)===-1)
cats.push(column);
    options.xAxis.categories = cats;
});
So far so good. I'm struggling though with how to build the series. This
is what I have currently (Column4 should be the series label and Column10
is the amount to be plotted for each series and each period):
  $($xml).find("Row").each(function(i, series) {
    var seriesOptions = {
        name: $(series).find("Column4").text(),
        data: []
    };
    $(series).find('Column10').each(function(i, point) {
        seriesOptions.data.push(
        parseInt($(point).text())
        );
    });
    // add it to the options
    options.series.push(seriesOptions);
});
This is obviously wrong because because I'm just searching every row in
the xml object. As a result my chart shows up with over 30 series all
displaying in period 01-12. I'm just unsure how to write this so that I
get a plot for each distinct value of Column 4 (e.g. "CEO" and "US
Operations") for each period, e.g. Column6 (01-12, 02-12 etc.).
I hope this makes sense and apologies for the long post. I don't expect
anyone to write the code for me but any syntax clues or suggestions or
whatever would be greatly appreciated.
 
No comments:
Post a Comment