Question

I am having problems populating the series portion for a Highcharts column graph given this data. Please note col1, col2, col3 are created dynamically so these names could change and the number of values can change as well.

 var objData = {
    "1995":{
            "col1":300.75,
            "col2":1000,
            "col3":500
           },
    "1996":{
            "col1":300,
            "col2":400,
            "col3":900}
 }

This is what I want in the series portion:

 series: [{
          name: 'col1', 
          data: [300.75,300]
         },
         {
          name: 'col2', 
          data: [1000,400]
         },
         {
          name: 'col3', 
          data: [500,900]
         }
        ];

I have created a JS fiddle: http://jsfiddle.net/jAPV5/1/ and have tried building a loop, but no luck.

If someone could help, that would be great.

Thanks Cheers

Was it helpful?

Solution

See the solution in this fiddle: http://jsfiddle.net/jAPV5/2/

for (var key in objData) {

 //console.log(key);  1995, 1996   
    var obj = objData[key];
    categories.push(key);

    for (var item in obj) {
        var targetSeries ;
        var bFound = false;
        for(var i=0; i< series.length; i++){
            if(item == series[i].name){
                bFound = true;
               targetSeries = series[i];
            }
        }
        if(!bFound) {
            targetSeries = {'name': item, 'data':[]};
            series.push(targetSeries);
        }
        var val = obj[item]
        targetSeries.data.push(val);
    }    

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top