2017-09-15 6 views
1

나는 반응 차트를 사용하고 있습니다. 나는 챠트 차트를 가지고 있으며, 2 세트의 데이터 사이를 전환 할 수 있기를 원합니다. 태그를 사용하여 드롭 다운을 만들었습니다. 라이브 예를 살펴 : React에서 select 태그를 사용하고 상태와 함께 작업하여 React Charts 데이터 세트간에 전환

https://codesandbox.io/s/mm2vnz6869

드롭 다운으로 이동하고 "수익"로 전환합니다. 원하는대로 다른 데이터 세트로 전환됩니다. 그러나 이제 다시 "지출"로 전환하십시오. 그것이 작동하지 않는 것을주의하십시오. 왜 그럴까요? 누군가 내 논리를 살펴보고 내가 뭘 잘못하고 있는지 알려 줄 수 있습니까? 감사.

import React, { Component } from 'react'; 
import { render } from 'react-dom'; 
import { Line } from 'react-chartjs-2'; 

let lineData; 

const lineDataSpend = { 
    labels: ['March', 'April', 'May', 'June', 'July', 'August', 'September'], 
    datasets: [ 
    { 
     label: 'Spend - Account 1', 
     fill: false, 
     lineTension: 0.1, 
     backgroundColor: 'green', 
     borderColor: 'green', 
     borderCapStyle: 'butt', 
     borderDash: [], 
     borderDashOffset: 0.0, 
     borderJoinStyle: 'miter', 
     pointBorderColor: 'rgba(75,192,192,1)', 
     pointBackgroundColor: '#fff', 
     pointBorderWidth: 1, 
     pointHoverRadius: 5, 
     pointHoverBackgroundColor: 'green', 
     pointHoverBorderColor: 'rgba(220,220,220,1)', 
     pointHoverBorderWidth: 2, 
     pointRadius: 1, 
     pointHitRadius: 10, 
     data: [65, 59, 80, 81, 56, 55, 40] 
    }, 
    { 
     label: 'Spend - Account 2', 
     fill: false, 
     lineTension: 0.1, 
     backgroundColor: 'blue', 
     borderColor: 'blue', 
     borderCapStyle: 'butt', 
     borderDash: [], 
     borderDashOffset: 0.0, 
     borderJoinStyle: 'miter', 
     pointBorderColor: 'rgba(75,192,192,1)', 
     pointBackgroundColor: '#fff', 
     pointBorderWidth: 1, 
     pointHoverRadius: 5, 
     pointHoverBackgroundColor: 'blue', 
     pointHoverBorderColor: 'rgba(220,220,220,1)', 
     pointHoverBorderWidth: 2, 
     pointRadius: 1, 
     pointHitRadius: 10, 
     data: [25, 5, 8, 53, 96, 35, 20] 
    } 
    ] 
}; 

const lineDataRev = { 
    labels: ['March', 'April', 'May', 'June', 'July', 'August', 'September'], 
    datasets: [ 
    { 
     label: 'Revenue - Account 1', 
     fill: false, 
     lineTension: 0.1, 
     backgroundColor: 'red', 
     borderColor: 'red', 
     borderCapStyle: 'butt', 
     borderDash: [], 
     borderDashOffset: 0.0, 
     borderJoinStyle: 'miter', 
     pointBorderColor: 'rgba(75,192,192,1)', 
     pointBackgroundColor: '#fff', 
     pointBorderWidth: 1, 
     pointHoverRadius: 5, 
     pointHoverBackgroundColor: 'red', 
     pointHoverBorderColor: 'rgba(220,220,220,1)', 
     pointHoverBorderWidth: 2, 
     pointRadius: 1, 
     pointHitRadius: 10, 
     data: [27, 9, 37, 31, 102, 42, 19] 
    }, 
    { 
     label: 'Revenue - Account 2', 
     fill: false, 
     lineTension: 0.1, 
     backgroundColor: 'yellow', 
     borderColor: 'yellow', 
     borderCapStyle: 'butt', 
     borderDash: [], 
     borderDashOffset: 0.0, 
     borderJoinStyle: 'miter', 
     pointBorderColor: 'rgba(75,192,192,1)', 
     pointBackgroundColor: '#fff', 
     pointBorderWidth: 1, 
     pointHoverRadius: 5, 
     pointHoverBackgroundColor: 'yellow', 
     pointHoverBorderColor: 'rgba(220,220,220,1)', 
     pointHoverBorderWidth: 2, 
     pointRadius: 1, 
     pointHitRadius: 10, 
     data: [1, 29, 4, 112, 26, 49, 81] 
    } 
    ] 
}; 

