Skip to content

Instantly share code, notes, and snippets.

@vicapow
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicapow/9535255 to your computer and use it in GitHub Desktop.
Save vicapow/9535255 to your computer and use it in GitHub Desktop.
Loading data into Angular, Part I
[73, 34, 42, 89]
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body ng-app="myApp">
<remote-data-donut-chart></remote-data-donut-chart>
<script>
var myApp = angular.module('myApp', []);
myApp.directive('remoteDataDonutChart', function(){
function link(scope, el, attr){
var color = d3.scale.category10();
var width = 200;
var height = 200;
var data = [];
var min = Math.min(width, height);
var svg = d3.select(el[0]).append('svg');
var pie = d3.layout.pie().sort(null);
var arc = d3.svg.arc()
.outerRadius(min / 2 * 0.9)
.innerRadius(min / 2 * 0.5);
svg.attr({width: width, height: height});
// center the donut chart
var g = svg.append('g')
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
// add the <path>s for each arc slice
var arcs = g.selectAll('path').data(pie(data))
.enter().append('path');
d3.json('donut-data.json', function(err, data){
if(err){ throw err; }
arcs = arcs.data(pie(data));
arcs.exit().remove();
arcs.enter().append('path')
.style('stroke', 'white')
.attr('fill', function(d, i){ return color(i) });
// update all the arcs (not just the ones that might have been added)
arcs.attr('d', arc);
});
}
return {
link: link,
restrict: 'E',
scope: {}
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment