reactjs - Highcharts-react(堆积图)未正确更新系列
问题描述
我在使用 highcharts-react 更改堆叠条形图的系列数据时遇到问题 我想要做的是:我制作了一个堆叠条形柱形图,并且我有 3 个按钮用于根据按钮单击过滤数据。
const dropoutByEthnicityOptions = {
chart: {
type: 'bar',
style: {
fontFamily: 'HelveticaNowText-Regular',
color: "#607D8B"
},
height: $(window).width() > 991 ? 450 : 350
},
title: {
text: 'Dropout by Ethnicity',
align: 'left',
},
xAxis: {
categories: ['Albanian','Serbian', 'Turkish', 'Bosnian', 'Gorani', 'Ashkali', 'Roma', 'Egyptian'],
labels: {
style: {
color: '#607D8B',
fontSize: '14px'
}
}
},
yAxis: {
min: 0,
title: {
text: null
}
},
legend: {
reversed: true,
align: 'right',
verticalAlign: 'top',
floating: true
},
tooltip: {
valueSuffix: undefined
},
plotOptions: {
series: {
stacking: 'normal',
pointWidth: 20
}
},
colors: ['#2CD9C5','#60F1C8', '#A4F7CC','#D7FBE8'],
series: this.state.showBasedOnGenderClicked ? this.state.genderData : this.state.totalStudents
};
const dropoutByGradeOptions = {
chart: {
type: 'bar',
style: {
fontFamily: 'HelveticaNowText-Regular',
color: "#607D8B"
},
height: $(window).width() > 991 ? 500 : 400
},
title: {
text: 'Dropout by Grade',
align: 'left',
},
xAxis: {
categories: ['Grade 1','Grade 2', 'Grade 3', 'Grade 4', 'Grade 5', 'Grade 6', 'Grade 7', 'Grade 8', 'Freshman', 'Sophomore', 'Junior', 'Senior', '13th Grade'],
labels: {
style: {
color: '#607D8B',
fontSize: '14px'
}
}
},
yAxis: {
min: 0,
title: {
text: null
}
},
legend: {
reversed: true,
align: 'right',
verticalAlign: 'top',
floating: true
},
tooltip: {
valueSuffix: '%'
},
plotOptions: {
series: {
stacking: 'normal',
pointWidth: 20
}
},
colors: ['#426660', '#2a4d48', '#225c55', '#32968b','#2ca89a', '#24bdab','#1bdec7','#0afce0'],
series: this.state.dropoutByGrade
};
我打电话给Highcharts:
{!this.state.totalDropoutByGradeClicked ?
<div>
<HighchartsReact highcharts={Highcharts} options={dropoutByEthnicityOptions} />
</div>
:
<div>
<HighchartsReact highcharts={Highcharts} options={dropoutByGradeOptions} />
</div> }
问题是,当我第一次单击按钮时,系列数据是正确的,但是在我第二次单击其中一个按钮后,数据没有正确更改。
这是我为按钮单击的系列数据设置状态的代码:
<div className="filter-box">
<button class={this.state.totalDropoutByGradeClicked ? "active" : ""} onClick={this.onDropoutGradesClicked}>Total Dropout by grade</button>
<button class={this.state.totalStudentsByEthnicityClicked ? "active" : ""} onClick={this.onTotalStudentsClicked}>Total Students by ethnicity</button>
<button class={this.state.showBasedOnGenderClicked ? "active" : ""} onClick={this.onGenderClicked}>Dropouts by Gender & Ethnicity</button>
</div>
onDropoutGradesClicked = () => {
this.setState({
totalDropoutByGradeClicked: true,
totalStudentsByEthnicityClicked: false,
showBasedOnGenderClicked: false,
})
}
onGenderClicked = () => {
this.setState({
data: this.state.genderData,
totalDropoutByGradeClicked: false,
totalStudentsByEthnicityClicked: false,
showBasedOnGenderClicked: true
});
}
onTotalStudentsClicked = () => {
this.setState({
data: this.state.totalStudents,
totalDropoutByGradeClicked: false,
totalStudentsByEthnicityClicked: true,
showBasedOnGenderClicked: false
});
}
解决方案
推荐阅读
- database - 如果 postgres 中的某些条件,我如何删除行
- c# - 在这种情况下,将数百万行插入 SQL 表的最快方法是什么?
- python - try/except 语句的最佳解决方案
- sql - 来自 SQL/92 的查询在更高版本中不起作用
- tensorflow - 启动 TF 会话(仅此而已)使用超过 350MB 的 GPU 内存
- java - 将“String*”(Scala)转换为/从“String...”(Java)
- vba - 将 Word 表单域设置为自动格式化为货币
- amazon-product-api - 错误 ActiveContractNotFound 是什么意思 - Amazon Gift Card On Demand (AGCOD)
- ruby-on-rails - 如何在表单中动态添加字段
- amazon-web-services - 如何为 AWS Step Functions 中的活动添加动态 TimeoutSeconds?