You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React中集成chartjs-plugin-waterfall实现瀑布图组件的报错求助

Fixing React + Chart.js + chartjs-plugin-waterfall Rendering Errors

Hey there, let's tackle those two errors you're facing with your React waterfall chart component. I'll break down each issue and show you how to fix them step by step:

1. "Canvas is already in use. Chart with ID '2' must be destroyed before the canvas can be reused."

What's going wrong?

Your current code uses a regular let chart = false variable and includes chart in the useEffect dependency array. Since you reassign chart inside the useEffect, this creates an infinite loop: every time chart updates, the effect runs again, creates a new Chart instance, and tries to reuse the same canvas without properly cleaning up the old one. Also, using document.getElementById in React can lead to race conditions where the canvas element isn't ready when you try to access it.

How to fix it:

  • Use useRef to store the Chart instance (this keeps the reference stable across re-renders)
  • Access the canvas element via a React ref instead of getElementById
  • Add a proper cleanup function in useEffect to destroy the chart when the component unmounts or re-renders
  • Remove chart from the useEffect dependency array (since we're using a ref, we don't need it as a dependency)

2. "Uncaught TypeError: Cannot set properties of undefined (setting 'filter')"

What's going wrong?

This error usually stems from one of two issues:

  • A version mismatch between Chart.js and chartjs-plugin-waterfall (the plugin might expect a different Chart.js version than what you're using)
  • Incorrect configuration in the waterFallPlugin options—specifically, the stepLines settings might be referencing properties the plugin doesn't recognize, leading it to try accessing an undefined object.

How to fix it:

  • First, double-check that your Chart.js and chartjs-plugin-waterfall versions are compatible. Most newer versions of the plugin require Chart.js 3.x or higher, so make sure your packages are up to date or matched correctly.
  • Simplify your stepLines configuration to default values first, then reintroduce your custom settings one by one to pinpoint which property is causing the issue.

Modified Working Code

Here's the revised component incorporating all these fixes:

import { useEffect, useRef } from 'react';
import Chart from 'chart.js/auto';
import waterFallPlugin from 'chartjs-plugin-waterfall';

const WaterFallType = () => {
  // Use refs to store chart instance and canvas element
  const chartRef = useRef(null);
  const canvasRef = useRef(null);

  useEffect(() => {
    // Clean up existing chart if it exists
    if (chartRef.current instanceof Chart) {
      chartRef.current.destroy();
    }

    // Exit if canvas ref isn't available yet
    if (!canvasRef.current) return;

    const ctx = canvasRef.current.getContext('2d');
    chartRef.current = new Chart(ctx, {
      type: "bar",
      plugins: [waterFallPlugin],
      data: {
        datasets: [
          { label: 'Closing Costs', data: [50], backgroundColor: '#e8cdd7', stack: 'stack 1', },
          { label: 'Purchase Price', data: [700], backgroundColor: '#d29baf', stack: 'stack 1', },
          { data: [200], waterfall: { dummyStack: true, }, stack: 'stack 2', },
          { label: 'Opening Loan Balance', data: [550], backgroundColor: '#bb6987', stack: 'stack 2', },
          { label: 'Initial Cash Investment', data: [200], backgroundColor: '#a53860', stack: 'stack 3', },
        ],
      },
      options: {
        plugins: {
          waterFallPlugin: {
            stepLines: {
              enabled: true,
              startColorStop: 0,
              endColorStop: 0.6,
              startColor: 'rgba(0, 0, 0, 0.55)',
              endColor: 'rgba(0, 0, 0, 0)',
              diagonalStepLines: true,
            },
          },
        },
      }
    });

    // Cleanup function to destroy chart when component unmounts or re-renders
    return () => {
      if (chartRef.current instanceof Chart) {
        chartRef.current.destroy();
      }
    };
  }, []); // Empty dependency array: runs once on mount, cleans up on unmount

  return (
    <div>
      <canvas ref={canvasRef} width="400px" height="400px"></canvas>
    </div>
  );
};

export default WaterFallType;

Additional Checks

  • Make sure you've installed all required packages correctly:
    npm install chart.js react-chartjs-2 chartjs-plugin-waterfall
    
  • If the filter error still persists, try registering the plugin globally instead of passing it in the plugins array:
    Chart.register(waterFallPlugin);
    
  • Test with a minimal stepLines configuration first (e.g., just enabled: true) to rule out configuration issues.

内容的提问来源于stack exchange,提问作者Benjamin Beugnet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:52:43