使用 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>
时间: 2024-08-06 07:22:40

使用 angular directive 和 json 数据的 D3 带标签 donut chart示例的相关文章

Angular $http解析通过接口获得的json数据

刚接触angular不久,对很多东西都不了解,今天需要用angular通过接口得到json数据,折腾了好久,总算是能获取到数据了,下面是部分源码,仅供参考: HTML部分: 1 <body ng-app="httpApp"> 2 <ul ng-controller="httpController"> 3 <li ng-repeat="(key,value) in infos"> 4 {{ key }} : {{

D3.js加载csv和json数据

1.加载数据的基本命令 D3提供了方法可以对不同的数据类型进行加载,比如d3.text(), d3.xml(), d3.json(), d3.csv(), 和d3.html(). <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test</title> &l

angular post json数据到服务器,跨域访问,多大括号

angular.module('myApp',[]).config(function($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers .common['X-Requested-With']; $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-url

js:使用angular的http获取json数据

一,本例中使用amaze的select框架,配合angular显示json数据 框架官网链接:http://amazeui.org/javascript/selected 由于页面的异步刷新导致select配置完成后不再接收angular的数据,导致数据无法显示,这里解决使用setTimeout延迟select框架的配置 完成angular数据的植入 1,使用的json数据: 2,具体实现代码: <!DOCTYPE html> <html> <head lang="

Diablo3英雄榜-使用Volley和Gson来处理暴雪API的Json数据

使用Volley和Gson来处理Json 暗黑3的API传递给我们的是一个Json数据.现在开始我们尝试来解析它.在百度了一下之后,我初步知道了2个工具.一个是Volley这个是用来获取Json数据.一个是Gson这个是用来解析Json数据. 本章的目标: 读取暴雪的API数据 解析该数据 使用Volley来获取Json数据 Volley支持原生字符串.图像.Json.可以让我们更专注于应用程序的逻辑.Volley通过下面的方法获取. $git clone https://android.goo

深究AngularJS(4)——Directive和Scope数据隔离与交互

什么是隔离 Scope AngularJS 的 directive 默认能共享父 scope 中定义的属性,例如在模版中直接使用父 scope 中的对象和属性.通常使用这种直接共享的方式可以实现一些简单的 directive 功能.当你需要创建一个可重复使用的 directive,只是偶尔需要访问或者修改父 scope 的数据,就需要使用隔离 scope.当使用隔离 scope 的时候,directive 会创建一个没有依赖父 scope 的 scope,并提供一些访问父 scope 的方式.

动态往json数据里添加新属性

/* orderBy排序包括其它过滤器都只在原本json数据就存在的情况下才能起作用 表头中单行的产品总价是通过数量*单价计算出来的json数据中并没有给出 所以我们要自己创建一个 用sum表示*/ /*此时打印出json数据 里面每一个object对象都增加了一个sum*/ $scope.sum=function(data){ angular.forEach($scope.data,function(elem,$index){ elem.sum=elem.count*elem.price; /

AngularJS Directive 隔离 Scope 数据交互

什么是隔离 Scope AngularJS 的 directive 默认能共享父 scope 中定义的属性,例如在模版中直接使用父 scope 中的对象和属性.通常使用这种直接共享的方式可以实现一些简单的 directive 功能.当你需要创建一个可重复使用的 directive,只是偶尔需要访问或者修改父 scope 的数据,就需要使用隔离 scope.当使用隔离 scope 的时候,directive 会创建一个没有依赖父 scope 的 scope,并提供一些访问父 scope 的方式.

Json数据导出生成Excel

最近在做一个导入导出Excel的功能,导出其他类型的文件都比较熟悉,但是导入跟导出一个Excel还是稍微特殊点.根据这次的经验,写了个导出的小样例. 总体思路就是json数据的key,value跟Excel的行列转换,还有就是解决数据在Excel表格中存放的位置,区域问题. 这里要用到的两个小插件,一个是xslx.js,一个是FileSaver.js,前者是来处理生成Excel的,后者是用来把文件下载保存到本地的. 下载地址: https://github.com/eligrey/FileSav