使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本
使用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>
使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本的更多相关文章
- java中json数据生成和解析(复杂对象演示)
1.json简单介绍 1.1 json是最流行和广泛通用的数据传输格式,简称JavaScript Object Notation,最早在JavaScript中使用. 1.2 举个例子,下面是一个jso ...
- Query通过Ajax向PHP服务端发送请求并返回JSON数据
Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...
- 对 JSON 数据进行序列化和反序列化
如何:对 JSON 数据进行序列化和反序列化 2017/03/30 作者 JSON(JavaScript 对象符号)是一种高效的数据编码格式,可用于在客户端浏览器和支持 AJAX 的 Web 服务之间 ...
- Angular $http解析通过接口获得的json数据
刚接触angular不久,对很多东西都不了解,今天需要用angular通过接口得到json数据,折腾了好久,总算是能获取到数据了,下面是部分源码,仅供参考: HTML部分: <body ng-a ...
- AngularJS Directive 隔离 Scope 数据交互
什么是隔离 Scope AngularJS 的 directive 默认能共享父 scope 中定义的属性,例如在模版中直接使用父 scope 中的对象和属性.通常使用这种直接共享的方式可以实现一些简 ...
- ngResource提交json数据如何带参数
ngResource提交json数据如何带参数 直接使用ngResource和REST服务接口交互可以让程序显得简洁,前提是配置好跨域和OPTIONS请求的支持,与此同时,如果需要带些额外的参数,有两 ...
- angular directive scope
angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...
- ng1 http 读取json数据
在前端开发过程中,有时后端还没开发出接口,需要经常自己构造获取本地mock数据. AngularJS XMLHttpRequest $http 是 AngularJS 中的一个核心服务,用于读取远程服 ...
- AngularJS学习笔记(3)——通过Ajax获取JSON数据
通过Ajax获取JSON数据 以我之前写的与用户交互的动态清单列表为例,使用JSON前todo.html代码如下: <!DOCTYPE html> <html ng-app=&quo ...
随机推荐
- 打破“中规中矩”,手机QQ何以萌翻众人?
随着移动互联网的迅猛发展,越来越多的手机应用展现在了用户面前,不过,面对林林总总的手机应用,有时候我们却提不起兴趣,因为功能的同质化,UI的千篇一律已经让我们多少有些审美疲劳的感觉. ...
- Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)
原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(一) 既然是内容筛选,或者说是搜索引擎,有索引,必然要有搜索.搜索虽然与索引有关,那也只是与索引后的文件有关,和索引的程序是无关的,因此 ...
- uploadfiy使用
动态加参数:$("#file_upload").uploadify("settings", "formData", { knowledgeI ...
- mormort 土拨鼠,做后端服务那是杠杠的,基于http.sys
http.sys你可以用 mormort 土拨鼠,做后端服务那是杠杠的,基于http.sys并且还是开源的,作者天天更新代码,非常勤奋,官方论坛提问,回答也快其实,稍微看看,就能玩的挺好的
- setjmp和longjmp函数使用详解
源地址:http://blog.csdn.net/zhuanshenweiliu/article/details/41961975 非局部跳转语句---setjmp和longjmp函数.非局部指的是, ...
- 运行Dos命令并得到dos的输出文本(使用管道函数CreatePipe和PeekNamedPipe)
function RunDOS(const CommandLine: string): string;var HRead, HWrite: THandle; StartInfo: TStartup ...
- Java编码浅析(注意区分三个概念)(转)
编码: (1)外部资源的字符集-----没有读入jvm中的数据都是外部资源 (2)jvm中数据的字符集-----都是unicode (1)和(2)之间发生交互时,如果不指定编码,则使用JVM平台默认字 ...
- VC调试技巧
Visual C++ 的 C 运行时刻函数库标识模板0xCD 已经分配的数据(alloCated Data)0xDD 已经释放的数据(Deleted Data)0xFD 被保护的数据 ...
- Linux vmstat命令详解
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- web desktop在线演示
http://mydesk.sinaapp.com 基于extjs的web desktop应用框架. 1.跨浏览器 2.动态载入所需css,js文件 3.权限管理 4.支持多语种 5.支持asp,js ...