0

I am trying to create a dataset pattern like this from the rest service in my angular application.

Example:

$scope.chartSeries = [
  {"name": "Google", "data": [100, 200, 400, 700, 300]},
  {"name": "Yahoo", "data": [300, 100, null, 500, 200]},
  {"name": "Facebook", "data": [500, 200, 200, 300, 500] },
  {"name": "Microsoft", "data": [100, 100, 200, 300, 200] }
];

Right now this is my code to build a similar pattern ,

 if (datai){ 
    $rootScope.DashboardData =[];
    $rootScope.DashboardData = datai;
    var _fieldData = [];
    widget.chartConfig.series  = [];
    widget.chartConfig.series  = 
    $rootScope.DashboardData.map(function(elm) {
        return { 
            name: elm[widget.seriesname], 
            data:_fieldData.push(elm[widget.dataname])
        };
    });
 }

Problem is data field is not creating an array. This is the final output which the above code generates.

 [{"name": "Google", "data": 1}];
8
  • 1
    you are assigning nothing to data. is a typo ? Commented Mar 16, 2015 at 5:39
  • Can you explain little more details about your problem? Where is your for loop? Commented Mar 16, 2015 at 5:39
  • @levi it was not aligned properly, now you can see Commented Mar 16, 2015 at 5:40
  • 1
    should be like $rootScope.DashboardData.map(function(elm) { _fieldData.push(elm[widget.dataname]) return { name: elm[widget.seriesname], data: _fieldData }; }); } Commented Mar 16, 2015 at 5:41
  • 1
    sorry guys, i did it in c# way. now got it! thanks @pankajparkar Commented Mar 16, 2015 at 5:48

4 Answers 4

3

push() returns the new array's length

$rootScope.DashboardData.map(function(elm) 
{
    var _fieldData = [];
    _fieldData.push(elm[widget.dataname]);
    return { name: elm[widget.seriesname], data: _fieldData };
});

or you can use concat()

return { name: elm[widget.seriesname], data: _fieldData.concat(elm[widget.dataname])};                                          
Sign up to request clarification or add additional context in comments.

Comments

2

You are assigning _fieldData.push(elm[widget.dataname]) to data and .push() method returns the new array length, so you need to push the element first and then assign the array itself.

_fieldData.push(elm[widget.dataname])
 return { name: elm[widget.seriesname], data:_fieldData};
      });

Comments

1

try this , You nee a loop for dataname

$rootScope.DashboardData.map(function(elm) 
{
   var _fieldData = [];
   for(var i=0;i<elm[widget.dataname.length;i++)
    {
      _fieldData.push(elm[widget.dataname][i]);
    }    
    return { name: elm[widget.seriesname], data: _fieldData
});

Comments

0

Array .push() always returns an new length of an array

You should push that variable into _fieldData.push(elm[widget.dataname]); then assign that variable late in data.

$rootScope.DashboardData.map(function(elm) 
{
    var _fieldData = [].push(elm[widget.dataname]);
    return { name: elm[widget.seriesname], data: _fieldData }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.