Skip to content

Commit efad9d7

Browse files
committed
simplify text layout
1 parent 2261a39 commit efad9d7

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/marks/tooltip.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,29 @@ export class Tooltip extends Mark {
164164
}
165165
}
166166
}
167-
const tspan = content
167+
168+
content
168169
.selectChildren()
169170
.data(text)
170171
.join("tspan")
171172
.attr("x", 0)
172-
.attr("y", (d, i) => `${i * lineHeight}em`);
173-
tspan
174-
.selectChildren()
175-
.data((d) => d.slice(0, 2))
176-
.join("tspan")
177-
.attr("font-weight", (d, i) => (i ? null : "bold"))
178-
.text(String);
179-
tspan
180-
.selectAll("title")
181-
.data((d) => (d.length > 2 ? [d[2]] : []))
182-
.join("title")
183-
.text(String);
173+
.attr("dy", `${lineHeight}em`)
174+
.call((tspan) =>
175+
tspan
176+
.selectChildren()
177+
.data((d) => d.slice(0, 2))
178+
.join("tspan")
179+
.attr("font-weight", (d, i) => (i ? null : "bold"))
180+
.text(String)
181+
)
182+
.call((tspan) =>
183+
tspan
184+
.selectAll("title")
185+
.data((d) => (d.length > 2 ? [d[2]] : []))
186+
.join("title")
187+
.text(String)
188+
);
189+
184190
const {width: w, height: h} = content.node().getBBox();
185191
const {width, height} = svg.getBBox();
186192
if (anchor === undefined) {
@@ -192,9 +198,8 @@ export class Tooltip extends Mark {
192198
const cy = (/^top-/.test(c) ? fitTop || !fitBottom : fitTop && !fitBottom) ? "top" : "bottom";
193199
c = `${cy}-${cx}`;
194200
}
195-
const oy = getLineOffset(c, text, lineHeight);
196-
tspan.attr("y", (d, i) => `${i * lineHeight + oy}em`);
197201
path.attr("d", getPath(c, m, r, w, h));
202+
content.attr("y", `${+getLineOffset(c, text, lineHeight).toFixed(6)}em`);
198203
content.attr("transform", getTextTransform(c, m, r, w, h));
199204
dot.attr("transform", `translate(${Math.round(xi)},${Math.round(yi)})`);
200205
dot.attr("display", "inline"); // make visible only after getBBox
@@ -281,7 +286,7 @@ function getSource2(channels, key) {
281286
}
282287

283288
function getLineOffset(anchor, text, lineHeight) {
284-
return /^top-/.test(anchor) ? 0.94 : -0.29 - (text.length - 1) * lineHeight;
289+
return /^top-/.test(anchor) ? 0.94 - lineHeight : -0.29 - text.length * lineHeight;
285290
}
286291

287292
function getTextTransform(anchor, m, r, width) {

0 commit comments

Comments
 (0)