angularjs - 使用 angular-nvd3 出现错误 "data.map is not a function"

标签 angularjs graph nvd3.js angular-nvd3

当我尝试使用 Angular-nvd3 配置简单的折线图时,出现一些严重的 nvd3 错误。

我编辑了给出的示例 plunker,看起来可能是我的数据格式错误,因为仅交换选项仍然有效。

这是一个发生故障的 plunkr: http://plnkr.co/edit/fznNKBw6hwNYavfZ3Nvi?p=preview

选项:

  $scope.options = {
    "chart": {
      "type": "lineChart",
      "height": 450,
      "useInteractiveGuideline": true,
      "dispatch": {},
      "xAxis": {
        "axisLabel": "Months"
      },
      "yAxis": {
        "axisLabel": "Team size",
      }
    }
  };

数据:

$scope.data = {
    "key": "Monthly",
    "values": [{
      "x": 0,
      "y": 2
    }, {
      "x": 1,
      "y": 6
    }, {
      "x": 2,
      "y": 10
    }]
  }

有人能发现这个问题吗?

最佳答案

我用 nvd3 站点上的示例替换了您的scope.data:

      $scope.data = sinAndCos();

    /*Random Data Generator */
    function sinAndCos() {
        var sin = [],sin2 = [],
            cos = [];

        //Data is represented as an array of {x,y} pairs.
        for (var i = 0; i < 100; i++) {
            sin.push({x: i, y: Math.sin(i/10)});
            sin2.push({x: i, y: i % 10 == 5 ? null : Math.sin(i/10) *0.25 + 0.5});
            cos.push({x: i, y: .5 * Math.cos(i/10+ 2) + Math.random() / 10});
        }

        //Line chart data should be sent as an array of series objects.
        return [
            {
                values: sin,      //values - represents the array of {x,y} data points
                key: 'Sine Wave', //key  - the name of the series.
                color: '#ff7f0e'  //color - optional: choose your own line color.
            },
            {
                values: cos,
                key: 'Cosine Wave',
                color: '#2ca02c'
            },
            {
                values: sin2,
                key: 'Another sine wave',
                color: '#7777ff',
                area: true      //area - set to true if you want this line to turn into a filled area chart.
            }
        ];
    };

这在这个 plunkr 中有效:

plunkr

因此,这意味着您尝试执行的操作的数据组件出现问题。通过添加/减去数据元素进行实验,看看是否有帮助。

编辑: 您的数据对象格式错误:它应该采用以下格式:

  $scope.data = [{
        "key" : "Monthly",
        values : [{
                "x" : 1,
                "y" : 6,
                "color" : 'blue'
            }, {
                "x" : 2,
                "y" : 10,
                "color" : 'red'
            }
        ]
    }
  ];  

因此,从文档来看,数据对象需要一个数组,然后值是另一个值对象数组:

quickstart 1/3 way down page

关于angularjs - 使用 angular-nvd3 出现错误 "data.map is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37164492/

相关文章:

javascript - 请求特定 PHP 函数的问题

mercurial - 有什么方法可以改变 TortoiseHg 中图表的绘制方式吗?

algorithm - 为运行时间为 O(k(|V|+|E|)) 的单源最短路径问题设计一个算法

css - 如何去除nvd3中悬停元素的边框

javascript - nvd3 : editing labels in pie chart legend

d3.js - nvd3 - 将毫秒格式设置为 h :m:s in Chart (Period of Time, 而不是日期)

javascript - 如果条件不适用于状态,则 Angular

javascript - Angular 1.6 $http.jsonp 同时使用 google sheets API

r - 使用ggplot2仅将一个分割添加到一个构面

javascript - Angular 绑定(bind)到服务值不更新