React implementation #28
-
I'm having the following question. How can this be included in a React project as a component. Screenshots / Sketches This is my TreeMapChart.js file for reference.
Context
Additional context g |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
const canvas = userRef(null);
const ctx = canvas.current.getContext('2d');
// rest of your stuff
return (
<div>
<canvas ref={canvas}></canvas>
</div>
); Also you'd probably need to place the chart initialization in useEffect hook, so that the elements are created before the chart is initialized. Where does |
Beta Was this translation helpful? Give feedback.
-
I was trying to create a Class Component, that's the reason why |
Beta Was this translation helpful? Give feedback.
-
Forgot import {useEffect, useRef} from 'react';
import Chart from 'chart.js/auto';
import {TreemapController, TreemapElement} from 'chartjs-chart-treemap';
Chart.register(TreemapController, TreemapElement);
export function ExampleChart() {
const canvas = useRef(null);
useEffect(() => {
const ctx = canvas.current.getContext('2d');
const chart = new Chart(ctx, {
type: 'treemap',
data: {
datasets: [{
tree: [1, 2, 3]
}]
}
})
return () => chart.destroy();
}, []);
return <div><canvas ref={canvas}></canvas></div>
} |
Beta Was this translation helpful? Give feedback.
Forgot
.current
from my example. Here is a full one: