我获取每个月的数据并将其输出为图表,因此当我更改要显示的年份时,新的将翻转为旧的,即数据不会重置,而只是添加到旧的,我不知道如何解决它,这是文件中可能引起错误的代码。 如果您需要更多信息,我将其发布。
componentWillMount() {
this.props.actions.subscribe(this.subscriptions)
.then(() => {
this.props.actions.fetchRegisters({year: this.state.currentYear, month: defaultMonths()})
.then(() => {
if (!this.props.registers.length) {
this.toaster.warning('There is no data for charts')
}
this.createReportState()
})
})
}
componentWillReceiveProps() {
if (this.isNextWorkspaceChanged()) {
this.props.actions.fetchRegisters({ year: this.state.currentYear, month: defaultMonths() })
.then(() => this.createReportState())
}
}
createInitialState() {
const date = new Date(),
year = date.getFullYear(),
month = date.getMonth() + 1
return {
registers: [],
current: {
year: year,
month: month
},
filter: {
years: []
}
}
}
componentWillUnmount() {
this.props.actions.unsubscribe(this.subscriptions)
}
isNextWorkspaceChanged() {
return this.props.actions.isNextWorkspaceChanged(this.props.nextWorkspace.id)
}
createInitialState() {
let currentYear = new Date().getFullYear()
let initData = new Array(12)
initData.fill(0)
return {
currentYear: currentYear,
chartsData: {
Revenue: Object.assign([], initData),
Cost: Object.assign([], initData),
Profit: Object.assign([], initData)
}
}
}
createReportState() {
const {registers, articles} = this.props
const {currentYear} = this.state
this.state = this.createInitialState()
let chartsData = Object.assign([], this.state.chartsData);
registers.forEach(register => {
let dataNow = new Date(register.date)
let modelMoun = monthsNames[dataNow.getMonth()]
let numModelMoun = dataNow.getMonth()
const article = articles.find(article => article.id === register.article_id)
let modelTypeArticle = article.type
chartsData[modelTypeArticle][numModelMoun] += Math.round(register.value)
chartsData['Profit'][numModelMoun] = Math.round(chartsData['Revenue'][numModelMoun] - chartsData['Cost'][numModelMoun])
})
this.setState({
currentYear,
chartsData: chartsData
});
}