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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/**
* tc-angular-chartjs - v1.0.11 - 2015-05-17
* Copyright (c) 2015 Carl Craig <carlcraig@3c-studios.com>
* Dual licensed with the Apache-2.0 or MIT license.
*/
(function() {
"use strict";
angular.module("tc.chartjs", []).directive("tcChartjs", TcChartjs).directive("tcChartjsLine", TcChartjsLine).directive("tcChartjsBar", TcChartjsBar).directive("tcChartjsRadar", TcChartjsRadar).directive("tcChartjsPolararea", TcChartjsPolararea).directive("tcChartjsPie", TcChartjsPie).directive("tcChartjsDoughnut", TcChartjsDoughnut).directive("tcChartjsLegend", TcChartjsLegend).factory("TcChartjsFactory", TcChartjsFactory);
function TcChartjs(TcChartjsFactory) {
return new TcChartjsFactory();
}
TcChartjs.$inject = [ "TcChartjsFactory" ];
function TcChartjsLine(TcChartjsFactory) {
return new TcChartjsFactory("line");
}
TcChartjsLine.$inject = [ "TcChartjsFactory" ];
function TcChartjsBar(TcChartjsFactory) {
return new TcChartjsFactory("bar");
}
TcChartjsBar.$inject = [ "TcChartjsFactory" ];
function TcChartjsRadar(TcChartjsFactory) {
return new TcChartjsFactory("radar");
}
TcChartjsRadar.$inject = [ "TcChartjsFactory" ];
function TcChartjsPolararea(TcChartjsFactory) {
return new TcChartjsFactory("polararea");
}
TcChartjsPolararea.$inject = [ "TcChartjsFactory" ];
function TcChartjsPie(TcChartjsFactory) {
return new TcChartjsFactory("pie");
}
TcChartjsPie.$inject = [ "TcChartjsFactory" ];
function TcChartjsDoughnut(TcChartjsFactory) {
return new TcChartjsFactory("doughnut");
}
TcChartjsDoughnut.$inject = [ "TcChartjsFactory" ];
function TcChartjsFactory() {
return function(chartType) {
var directive = {
restrict: "A",
scope: {
data: "=chartData",
options: "=chartOptions",
type: "@chartType",
legend: "=chartLegend",
chart: "=chart"
},
link: link
};
return directive;
function link($scope, $elem, $attrs) {
var ctx = $elem[0].getContext("2d");
var chart = new Chart(ctx);
var chartObj;
var showLegend = false;
var autoLegend = false;
var exposeChart = false;
var legendElem = null;
for (var attr in $attrs) {
if (attr === "chartLegend") {
showLegend = true;
} else if (attr === "chart") {
exposeChart = true;
} else if (attr === "autoLegend") {
autoLegend = true;
}
}
$scope.$on("$destroy", function() {
if (chartObj) {
chartObj.destroy();
}
});
$scope.$watch("data", function(value) {
if (value) {
if (chartObj) {
chartObj.destroy();
}
if (chartType) {
chartObj = chart[cleanChartName(chartType)]($scope.data, $scope.options);
} else if ($scope.type) {
chartObj = chart[cleanChartName($scope.type)]($scope.data, $scope.options);
} else {
throw "Error creating chart: Chart type required.";
}
if (showLegend) {
$scope.legend = chartObj.generateLegend();
}
if (autoLegend) {
if (legendElem) {
legendElem.remove();
}
angular.element($elem[0]).after(chartObj.generateLegend());
legendElem = angular.element($elem[0]).next();
}
if (exposeChart) {
$scope.chart = chartObj;
}
chartObj.resize();
}
}, true);
}
function cleanChartName(type) {
var typeLowerCase = type.toLowerCase();
switch (typeLowerCase) {
case "line":
return "Line";
case "bar":
return "Bar";
case "radar":
return "Radar";
case "polararea":
return "PolarArea";
case "pie":
return "Pie";
case "doughnut":
return "Doughnut";
default:
return type;
}
}
};
}
function TcChartjsLegend() {
var directive = {
restrict: "A",
scope: {
legend: "=chartLegend"
},
link: link
};
return directive;
function link($scope, $elem) {
$scope.$watch("legend", function(value) {
if (value) {
$elem.html(value);
}
}, true);
}
}
})();
|