| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- /**
- * 钱包概述页面优化版
- * @邠心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 (
- <View style={this.props.shown ? ui.flex1 : styles.hide}>
- { this.props.atAglance &&
- <View style={styles.glanceView}>
- <TextView style={styles.glanceTitle}>{$t('wallet.atAglance')}</TextView>
- <View style={styles.overviewRow}>
- <View style={ui.flex1}>
- <TextView style={styles.valueText}>{this.state.glanceData.averageCharge ?? 0}</TextView>
- <TextView style={styles.titleText}>kWh/{$t('wallet.perWeek')}</TextView>
- <TextView style={styles.subTitleText}>{$t('wallet.averageCharge')}</TextView>
- </View>
- <View style={ui.flex1}>
- <TextView style={styles.valueText}>{this.state.glanceData.averageSpend ?? 0}</TextView>
- <TextView style={styles.titleText}>{currency}/{$t('wallet.perWeek')}</TextView>
- <TextView style={styles.subTitleText}>{$t('wallet.averageSpend')}</TextView>
- </View>
- <View style={ui.flex1}>
- <TextView style={styles.valueText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}</TextView>
- <TextView style={styles.titleText}>{$t('wallet.perHrWeek')}</TextView>
- <TextView style={styles.subTitleText}>{$t('wallet.averageTime')}</TextView>
- </View>
- </View>
- </View>
- }
- <View style={styles.statisticView}>
- <View style={ui.flexcw}>
- <TextView style={styles.sectionTitle}>{$t('wallet.forWeekOf')}</TextView>
- {/* <Text style={styles.linkText}>1st Jan to 8th Jan </Text> */}
- </View>
- <View style={styles.overviewRow}>
- <View style={ui.flex1}>
- {/* <Text style={styles.valueText}>{this.state.glanceData.averageCharge ?? 0}</Text> */}
- <TextView style={styles.titleText}>{this.state.glanceData.averageCharge ?? 0} kWh{/*$t('wallet.perWeek')*/}</TextView>
- <TextView style={styles.subTitleText}>{$t('wallet.averageCharge')}</TextView>
- </View>
- <View style={styles.overviewDivide}></View>
- <View style={ui.flex1}>
- {/* <Text style={styles.valueText}>{this.state.glanceData.averageSpend ?? 0}</Text> */}
- <TextView style={styles.titleText}>{this.state.glanceData.currencySymbol} {this.state.glanceData.averageSpend ?? 0}{/*$t('wallet.perWeek')*/}</TextView>
- <TextView style={styles.subTitleText}>{$t('wallet.averageSpend')}</TextView>
- </View>
- <View style={styles.overviewDivide}></View>
- <View style={ui.flex1}>
- {/* <Text style={styles.valueText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}</Text> */}
- <TextView style={styles.titleText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}{/*$t('wallet.perWeek')*/}</TextView>
- <TextView style={styles.subTitleText}>{$t('wallet.averageTime')}</TextView>
- </View>
- </View>
- </View>
- <View style={ui.flex1}>
- <View style={styles.statisticView}>
- <TextView style={styles.sectionTitle}>{$t('wallet.statistics4week')}</TextView>
- <TextView style={styles.statisticTitle}>{this.state.weekdayData[this.state.weekIndex]?.title}</TextView>
- <View style={styles.barChartView}>
- { this.state.weekdayData.map((item, index) => (
- <View
- style={styles.barChartItem}
- key={index}>
- <Text
- style={styles.barLabelText}
- onPress={() => this.changeWeek(index)}>
- {item.label}
- </Text>
- <Pressable
- style={[
- styles.barChartValue,
- {
- height: this.getWeekHeight(item.y),
- opacity: index == this.state.weekIndex ? 1 : 0.6
- }
- ]}
- onPress={() => this.changeWeek(index)}
- />
- <Text style={styles.barLabelText}>{item.x}</Text>
- </View>
- ))}
- </View>
- </View>
- <View style={styles.statisticView}>
- <TextView style={styles.sectionTitle}>{$t('wallet.statistics4HalfYear')}</TextView>
- <TextView style={styles.statisticTitle}>{this.state.monthData[this.state.monthIndex]?.title}</TextView>
- <View style={styles.barChartView}>
- { this.state.monthData.map((item, index) => (
- <View
- style={styles.barChartItem}
- key={index}>
- <Text
- style={styles.barLabelText}
- onPress={() => this.changeMonth(index)}>
- {item.label}
- </Text>
- <Pressable
- style={[
- styles.barChartValue,
- {
- height: this.getMonthHeight(item.y),
- opacity: index == this.state.monthIndex ? 1 : 0.6
- }
- ]}
- onPress={() => this.changeMonth(index)}
- />
- <Text style={styles.barLabelText}>{item.x}</Text>
- </View>
- ))}
- </View>
- </View>
- </View>
- </View>
- );
- }
- }
- 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'
- },
- 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
- }
- })
|