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

Feature request: toBase64Image

Open volkandkaya opened this issue 9 years ago • 8 comments

The ability to use base64 would be very helpful.

https://github.com/reactjs/react-chartjs/blob/master/lib/core.js

Them seem to have it in extras.

volkandkaya avatar Dec 13 '16 10:12 volkandkaya

Best to do this by getting a ref of the Chart component? That way we can just add a wrapper function to the Chart class that just calls the underlying chartjs function.

andrewkchan avatar May 26 '17 23:05 andrewkchan

I am currently using https://gist.github.com/volkandkaya/a2a34e4a8f7f88a17b4f8aff7d35557c. It is a copy with a lot of the props out.

volkandkaya avatar May 27 '17 07:05 volkandkaya

Actually, I think you can obtain a ref to the internal chartjs chart instance already. See the readme (I'm on mobile so can't link to section):

https://github.com/gor181/react-chartjs-2/blob/master/README.md

And once you have an instance you can just call the .toBase64Image function on it.

andrewkchan avatar May 27 '17 23:05 andrewkchan

You can certainly do this today.

You can get the URL via the animation.onComplete config option like

handleURL = (anim) => {
    const url = this.refs.chart.chart_instance.toBase64Image();
    // do something with the URL
}

you can then pass that to URL up to some component that provides a download link

I have also found you need something like this

componentWillMount = () => {
    // required for png export as per https://github.com/chartjs/Chart.js/issues/2830
    Chart.plugins.register({
        beforeDraw: (chartInstance) => {
            const ctx = chartInstance.chart.ctx;
            ctx.fillStyle = this.props.customChartConfig ? this.toRGBA(this.props.customChartConfig.colours.background) : 'white';
            ctx.fillRect(0, 0, chartInstance.chart.width, chartInstance.chart.height);
        }
    });
};

otherwise you get a black background

3ll3d00d avatar May 29 '17 15:05 3ll3d00d

Hi,

I am facing same issue while using toBase64Image() in react-chart2. Im not getting any data. Can please let me know whether this functionality is available in react-chart2 or not.

i tried this, calling the done() function onComplete of animation. but im getting only "data:,". Can anyone help? done = () => { let chart = this.refs.mychart.chartInstance; console.log(chart.toBase64Image(chart));
}

kumarsunny0013 avatar Dec 03 '18 12:12 kumarsunny0013

It should be written in doc

or

options{
    animation: false
}

option

moffsugita avatar Jan 10 '19 11:01 moffsugita

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

To expand on the response above from @jdgarcia1381 - this is a slightly adapted version of the line chart example that console logs out the base64 image.

import React, { useRef, useEffect } from 'react';
import { Line } from 'react-chartjs-2';

const data = {
  labels: ['1', '2', '3', '4', '5', '6'],
  datasets: [
    {
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      fill: false,
      backgroundColor: 'rgb(255, 99, 132)',
      borderColor: 'rgba(255, 99, 132, 0.2)',
    },
  ],
};

const options = {
  scales: {
    yAxes: [
      {
        ticks: {
          beginAtZero: true,
        },
      },
    ],
  },
};

const LineChart = () => {
  const ref = useRef();

  useEffect(() => {
    console.log(ref.current.chartInstance.toBase64Image());
  }, [ref]);

  return (<Line ref={ref} data={data} options={options} />);
};

export default LineChart;

s16tom avatar Sep 03 '21 15:09 s16tom