angularjs和D3 - 为Donutchart返回数组

时间:2018-01-28 00:28:44

标签: angularjs d3.js nvd3.js

我在控制器中试过这个:

var one = 0;
var two = 0;

var array = [{ key:"Discounted", y:one},

{key: "NotDiscounted", y: two }];

$scope.donatdata = function(one,two) {

    var one = one;
    var two = two;

    return  array


    }; 

在标记中:

<nvd3 options="donutoptions" data="donatdata(30,20)"></nvd3>

我一直这样:

d3.js:672错误:属性转换:预期数字,&#34;翻译(NaN,NaN)旋转(...&#34;。

并且没有画甜甜圈

然而,当我这样做时:

  $scope.donatdata = function(one,two) {


    return  [{ key:"Discounted", y:one},

    {key: "NotDiscounted", y: two }];


    }; 

有效!但是在控制台中发生了很多错误:

enter image description here

错误描述如下:https://code.angularjs.org/1.5.3/docs/error/ $ rootScope / infdig?p0 = 10&amp; p1 =%5B%5B%7B%22msg%22:%22fn:%20function%20(c,e,f,克)%7BF%的3DD%26%26克%3FG%5B0%5D:一个(C,E,F,G);返回%20b中(F,C,E)%7D%22%22newVal%22:%5B %7B%22key%22:%22Discounted%22%7D,%7B%22key%22:%22NotDiscounted%22%7D%5D,%22oldVal%22:%5B%7B%22key%22:%22Discounted%22%7D ,%7B%22key%22:%22NotDiscounted%22%7D%5D%7D%5D,%5B%7B%22msg%22:%22fn:%20function%20(C,E,F,G)%7BF%的3DD %26%26克%3FG%5B0%5D:一个(C,E,F,G);返回%20b中(F,C,E)%7D%22%22newVal%22:%5B%7B%22key%22 :%22Discounted%22%7D,%7B%22key%22:%22NotDiscounted%22%7D%5D,%22oldVal%22:%5B%7B%22key%22:%22Discounted%22%7D,%7B%22key% 22:%22NotDiscounted%22%7D%5D%7D%5D,%5B%7B%22msg%22:%22fn:%20function%20(C,E,F,G)%7BF%的3DD%26%26克%3FG %5B0%5D:一个(C,E,F,G);返回%20b中(F,C,E)%7D%22%22newVal%22:%5B%7B%22key%22:%22Discounted%22%如图7D所示,%7B%22key%22:%22NotDiscounted%22%7D%5D,%22oldVal%22:%5B%7B%22key%22:%22Discounted%22%7D,%7B%22key%22:%22NotDiscounted%22 %7D%5D%7D%5D,%5B%7B%22msg%22:%22fn:%20function%20(C,E,F,G)%7BF%的3DD%26%26克%3 FG%5B0%5D:一个(C,E,F,G);返回%20b中(F,C,E)%7D%22%22newVal%22:%5B%7B%22key%22:%22Discounted%22 %7D,%7B%22key%22:%22NotDiscounted%22%7D%5D,%22oldVal%22:%5B%7B%22key%22:%22Discounted%22%7D,%7B%22key%22:%22NotDiscounted% 22%7D%5D%7D%5D,%5B%7B%22msg%22:%22fn:%20function%20(C,E,F,G)%7BF%的3DD%26%26克%3FG%5B0%5D:一个(C,E,F,G);返回%20b中(F,C,E)%7D%22%22newVal%22:%5B%7B%22key%22:%22Discounted%22%7D,%7B% 22key%22:%22NotDiscounted%22%7D%5D,%22oldVal%22:%5B%7B%22key%22:%22Discounted%22%7D,%7B%22key%22:%22NotDiscounted%22%7D%5D% 7D%5D%5D

但我不知道如何解决方法!!

我该如何解决这个问题?为什么在第一种方法中,一和二的价值没有变化?

1 个答案:

答案 0 :(得分:0)

你应该传递一个不是函数的对象作为&#34;数据&#34;属性:

<nvd3 options="donutoptions" data="donatdata"></nvd3>
控制器中的

$scope.donatdata = [{ key:"Discounted", y:one}, key: "NotDiscounted", y: two}]

如果donatdata变量应包含动态数据(例如来自数据库),则在需要时填充/更改它。