日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用 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 radiusvar width=960,height=500,radius=Math.min(width,height)/2;//set color rangevar 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 chartvar arc=d3.svg.arc().outerRadius(radius-80).innerRadius(radius-150);//watch data change from jsonscope.$watch('data', function(data){if(!data){ return; }//change json to two arrays for category and count//count arrayvar countArr=[];//category arrayvar categoryArr=[];//total number of issuesvar total=0;for (key in data){if(data.hasOwnProperty(key)){categoryArr.push(key);countArr.push(data[key]);total+=data[key];}}//get the graph parent elementel = el[0];// remove the graph if already exist, in case of repeatd3.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 grahg.append("path").attr("d",arc).style("fill",function(d,i){return color(i);});//paint the textg.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 graphg.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示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。