Articles in this section
Category / Section

How to Show Tooltip Template on Button Click Using React Chart?

4 mins read

This article explains how to programmatically show a tooltip template in a React Chart component when a button is clicked. Instead of relying on mouse hover, we simulate a mousemove event at the chart point’s location to trigger the tooltip.

This technique is useful when you want to highlight specific data points based on user actions like button clicks.

Code Example

return (<div className='control-pane'>
                       <ButtonComponent onClick={onClick1.bind(this)} isPrimary={true}>Point 1</ButtonComponent>
               <ChartComponent id='chartTooltip' ref={chart => chartInstance = chart} style={{ textAlign: "center" }} 
                               primaryXAxis={{ valueType: 'Category'}} primaryYAxis={{ title: 'Billion Bushels'}} 
                               tooltip={{ template: tooltipTemplate.bind(this) }} pointRender={onPointRender.bind(this)}>
                              ...
                               </ChartComponent>
       </div>);
   function onChartLoad(args) {
       let chart = document.getElementById('chartTooltip');
       chart.setAttribute('title', '');
   }
   function onPointRender(args){
       pointLocations.push(args.point.symbolLocations[0]);
   }
   function mousemoveEvent(element, sx, sy, cx, cy) {
       let mousemove = document.createEvent('MouseEvent');
       mousemove.initMouseEvent(
         'mousemove',
         true,
         false,
         window,
         1,
         sx,
         sy,
         cx,
         cy,
         false,
         false,
         false,
         false,
         0,
         null
       );
      element.dispatchEvent(mousemove);
     }
   function displayTooltip(location){
       var chart = document.getElementById('chartTooltip');
       var container1Bounds = chart.getBoundingClientRect();
       var areaBounds = chartInstance.initialClipRect;
       mousemoveEvent(
           chart,
           areaBounds.x + location.x,
           areaBounds.y + location.y + container1Bounds.top,
           areaBounds.x + location.x,
           areaBounds.y + location.y + container1Bounds.top
         );
   }
   function onClick1(e) {
      displayTooltip(pointLocations[0]);
   }

Output

image.png

Sample

StackBlitz Sample

Conclusion

I hope you enjoyed learning about how to show tooltip template on button click using React Chart.

You can refer to our React Chart’s feature tour page to learn about its other groundbreaking feature representations. You can also explore our React Chart Documentation to understand how to present and manipulate data.

For current customers, you can check out our React components on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore our React Chart and other React components.

If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied