@@ -26,6 +26,7 @@ const tooltipComponent = ({ title, isTooltipClickable, items }) => `
2626 *
2727 * @param {Object } context - The chart.js context, typically passed as `this` within chart hooks.
2828 * @param {Object } params - An object containing configuration for the tooltip.
29+ * @param {Object } params.name - A string that serves as an identifier for the tooltip.
2930 * @param {string } params.title - The title text to display in the tooltip.
3031 * @param {Array } params.items - An array of items (with ids) to be displayed inside the tooltip.
3132 * @param {Array } params.onTooltipClick - A callback that receives `tooltipModel` and gets triggered when the tooltip is clicked.
@@ -36,6 +37,7 @@ const tooltipComponent = ({ title, isTooltipClickable, items }) => `
3637 *
3738 * @example
3839 * alignedTooltip(context, {
40+ * name: "my-chart",
3941 * title: "Tooltip Title",
4042 * items: [{ title: "Cost", id: "cost_id" }, { title: "Timestamp", id: "timestamp_id" }],
4143 * onTooltipClick: (tooltipModel) => {
@@ -58,15 +60,15 @@ const tooltipComponent = ({ title, isTooltipClickable, items }) => `
5860 */
5961export const alignedTooltip = (
6062 context ,
61- { title, items, onTooltipClick, onTooltipUpdate }
63+ { name , title, items, onTooltipClick, onTooltipUpdate }
6264) => {
6365 const tooltipModel = context . tooltip ;
64- let tooltipEl = document . getElementById ( " chartjs-tooltip" ) ;
66+ let tooltipEl = document . getElementById ( ` chartjs-tooltip- ${ name } ` ) ;
6567 if ( ! tooltipEl ) {
6668 tooltipEl = document . createElement ( "div" ) ;
6769 tooltipEl . style = "transition: opacity 0.3s;" ;
6870 tooltipEl . style = "transition: left 0.1s;" ;
69- tooltipEl . id = " chartjs-tooltip" ;
71+ tooltipEl . id = ` chartjs-tooltip- ${ name } ` ;
7072 tooltipEl . innerHTML = tooltipComponent ( {
7173 title,
7274 isTooltipClickable : ! ! onTooltipClick ,
@@ -81,6 +83,10 @@ export const alignedTooltip = (
8183 tooltipEl . style . opacity = 0 ;
8284 tooltipEl . style . zIndex = - 1 ;
8385 } ;
86+ // this is needed to maintain responsiveness
87+ window . addEventListener ( "resize" , ( ) => {
88+ tooltipEl . remove ( ) ;
89+ } ) ;
8490 if ( onTooltipClick )
8591 tooltipEl . querySelector ( ".chart-tooltip-dot" ) . onclick = ( ) =>
8692 onTooltipClick ( tooltipModel ) ;
@@ -91,12 +97,6 @@ export const alignedTooltip = (
9197 tooltipEl . style . opacity = 0 ;
9298 return ;
9399 }
94- tooltipEl . classList . remove ( "above" , "below" , "no-transform" ) ;
95- if ( tooltipModel . yAlign ) {
96- tooltipEl . classList . add ( tooltipModel . yAlign ) ;
97- } else {
98- tooltipEl . classList . add ( "no-transform" ) ;
99- }
100100
101101 const values = onTooltipUpdate ( tooltipModel ) ;
102102 items . forEach ( ( item ) => {
@@ -115,5 +115,9 @@ export const alignedTooltip = (
115115 tooltipModel . caretX +
116116 "px" ;
117117 tooltipEl . style . top =
118- position . top + window . scrollY + tooltipModel . caretY + "px" ;
118+ position . top -
119+ tooltipEl . offsetHeight +
120+ window . scrollY +
121+ tooltipModel . caretY +
122+ "px" ;
119123} ;
0 commit comments