-
Notifications
You must be signed in to change notification settings - Fork 0
/
highcharts-ng.js
97 lines (88 loc) · 3.38 KB
/
highcharts-ng.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
/**
* Created by admin on 2017/7/26.
*/
'use strict';
angular.module('highcharts-ng', [])
.directive('highchart', function () {
var seriesId = 0;
var ensureIds = function (series) {
series.forEach(function (s) {
if (!angular.isDefined(s.id)) {
s.id = "series-" + seriesId++;
}
});
}
var getMergedOptions = function (element, options, series) {
var defaultOptions = {
chart: {
renderTo: element[0]
},
title: {},
series: []
}
var mergedOptions = {}
if (options) {
mergedOptions = $.extend(true, {}, defaultOptions, options);
} else {
mergedOptions = defaultOptions;
}
if(series) {
mergedOptions.series = series
}
return mergedOptions
}
return {
restrict: 'EC',
replace: false,
scope: {
series: '=',
options: '=',
title: '='
},
link: function (scope, element, attrs) {
var mergedOptions = getMergedOptions(element, scope.options, scope.series);
var chart = new Highcharts.Chart(mergedOptions);
scope.$watch("series", function (newSeries, oldSeries) {
//do nothing when called on registration
if (newSeries === oldSeries) return;
if (newSeries) {
ensureIds(newSeries);
var ids = []
//Find series to add or update
newSeries.forEach(function (s) {
ids.push(s.id)
var chartSeries = chart.get(s.id);
if (chartSeries) {
chartSeries.update(angular.copy(s), false);
} else {
chart.addSeries(angular.copy(s), false)
}
});
//Now remove any missing series
chart.series.forEach(function (s) {
if (ids.indexOf(s.options.id) < 0) {
s.remove(false);
}
});
chart.redraw();
}
}, true);
scope.$watch("title", function (newTitle) {
chart.setTitle(newTitle, true);
}, true);
scope.$watch("options", function (newOptions, oldOptions, scope) {
//do nothing when called on registration
if (newOptions === oldOptions) return;
chart.destroy()
var mergedOptions = getMergedOptions(element, newOptions);
chart = new Highcharts.Chart(mergedOptions);
chart.setTitle(scope.title, true);
ensureIds(scope.series);
scope.series.forEach(function (s) {
chart.addSeries(angular.copy(s), false)
});
chart.redraw()
}, true);
}
}
});