toBase64Image
Hi, I am new to React .
Chart.js instance chartReference = {};
componentDidMount() { console.log(this.chartReference); // returns a Chart.js instance reference }
render() { return (<Doughnut ref={(reference) => this.chartReference = reference } data={data} />) }
how to use this.chartReference to get a base 64 encoded string of the chart in it's current state. I tried this.chartReference.toBase64Image but it is not function error
Got it
could you please explain, how to convert chart into base64 format?
create a ref and use its instant
On Fri, Feb 21, 2020 at 10:50 AM Susmitha-Bogala [email protected] wrote:
could you please explain, how to convert chart into base64 format?
— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/jerairrest/react-chartjs-2/issues/464?email_source=notifications&email_token=AHOOZVVOKZIZTPLNEE3XUHLRD5QBTA5CNFSM4JZMQTPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMRRVFQ#issuecomment-589503126, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHOOZVVPCYCD37DQLGLJMV3RD5QBTANCNFSM4JZMQTPA .
i tried to do the same.but getting "toBase64Image is not a function" this error.
Same problem here, I create a ref and use it. But"toBase64Image is not a function"
Console log that instant ref and search for ref.chartinstance.base64
On Wed, 11 Mar, 2020, 5:59 PM simromm, [email protected] wrote:
Same problem here, I create a ref and use it. But"toBase64Image is not a function"
— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/jerairrest/react-chartjs-2/issues/464#issuecomment-597604876, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHOOZVQTIIA7B4WNFE5XJITRG57ZRANCNFSM4JZMQTPA .
I already did but nothing found. I use also this method => https://github.com/jerairrest/react-chartjs-2#chartjs-instance to get mychartjs instance.
Then console.log(this.chartReference) when the component is mounted.

Email me personally. I ll look into it.
On Wed, 11 Mar, 2020, 6:50 PM simromm, [email protected] wrote:
I already did but nothing founded. I use also this method => https://github.com/jerairrest/react-chartjs-2#chartjs-instance to get mychartjs instance.
Then console.log(this.chartReference) when the component is mounted.
[image: ref] https://user-images.githubusercontent.com/48606574/76420923-2e8c7f00-63a3-11ea-8233-65f795932b74.png
— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/jerairrest/react-chartjs-2/issues/464#issuecomment-597628498, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHOOZVUDZ4MYVPLMUPL527DRG6FYBANCNFSM4JZMQTPA .
You need to get a ChartJS instance reference and then call .toBase64Image(). To do that, first create an empty reference:
const chartRef = useRef(null);
Then, assing the reference. For example, if you are using a Bar chart:
<Bar data={data} ref={chartRef}/>
Finally, get the base64 image using the reference. Remember to call .current.chartInstance before toBase64Image():
const base64Image = chartRef.current.chartInstance.toBase64Image();
I think, at this point of time, react-chart-js has migrated. So, for me, when I did const base64Image = chartRef.current.toBase64Image() ; instead of const base64Image = chartRef.current.chartInstance.toBase64Image(), I could get the values.
But still I when I saved in my harddisk (using react-download-link), and tried to open , it said, file format not supported. I could only open in txt version, but not image
- to prakashsubedi0440 regarding new chart.js version just use directly:
const base64Image = chartRef.current.toBase64Image();
This code worked well for me
const chart = chartRef.current;
const img = chart.toBase64Image('image/jpeg', 1)
console.log(img)
https://codesandbox.io/s/blazing-silence-077xee?file=/App.tsx:1903-1957