Search code examples
javascriptreactjschartsreact-routerapexcharts

How can I get URI from ApexCharts with React Js


I am trying to get the chart URI by using the Data URI method. I've seen a bunch of examples using Apexchart Js to get pdf like this CodePen from ApexChart, when trying to reproduce it on react I got TypeError: Cannot read property 'type' of undefined

Here is my component did mount look like this:

componentDidMount() {

console.log(this.state.loading);
if (this.state.loading === false) {
  const {options} = this.state;
  const node = ReactDOM.findDOMNode(this);
  if (node instanceof HTMLElement) {
    var chart = new ApexCharts(node.querySelector('.charty'), options);
    chart.render().then(() => {
      setTimeout(function() {
        chart.dataURI().then(uri => {
          console.log(uri);
        });
      }, 4000);
    });
  }
} else {
  return;
}
}

Type is defined as well in my state and in the render like so :

    <div className='rfe'>
      <div className='rfea'>
        From {this.state.AxisMonth[0]} to{' '}
        {this.state.AxisMonth[this.state.AxisMonth.length - 1]}
      </div>
      <Chart
        className='charty'
        type='area'
        options={this.state.options}
        series={this.state.series}
        width='1000'
        height='380'
      />
    </div>
  </div>

here is the error I got

enter image description here

Really need help with this.


Solution

  • You can use the exec function to call any method of ApexCharts from a React component.

     getDataUri() {
        ApexCharts.exec("basic-bar", "dataURI").then(({ imgURI, blob }) => {
          console.log(imgURI);
        });
      }
    

    Here's a full codesandbox example