-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathchart-directive.js
136 lines (108 loc) · 3.99 KB
/
chart-directive.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
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
'use strict';
angular.module('myApp.directives', []).
directive('chart', [function () {
return {
restrict: 'E',
scope: {
chartdata: '=chartdata'
},
template: '<div></div>',
replace: true,
controller: function ($scope, $element) {
},
link: function (scope, element, attrs) {
scope.$watch('chartdata', function (chartdata, oldchartdata) {
if (chartdata) {
//set chart defaults through tag attributes
var chartsDefaults = {
chart: {
renderTo: element[0],
type: attrs.type || null,
height: attrs.height || null,
width: attrs.width || null,
reflow: true,
animation: false,
zoomType: 'x'
// events: {
// redraw: resize,
// load: resize
// }
}
}
if (attrs.type === 'pie') {
chartsDefaults.chart.margin = [0, 0, 0, 0];
chartsDefaults.chart.spacingLeft = 0;
chartsDefaults.chart.spacingRight = 0;
chartsDefaults.chart.spacingTop = 0;
chartsDefaults.chart.spacingBottom = 0;
if (attrs.titleimage) {
chartdata.title.text = '<img src=\"' + attrs.titleimage + '\">';
}
if (attrs.titleicon) {
chartdata.title.text = '<i class=\"pictogram title\">' + attrs.titleicon + '</i>';
}
if (attrs.titlecolor) {
chartdata.title.style.color = attrs.titlecolor;
}
if (attrs.titleimagetop) {
chartdata.title.style.marginTop = attrs.titleimagetop;
}
if (attrs.titleimageleft) {
chartdata.title.style.marginLeft = attrs.titleimageleft;
}
}
if (attrs.type === 'line') {
chartsDefaults.chart.marginTop = 30;
chartsDefaults.chart.spacingTop = 50;
// chartsDefaults.chart.zoomType = null;
}
if (attrs.type === 'bar') {
chartsDefaults.chart.marginBottom = 80;
chartsDefaults.chart.defaultSeriesType = 'column';
// chartsDefaults.chart.spacingBottom = 50;
// chartsDefaults.chart.zoomType = null;
}
if (attrs.type === 'area') {
chartsDefaults.chart.spacingLeft = 0;
chartsDefaults.chart.spacingRight = 0;
chartsDefaults.chart.marginLeft = 0;
chartsDefaults.chart.marginRight = 0;
}
Highcharts.setOptions({
global: {
useUTC: false
},
chart: {
style: {
fontFamily: 'Lato, Helvetica, Arial, sans-serif'
}
}
});
if (attrs.type === 'line' || attrs.type === 'area') {
var xAxis1 = chartdata.xAxis[0];
//check for previous setting from service layer or json template... if it doesn't exist use the attr value
if (!xAxis1.labels.formatter) {
xAxis1.labels.formatter = new Function(attrs.xaxislabel);
}
if (!xAxis1.labels.step) {
xAxis1.labels.step = attrs.xaxisstep;
}
//end check
}
//pull any stringified from template JS and eval it
if (chartdata.tooltip) {
if (typeof chartdata.tooltip.formatter === 'string') {
chartdata.tooltip.formatter = new Function(chartdata.tooltip.formatter);
}
// chartdata.tooltip.shared = true;
}
renderChart(chartsDefaults, chartdata);
}
});
}
};
}]);
function renderChart(chartsDefaults, chartdata, attrs) {
angular.extend(chartsDefaults, chartdata);
new Highcharts.Chart(chartsDefaults);
}