coreui / coreui-free-react-admin-template

Open source admin template based on Bootstrap 5 and React.js
https://coreui.io/product/free-react-admin-template/
MIT License
4.6k stars 2.33k forks source link

Chart reference for @coreui/react-chartjs (useRef returns undefined) #335

Closed tamojay-dey closed 2 years ago

tamojay-dey commented 2 years ago

I have tried to capture the reference of a react chart but it returns undefined. <CCardBody> <CChart ref={chartRef} id="react-chart" type="line" datasets={finaldata} options={options} labels={Xaxis} /> </CCardBody>

and defining chartRef as: const chartRef = useRef()

I thought it could be because of the fact that it lacks react-chartjs-2 dependency as told here. but it is still returning undefined. I am using Core UI 3.2. Any suggestion how to get the reference? I need the chart reference to save it to a base64image.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

james-e-morris commented 2 years ago

You can pass innerRef to props, which gives you a ref to the div surrounding the chart.js canvas. You can also access the ref through some handler functions. For example, I use animation onComplete() to get my ref and base64 image:

options.animation = {
    onComplete: (chartElement) => {
        const base64Img = chartElement.chartInstance.toBase64Image();
    }
}