Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add export functionality to custom button in React highcharts?

I have this project in which I am trying to implement export highchart functionality from out side of the chart.

Is there any way I can achieve that? I am using React highcharts and the download formats are Jpeg and CSV.

Thanks in advance

like image 319
Abhishek Gangwar Avatar asked Mar 22 '26 00:03

Abhishek Gangwar


2 Answers

I found an solution using functional components:

First import the modules:

import * as Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';

require('highcharts/modules/exporting')(Highcharts);
require('highcharts/modules/export-data')(Highcharts);

Then create a ref to the chart:

  const chart = useRef();
....
....
<HighchartsReact ref={chart} highcharts={Highcharts} options={chartOptions} />

Then create a method like this one, triggered by a click event:

  const downloadCSV = () => {
    if (chart && chart.current && chart.current.chart) {
      chart.current.chart.downloadCSV();
    }
  };
like image 71
Yannick Avatar answered Mar 23 '26 18:03

Yannick


From @Yannick response... This works for me.

const chart = useRef(null);  <-------- important
....
....
<HighchartsReact ref={chart} highcharts={Highcharts} options={chartOptions} />
like image 43
Wilson Arteaga Avatar answered Mar 23 '26 17:03

Wilson Arteaga