使用 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 ...
随机推荐
- Computational Geometry Template
顿时觉得神清气爽!! #include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)& ...
- 基于特定值来推断隐藏显示元素的jQuery插件
jQuery-Visibly是一款小巧简单的jQuery隐藏显示元素插件.该插件依据某个元素的值,例如以下拉框的值.输入框的值等来推断是否显示某个指定的元素. 用于推断的值能够是单个值,或者是多个值, ...
- hdu 1262 寻找素数对 数论 打表。
寻找素数对 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Java实现定时任务的三种方法(转)
在应用里经常都有用到在后台跑定时任务的需求.举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法: 普通thread实现 Tim ...
- pathload --有效的网络带宽估计方法
上一篇博客简述了现行的带宽估计的方法,分类,以及一些问题. 见:http://blog.csdn.net/ice110956/article/details/11071969 上文列出了13种现行的方 ...
- 如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。
如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断.
- Swift - 使用UIScrollView实现页面滚动切换
UIScrollView提供了以页面为单位滚动显示各个子页面内容的功能,每次手指滑动后会滚动一屏的内容. 要实现该功能,需要如下操作: 1,将UIScrollView的pagingEnabled属 ...
- ADO面板上的控件简介
ADO面板上的控件简介 一. TADOConnection组件该组件用于建立数据库的连接.ADO的数据源组件和命令组件可以通过该组件运行命令及数据库中提取数据等.该组件用于建立数据库的连接,该连接可被 ...
- Common Lisp Style Guide - Ariel Networks Labs
Common Lisp Style Guide - Ariel Networks Labs Common Lisp Style Guide
- 实例:怎样使用 Netty 下载文件
本实例主要參考的是官网的examples:点击这里 使用场景:client向Netty请求一个文件,Netty服务端下载指定位置文件到client. 本实例使用的是Http协议,当然,能够通过简单的改 ...