-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Provide out-of-the-box HTML tooltip capability #7195
Comments
You can do that today. See https://www.chartjs.org/docs/latest/configuration/tooltip.html#external-custom-tooltips |
Thank you for the link, I appreciate that there is a workaround for now and I will look into implementing it for my app. On a more long-term discussion, this feels like the custom tooltip workaround you linked should be the default behavior of chart.js. That workaround involves a significant chunk of code for each chart just to render a tooltip outside the canvas (not to mention directly messing with the DOM, which is frowned upon in an Angular app). Are there reasons to avoid that behavior as default? Another potential solution would be to specify the behavior using an option in the ChartOptions legend dictionary to specify the desired behavior. |
It's an interesting thought. It'd probably be much better performance too since redrawing the canvas whenever the mouse is moved is quite expensive |
In terms of integration with other frameworks, having it outside the library is better. I think it should be possible for the tooltip to be defined by the framework and then integrated. If we had to ship angular, react, etc bindings it’d be a lot of bloat. Perhaps this is a plugin opportunity since I’m v3 the tooltip has been converted to a plugin. |
Couldn't we have a default implementation that's just pure DOM though? I'm not sure why we'd necessarily need to integrate with other frameworks. |
Yeah I don't mean to imply that Chart.js should support integrations for the major frameworks - there area couple of libraries out there that wrap chart.js for that purpose (ng2-chart, etc.). My meaning was more along the lines of "it'd be nice to not have to write custom tooltip code that directly manipulates the DOM when I'm working in a framework intended to do that for me". Angular in particular recommends not directly manipulating the DOM since it could conflict with the framework DOM update. A default implementation that is pure DOM would certainly be acceptable from my perspective. I can't comment on v3 stuff since I'm not familiar with it. Thanks for your feedback! |
That's fair, we can look at what would be needed for a DOM version shipped with the library or as a plugin. I looked at plnkr above and I think an HTML tooltip is going to be the only workable solution. Given there are 10 items in the tooltip, I don't think the canvas is tall enough to even render it. |
If you are looking for a quick fix, I have noticed you can avoid tooltip cutoff by adjusting the Option 1) In chart options root level:
Option 2) If you need a title or instruction text, you could add this inside canvas using In chart options root level:
Option 3) If you need In chart options root level:
Note that adjusting the layout might change your actual chart size. You can fix this by adjusting the height in your chart wrapper In template:
And in (s)css:
Here is a fix for the reproducible example mentioned earlier: Other minor adjustments are adjusting the tooltip font size and font family or reducing the tooltip text/lines with custom settings, more about tooltip customization in the documentation https://www.chartjs.org/docs/latest/configuration/tooltip.html. |
maybe just adding a simple property like |
Hi all, glad to see many are thinking about external tooltips. My preference, for UX point of view, would be to have a tooltip defined withing a certain space of the html (e.g. a div box under or aside the chart) that will change content on mouse over (desktop) or finger tip (mobile) on the data point. So that I could load arbitrary length information relate to that data point. Could you maybe offer an example for reference ? |
I am using chartjs 2.9.3 and currently cant find any article online ! can you share your solution @s4m0r4m4 |
Here is a external tooltip that looks like almost exactly like the one from the canvas and also have some basic calculations so it is not outside screen boundaries
|
This issue was created after discussion on #622 (comment). Thanks for your help with this!
Expected Behavior
Tooltips should not get cut off/cropped.
Current Behavior
The tooltip is rendered within the canvas element, which causes it to be cutoff if it extends beyond the canvas element. For instance, I have a horizontal bar chart with a single bar (used for status display), and the tooltip has multiple entries that are cropped out of view.
Possible Solution
I've heard suggestions of rendering the tooltip outside of the canvas so its visible regardless of canvas sizing, but I'm not in a good position to say whether that's the best path forward.
Steps to Reproduce (for bugs)
Here is my reproducible example: https://plnkr.co/edit/QKIivEiOLyanvis0?open=lib%2Fapp.ts&deferRun=1
Hover your cursor over the bar to see the tooltip get cutoff.
Environment
The text was updated successfully, but these errors were encountered: