question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tooltips should not leave (window)

See original GitHub issue

echarts_screen

When using a chart near the edge of your browser window, it may get cut off. I’ve tried setting confine: true, but this does not give the desired effect, since then it will be completely isolated in the chart div. This may not be a problem for large charts, but for small sparkline charts it is a problem.

Since I think this is unintended behaviour (I can’t think of a reason why a tooltip would display outside of your window), I think this is a bug.

I am using the latest eCharts (3.4.0) with Google Chrome (55.0.2) on Windows 10

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
AnrichVScommented, Nov 29, 2019

For anyone else struggling with this, I’ve written a function for the position callback that solves this. It first attempts to place the tool-tip to the top right of the mouse position. If it overflows to the right, it flips it to the left. If it then overflows to the left, it places it a fixed distance from the left edge of the window (since there’s no better placement on the X axis at this point). And vice versa for top and bottom placement, with the fallback here placing it a fixed distance from the top edge of the window.

Notes:

  • The efficiency of this can probably be improved
  • Feel free to make a pull request incorporating this into the default tool-tip position logic. Please comment here if you do, because I might do it myself at a later stage.
{
    tooltip: {
        position: function (canvasMousePos, params, tooltipDom, rect, sizes) {
            var margin = 10; // How far away from the mouse should the tooltip be
            var overflowMargin = 5; // If no satisfactory position can be found, how far away from the edge of the window should the tooltip be kept

            // The chart canvas position
            var canvasRect = tooltipDom.parentElement.getElementsByTagName('canvas')[0].getBoundingClientRect();

            // The mouse coordinates relative to the whole window
            // The first parameter to the position function is the mouse position relative to the canvas
            var mouseX = canvasMousePos[0] + canvasRect.x;
            var mouseY = canvasMousePos[1] + canvasRect.y;

            // The width and height of the tooltip dom element
            var tooltipWidth = sizes.contentSize[0];
            var tooltipHeight = sizes.contentSize[1];

            // Start by placing the tooltip top and right relative to the mouse position
            var xPos = mouseX + margin;
            var yPos = mouseY - margin - tooltipHeight;

            // The tooltip is overflowing past the right edge of the window
            if (xPos + tooltipWidth >= document.documentElement.clientWidth) {

                // Attempt to place the tooltip to the left of the mouse position
                xPos = mouseX - margin - tooltipWidth;

                // The tooltip is overflowing past the left edge of the window
                if (xPos <= 0)

                    // Place the tooltip a fixed distance from the left edge of the window
                    xPos = overflowMargin;
            }

            // The tooltip is overflowing past the top edge of the window
            if (yPos <= 0) {

                // Attempt to place the tooltip to the bottom of the mouse position
                yPos = mouseY + margin;

                // The tooltip is overflowing past the bottom edge of the window
                if (yPos + tooltipHeight >= document.documentElement.clientHeight)

                    // Place the tooltip a fixed distance from the top edge of the window
                    yPos = overflowMargin;
            }

            // Return the position (converted back to a relative position on the canvas)
            return [xPos - canvasRect.x, yPos - canvasRect.y]
        }
    }
}
6reactions
molbalcommented, May 11, 2021

I know its an old ticket, but its high in Google results. eCharts 3 now has a new property which does the job perfectly.

{
    tooltip: {
        position: 'top',
        confine: true
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent CSS tooltip from going out of page/window
Unfortunately, if you want to keep your tooltip good positioning, it isn't possible using only CSS. Script solutions.
Read more >
Tooltips should not leave (window) · Issue #5004 - GitHub
I've tried setting confine: true , but this does not give the desired effect, since then it will be completely isolated in the...
Read more >
A tooltip is stuck on the desktop and will not go away.
I stumbled across a potential solution in my daily usage. Try pressing [Windows Key] + [D]. This should minimize all the windows and...
Read more >
Tooltip Guidelines - Nielsen Norman Group
Provide brief and helpful content inside the tooltip.​​ Additionally, lengthy content is no longer a 'tip', so keep it brief. Tooltips are ...
Read more >
How to identify which program has left a portion of its UI ...
Find the tab it came from and just move your mouse around. The tooltip should disappear. (Unfortuantely, there's no easy way to determine...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found