Skip to content

Commit

Permalink
use dy to position multi-line text (observablehq#1582)
Browse files Browse the repository at this point in the history
* use dy to position multi-line text

* fix blank initial lines
  • Loading branch information
mbostock authored May 18, 2023
1 parent 46301cf commit c8ec5ec
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 139 deletions.
6 changes: 5 additions & 1 deletion src/marks/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@ function applyMultilineText(selection, mark, T, TL) {
const n = lines.length;
const y = lineAnchor === "top" ? 0.71 : lineAnchor === "bottom" ? 1 - n : (164 - n * 100) / 200;
if (n > 1) {
let m = 0;
for (let i = 0; i < n; ++i) {
++m;
if (!lines[i]) continue;
const tspan = this.ownerDocument.createElementNS(namespaces.svg, "tspan");
tspan.setAttribute("x", 0);
tspan.setAttribute("y", `${(y + i) * lineHeight}em`);
if (i === m - 1) tspan.setAttribute("y", `${(y + i) * lineHeight}em`);
else tspan.setAttribute("dy", `${m * lineHeight}em`);
tspan.textContent = lines[i];
this.appendChild(tspan);
m = 0;
}
} else {
if (y) this.setAttribute("y", `${y * lineHeight}em`);
Expand Down
10 changes: 5 additions & 5 deletions test/output/covidIhmeProjectedDeaths.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 26 additions & 26 deletions test/output/letterFrequencyWheel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions test/output/longLabels.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/mobyDick.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/penguinAnnotated.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/penguinFacetAnnotatedX.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c8ec5ec

Please sign in to comment.