-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabel.js
More file actions
executable file
·82 lines (66 loc) · 1.88 KB
/
Label.js
File metadata and controls
executable file
·82 lines (66 loc) · 1.88 KB
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
var d3 = require('app/components/d3js.js');
var Label = function (id, name, height, x, y, boxWidth = 0) {
this.name = name
this.id = id
this.x = x
this.y = y
this.width = 0
this.height = height
this.draw = function (graph) {
this.text = graph.append("text")
.attr("transform", "translate(" + this.x + "," + (this.y + this.height - 3) + ")")
.attr("class", "legendText")
.attr("id", this.id)
.style("text-anchor", "start")
.text(this.name)
this.width = this.text.node().getComputedTextLength()
// Set minimum width
if (this.width == 0) {
this.width = 150
}
}
this.addOffset = function (x, y) {
this.x += x
this.y += y
}
this.setX = function (x) {
this.x = x
}
this.setY = function (y) {
this.y = y
}
this.wrap = function (tx, width, height=1) {
var totalLineNumbers = 0
tx.each(function() {
var text = d3.select(this);
var words = text.text().split(/\s+/).reverse();
var line = [];
var lineNumber = 0;
var lineHeight = height;
var tspan = text.text(null).append("tspan").attr("x", 0).attr("y", 0).attr("dy", "0.0em");
if (words.length > 1) {
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("alignment-baseline", "central")
.attr("x", 0)
.attr("y", 0)
.attr("dy", (++lineNumber * lineHeight) + "em")
.text(word);
}
}
} else {
line.push(words[0]);
tspan.text(line.join(" "));
}
totalLineNumbers += lineNumber
});
return totalLineNumbers
}
}
exports.Label = Label