Here is an example of using bars, a controller that processes your data and a directive with D3. All of this was found at this link, but I changed it a bit for a better angular code style. http://odiseo.net/angularjs/proper-use-of-d3-js-with-angular-directives .
- All your logic and presentation D3 should be contained in the directive
- Use HTML declarative syntax to feed data to your directive instances
- Thus, you can store data in your controller by passing it to your D3 directive by way of two-way data binding
angular.module('myApp', []) .controller('BarsController', function($scope) { $scope.myData = [10,20,30,40,60, 80, 20, 50]; })
.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>
source share