Skip to content

Commit 11e9326

Browse files
committed
same logic for the vertical case + tests
1 parent 94d8bd7 commit 11e9326

7 files changed

Lines changed: 174 additions & 119 deletions

File tree

src/marks/tip.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ export class Tip extends Mark {
240240
? "left"
241241
: fitRight
242242
? "right"
243+
: fitTop
244+
? "top"
245+
: fitBottom
246+
? "bottom"
243247
: mark.preferredAnchor;
244248
}
245249
const path = this.firstChild; // note: assumes exactly two children!

test/output/tipAnchorOverflowY.svg

Lines changed: 29 additions & 0 deletions
Loading

test/output/tipAnchorPositions.svg

Lines changed: 65 additions & 0 deletions
Loading

test/output/tipAnchors.svg

Lines changed: 0 additions & 98 deletions
This file was deleted.

test/plots/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ import "./text-overflow.js";
318318
import "./this-is-just-to-say.js";
319319
import "./tick-format.js";
320320
import "./time-axis.js";
321-
import "./tip-anchor-overflow.js";
321+
import "./tip-anchor.js";
322322
import "./tip-format.js";
323323
import "./tip.js";
324324
import "./title.js";

test/plots/tip-anchor-overflow.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/plots/tip-anchor.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as Plot from "@observablehq/plot";
2+
import {test} from "test/plot";
3+
4+
// Test tip anchor selection near the right edge of the chart.
5+
test(async function tipAnchorOverflow() {
6+
const plot = Plot.rectX([1, 1, 1, 1, 1], {
7+
x: Plot.identity,
8+
fill: Plot.indexOf,
9+
title: () =>
10+
"Lorem ipsum lorem ipsum lorem ipsum Lorem ipsum lorem ipsum lorem ipsum Lorem ipsum lorem ipsum lorem ipsum",
11+
tip: true
12+
}).plot({height: 80, marginTop: 20, axis: null});
13+
plot.dispatchEvent(
14+
new PointerEvent("pointermove", {
15+
pointerType: "mouse",
16+
clientX: 580,
17+
clientY: 30
18+
})
19+
);
20+
return Object.assign(plot, {ready: new Promise((resolve) => setTimeout(resolve, 100))});
21+
});
22+
23+
// Test tip anchor selection near the bottom edge of the chart.
24+
test(async function tipAnchorOverflowY() {
25+
const plot = Plot.rectY([1, 1, 1, 1, 1], {
26+
y: Plot.identity,
27+
fill: Plot.indexOf,
28+
title: () =>
29+
"Lorem ipsum lorem ipsum lorem ipsum Lorem ipsum lorem ipsum lorem ipsum Lorem ipsum lorem ipsum lorem ipsum",
30+
tip: {lineWidth: 5}
31+
}).plot({width: 80, marginLeft: 20, axis: null});
32+
plot.dispatchEvent(
33+
new PointerEvent("pointermove", {
34+
pointerType: "mouse",
35+
clientX: 30,
36+
clientY: 20
37+
})
38+
);
39+
return Object.assign(plot, {ready: new Promise((resolve) => setTimeout(resolve, 100))});
40+
});
41+
42+
// Test tip anchor selection at all 9 positions across the chart.
43+
test(async function tipAnchorPositions() {
44+
const data = [];
45+
for (const px of [40, 320, 600]) {
46+
for (const py of [20, 100, 180]) {
47+
data.push({px, py});
48+
}
49+
}
50+
const plot = Plot.plot({
51+
width: 640,
52+
height: 200,
53+
axis: null,
54+
inset: 20,
55+
marks: [
56+
Plot.dot(data, {x: "px", y: "py", r: 5, fill: "currentColor"}),
57+
Plot.tip(data, {
58+
x: "px",
59+
y: "py",
60+
title: () => "A tip that is wide enough to test anchor fitting behavior across positions"
61+
})
62+
]
63+
});
64+
// Dispatch a pointer event to trigger all tips
65+
for (const {px, py} of data) {
66+
plot.dispatchEvent(
67+
new PointerEvent("pointermove", {
68+
pointerType: "mouse",
69+
clientX: px,
70+
clientY: py
71+
})
72+
);
73+
}
74+
return Object.assign(plot, {ready: new Promise((resolve) => setTimeout(resolve, 100))});
75+
});

0 commit comments

Comments
 (0)