forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouteServicePie.js
121 lines (107 loc) · 3.44 KB
/
routeServicePie.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
"use strict";
angular.module('openshiftConsole')
.directive('routeServicePie', function() {
return {
restrict: 'E',
scope: {
route: '=',
},
template: '<div ng-show="totalWeight" ng-attr-id="{{chartId}}"></div>',
link: function($scope) {
var chart, config;
var mobile = window.matchMedia("(max-width: 400px)").matches;
$scope.chartId = _.uniqueId('route-service-chart-');
config = {
bindto: '#' + $scope.chartId,
color: {
pattern: [$.pfPaletteColors.blue, $.pfPaletteColors.orange, $.pfPaletteColors.green, $.pfPaletteColors.red]
},
legend: {
show: true,
position: mobile ? 'bottom' : 'right'
},
pie: {
label: {
show: false
}
},
size: {
height: mobile ? 150 : 115
},
tooltip: {
format: {
name: function(name, ratio, id) {
// Show the full name on hover, even if name is truncated for the legend.
return id;
}
}
},
data: {
type: "pie",
// Keep groups in our order.
order: null,
selection: {
enabled: false
}
}
};
var getData = function(routeTarget) {
return [
routeTarget.name,
routeTarget.weight
];
};
var getDataset = function(column) {
return _.head(column);
};
var previousData;
var unloadRemovedServices = function(data) {
var datasets = {};
_.each(data.columns, function(column) {
var dataset = getDataset(column);
datasets[dataset] = true;
});
var previousColumns = _.get(previousData, 'columns', []);
data.unload = _.chain(previousColumns).reject(function(column) {
var dataset = getDataset(column);
return _.has(datasets, [dataset]);
}).map(getDataset).value();
};
function updateChart() {
var data = {
columns: [],
names: {}
};
if ($scope.route) {
data.columns.push(getData($scope.route.spec.to));
data.names[$scope.route.spec.to.name] = _.trunc($scope.route.spec.to.name, { length: 30 });
$scope.totalWeight = $scope.route.spec.to.weight;
_.each($scope.route.spec.alternateBackends, function(routeTarget) {
data.columns.push(getData(routeTarget));
data.names[routeTarget.name] = _.trunc(routeTarget.name, { length: 30 });
$scope.totalWeight += routeTarget.weight;
});
}
if (!$scope.totalWeight) {
return;
}
if (!chart) {
config.data.columns = data.columns;
chart = c3.generate(config);
} else {
unloadRemovedServices(data);
chart.load(data);
}
// Remember previous data so we know what alternate backends to unload when they change.
previousData = data;
}
$scope.$watch('route', updateChart);
$scope.$on('destroy', function() {
if (chart) {
// http://c3js.org/reference.html#api-destroy
chart = chart.destroy();
}
});
}
};
});