/** * 钱包概述页面 * @邠心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 'victory-native'; import apiWallet from '../../api/apiWallet'; import Svg, { Defs, LinearGradient, Stop } from 'react-native-svg'; import utils from '../../utils/utils'; const chartThemes = "#6672B8"; //配置柱状图颜色 export default class Overview extends Component { constructor(props) { super(props); this.state = { glanceData: {}, monthData: [], weekdayData: [], weekIndex: 0, monthIndex: 0, chartReady: false, }; this.nowDataString = new Date().toDateString() this.refreshing = false; this.enableChartArea = true; } componentDidMount() { this.getOverview(); } componentDidUpdate() { if (this.props.shown) { if (this.props.refresh && !this.refreshing) { this.refreshing = true; this.getOverview(); } else if (!this.state.chartReady) { setTimeout(() => { this.setState({ chartReady: true }) }, 100); } } 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({ chartReady: true }) }); this.stopRefresh(); }).catch(err => { toastShort(err); this.stopRefresh(); }); } stopRefresh() { if (this.props.refreshed) { this.props.refreshed(); } this.refreshing = false; } barTheme = (active) => ({ data: { fill: ({datum, index}) => { return (datum.y > 0 ? (index == active ? colorAccent : chartThemes) : "#999999"); } }, labels: { fontSize: 12, fill: ({index}) => (index == active ? "#000" : "#555") } }) areaTheme = () => ({ data: { //fill: chartThemes, fill: chartThemes, fillOpacity: 0.7, stroke: colorPrimary, strokeWidth: 1 }, labels: { fontSize: 10, fill: textSecondary } }) getFill = ({datum, index}) => (datum.y > 0 ? (index == this.state.monthIndex ? colorPrimary : chartThemes) : "#999999"); render() { return ( { this.props.atAglance && At a glance {this.state.glanceData.averageCharge ?? 0} kWh/Week Average Charge {this.state.glanceData.averageSpend ?? 0} {currency}/Week Average Spend {utils.hour2HHmm(this.state.glanceData.averageTime)} Hr/Week Average Time } For the week of {/* 1st Jan to 8th Jan */} {/* {this.state.glanceData.averageCharge ?? 0} */} {this.state.glanceData.averageCharge ?? 0} kWh/Week Average Charge {/* {this.state.glanceData.averageSpend ?? 0} */} {currency}{this.state.glanceData.averageSpend ?? 0}/Week Average Spend {/* {utils.hour2HHmm(this.state.glanceData.averageTime)} */} {utils.hour2HHmm(this.state.glanceData.averageTime)}/Week Average Time Statistics for this week {this.state.weekdayData[this.state.weekIndex]?.title} { this.state.chartReady && { return [{ target: "data", mutation: (props) => { this.setState({ weekIndex: props.index }); } }]; } } }]} /> } { this.state.monthData.length > 0 && Statistics for the past 6 months {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 }); } }]; } } }]} /> } } ); } } 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 } })