Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm using n-3 charts (http://n3-charts.github.io/) to create some graphs in my angular web app. Here is some sample data of some points where there is a data values with 3 series:

$scope.data = [
      {x: new Date(1391448620668), current: 0, baseline: 0, original: 0, val_3: 0},
      {x: new Date(1391535020668), current: 0.993, baseline: 3.894, original: 8.47, val_3: 14.347},
      {x: new Date(1391621420668), current: 1.947, baseline: 7.174, original: 13.981, val_3: 19.991},
      {x: new Date(1391707820668), current: 2.823, baseline: 9.32, original: 14.608, val_3: 13.509},
      {x: new Date(1391794220668), current: 3.587, baseline: 9.996, original: 10.132, val_3: -1.167},
];

In my code I'm trying to create this under that same construct like so:

$scope.addGraphData = function(){
  var firstSubDate  = $scope.FirstSubjectDoseDt;
  var xValues = new Array(); // date data points
  var cValues = new Array(); // current values
  var bValues = new Array(); // baseline values
  var oValues = new Array(); // original baseline values

  for (var i = 0; i < $scope.currentData.length; i++) {
    xValues[i] = new Date(firstSubDate+([i]*2628000000));
    cValues[i] = $scope.currentData[i].currentValue;
    bValues[i] = $scope.currentData[i].baselineValue;
    oValues[i] = $scope.currentData[i].origValue;
  };

  for (var i = 0; i < $scope.currentData.length; i++) {
    $scope.data[i] = {x: xValues[i], current: cValues[i], baseline: bValues[i], original: oValues[i]};
  };
  console.log("Graph Data: " + $scope.data);
};

But the values in the $scope.data array exist, but the console gives me

Graph Data: [object Object],[object Object],[object Object]

for my 3 data points and it plots nothing. Where am I going wrong here?

share|improve this question
    
Can you post a JSFiddle or Plunker? How are you populating that data? Is it in an AngularJS $digest loop? – rtcherry Mar 4 '14 at 0:12
    
@rtcherry they are being populated by a form in the same scope. I'll try and build a quick jsfiddle. I forgot some of the code, updated question. – Macness Mar 4 '14 at 0:16
1  
console.log("Graph Data:", $scope.data) is usually more useful – Jesse the Game Mar 4 '14 at 0:38
up vote 1 down vote accepted

The issue was that the chart cannot call the data set $scope.data if one of the values is undefined.

For example:

$scope.data = [
      {x: new Date(1391448620668), current: undefined, baseline: 0, original: 0, val_3: 0},
      {x: new Date(1391535020668), current: 0.993, baseline: 3.894, original: 8.47, val_3: 14.347}
];

I modified my function to be more efficient when creating the data array and added an OR condition to plot the value at 0 if undefined. I also fixed a date bug.

$scope.addGraphData = function(){
  var firstSubDate  = $scope.FirstSubjectDoseDt;

  $scope.data = [];

  console.log("firstSubDate:", firstSubDate);

  for (var i = 0; i < $scope.currentData.length; i++) {
    $scope.data[i] = {
      x: new Date( new Date(firstSubDate).getTime() + i*2628000000 ),
      current:  $scope.currentData[i].currentValue || 0,
      baseline: $scope.currentData[i].baselineValue || 0,
      original: $scope.currentData[i].origValue || 0
    };

  };

  console.log("Monthly Graph Data:", $scope.data);

};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.