-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpiechart.js
101 lines (83 loc) · 2.47 KB
/
piechart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
angular.module('piechart', [])
.constant('piechartConfig', {
radius: 10
})
.controller('PiechartController', ['$scope', '$attrs', 'piechartConfig', function($scope, $attrs, piechartConfig) {
var slices;
var getArc = function(startAngle, endAngle) {
function convertToRadians(angle) {
return angle * (Math.PI / 180);
};
function getPointOnCircle(angle) {
return {
x: Math.cos(angle),
y: Math.sin(angle)
};
};
var midAngle = startAngle + (((endAngle || 360) - startAngle) / 2);
return {
start: getPointOnCircle(convertToRadians(startAngle)),
mid: getPointOnCircle(convertToRadians(midAngle)),
end: getPointOnCircle(convertToRadians(endAngle))
};
};
this.slices = slices = [];
this.addSlice = function(sliceScope) {
var that = this;
slices.push(sliceScope);
sliceScope.$on('$destroy', function() {
that.removeSlice(sliceScope);
})
};
this.removeSlice = function(sliceScope) {
slices.splice(slices.indexOf(sliceScope), 1);
this.setArcs();
};
this.setArcs = function() {
var prevStartAngle = 0;
var totalValue = 0;
$scope.radius = angular.isDefined($attrs.radius) ? $scope.$eval($attrs.radius) : piechartConfig.radius;
angular.forEach(slices, function(slice) {
totalValue += slice.value;
});
angular.forEach(slices, function(slice) {
slice.arc = getArc(
prevStartAngle,
prevStartAngle = (prevStartAngle + (360 / (totalValue / slice.value))) % 360
);
slice.arc.large = slice.value > (totalValue / 2);
});
};
}])
.directive('piechart', function() {
return {
restrict: 'EA',
replace: true,
controller: 'PiechartController',
templateUrl: 'template/piechart.html',
transclude: true,
scope: {
radius: '@'
}
};
})
.directive('piechartSlice', function() {
return {
restrict: 'EA',
require: '^piechart',
replace: true,
templateNamespace: 'svg',
templateUrl: 'template/piechart-slice.html',
scope: {
value: '@'
},
link: function(scope, element, attrs, ctrl) {
scope.value = parseInt(scope.value, 10);
ctrl.addSlice(scope);
attrs.$observe('value', function(value) {
scope.value = parseInt(value, 10);
ctrl.setArcs();
});
}
};
});