Skip to content

Commit 5850da0

Browse files
Filmbostock
andauthored
tip: respect empty lines (#1579)
* tip: respect empty lines closes #1578 * set value to a space if name and value are empty * tweak * fix test snapshot --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent c8ec5ec commit 5850da0

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

src/marks/tip.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,26 +181,25 @@ export class Tip extends Mark {
181181
// exact text metrics and translate the text as needed once we know the
182182
// tip’s orientation (anchor).
183183
function renderLine(selection, name, value, color) {
184-
if (name) name = "\u200b" + name; // zwsp for double-click
185184
let title;
186185
let w = lineWidth * 100;
187186
const [j] = cut(name, w, widthof, ee);
188187
if (j >= 0) {
189188
// name is truncated
190189
name = name.slice(0, j).trimEnd() + ellipsis;
191-
value = "";
192190
title = value.trim();
191+
value = "";
193192
} else {
194-
if (name) value = " " + value;
193+
if (name || (!value && !color)) value = " " + value;
195194
const [k] = cut(value, w - widthof(name), widthof, ee);
196195
if (k >= 0) {
197196
// value is truncated
198197
value = value.slice(0, k).trimEnd() + ellipsis;
199198
title = value.trim();
200199
}
201200
}
202-
const line = selection.append("tspan").attr("x", 0).attr("dy", `${lineHeight}em`);
203-
line.append("tspan").attr("font-weight", "bold").text(name);
201+
const line = selection.append("tspan").attr("x", 0).attr("dy", `${lineHeight}em`).text("\u200b"); // zwsp for double-click
202+
if (name) line.append("tspan").attr("font-weight", "bold").text(name);
204203
if (value) line.append(() => document.createTextNode(value));
205204
if (color) line.append("tspan").text(" ■").attr("fill", color).style("user-select", "none");
206205
if (title) line.append("title").text(title);

test/output/tipNewLines.svg

Lines changed: 48 additions & 0 deletions
Loading

test/plots/tip.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,29 @@ export async function tipRule() {
144144
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
145145
return Plot.ruleX(penguins, {x: "body_mass_g", tip: true}).plot();
146146
}
147+
148+
export async function tipNewLines() {
149+
return Plot.plot({
150+
height: 40,
151+
style: "overflow: visible;",
152+
x: {axis: "top", label: null},
153+
marks: [
154+
Plot.tip(
155+
[
156+
{x: "after", label: `Hello\n\n`},
157+
{x: "before", label: `\n\nWorld`},
158+
{x: "between", label: `{\n\n}`}
159+
],
160+
{
161+
x: "x",
162+
anchor: "top",
163+
title: "label"
164+
}
165+
),
166+
Plot.tip([{x: "no name"}], {
167+
x: "x",
168+
channels: {a: ["first"], b: ["second"], "": [""]}
169+
})
170+
]
171+
});
172+
}

0 commit comments

Comments
 (0)