/** * 钱包概述页面优化版 * @邠心vbe on 2024/03/27 */ import React, { Component } from 'react'; import { View, StyleSheet, Text, Pressable } from 'react-native'; import { ElevationObject } from '../../components/Button'; import apiWallet from '../../api/apiWallet'; import utils from '../../utils/utils'; import TextView from '../../components/TextView'; import Dialog from '../../components/Dialog'; const chartThemes = "#A6EB7C"; //配置柱状图颜色 export default class OverviewV2 extends Component { constructor(props) { super(props); this.state = { skeleton: true, glanceData: {}, monthData: [], weekdayData: [], weekIndex: 0, monthIndex: 0, maxWeek: 0, maxMonth: 0 }; this.nowDataString = new Date().toDateString() this.refreshing = false; } componentDidMount() { console.log("概述", this.props); if (!this.props.skeleton) { this.refreshing = true; //Dialog.showProgressDialog(); this.getOverview(); } } componentDidUpdate() { if (this.props.shown && !this.props.skeleton) { if (this.props.refresh && !this.refreshing) { this.refreshing = true; this.getOverview(); } else if (this.state.skeleton && !this.refreshing) { this.refreshing = true; this.getOverview(); } } } getOverview() { apiWallet.getOverviewData().then(res => { var glanceData = {} var weekdayData = [] var monthData = [] var weekIndex = 0 var monthIndex = 0 let maxWeek = 0; let maxMonth = 0; if (res.data) { if (res.data.atAGlance) { glanceData = res.data.atAGlance } if (res.data.statisticsForThisWeek) { res.data.statisticsForThisWeek.forEach((item, index) => { if (this.nowDataString.indexOf(item.x) >= 0) { weekIndex = index; } if (item.y > maxWeek) { maxWeek = item.y; } //item.y += index + 2; item.label = currency + item.y; item.title = item.dateTimeStr + ' | ' + item.label + ' | ' + item.power + 'kW'; weekdayData.push(item); }); } if (res.data.pastSixMonths) { res.data.pastSixMonths.forEach((item, index) => { if (this.nowDataString.indexOf(item.x) >= 0) { monthIndex = index; } //item.y += index + 3; if (item.y > maxMonth) { maxMonth = item.y; } item.label = currency + item.y; item.title = item.x + ' | ' + item.label + ' | ' + item.power + 'kW'; monthData.push(item); }); } } this.setState({ glanceData: glanceData, weekIndex: weekIndex, monthIndex: monthIndex, weekdayData: weekdayData, monthData: monthData, maxWeek: maxWeek, maxMonth: maxMonth }, () => { this.setState({ skeleton: false }, () => { this.stopRefresh(); }) }); }).catch(err => { toastShort(err); this.stopRefresh(); }); } stopRefresh() { if (this.props.refreshed) { this.props.refreshed(); } this.refreshing = false; Dialog.dismissLoading(); } changeWeek(index) { this.setState({ weekIndex: index }); } changeMonth(index) { this.setState({ monthIndex: index }); } getWeekHeight(y) { if (y) { let r = y / this.state.maxWeek * 100; return Math.ceil(r); } else { return 1; } } getMonthHeight(y) { if (y) { let r = y / this.state.maxMonth * 100; return Math.ceil(r); } else { return 1; } } render() { return ( { this.props.atAglance && {$t('wallet.atAglance')} {this.state.glanceData.averageCharge ?? 0} kWh/{$t('wallet.perWeek')} {$t('wallet.averageCharge')} {this.state.glanceData.averageSpend ?? 0} {currency}/{$t('wallet.perWeek')} {$t('wallet.averageSpend')} {utils.hour2HHmm(this.state.glanceData.averageTime)} {$t('wallet.perHrWeek')} {$t('wallet.averageTime')} } {$t('wallet.forWeekOf')} {/* 1st Jan to 8th Jan */} {/* {this.state.glanceData.averageCharge ?? 0} */} {this.state.glanceData.averageCharge ?? 0} kWh{/*$t('wallet.perWeek')*/} {$t('wallet.averageCharge')} {/* {this.state.glanceData.averageSpend ?? 0} */} {this.state.glanceData.currencySymbol} {this.state.glanceData.averageSpend ?? 0}{/*$t('wallet.perWeek')*/} {$t('wallet.averageSpend')} {/* {utils.hour2HHmm(this.state.glanceData.averageTime)} */} {utils.hour2HHmm(this.state.glanceData.averageTime)}{/*$t('wallet.perWeek')*/} {$t('wallet.averageTime')} {$t('wallet.statistics4week')} {this.state.weekdayData[this.state.weekIndex]?.title} { this.state.weekdayData.map((item, index) => ( this.changeWeek(index)}> {item.label} this.changeWeek(index)} /> {item.x} ))} {$t('wallet.statistics4HalfYear')} {this.state.monthData[this.state.monthIndex]?.title} { this.state.monthData.map((item, index) => ( this.changeMonth(index)}> {item.label} this.changeMonth(index)} /> {item.x} ))} ); } } const animate = { duration: 500, onLoad: { duration: 200 } } const axisTheme = { axis: {stroke: "#fff"}, grid: {strokeWidth: 0}, ticks: {size: 0}, tickLabels: {color: '#666', fontSize: 12, padding: 8}, } const styles = StyleSheet.create({ hide: { display: 'none' }, glanceView: { marginTop: 16, marginLeft: 16, marginRight: 16, marginBottom: 4, ...ElevationObject(2), overflow: 'hidden', borderTopLeftRadius: 6, borderTopRightRadius: 6, backgroundColor: colorLight }, glanceTitle: { color: textPrimary, fontSize: 16, paddingTop: 4, paddingLeft: 16, paddingBottom: 4, backgroundColor: '#C4C8DF' }, overviewRow: { paddingTop: 16, paddingBottom: 10, alignItems: 'center', flexDirection: 'row' }, overviewDivide: { width: 1, height: 12, backgroundColor: '#D9D9D9' }, valueText: { color: '#000', fontSize: 22, paddingBottom: 8, textAlign: 'center' }, titleText: { color: textPrimary, fontSize: 14, textAlign: 'center' }, subTitleText: { color: textCancel, fontSize: 12, textAlign: 'center' }, statisticView: { marginTop: 0, paddingBottom: 16, backgroundColor: colorLight }, sectionTitle: { color: textPrimary, fontSize: 14, paddingLeft: 16, fontWeight: 'bold' }, sectionTitleV2: { flex: 1, color: textPrimary, fontSize: 14, marginLeft: 16, marginRight: 16, paddingBottom: 4, fontWeight: 'bold', textTransform: 'uppercase', borderBottomWidth: 1, borderBottomColor: colorPrimary }, statisticTitle: { color: textPrimary, fontSize: 14, paddingTop: 24, paddingBottom: 8, textAlign: 'center' }, statisticChart: { height: 120 }, linkText: { ...ui.link, fontSize: 14, paddingRight: 16 }, barChartView: { height: 200, padding: 16, alignItems: 'flex-end', flexDirection: 'row', justifyContent: 'space-between' }, barChartItem: { alignItems: 'center' }, barLabelText: { padding: 4, fontSize: 12, textAlign: 'center' }, barChartValue: { width: 24, minHeight: 1, backgroundColor: colorAccent } })