forked from gonewandering/angles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angles.js
100 lines (86 loc) · 3.5 KB
/
angles.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
var angles = angular.module("angles", []);
angles.chart = function (type) {
return {
restrict: "A",
scope: {
data: "=",
options: "=",
id: "@",
width: "=",
height: "=",
resize: "=",
chart: "@",
segments: "@",
responsive: "=",
tooltip: "=",
legend: "="
},
link: function ($scope, $elem) {
var ctx = $elem[0].getContext("2d");
var autosize = false;
$scope.size = function () {
if ($scope.width <= 0) {
$elem.width($elem.parent().width());
ctx.canvas.width = $elem.width();
} else {
ctx.canvas.width = $scope.width || ctx.canvas.width;
autosize = true;
}
if($scope.height <= 0){
$elem.height($elem.parent().height());
ctx.canvas.height = ctx.canvas.width / 2;
} else {
ctx.canvas.height = $scope.height || ctx.canvas.height;
autosize = true;
}
}
$scope.$watch("data", function (newVal, oldVal) {
if(chartCreated)
chartCreated.destroy();
var options = $scope.options || {};
// if data not defined, exit
if (!newVal) {
return;
}
if ($scope.chart) { type = $scope.chart; }
if(autosize){
$scope.size();
chart = new Chart(ctx);
};
if($scope.responsive || $scope.resize)
options.responsive = true;
if($scope.responsive !== undefined)
options.responsive = $scope.responsive;
chartCreated = chart[type]($scope.data, options);
chartCreated.update();
if($scope.legend)
angular.element($elem[0]).parent().after( chartCreated.generateLegend() );
}, true);
$scope.$watch("tooltip", function (newVal, oldVal) {
if (chartCreated)
chartCreated.draw();
if(newVal===undefined || !chartCreated.segments)
return;
if(!isFinite(newVal) || newVal >= chartCreated.segments.length || newVal < 0)
return;
var activeSegment = chartCreated.segments[newVal];
activeSegment.save();
activeSegment.fillColor = activeSegment.highlightColor;
chartCreated.showTooltip([activeSegment]);
activeSegment.restore();
}, true);
$scope.size();
var chart = new Chart(ctx);
var chartCreated;
}
}
}
/* Aliases for various chart types */
angles.directive("chart", function () { return angles.chart(); });
angles.directive("linechart", function () { return angles.chart("Line"); });
angles.directive("barchart", function () { return angles.chart("Bar"); });
angles.directive("radarchart", function () { return angles.chart("Radar"); });
angles.directive("polarchart", function () { return angles.chart("PolarArea"); });
angles.directive("piechart", function () { return angles.chart("Pie"); });
angles.directive("doughnutchart", function () { return angles.chart("Doughnut"); });
angles.directive("donutchart", function () { return angles.chart("Doughnut"); });