MCPcopy
hub / github.com/reactchartjs/react-chartjs-2 / ChartComponent

Function ChartComponent

src/chart.tsx:14–113  ·  view source on GitHub ↗
(
  props: ChartProps<TType, TData, TLabel>,
  ref: ForwardedRef<ChartJS<TType, TData, TLabel>>
)

Source from the content-addressed store, hash-verified

12} from './utils.js';
13
14function ChartComponent<
15 TType extends ChartType = ChartType,
16 TData = DefaultDataPoint<TType>,
17 TLabel = unknown,
18>(
19 props: ChartProps<TType, TData, TLabel>,
20 ref: ForwardedRef<ChartJS<TType, TData, TLabel>>
21) {
22 const {
23 height = 150,
24 width = 300,
25 redraw = false,
26 datasetIdKey,
27 type,
28 data,
29 options,
30 plugins = [],
31 fallbackContent,
32 updateMode,
33 ...canvasProps
34 } = props;
35 const canvasRef = useRef<HTMLCanvasElement>(null);
36 const chartRef = useRef<ChartJS<TType, TData, TLabel> | null>(null);
37
38 const renderChart = () => {
39 if (!canvasRef.current) return;
40
41 chartRef.current = new ChartJS(canvasRef.current, {
42 type,
43 data: cloneData(data, datasetIdKey),
44 options: options && { ...options },
45 plugins,
46 });
47
48 reforwardRef(ref, chartRef.current);
49 };
50
51 const destroyChart = () => {
52 reforwardRef(ref, null);
53
54 if (chartRef.current) {
55 chartRef.current.destroy();
56 chartRef.current = null;
57 }
58 };
59
60 useEffect(() => {
61 if (!redraw && chartRef.current && options) {
62 setOptions(chartRef.current, options);
63 }
64 }, [redraw, options]);
65
66 useEffect(() => {
67 if (!redraw && chartRef.current) {
68 setLabels(chartRef.current.config.data, data.labels);
69 }
70 }, [redraw, data.labels]);
71

Callers

nothing calls this directly

Calls 5

setOptionsFunction · 0.85
setLabelsFunction · 0.85
setDatasetsFunction · 0.85
destroyChartFunction · 0.85
renderChartFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…