使用 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>




本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/570546.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第一个express app 详细步骤

1. 全局安装node.js&#xff08;请参考网上教程&#xff09;。 如何判断是否全局安装成功&#xff1f;打开命令行终端&#xff0c;进入任意文件夹&#xff0c;输入node&#xff0c;不报错。 2. 安装express 2.1 创建app文件夹。并在此文件夹下创建文件package.json如下&#…

【scarletthln 关于算法的一点总结】

1. 分解问题的角度: fix 某一维度&#xff0c;尝试另一维度上的所有可能 a. 可能是array的(i, j)pointers, b. 可能是矩形的长与宽, c. 可能是tree的每一个subtree, d. 可能是情景题的每一对pair...2. 求所有解的, 暴力上backtracking吧3. 如果问最短/最少的, 先想BFS、DP这对…

Angularjs Nodejs Grunt 一个例子

做了一个简单的示例&#xff0c;目的是记录环境配置以及这套框架的结构流程。 1.配置环境 默认nodejs已安装。 安装以下模块&#xff1a;express&#xff08;nodejs框架&#xff09;,grunt&#xff08;javascript task runner&#xff09;,grunt-contrib-watch&#xff08;g…

【C#/WPF】用Thumb做可拖拽的UI控件

【C#/WPF】用Thumb做可拖拽的UI控件 原文:【C#/WPF】用Thumb做可拖拽的UI控件需求&#xff1a;简单的可拖拽的图片 使用System.Windows.Controls.Primitives.Thumb类 前台&#xff1a; <Canvas x:Name"g"><Thumb Canvas.Left"10" Canvas.Top"…

PHP 常用设计模式 (转载)

1.单例模式 单例模式顾名思义&#xff0c;就是只有一个实例。作为对象的创建模式&#xff0c; 单例模式确保某一个类只有一个实例&#xff0c;而且自行实例化并向整个系统提供这个实例。 单例模式的要点有三个&#xff1a; 一是某个类只能有一个实例&#xff1b;二是它必须自行…

Angularjs切换网站配色模式简单示例1(切换css文件)

一个网站可以有多种配色方案&#xff0c;例如正常模式&#xff0c;夜间模式等。 简单示例一个通过点击按钮&#xff0c;更换css文件&#xff0c;达到切换配色模式的angularjs 小app。 主要文件有三个&#xff1a;index.html&#xff08;主文件&#xff09;&#xff0c;white.…

【转】在树莓派上实现人脸识别

教程参考地址&#xff1a;http://shumeipai.nxez.com/2018/08/12/facial-recognition-identification-on-raspberry-pi.html 转载于:https://www.cnblogs.com/little-kwy/p/9481259.html

Angularjs切换网站配色模式简单示例2(切换body元素的class)

一个网站可以有多种配色方案&#xff0c;例如正常模式&#xff0c;夜间模式等。 简单示例一个通过点击toggle 按钮&#xff0c;切换body元素的class&#xff0c;达到切换配色模式的angularjs小app。 1. Live范例可以在以下Codepen网址查看&#xff1a; http://codepen.io/Chris…

Eclipse中Maven项目出现红色感叹号问题

在Eclipse环境下&#xff0c;有时我们会遇到Maven项目出现红色感叹号的情形&#xff0c;而一旦项目出现感叹号&#xff0c;Eclipse便无法帮我们进行自动编译等工作&#xff0c;尽管有时候不会影响运行&#xff0c;但每次只能手动启动Maven重新编译&#xff0c;非常不便&#xf…

GitHub 配置及简单使用

一. 初始化 1. 初始化 git 目录 终端中进入到项目文件夹&#xff0c;然后输入以下命令。 git init 命令执行后&#xff0c;文件夹中会多一个.git 文件夹 2. 设置本机关联 GItHub 的用户名和邮箱 git config --global user.name "<username>" git conf…

斐波那契数列 (C#)

斐波那契数列&#xff08;Fibonacci sequence&#xff09;&#xff0c;又称黄金分割数列、因数学家列昂纳多斐波那契&#xff08;Leonardoda Fibonacci&#xff09;以兔子繁殖为例子而引入&#xff0c;故又称为“兔子数列”&#xff0c;指的是这样一个数列&#xff1a;1、1、2、…

Javascript 处理 JSON 数据 示例

最近做了一个 MEAN stack 的 app 。后台用 NodeJS 从 Jira rest api 获得JSON数据并做处理&#xff0c;然后前端用 AngularJS Ajax call 获得处理后的 JSON 数据&#xff0c;显示到 App 上。处理了很多 JSON 数据&#xff0c;决定编一个例子&#xff0c;写一个总结。 JSON 数据…

ansible常用命令

一、ansible常用命令 一、ansible命令的常用参数 ansible 默认提供了很多模块来供我们使用。在 Linux 中&#xff0c;我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块&#xff0c;通过 ansible-doc -s 模块名 又可以查看该模块有哪些参数可以使用。 1、…

2018.08.16 洛谷P2029 跳舞(线性dp)

传送门 简单的线性dpdp。 直接推一推就行了。 貌似三个状态会卡空间啊。。。 笔者分了两个阶段考虑状态转移。 代码&#xff1a; #include<bits/stdc.h> #define N 5001 #define inf 0x3f3f3f3f using namespace std; char xxx; int n,t,f[N][N],s[N],b[N],ans0; ch…

第一个 Rails App 从安装到创建(windows版本)

1. 在以下网址下载并运行 Rails 安装包&#xff1a; 点击打开链接 2. 检查 ruby&#xff0c;sqlite 和 rails 是否安装成功 2.1 查看 ruby 版本&#xff0c; 在命令行中输入&#xff1a; ruby -v 运行截图如下&#xff1a; 2.2 查看 sqlite 版本&#xff0c;在命令行中输入&…

Ruby Variable Scope 简单讲解

Name Begins WithVariable Scope$A global variableAn instance variable[a-z] or _A local variable[A-Z]A constantA class variable 以一个简单例子示例各种变量的区别&#xff1a;class Female# ConstantSEX female# Class variable: shared among all instances of this …

【Luogu P2781】 传教

这题是可以用线段树做的。虽然$n\leq 10^9$可以发现&#xff0c;真正需要用到的节点很少&#xff0c;故动态开点&#xff0c;只有需要用到的时候才新建节点。这里我在下放标记的时候新建节点&#xff0c;因为每操作/查询一个节点都需要先下放标记。时间复杂度$O(m\log n)$&…

P1481 魔族密码 (LIS)

题的连接&#xff1a;https://www.luogu.org/problemnew/show/P1481 简单思路&#xff1a; 就是LIS&#xff0c;最长上升子序列&#xff0c;当然把条件改一下&#xff0c;从模板里的A[ i ]> A[ j ]变成ss[ i ].find(ss[ j ])0&#xff1b;即可。好好理解最长上升子序列。是不…

Rails Minitest style 指南

In the test, we should use the ‘describe’, ‘context’, ‘it’ structure. Here I use a controller test as an example. I have this my_controller.rb with two controller actions: class MyController < ApplicationControllerdef controller_action_1# Code g…

浅谈离散数学中数理逻辑与集合论的数学本质

申华 , 张胜元 - 《大学教育》 - 2013 离散数学本质上是一门数学课程&#xff0c;是学生数学知识结构和数学素质的重要组成部分。数学这门学科体系虽然很庞大&#xff0c;但大致可分为连续型、离散型和随机型这三大类。在大多数的理工科专业的课程设计中&#xff0c;数学…