forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotaUsageChart.js
173 lines (160 loc) · 5.5 KB
/
quotaUsageChart.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";
angular.module('openshiftConsole')
.directive('quotaUsageChart', function($filter, Logger) {
return {
restrict: 'E',
scope: {
used: '=',
total: '=',
// 'cpu' or 'memory'
type: '@',
// Defaults to 'bottom'.
// http://c3js.org/reference.html#legend-position
legendPosition: '@?'
},
// Replace the element so it can be centered using class="center-block".
replace: true,
templateUrl: 'views/_quota-usage-chart.html',
link: function($scope, element) {
var usageValue = $filter('usageValue');
var usageWithUnits = $filter('usageWithUnits');
var amountAndUnit = $filter('amountAndUnit');
function updateCenterText() {
var donutChartTitle = d3.select(element[0]).select('text.c3-chart-arcs-title');
if (!donutChartTitle) {
Logger.warn("Can't select donut title element");
return;
}
var replaceText = _.spread(function(amount, unit) {
// Replace donut title content.
donutChartTitle.selectAll('*').remove();
donutChartTitle
.insert('tspan')
.text(amount)
.classed('pod-count donut-title-big-pf', true)
.attr('dy', 0)
.attr('x', 0);
donutChartTitle
.insert('tspan')
.text(unit)
.classed('donut-title-small-pf', true)
.attr('dy', 20)
.attr('x', 0);
});
replaceText(amountAndUnit($scope.total, $scope.type));
}
// Adjust size based on legend position.
if ($scope.legendPosition === 'right') {
$scope.height = 175;
$scope.width = 250;
} else {
$scope.height = 200;
$scope.width = 175;
}
// https://github.com/mbostock/d3/wiki/Formatting
var percentage = d3.format(".2p");
// Chart configuration, see http://c3js.org/reference.html
$scope.chartID = _.uniqueId('quota-usage-chart-');
var config = {
type: "donut",
bindto: '#' + $scope.chartID,
donut: {
label: {
show: false
},
width: 10
},
size: {
height: $scope.height,
width: $scope.width
},
legend: {
show: true,
position: $scope.legendPosition || 'bottom',
item: {
// Don't hide arcs when clicking the legend.
onclick: _.noop
}
},
onrendered: updateCenterText,
tooltip: {
position: function() {
return { top: 0, left: 0 };
},
// Use custom tooltip HTML to avoid problems with content wrapping.
// For example,
//
// <table class="c3-tooltip" style="width: 175px;">
// <tr>
// <td class="name nowrap">
// <span style="background-color: rgb(31, 119, 180);"></span>
// <span>Used</span>
// </td>
// </tr>
// <tr>
// <td class="value" style="text-align: left;">34% of 1 GiB</td>
// </tr>
// </table>
contents: function(d, defaultTitleFormat, defaultValueFormat, color) {
var table = $('<table class="c3-tooltip"></table>')
.css({ width: $scope.width + 'px' });
var trName = $('<tr/>').appendTo(table);
var tdName = $('<td class="name nowrap"></td>').appendTo(trName);
// Color
$('<span/>')
.css({
'background-color': color(d[0].id)
})
.appendTo(tdName);
// Name
$('<span/>')
.text(d[0].name)
.appendTo(tdName);
// Value
var value;
if (!$scope.total) {
value = usageWithUnits($scope.used, $scope.type);
} else {
value = percentage(d[0].value / usageValue($scope.total)) + " of " + usageWithUnits($scope.total, $scope.type);
}
var trValue = $('<tr/>').appendTo(table);
$('<td class="value" style="text-align: left;"></td>')
.text(value)
.appendTo(trValue);
return table.get(0).outerHTML;
}
},
data: {
type: "donut",
// Keep groups in our order.
order: null
}
};
var chart;
var updateChart = function() {
var used = usageValue($scope.used) || 0,
available = Math.max(usageValue($scope.total) - used, 0),
data = {
columns: [
['Used', used],
['Available', available]
],
// https://www.patternfly.org/styles/color-palette/
colors: {
// Orange if at quota, blue otherwise
Used: available ? "#0088ce" : "#ec7a08",
// Gray
Available: "#d1d1d1"
}
};
if (!chart) {
_.assign(config.data, data);
chart = c3.generate(config);
} else {
chart.load(data);
}
};
$scope.$watchGroup(['used', 'total'], _.debounce(updateChart, 300));
}
};
});