Search code examples
reactjscanvaschartsreact-propsreact-chartjs

Re-draw Chart on button React chartjs


I have a bar chart that takes data according to the year. I take the current name of the year from the main component as follows:

App:

<DonateChart year={this.state.active_menu}/>

DonateChart:

<Bar
        data={{
            labels: this.props.year === 2018 ? ['20.01', '20.02'] : [20.03],
            datasets: [{
                label: false,
                fill: true,
                backgroundColor: "#D9D9D9",
                borderCapStyle: 'square',
                pointBorderColor: "white",
                data: this.props.year === 2018 ? ['2222', '3333'] : ['4444'],
                spanGaps: false,
            },
                {
                    label: false,
                    fill: true,
                    backgroundColor: "#00ACE5",
                    borderCapStyle: 'square',
                    pointBorderColor: "white",
                    data: this.props.year === 2018 ? ['2222', '3333'] : ['4444'],
                    spanGaps: false,
                }],
        }}
        options={{
            responsive: false,
            fullCornerRadius: false,
            maintainAspectRatio: false,
            tooltips: {enabled: false},
            hover: {mode: null},
            scales: {
                yAxes: [{
                    stacked: true,
                    ticks: {
                        fontColor: 'black',
                        beginAtZero: true,
                        min: 0,
                        max: 5000,
                        stepSize: 1000,
                        fontFamily: 'HelveticaNeueCyr',
                        fontSize: 10,
                    },
                    gridLines: {
                        color: "rgba(0, 0, 0, 0.1)",
                    }
                }],
                xAxes: [{
                    stacked: true,
                    ticks: {
                        fontColor: '#black',
                        fontFamily: 'HelveticaNeueCyr',
                        fontSize: isMobile ? 8 : 10,
                    },
                    gridLines: {
                        color: "rgba(0, 0, 0, 0)",
                    },
                }]
            },
            legend: {
                display: false
            },
            title: {
                display: false,
            }
        }}/>

But when I switch the year and the new key is in the this.state.year, the chart is not drawn. After that, I tried to create two different charts, and switch between them depending on the state, like this:

const bar1 = <Bar some code />
const bar2 = <Bar some code />
return(
 this.state.year === 2018 ? bar1 : bar2
)

It also did not lead to anything, the schedule is distorted.

Tell me, please, how to implement such functionality correctly. I would be grateful for any advice


Solution

  • In charts there is a regular redrawing method. If i pass into it true, like this: redraw={this.state.update}, everything works fine!