Skip to content

Commit ab6d46c

Browse files
committed
support textOverflow in tips with key/value pairs
closes #2321
1 parent 90a5689 commit ab6d46c

File tree

5 files changed

+121
-9
lines changed

5 files changed

+121
-9
lines changed

src/marks/tip.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class Tip extends Mark {
4545
frameAnchor,
4646
format,
4747
textAnchor = "start",
48-
textOverflow,
48+
textOverflow = "ellipsis",
4949
textPadding = 8,
5050
title,
5151
pointerSize = 12,
@@ -90,7 +90,7 @@ export class Tip extends Mark {
9090
const mark = this;
9191
const {x, y, fx, fy} = scales;
9292
const {ownerSVGElement: svg, document} = context;
93-
const {anchor, monospace, lineHeight, lineWidth} = this;
93+
const {anchor, monospace, lineHeight, lineWidth, textOverflow} = this;
9494
const {textPadding: r, pointerSize: m, pathFilter} = this;
9595
const {marginTop, marginLeft} = dimensions;
9696

@@ -185,13 +185,11 @@ export class Tip extends Mark {
185185
title = value.trim();
186186
value = "";
187187
} else {
188-
if (label || (!value && !swatch)) value = " " + value;
189-
const [k] = cut(value, w - widthof(label), widthof, ee);
190-
if (k >= 0) {
191-
// value is truncated
192-
title = value.trim();
193-
value = value.slice(0, k).trimEnd() + ellipsis;
194-
}
188+
const space = label || (!value && !swatch) ? " " : "";
189+
const text = clipper({monospace, lineWidth: lineWidth - widthof(label + space) / 100, textOverflow})(value);
190+
// value is truncated
191+
if (text !== value) title = value.trim();
192+
value = space + text;
195193
}
196194
const line = selection.append("tspan").attr("x", 0).attr("dy", `${lineHeight}em`).text("\u200b"); // zwsp for double-click
197195
if (label) line.append("tspan").attr("font-weight", "bold").text(label);
+31
Loading
+31
Loading
+31
Loading

test/plots/tip.ts

+21
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,27 @@ export async function tipLongText() {
185185
return Plot.tip([{x: "Long sentence that gets cropped after a certain length"}], {x: "x"}).plot();
186186
}
187187

188+
export async function tipLongTextEllipsisEnd() {
189+
return Plot.tip([{x: "Long sentence that gets clipped at the end after a certain length"}], {
190+
x: "x",
191+
textOverflow: "ellipsis" // "ellipsis-end"
192+
}).plot();
193+
}
194+
195+
export async function tipLongTextEllipsisMiddle() {
196+
return Plot.tip([{x: "Long sentence that gets clipped in the middle after a certain length"}], {
197+
x: "x",
198+
textOverflow: "ellipsis-middle"
199+
}).plot();
200+
}
201+
202+
export async function tipLongTextEllipsisStart() {
203+
return Plot.tip([{x: "Long sentence that gets clipped at the start after a certain length"}], {
204+
x: "x",
205+
textOverflow: "ellipsis-start"
206+
}).plot();
207+
}
208+
188209
export async function tipNewLines() {
189210
return Plot.plot({
190211
height: 40,

0 commit comments

Comments
 (0)