I'm new to Angular and I wanted to use a jQuery Plugin (d3pie.js). I googled my need and I found few examples that explain that I have to create a directive and put in my parameters (and to be sincere it's a little bit confusing for a beginner like me).

我是Angular的新手,我想使用jQuery插件(d3pie.js)。我用Google搜索了我的需求,我找到了一些例子来解释我必须创建一个指令并输入我的参数(并且真诚地对于像我这样的初学者来说有点混乱)。

My problem is that I didn't find how to use a plugin that requires an object as argument, knowing that this object is in an other controller ?

我的问题是我没有找到如何使用需要对象作为参数的插件,知道这个对象在另一个控制器中?

1 个解决方案

#1


2

Here is an example using bars, a controller that hods your data and a directive with D3. This was all found using this link but I modified it slightly for better angular code style. http://odiseo.net/angularjs/proper-use-of-d3-js-with-angular-directives.

下面是一个使用条形图的示例,一个用于控制数据的控制器和一个带有D3的指令。这都是使用此链接找到的,但我稍微修改了它以获得更好的角度代码样式。 http://odiseo.net/angularjs/proper-use-of-d3-js-with-angular-directives。

  1. All your D3 logic and presentation must be contained within a directive
  2. 您的所有D3逻辑和演示文稿都必须包含在指令中

  3. Use HTML-declarative syntax to feed of data to your directive instances
  4. 使用HTML声明性语法将数据提供给指令实例

  5. By doing that, you can store the data in your controller, passing it to your D3 directive via two-way data binding of parameters
  6. 通过这样做,您可以将数据存储在控制器中,通过参数的双向数据绑定将其传递给D3指令

angular.module('myApp', [])
  .controller('BarsController', function($scope) {
    $scope.myData = [10,20,30,40,60, 80, 20, 50];
  })
  //camel cased directive name
  //in your HTML, this will be named as bars-chart
  .directive('barsChart', function ($parse) {
    //explicitly creating a directive definition variable
    //this may look verbose but is good for clarification purposes
    //in real life you'd want to simply return the object {...}
    var directiveDefinitionObject = {
      //We restrict its use to an element
      //as usually  <bars-chart> is semantically
      //more understandable
      restrict: 'E',
      //this is important,
      //we don't want to overwrite our directive declaration
      //in the HTML mark-up
      replace: false,
      //our data source would be an array
      //passed thru chart-data attribute
      scope: {data: '=chartData'},
      link: function (scope, element, attrs) {
        //in D3, any selection[0] contains the group
        //selection[0][0] is the DOM node
        //but we won't need that this time
        var chart = d3.select(element[0]);
        //to our original directive markup bars-chart
        //we add a div with out chart stling and bind each
        //data entry to the chart
        chart.append("div").attr("class", "chart")
          .selectAll('div')
          .data(scope.data).enter().append("div")
          .transition().ease("elastic")
          .style("width", function(d) { return d + "%"; })
          .text(function(d) { return d + "%"; });
        //a little of magic: setting it's width based
        //on the data value (d)
        //and text all with a smooth transition
      }
    };
    return directiveDefinitionObject;
  });
.chart {
    background: #eee;
    padding: 3px;
}

.chart div {
  width: 0;
  transition: all 1s ease-out;
  -moz-transition: all 1s ease-out;
  -webkit-transition: all 1s ease-out;
}

.chart div {
  font: 10px sans-serif;
  background-color: steelblue;
  text-align: right;
  padding: 3px;
  margin: 5px;
  color: white;
  box-shadow: 2px 2px 2px #666;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="BarsController">
    <bars-chart chart-data="myData"  ></bars-chart>
</div>

更多相关文章

  1. XGBoost中参数调优的完整指南(含Python-3.X代码)
  2. python传递列表作为函数参数
  3. Python学习之路:函数的非固定参数
  4. 函数参数中裸星号的目的是什么?
  5. 在Python中强制使用函数参数类型?
  6. python中函数参数传递的几种方法
  7. Python3语法——Python3函数参数的各种形式
  8. python 函数、参数及参数解构
  9. 从bash脚本传递参数到python解释器

随机推荐

  1. Android(安卓)>> 26. RecyclerView(一)
  2. 通过eclipse查看Android源代码(Java)
  3. 我所理解的Android模块化(二)——模块通信
  4. android中的push机制实现:android push no
  5. Android 单击listview弹出popupwindow弹
  6. Android Drawable之GradientDrawable
  7. Android studio实现左右滑动切换图片
  8. Android 的selector背景选择器应用
  9. Data Binding自定义属性
  10. 关于FragmentLayout布局的位置问题