|
| 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