lineData = lineDataSpend; //init the graph data to 'Spend' 

class App extends Component { 

    constructor(props) { 
    super(props); 
    this.changeMetric = this.changeMetric.bind(this); 

    this.state = { 
     selectedMetric: 'Spend' 
    }; 
    } 

    changeMetric(event) { 

    this.setState({ 
     selectedMetric: event.target.value 
    }); 

    switch (event.target.value) { 
     case 'Spend': 
     lineData = lineDataSpend; 
     break; 
     case 'Revenue': 
     lineData = lineDataRev; 
     break; 
     default: 
    } 
    } 

    render() { 
    const lineOptions = { 
     title: { 
     display: true, 
     text: 'Account 1 vs Account 2' 
     }, 
     tooltips: { 
     enabled: true, 
     callbacks: { 
      label: function (value, data) { 
      console.log('data', data) 
      const currentLabel = data.datasets[value.datasetIndex].label; 
      return currentLabel + ': ' + '$' + value.yLabel; 
      } 
     } 
     }, 
     legend: { 
     display: true 
     }, 
     maintainAspectRatio: true, 
     scales: { 
     yAxes: [{ 
      ticks: { 
      callback: function (value) { 
       return '$' + parseFloat(value.toFixed(2)); 
      } 
      }, 
      stacked: false, 
      gridLines: { 
      display: true, 
      color: "rgba(255,99,132,0.2)" 
      } 
     }], 
     xAxes: [{ 
      gridLines: { 
      display: false 
      } 
     }] 
     } 

    }; 

    return (
     <div> 


     <select onChange={this.changeMetric} value={this.state.selectedMetric}> 
      <option value="Spend">Spend</option> 
      <option value="Revenue">Revenue</option> 
     </select> 

     <div className="row"> 
      <div className="col-xl-10"> 
      <div className="card"> 
       <div className="card-header"> 
       <i className="fa fa-align-justify" /> 
       </div> 
       <div className="card-block"> 
       <Line data={lineData} options={lineOptions} /> 
       </div> 
      </div> 
      </div> 
     </div> 

     </div> 

    ) 

    } 
} 


render(<App />, document.body); 

답변

2

lineDataSpend에 대한 귀하의 lineData 초기화가 문제의 원인입니다. lineDataSpend의 객체를 보유하는 대신 lineData에 직접 기본 객체를 할당하면 문제가 해결됩니다. 당신이

lineData = lineDataSpend; //init the graph data to 'Spend' 

lineData = { 
labels: ['March', 'April', 'May', 'June', 'July', 'August', 'September'], 
    datasets: [ 
    { 
     label: 'Spend - Account 1', 
     fill: false, 
     lineTension: 0.1, 
     backgroundColor: 'green', 
     borderColor: 'green', 
     borderCapStyle: 'butt', 
     borderDash: [], 
     borderDashOffset: 0.0, 
     borderJoinStyle: 'miter', 
     pointBorderColor: 'rgba(75,192,192,1)', 
     pointBackgroundColor: '#fff', 
     pointBorderWidth: 1, 
     pointHoverRadius: 5, 
     pointHoverBackgroundColor: 'green', 
     pointHoverBorderColor: 'rgba(220,220,220,1)', 
     pointHoverBorderWidth: 2, 
     pointRadius: 1, 
     pointHitRadius: 10, 
     data: [65, 59, 80, 81, 56, 55, 40] 
    }, 
    { 
     label: 'Spend - Account 2', 
     fill: false, 
     lineTension: 0.1, 
     backgroundColor: 'blue', 
     borderColor: 'blue', 
     borderCapStyle: 'butt', 
     borderDash: [], 
     borderDashOffset: 0.0, 
     borderJoinStyle: 'miter', 
     pointBorderColor: 'rgba(75,192,192,1)', 
     pointBackgroundColor: '#fff', 
     pointBorderWidth: 1, 
     pointHoverRadius: 5, 
     pointHoverBackgroundColor: 'blue', 
     pointHoverBorderColor: 'rgba(220,220,220,1)', 
     pointHoverBorderWidth: 2, 
     pointRadius: 1, 
     pointHitRadius: 10, 
     data: [25, 5, 8, 53, 96, 35, 20] 
    } 
    ] 
} 

에, 라인 (103) 변경하는 경우

그래서, 문제가 해결 얻을 것이다. 여기에서 문제를 테스트 할 수 있습니다 - https://codesandbox.io/s/3rko469pkm

하지만 할당이 완벽하게 잘되어있어서 초기 할당이 문제를 일으키는 이유는 분명하지 않습니다. 이 문제의 원인을 더 깊이 파헤쳐 야합니다.

관련 문제