重新绘制图表上的按钮React Chartjs



我有一个条形图,该条形图根据年份获取数据。我从主要组成部分中获取当前年度名称:

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,
            }
        }}/>

但是,当我切换年份并且新密钥在this.state.year中时,图表并未绘制。之后,我尝试创建两个不同的图表,并根据状态进行切换,例如:

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

它也没有导致任何事情,时间表被扭曲。

请告诉我,如何正确实施此类功能。我会感谢任何建议

在图表中有一种常规的重划线方法。如果我传递给true,则像这样:redraw={this.state.update},一切正常!

最新更新