在AngularJS中为图形创建对象数组

时间:2014-03-04 00:06:17

标签: javascript angularjs

我正在使用n-3图表(http://n3-charts.github.io/)在我的角度网页应用中创建一些图表。以下是一些数据值为3系列的点的示例数据:

$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},
];

在我的代码中,我试图在同样的构造下创建它:

$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);
};

但是$ scope.data数组中的值存在,但控制台给了我

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

我的3个数据点并没有绘制任何内容。我在哪里错了?

1 个答案:

答案 0 :(得分:1)

问题是,如果其中一个值为$scope.data,则图表无法调用数据集undefined

例如:

$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}
];

我在创建data数组时修改了我的函数以提高效率,并添加了OR条件以将值绘制为0(如果未定义)。我还修了一个日期错误。

$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);

};