/
utils.js
61 lines (50 loc) · 1.6 KB
/
utils.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
// Calculate the correct positioning of a bar's label.
function calculateLabelPosition(bar, label) {
var box = label.node().getBBox(),
xpos = Number(bar.attr('x')) + Number(bar.attr('width')) / 2 - box.width / 2,
ypos = (Number(bar.attr('height')) / 2) + (box.height / 4) + 2;
return {
'x': xpos,
'y': ypos
};
}
// Shorten the node's text if it is too wide
function shortenText(node, max_width, partial_allowed) {
if (node.node() === null) { return node; }
var box;
// First try shortening the text to fit inside the box.
if (partial_allowed === true) {
box = node.node().getBBox();
if (box.width > max_width) {
var sub = Math.floor(max_width / box.width * node.text().length) - 3,
name = jQuery.trim(node.text().substring(0, sub)) + '...';
node.text(name);
} else { return node; }
}
// Truncate text to '...' if still too wide.
box = node.node().getBBox();
if (box.width > max_width) {
node.text('...');
} else { return node; }
// Fully truncate text if it is still too wide.
box = node.node().getBBox();
if (box.width > max_width) {
node.text('');
}
return node;
}
function getHours(time) {
return Math.floor(time);
}
function getMinutes(time) {
return Math.floor((time - getHours(time)) * 60);
}
function humanizeTime(time) {
var hours = getHours(time);
minutes = getMinutes(time);
humanized_time = hours + 'h';
if (minutes > 0) {
humanized_time += ' ' + minutes + 'm';
}
return humanized_time;
}