使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图。执行index.html其结果见于图。:

priorityData.json中json数据例如以下:

{
"priority":{
"Blocker":12,
"Critical":18,
"Major":5,
"Minor":30,
"Trivial":24
}
}

index.html代码例如以下:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular-resource.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body ng-app="myApp" ng-controller=MainCtrl> <li ng-repeat="(priority, val) in priorityData">
<span >{{priority}}</span>
<span >{{val}}</span>
</li>
<priority-graph data="priorityData"></priority-graph> <script>
var myApp = angular.module('myApp', ['ngResource']);
//define app factory
myApp.factory('DataFac',function($resource){
return $resource('priorityData.json',{});
}); //define app controller
myApp.controller('MainCtrl',function($scope,DataFac){
DataFac.get(function(response){
$scope.priorityData = response.priority;
})
}); //define app directive
myApp.directive('priorityGraph', function(){
function link(scope, el, attr){
//set graph width,height and radius
var width=960,
height=500,
radius=Math.min(width,height)/2;
//set color range
var color=d3.scale.ordinal().range(["rgb(166,60,48)", "rgb(229,144,78)", " rgb(221,226,77)", "rgb(157,211,72)", "rgb(40,106,151)"]);
//set outer radius and inner radius for donut chart
var arc=d3.svg.arc()
.outerRadius(radius-80)
.innerRadius(radius-150); //watch data change from json
scope.$watch('data', function(data){
if(!data){ return; }
//change json to two arrays for category and count
//count array
var countArr=[];
//category array
var categoryArr=[];
//total number of issues
var total=0; for (key in data){
if(data.hasOwnProperty(key)){
categoryArr.push(key);
countArr.push(data[key]);
total+=data[key];
}
}
//get the graph parent element
el = el[0];
// remove the graph if already exist, in case of repeat
d3.select( el ).select( 'svg' ).remove(); var pie=d3.layout.pie()
.sort(null)
.value(function(d){return d}); var svg=d3.select(el).append("svg")
.attr("width",width)
.attr("height",height)
.append("g")
.attr("transform","translate("+width/2+","+height/2+")"); var g=svg.selectAll(".arc")
.data(pie(countArr))
.enter().append("g")
.attr("class","arc"); //paint the grah
g.append("path")
.attr("d",arc)
.style("fill",function(d,i){return color(i);}); //paint the text
g.append("text")
.attr("transform",function(d){return "translate("+arc.centroid(d)+")";})
.attr("dy",".35em")
.style("text-anchor","middle")
.text(function(d,i){return categoryArr[i]+":"+countArr[i]}); //paint total number in the middle of graph
g.append("text")
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { return total+" tickets"; });
}, true);
}
return {
link: link,
restrict: 'E',
scope: { data: '=' }
};
});
</script>
</body>
</html>

最新文章

  1. Python开发入门与实战22-简单消息回复
  2. python基础知识3——基本的数据类型2——列表,元组,字典,集合
  3. springmvc中RedirectAttributes的作用
  4. sloth——算法工程师标注数据的福音
  5. poj 1990 MooFest
  6. struts2传map到前台出现的问题
  7. Linux Shell 数字计算与比较
  8. String声明为NULL和&quot;&quot;的区别
  9. js的异步的问题的再次理解
  10. 利用KVC实现无需协议的委托模式
  11. DelayQueue使用示例之KTV包厢记时
  12. Python学习(二):函数入门
  13. es6面向对象
  14. 每天一个linux命令(1):wc命令
  15. Confluence 6 的 WebDAV 客户端整合介绍
  16. Two Melodies CodeForces - 813D (DP,技巧)
  17. 3D Touch开发技巧的笔记
  18. hibernate的flush()、refresh()、clear()针对一级缓存的操作的区别
  19. e777. 获得JList组件的所有项
  20. linux配置裸设备

热门文章

  1. boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET
  2. Flash Builder 条件编译的实现
  3. Raspberry pi raspbain系统下使用vim
  4. Perl入门(四)Perl的正則表達式
  5. ubuntu安装iscsi
  6. delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)
  7. JPA的Embeddable注解
  8. Ubuntu通过源代码编译安装Octave 4.0
  9. Mono和Jexus并且部署ASP.NET MVC3、4、5和WebApi
  10. 关于java堆内存溢出的几种情况(转)