Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tooltips should not leave (window) #5004

Closed
jeffrey-roosendaal opened this issue Jan 27, 2017 · 8 comments
Closed

Tooltips should not leave (window) #5004

jeffrey-roosendaal opened this issue Jan 27, 2017 · 8 comments
Labels
stale Inactive for a long time. Will be closed in 7 days.

Comments

@jeffrey-roosendaal
Copy link

jeffrey-roosendaal commented Jan 27, 2017

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

@pissang
Copy link
Contributor

pissang commented Jan 29, 2017

Try https://ecomfe.github.io/echarts-doc/public/en/option.html#tooltip.confine

@jeffrey-roosendaal
Copy link
Author

Thanks for your reply, and pointing me to the English docs. I didn't even know the English docs for eCharts 3 were already available.

Well, confine: true does not work for sparklines, see:

chart_confine

It completely overlaps the chart. Also, one thing I really like about eCharts is the ability for tooltips to be outside of the chart canvas. You do not see that often in charting libraries.

@vatsalpande
Copy link

@JeffreyRoosendaal - were you able to fix this?

@pauturvalles
Copy link

pauturvalles commented Jan 18, 2019

@JeffreyRoosendaal Interested in this as well, it makes no sense to display the tooltip outside of the window.

@Ovilia Ovilia added the stale Inactive for a long time. Will be closed in 7 days. label Aug 3, 2019
@stale stale bot closed this as completed Aug 10, 2019
@AnrichVS
Copy link

Hi,

This remains an issue on the latest version (4.5.0). I am not sure why this was marked as stale, as there is currently no sufficient solution.

@AnrichVS
Copy link

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

@Z-Saffar
Copy link

when you have more than one chart in a row the above solution can not help you. I changed a condition for tooltip position as below :

{
    tooltip: {
        position: function(canvasMousePos: any, params: any, tooltipDom: any, rect: any, sizes: any) {
            debugger;
            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;
            console.log('xPos: ', xPos);
            console.log('yPos: ', yPos);
            console.log('xPos-tooltip: ', xPos - tooltipWidth);

            // The tooltip is overflowing past the right edge of the window
            if (Math.abs(tooltipDom.closest('.GridCardContent').clientWidth - canvasMousePos[0]) < tooltipWidth) {
                // 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 >= tooltipDom.closest('.GridCardContent').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];
        },
    }
}

@molbal
Copy link

molbal commented 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
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Inactive for a long time. Will be closed in 7 days.
Projects
None yet
Development

No branches or pull requests

8 participants