Skip to content

Get Chart Ref

Example of getting ref with the <Chart> component.

App.tsx
import {
useRef,
useEffect
} from 'react'
import {
Chart as ChartJS,
LineController,
LinearScale,
CategoryScale,
BarController,
BarElement,
PointElement,
LineElement,
Legend,
Tooltip
} from 'chart.js'
import { Chart } from 'react-chartjs-2'
import { faker } from '@faker-js/faker'
ChartJS.register(
LineController,
LinearScale,
CategoryScale,
BarController,
BarElement,
PointElement,
LineElement,
Legend,
Tooltip
)
const labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July']
export const data = {
labels,
datasets: [
{
type: 'line' as const,
label: 'Dataset 1',
borderColor: 'rgb(255, 99, 132)',
borderWidth: 2,
fill: false,
data: labels.map(() => faker.number.int({
min: -1000,
max: 1000
}))
},
{
type: 'bar' as const,
label: 'Dataset 2',
backgroundColor: 'rgb(75, 192, 192)',
data: labels.map(() => faker.number.int({
min: -1000,
max: 1000
})),
borderColor: 'white',
borderWidth: 2
},
{
type: 'bar' as const,
label: 'Dataset 3',
backgroundColor: 'rgb(53, 162, 235)',
data: labels.map(() => faker.number.int({
min: -1000,
max: 1000
}))
}
]
}
function triggerTooltip(chart: ChartJS | null) {
const tooltip = chart?.tooltip
if (!tooltip) {
return
}
if (tooltip.getActiveElements().length > 0) {
tooltip.setActiveElements([], {
x: 0,
y: 0
})
} else {
const { chartArea } = chart
tooltip.setActiveElements(
[
{
datasetIndex: 0,
index: 2
},
{
datasetIndex: 1,
index: 2
}
],
{
x: (chartArea.left + chartArea.right) / 2,
y: (chartArea.top + chartArea.bottom) / 2
}
)
}
chart.update()
}
export function App() {
const chartRef = useRef<ChartJS>(null)
useEffect(() => {
const chart = chartRef.current
triggerTooltip(chart)
}, [])
return (
<Chart
ref={chartRef}
type='bar'
data={data}
/>
)
}