-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtext.js
168 lines (155 loc) · 4.42 KB
/
text.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
export function text(selection, width, height, options = {}) {
let position = options.position ? options.position : "topright";
let text = options.text ? options.text : "Your text here!";
let fontSize = options.fontSize ? options.fontSize : 15;
let fontFamily = options.fontFamily ? options.fontFamily : "Roboto";
let margin = options.margin ? options.margin : 0;
let textDecoration = options.textDecoration ? options.textDecoration : "none";
let fontWeight = options.fontWeight ? options.fontWeight : "normal";
let fontStyle = options.fontStyle ? options.fontStyle : "normal";
let anchor = options.anchor ? options.anchor : "start"; // start, middle, end
let baseline = options.baseline ? options.baseline : "baseline"; // baseline, middle, hanging
let fill = options.fill ? options.fill : "#474342";
let stroke = options.stroke ? options.stroke : "none";
let frame_fill = options.frame_fill ? options.frame_fill : "none";
let frame_stroke = options.frame_stroke ? options.frame_stroke : "none";
let frame_strokeWidth = options.strokeWidth ? options.strokeWidth : 1;
let frame_opacity = options.frame_opacity ? options.frame_opacity : 1;
let x;
let y;
switch (position) {
case "topleft":
anchor = "start";
baseline = "hanging";
x = 5;
y = 5;
break;
case "topright":
anchor = "end";
baseline = "hanging";
x = width - 5;
y = 5;
break;
case "top":
anchor = "middle";
baseline = "hanging";
x = width / 2;
y = 5;
break;
case "left":
anchor = "start";
baseline = "middle";
x = 5;
y = height / 2;
break;
case "middle":
anchor = "middle";
baseline = "middle";
x = width / 2;
y = height / 2;
break;
case "right":
anchor = "end";
baseline = "middle";
x = width - 5;
y = height / 2;
break;
case "bottomleft":
anchor = "start";
baseline = "baseline";
x = 5;
y = height - 5;
break;
case "bottom":
anchor = "middle";
baseline = "baseline";
x = width / 2;
y = height - 5;
break;
case "bottomright":
anchor = "end";
baseline = "baseline";
x = width - 5;
y = height - 5;
break;
default:
x = position[0];
y = position[1];
}
let margin_x;
let margin_y;
let delta;
let delta2;
let txt = text.split("\n");
let count = [];
txt.forEach((e) => count.push(e.length));
let i = count.indexOf(Math.max(...count));
let tmp = selection
.append("text")
.attr("font-size", `${fontSize}px`)
.attr("font-family", fontFamily)
.attr("font-style", fontStyle)
.attr("text-decoration", textDecoration)
.attr("font-weight", fontWeight)
.text(txt[i]);
selection.node().appendChild(tmp.node());
document.body.appendChild(selection.node());
const w = tmp.node().getBBox().width;
document.body.removeChild(selection.node());
tmp.remove();
let h = fontSize * count.length;
if (baseline == "hanging") {
delta = 0;
margin_y = margin;
}
if (baseline == "middle") {
delta = (fontSize * txt.length) / 2;
margin_y = 0;
}
if (baseline == "baseline") {
delta = fontSize * txt.length;
margin_y = -margin;
}
if (anchor == "start") {
delta2 = 0;
margin_x = margin;
}
if (anchor == "middle") {
delta2 = w / 2;
margin_x = 0;
}
if (anchor == "end") {
delta2 = w;
margin_x = -margin;
}
let l = selection
.append("g")
.attr("class", options.id)
.attr("data-layer", JSON.stringify({ _type: "text" }));
l.append("rect")
.attr("x", x - margin - delta2 + margin_x)
.attr("y", y - margin - delta + margin_y)
.attr("height", h + margin * 2)
.attr("width", w + margin * 2)
.attr("fill", frame_fill)
.attr("stroke-width", frame_strokeWidth)
.attr("fill-opacity", frame_opacity)
.attr("stroke", frame_stroke);
l.selectAll("text")
.data(txt)
.join("text")
.attr("x", x + margin_x)
.attr("y", y - +delta + margin_y)
.attr("font-size", `${fontSize}px`)
.attr("font-style", fontStyle)
.attr("text-decoration", textDecoration)
.attr("font-weight", fontWeight)
.attr("font-family", fontFamily)
.attr("dy", (d, i) => i * fontSize)
.attr("text-anchor", anchor)
.attr("dominant-baseline", "hanging")
.attr("fill", fill)
.attr("stroke", stroke)
.text((d) => d);
//return svg.node();
}