[javascript] Recharts에서 동적 데이터를 처리하는 방법은 무엇인가요?

Recharts는 동적 데이터를 처리하기 위한 다양한 방법을 제공합니다. 아래에 동적 데이터를 처리하는 몇 가지 예시를 제공하겠습니다.

  1. setState를 사용하여 데이터 업데이트하기:
    import React, { Component } from 'react';
    import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts';
    
    class DynamicChart extends Component {
      constructor(props) {
        super(props);
        this.state = {
          data: [
            { name: 'A', value: 10 },
            { name: 'B', value: 20 },
            { name: 'C', value: 30 },
          ],
        };
        this.handleDataUpdate = this.handleDataUpdate.bind(this);
      }
    
      handleDataUpdate() {
        // 데이터 업데이트 로직
        const newData = [
          { name: 'A', value: 15 },
          { name: 'B', value: 25 },
          { name: 'C', value: 35 },
          { name: 'D', value: 45 },
        ];
        this.setState({ data: newData });
      }
    
      render() {
        return (
          <div>
            <button onClick={this.handleDataUpdate}>데이터 업데이트</button>
            <LineChart width={400} height={300} data={this.state.data}>
              <Line type="monotone" dataKey="value" stroke="#8884d8" />
              <XAxis dataKey="name" />
              <YAxis />
              <CartesianGrid stroke="#ccc" />
              <Tooltip />
              <Legend />
            </LineChart>
          </div>
        );
      }
    }
    
    export default DynamicChart;
    
  2. useRef를 사용하여 차트 데이터 업데이트하기:
    import React, { useRef, useEffect } from 'react';
    import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts';
    
    const DynamicChart = () => {
      const chartRef = useRef(null);
      const dataRef = useRef([
        { name: 'A', value: 10 },
        { name: 'B', value: 20 },
        { name: 'C', value: 30 },
      ]);
    
      const handleDataUpdate = () => {
        // 데이터 업데이트 로직
        const newData = [
          { name: 'A', value: 15 },
          { name: 'B', value: 25 },
          { name: 'C', value: 35 },
          { name: 'D', value: 45 },
        ];
        dataRef.current = newData;
      };
    
      useEffect(() => {
        chartRef.current && chartRef.current.updateChart();
      }, [dataRef.current]);
    
      return (
        <div>
          <button onClick={handleDataUpdate}>데이터 업데이트</button>
          <DynamicLineChart ref={chartRef} data={dataRef.current} />
        </div>
      );
    };
    
    const DynamicLineChart = React.forwardRef(({ data }, ref) => {
      const chartRef = useRef(null);
    
      useEffect(() => {
        chartRef.current.updateChart = () => {
          chartRef.current.forceUpdate();
        };
      }, []);
    
      return (
        <LineChart width={400} height={300} data={data} ref={chartRef}>
          <Line type="monotone" dataKey="value" stroke="#8884d8" />
          <XAxis dataKey="name" />
          <YAxis />
          <CartesianGrid stroke="#ccc" />
          <Tooltip />
          <Legend />
        </LineChart>
      );
    });
    
    export default DynamicChart;
    

이처럼 setState나 useRef를 활용하여 Recharts에서 동적 데이터를 처리할 수 있습니다. Recharts의 문서를 참고하여 추가로 필요한 기능을 구현할 수도 있습니다.