react-chartjs-2 icon indicating copy to clipboard operation
react-chartjs-2 copied to clipboard

toBase64Image

Open singhqwerty6 opened this issue 6 years ago • 12 comments

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

singhqwerty6 avatar Dec 11 '19 10:12 singhqwerty6

Got it

singhqwerty6 avatar Dec 11 '19 12:12 singhqwerty6

could you please explain, how to convert chart into base64 format?

Susmitha-Bogala avatar Feb 21 '20 05:02 Susmitha-Bogala

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 .

singhqwerty6 avatar Feb 21 '20 07:02 singhqwerty6

i tried to do the same.but getting "toBase64Image is not a function" this error.

Susmitha-Bogala avatar Feb 26 '20 05:02 Susmitha-Bogala

Same problem here, I create a ref and use it. But"toBase64Image is not a function"

simromm avatar Mar 11 '20 12:03 simromm

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 .

singhqwerty6 avatar Mar 11 '20 13:03 singhqwerty6

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.

ref

simromm avatar Mar 11 '20 13:03 simromm

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 .

singhqwerty6 avatar Mar 11 '20 14:03 singhqwerty6

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();

jdgarcia1381 avatar Aug 05 '20 23:08 jdgarcia1381

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

prakashsubedi0440 avatar May 28 '21 08:05 prakashsubedi0440

  • to prakashsubedi0440 regarding new chart.js version just use directly:

const base64Image = chartRef.current.toBase64Image();

HurYur avatar Jun 21 '21 07:06 HurYur

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

kindyALkindy avatar Mar 21 '22 04:03 kindyALkindy