-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
148 lines (136 loc) · 4.35 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
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
// TODO refactor with crop and wrap
function fade(text, width) {
text.each(function(d) {
var padding = 10;
var text = d3.select(this),
words = text.text().split("").reverse(),
word,
line = [],
lineNumber = 0,
x = text.attr("x"),
y = text.attr("y"),
dy = 0, //parseFloat(text.attr("dy")),
tspan = text.text(null)
.append("tspan")
.attr("x", x)
.attr("y", y)
.attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(""));
if (tspan.node().getComputedTextLength() > width) {
line.pop()
tspan.text(line.join(""));
text.style("fill", "url(#text-fade)")
}
}
});
}
function crop(text, width) {
text.each(function(d) {
var padding = 10;
// TODO wish there was a cleaner way to pass the width in to this
// function
var width = width || (d.x1 - d.x0 - padding);
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
x = text.attr("x"),
y = text.attr("y"),
dy = 0, //parseFloat(text.attr("dy")),
tspan = text.text(null)
.append("tspan")
.attr("x", x)
.attr("y", y)
.attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop()
tspan.text(line.join(" "));
}
}
});
}
function wrap(text, width, lineHeight) {
lineHeight = lineHeight || 1.1; // ems
text.each(function(d) {
var padding = 10;
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
x = text.attr("x"),
y = text.attr("y"),
dy = 0, //parseFloat(text.attr("dy")),
tspan = text.text(null)
.append("tspan")
.attr("x", x)
.attr("y", y)
.attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan")
.attr("x", 0)
.attr("y", y)
.attr("dy", ++lineNumber * lineHeight + dy + "em")
.text(word);
}
}
});
}
function unique(x) {
return x.reverse().filter(function (e, i, x) {return x.indexOf(e, i+1) === -1;}).reverse();
}
var rand_human_fmt = function(x) {
if (x >= 1000000000) {
return rand_fmt(x / 1000000000) + " billion"
} else if (x >= 1000000) {
return rand_fmt(x / 1000000) + " million"
} else {
return rand_fmt(x)
}
}
var rand_fmt = function(x) {
return "R" + d3.format(",.0f")(x);
}
var slugify = function(x) {
return x.replace(/\s+/g, "-").toLowerCase()
}
var getViewportDimensions = function() {
// Dynamically get the size of the viewport
var baseWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var baseHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
return {
width: baseWidth,
height: baseHeight
}
}
var getDimensions = function(el) {
rect = el.node().getBoundingClientRect();
return {
x: rect.x || rect.left,
y: rect.y || rect.top,
width: rect.width,
height: rect.height
}
}
var createBoundingBox = function(container, el) {
var d = getDimensions(el);
var box = container.append("rect")
.attr("x", d.x)
.attr("y", d.y)
.attr("width", d.width)
.attr("height", d.height)
.classed("bounding-box", true)
return box
}