【问题标题】:Render a d3 pie chart in angular with angular-nvd3使用 angular-nvd3 以角度渲染 d3 饼图
【发布时间】:2016-08-25 16:59:34
【问题描述】:

https://plnkr.co/edit/vE5Km2?p=preview

我想用 angular-nvd3 以角度呈现 d3 饼图。我没有错误,所有相关的脚本都在加载。

我不确定如何调用指令内的函数—— 提供的指令是:

<nvd3 options='options' data='data'></nvd3> 

我改成了:

<nvd3 options='vm.options' data='vm.data'></nvd3>

HTML:

  <div ng-controller='MainController'>
    <nvd3 options='vm.options' data='vm.data'></nvd3>
  </div>

ma​​in.controller:

(function (angular) {
    'use strict';
    angular
        .module('blah').controller('MainController', MainController);

    function MainController () {
        var vm = this;

        vm.initSampleData = initSampleData; // set up the var

        function initSampleData() {
            vm.options = {
                'chart': {
                    'type': 'pieChart',
                    'height': 500,
                    'showLabels': true,
                    'duration': 500,
                    'labelThreshold': 0.01,
                    'labelSunbeamLayout': true,
                    'legend': {
                        'margin': {
                            'top': 5,
                            'right': 35,
                            'bottom': 5,
                            'left': 0
                        }
                    }
                }
            };

            vm.data = [
                {key: 'One', y: 5},
                {key: 'Two', y: 2},
                {key: 'Three', y: 9},
                {key: 'Four', y: 7},
                {key: 'Five', y: 4},
                {key: 'Six', y: 3},
                {key: 'Seven', y: .5}
            ];
        }
    }

})(window.angular);

如果我添加路线(不使用指令,为 Angular 2.0 做好准备) MainRoute.$inject = ['$routeProvider', 'nvd3'];它错误...

function MainRoute($routeProvider){
        $routeProvider.when('/', {
            controller: 'MainController as vm',
            templateUrl : 'main.html',
            bindToController: false
        });
    }

【问题讨论】:

    标签: javascript angularjs d3.js nvd3.js


    【解决方案1】:

    我认为你需要为你的控制器创建一个别名,它会解决你的问题。

    <div ng-controller='MainController as vm'>
    

    您将data 分配给控制器对象,但在视图中,在您给控制器别名之前,它可以用自己的名称调用,所以在添加别名之前它是MainController.data,在添加它之后就像我提到的在它之前 - vm.data

    希望对你有帮助

    已编辑

    这里是Plunker 和工作代码。你的问题是:

    • 您没有使用ng-app="yourModule" 初始化角度模块
    • 您插入了路由,但没有使用&lt;ui-view&gt; 让他们知道应该在哪里使用它

    【讨论】:

    • @cdb,你能提供 plunker 吗?玩起来会容易得多
    • @cdb,示例已损坏,请查看控制台。
    • 嗨 Mikki,好的,修复了这些库错误,感谢您告诉我。现在没有错误..但仍然没有饼图;)
    • @cdb,更新我的答案
    • 非常感谢 Mikki 抽出时间来帮助我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    相关资源
    最近更新 更多