/** * 钱包概述页面 * @邠心vbe on 2021/05/08 */ import React, { Component } from 'react'; import { View, Text, StyleSheet, TouchableOpacity } from 'react-native'; import { ElevationObject } from '../../components/Button'; import {VictoryAxis, VictoryBar, VictoryArea, VictoryChart, VictoryTheme, VictoryLabel} from 'react-native'; import apiWallet from '../../api/apiWallet'; import Svg, { Defs, LinearGradient, Stop } from 'react-native-svg'; import utils from '../../utils/utils'; import TextView from '../../components/TextView'; import Dialog from '../../components/Dialog'; const chartThemes = "#A6EB7C"; //配置柱状图颜色 export default class Overview extends Component { constructor(props) { super(props); this.state = { skeleton: true, glanceData: {}, monthData: [], weekdayData: [], weekIndex: 0, monthIndex: 0, chartReady: false, }; this.nowDataString = new Date().toDateString() this.refreshing = false; this.enableChartArea = true; } componentDidMount() { console.log("概述", this.props); Dialog.showProgressDialog(); if (!this.props.skeleton) { this.refreshing = true; 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(); } else if (!this.state.chartReady) { setTimeout(() => { this.setState({ chartReady: true }) }, 500); } } else if (this.state.chartReady) { this.setState({ chartReady: false }) } } getOverview() { apiWallet.getOverviewData().then(res => { var glanceData = {} var weekdayData = [] var monthData = [] var weekIndex = 0 var monthIndex = 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; } //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; item.y0 = 0; item.label = currency + item.y; item.title = item.x + ' | ' + item.label + ' | ' + item.power + 'kW'; /*if (this.enableChartArea) { if (index == 0) { item.label = " " + currency + item.y } else if (index == res.data.pastSixMonths.length - 1) { item.label = currency + item.y + " " } item.y += (item.y * 10 + 5); }*/ monthData.push(item); }); } } this.setState({ glanceData: glanceData, weekIndex: weekIndex, monthIndex: monthIndex, weekdayData: weekdayData, monthData: monthData }, () => { this.setState({ skeleton: false, chartReady: true }, () => { this.stopRefresh(); }) }); }).catch(err => { toastShort(err); this.stopRefresh(); }); } stopRefresh() { if (this.props.refreshed) { this.props.refreshed(); } this.refreshing = false; setTimeout(() => { Dialog.dismissLoading(); }, 1000); } barTheme = (active) => ({ data: { fill: ({datum, index}) => { return (datum.y > 0 ? (index == active ? colorAccent : chartThemes) : "#999999"); } }, labels: { fontSize: 12, fill: ({index}) => (index == active ? "#000" : textSecondary) } }) barSpacesTheme = () => ({ data: { fill: "transparent", }, labels: { fontSize: 12, fill: textSecondary } }) areaTheme = () => ({ data: { fill: chartThemes, fillOpacity: 0.7, stroke: colorPrimary, strokeWidth: 1 }, labels: { fontSize: 1, fill: "transparent"//textSecondary } }) getFill = ({datum, index}) => (datum.y > 0 ? (index == this.state.monthIndex ? colorAccent : chartThemes) : "#999999"); 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.chartReady && { return [{ target: "data", mutation: (props) => { this.setState({ weekIndex: props.index }); } }]; } } }]} /> } { this.state.skeleton && {$t('wallet.statistics4HalfYear')} } { this.state.monthData.length > 0 && {$t('wallet.statistics4HalfYear')} {this.state.monthData[this.state.monthIndex].title} {/* */} { this.enableChartArea ? { this.state.chartReady && datum.y} labelComponent={} /> { return [{ target: "data", mutation: (props) => { this.setState({ monthIndex: props.index }); } }]; } } }]} /> } : { return [{ target: "data", mutation: (props) => { this.setState({ monthIndex: props.index }); } }]; } } }]} /> } } ); } } 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, paddingTop: 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 } })