Abhishek Gangwar
Abhishek Gangwar

Reputation: 2408

How to Add button in React Highchart Tooltip?

I am trying to add a button to the High-chart Tool-tip.But I am getting Uncaught ReferenceError: showMoreDetails is not defined at HTMLButtonElement.onclick

Following is the code I have for chart options. I am trying to add tooltip using tooltip formatter. Please let me know if there is a solution to listen to click events on React- highchart tooltip

showMoreDetails = () => {
console.log('showDetails');
}


chartOptions = {
    ...options,
    tooltip: {
      useHTML: true,
      backgroundColor: '#000000',
      borderRadius: 6,
      borderColor: '#000000',
      valueDecimals: 2,
      animation: true,
      style: {
        color: 'white',
        opacity: 0.75,
        shadow: '0px 3px 6px #000000',
        pointerEvents: 'auto'
      },
      hideDelay: 1000,
      formatter() {
        const { point } = this;
       return `<span>
        <span>Rating = ${point.y}</span>
        <button type="button" onclick="showMoreDetails()">More Details</button>
       </span>`
      }
    }
  }

and then I am passing this as options to React High-charts.

On the other hand when I add simple console.log or alert in onclick it works like charm.

<button type="button" onclick="alert('this shows an alert')"> details </button>

Please help me on this. Thanks in advance.

Upvotes: 3

Views: 2477

Answers (1)

ppotaczek
ppotaczek

Reputation: 39099

You need to set showMoreDetails as a global function. Please check below examples with function:


window.showMoreDetails = function() {
  console.log("showMoreDetails");
};

Live demo: https://codesandbox.io/s/highcharts-react-demo-wgznb

Upvotes: 3

Related Questions