Skip to content

React implementation #28

Answered by kurkle
alvaroserrrano asked this question in Q&A
May 24, 2021 · 3 comments · 1 reply
Discussion options

You must be logged in to vote

Forgot .current from my example. Here is a full one:

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>
}

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@alvaroserrrano
Comment options

Answer selected by kurkle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #26 on May 24, 2021 11:06.