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

Adding tooltip to x/y axis tick labels #944

Closed
AJK92 opened this issue Nov 24, 2020 · 5 comments
Closed

Adding tooltip to x/y axis tick labels #944

AJK92 opened this issue Nov 24, 2020 · 5 comments

Comments

@AJK92
Copy link

AJK92 commented Nov 24, 2020

Hi Guys,
I'm trying to add a Tooltip (MaterialUI) on <AxisLeft/> currently using tickComponent. Reason is that I'm getting lengthy characters in y axis and I need to trim it and show. But when the user hovers the axis label, we need to show the entire text.

Doesn't work -

tickComponent={ ({ formattedValue, ...tickProps }) => (
<Tooltip title="Test" arrow><text {...tickProps}>{formattedValue}</text></Tooltip>
)}

But if I remove the Tooltip wrapper, it will work.

Works-

tickComponent={ ({ formattedValue, ...tickProps }) => (
<text {...tickProps}>{formattedValue}</text>
)}

Also, if I don't use <text/> component and the {...tickProps}, then I have to manually code and position all the axis labels, I guess.
So is there any way to achieve this

Any thought on this ?

Thanks,
Abi

@kiranvj
Copy link

kiranvj commented Nov 24, 2020

I guess this happens because you are mixing html (Tooltip component) with the SVG (text). One workaround would be to use the SVG title to show the tooltip

Example

tickComponent={ ({ formattedValue, ...tickProps }) => (
   <text {...tickProps}><title>Your tool tip here</title>{formattedValue}</text>
)}

@williaster
Copy link
Collaborator

I think @kiranvj is probably right that it's failing due to rendering HTML as a child of an SVG element. One alternative solution then would be to render the HTML tooltip inside a react Portal.

We actually export Portal and useTooltipInPortal from @visx/tooltip, I'll see if I can get a working example.

@williaster
Copy link
Collaborator

williaster commented Nov 24, 2020

Here's a working demo. Likely can still optimize the size of the tick mouse target and the tooltip positioning / styles (should be able to use material styles inside the Portal if you need) but illustrates the general approach.

Just to explain the containerRef: in order for TooltipInPortal to position itself correctly, it needs to be able to figure out the needed page-level x/y offset, relative to the parent it will be rendered in. It uses react-use-measure under the hood which requires a ResizeObserver polyfill (see @visx/tooltip docs for more)

Nov-24-2020 11-39-58

@AJK92
Copy link
Author

AJK92 commented Nov 25, 2020

Cool. Thanks @williaster for the quick response. I'll give it a try :)

@williaster
Copy link
Collaborator

Going to close this for now, please feel free to re-open if you have any more questions/issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants