Overview.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /**
  2. * 钱包概述页面
  3. * @邠心vbe on 2021/05/08
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
  7. import { ElevationObject } from '../../components/Button';
  8. import {VictoryAxis, VictoryBar, VictoryArea, VictoryChart, VictoryTheme, VictoryLabel} from 'victory-native';
  9. import apiWallet from '../../api/apiWallet';
  10. import Svg, { Defs, LinearGradient, Stop } from 'react-native-svg';
  11. import utils from '../../utils/utils';
  12. import TextView from '../../components/TextView';
  13. import Dialog from '../../components/Dialog';
  14. const chartThemes = "#A6EB7C"; //配置柱状图颜色
  15. export default class Overview extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. skeleton: true,
  20. glanceData: {},
  21. monthData: [],
  22. weekdayData: [],
  23. weekIndex: 0,
  24. monthIndex: 0,
  25. chartReady: false,
  26. };
  27. this.nowDataString = new Date().toDateString()
  28. this.refreshing = false;
  29. this.enableChartArea = true;
  30. }
  31. componentDidMount() {
  32. console.log("概述", this.props);
  33. Dialog.showProgressDialog();
  34. if (!this.props.skeleton) {
  35. this.refreshing = true;
  36. this.getOverview();
  37. }
  38. }
  39. componentDidUpdate() {
  40. if (this.props.shown && !this.props.skeleton) {
  41. if (this.props.refresh && !this.refreshing) {
  42. this.refreshing = true;
  43. this.getOverview();
  44. } else if (this.state.skeleton && !this.refreshing) {
  45. this.refreshing = true;
  46. this.getOverview();
  47. } else if (!this.state.chartReady) {
  48. setTimeout(() => {
  49. this.setState({
  50. chartReady: true
  51. })
  52. }, 500);
  53. }
  54. } else if (this.state.chartReady) {
  55. this.setState({
  56. chartReady: false
  57. })
  58. }
  59. }
  60. getOverview() {
  61. apiWallet.getOverviewData().then(res => {
  62. var glanceData = {}
  63. var weekdayData = []
  64. var monthData = []
  65. var weekIndex = 0
  66. var monthIndex = 0
  67. if (res.data) {
  68. if (res.data.atAGlance) {
  69. glanceData = res.data.atAGlance
  70. }
  71. if (res.data.statisticsForThisWeek) {
  72. res.data.statisticsForThisWeek.forEach((item, index) => {
  73. if (this.nowDataString.indexOf(item.x) >= 0) {
  74. weekIndex = index;
  75. }
  76. //item.y += index + 2;
  77. item.label = currency + item.y;
  78. item.title = item.dateTimeStr + ' | ' + item.label + ' | ' + item.power + 'kW';
  79. weekdayData.push(item);
  80. });
  81. }
  82. if (res.data.pastSixMonths) {
  83. res.data.pastSixMonths.forEach((item, index) => {
  84. if (this.nowDataString.indexOf(item.x) >= 0) {
  85. monthIndex = index;
  86. }
  87. //item.y += index + 3;
  88. item.y0 = 0;
  89. item.label = currency + item.y;
  90. item.title = item.x + ' | ' + item.label + ' | ' + item.power + 'kW';
  91. /*if (this.enableChartArea) {
  92. if (index == 0) {
  93. item.label = " " + currency + item.y
  94. } else if (index == res.data.pastSixMonths.length - 1) {
  95. item.label = currency + item.y + " "
  96. }
  97. item.y += (item.y * 10 + 5);
  98. }*/
  99. monthData.push(item);
  100. });
  101. }
  102. }
  103. this.setState({
  104. glanceData: glanceData,
  105. weekIndex: weekIndex,
  106. monthIndex: monthIndex,
  107. weekdayData: weekdayData,
  108. monthData: monthData
  109. }, () => {
  110. this.setState({
  111. skeleton: false,
  112. chartReady: true
  113. })
  114. });
  115. this.stopRefresh();
  116. }).catch(err => {
  117. toastShort(err);
  118. this.stopRefresh();
  119. });
  120. }
  121. stopRefresh() {
  122. if (this.props.refreshed) {
  123. this.props.refreshed();
  124. }
  125. this.refreshing = false;
  126. setTimeout(() => {
  127. Dialog.dismissLoading();
  128. }, 300);
  129. }
  130. barTheme = (active) => ({
  131. data: {
  132. fill: ({datum, index}) => {
  133. return (datum.y > 0 ? (index == active ? colorAccent : chartThemes) : "#999999");
  134. }
  135. },
  136. labels: {
  137. fontSize: 12,
  138. fill: ({index}) => (index == active ? "#000" : textSecondary)
  139. }
  140. })
  141. barSpacesTheme = () => ({
  142. data: {
  143. fill: "transparent",
  144. },
  145. labels: {
  146. fontSize: 12,
  147. fill: textSecondary
  148. }
  149. })
  150. areaTheme = () => ({
  151. data: {
  152. fill: chartThemes,
  153. fillOpacity: 0.7,
  154. stroke: colorPrimary,
  155. strokeWidth: 1
  156. },
  157. labels: {
  158. fontSize: 1,
  159. fill: "transparent"//textSecondary
  160. }
  161. })
  162. getFill = ({datum, index}) => (datum.y > 0 ? (index == this.state.monthIndex ? colorAccent : chartThemes) : "#999999");
  163. render() {
  164. return (
  165. <View style={this.props.shown ? ui.flex1 : styles.hide}>
  166. { this.props.atAglance &&
  167. <View style={styles.glanceView}>
  168. <TextView style={styles.glanceTitle}>{$t('wallet.atAglance')}</TextView>
  169. <View style={styles.overviewRow}>
  170. <View style={ui.flex1}>
  171. <TextView style={styles.valueText}>{this.state.glanceData.averageCharge ?? 0}</TextView>
  172. <TextView style={styles.titleText}>kWh/{$t('wallet.perWeek')}</TextView>
  173. <TextView style={styles.subTitleText}>{$t('wallet.averageCharge')}</TextView>
  174. </View>
  175. <View style={ui.flex1}>
  176. <TextView style={styles.valueText}>{this.state.glanceData.averageSpend ?? 0}</TextView>
  177. <TextView style={styles.titleText}>{currency}/{$t('wallet.perWeek')}</TextView>
  178. <TextView style={styles.subTitleText}>{$t('wallet.averageSpend')}</TextView>
  179. </View>
  180. <View style={ui.flex1}>
  181. <TextView style={styles.valueText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}</TextView>
  182. <TextView style={styles.titleText}>{$t('wallet.perHrWeek')}</TextView>
  183. <TextView style={styles.subTitleText}>{$t('wallet.averageTime')}</TextView>
  184. </View>
  185. </View>
  186. </View>
  187. }
  188. <View style={styles.statisticView}>
  189. <View style={ui.flexcw}>
  190. <TextView style={styles.sectionTitle}>{$t('wallet.forWeekOf')}</TextView>
  191. {/* <Text style={styles.linkText}>1st Jan to 8th Jan </Text> */}
  192. </View>
  193. <View style={styles.overviewRow}>
  194. <View style={ui.flex1}>
  195. {/* <Text style={styles.valueText}>{this.state.glanceData.averageCharge ?? 0}</Text> */}
  196. <TextView style={styles.titleText}>{this.state.glanceData.averageCharge ?? 0} kWh{/*$t('wallet.perWeek')*/}</TextView>
  197. <TextView style={styles.subTitleText}>{$t('wallet.averageCharge')}</TextView>
  198. </View>
  199. <View style={styles.overviewDivide}></View>
  200. <View style={ui.flex1}>
  201. {/* <Text style={styles.valueText}>{this.state.glanceData.averageSpend ?? 0}</Text> */}
  202. <TextView style={styles.titleText}>{this.state.glanceData.currencySymbol} {this.state.glanceData.averageSpend ?? 0}{/*$t('wallet.perWeek')*/}</TextView>
  203. <TextView style={styles.subTitleText}>{$t('wallet.averageSpend')}</TextView>
  204. </View>
  205. <View style={styles.overviewDivide}></View>
  206. <View style={ui.flex1}>
  207. {/* <Text style={styles.valueText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}</Text> */}
  208. <TextView style={styles.titleText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}{/*$t('wallet.perWeek')*/}</TextView>
  209. <TextView style={styles.subTitleText}>{$t('wallet.averageTime')}</TextView>
  210. </View>
  211. </View>
  212. </View>
  213. <View style={ui.flex1}>
  214. <View style={styles.statisticView}>
  215. <TextView style={styles.sectionTitle}>{$t('wallet.statistics4week')}</TextView>
  216. <TextView style={styles.statisticTitle}>{this.state.weekdayData[this.state.weekIndex]?.title}</TextView>
  217. <Svg height={200}>
  218. { this.state.chartReady &&
  219. <VictoryChart
  220. theme={VictoryTheme.material}
  221. animate={animate}
  222. height={200}
  223. padding={{top: 50, left: 32, right: 60, bottom: 50}}>
  224. <VictoryAxis style={axisTheme}/>
  225. <VictoryBar
  226. barWidth={25}
  227. style={this.barTheme(this.state.weekIndex)}
  228. data={this.state.weekdayData}
  229. events={[{
  230. target: "data",
  231. eventHandlers: {
  232. onPress: () => {
  233. return [{
  234. target: "data",
  235. mutation: (props) => {
  236. this.setState({
  237. weekIndex: props.index
  238. });
  239. }
  240. }];
  241. }
  242. }
  243. }]}
  244. />
  245. </VictoryChart>
  246. }
  247. </Svg>
  248. </View>
  249. { this.state.skeleton &&
  250. <View style={styles.statisticView}>
  251. <TextView style={styles.sectionTitle}>{$t('wallet.statistics4HalfYear')}</TextView>
  252. <View style={{height: 200}}></View>
  253. </View>
  254. }
  255. { this.state.monthData.length > 0 &&
  256. <View style={styles.statisticView}>
  257. <TextView style={styles.sectionTitle}>{$t('wallet.statistics4HalfYear')}</TextView>
  258. <TextView style={styles.statisticTitle}>{this.state.monthData[this.state.monthIndex].title}</TextView>
  259. {/* <svg style={{height: 0}}>
  260. <defs>
  261. <linearGradient id="myGradient" gradientUnits="userSpaceOnUse">
  262. <stop stopColor={chartThemes} stopOpacity="0.8"/>
  263. <stop offset="1" stopColor={chartThemes} stopOpacity="0"/>
  264. </linearGradient>
  265. </defs>
  266. </svg> */}
  267. { this.enableChartArea
  268. ? <Svg height={200}>
  269. { this.state.chartReady &&
  270. <VictoryChart
  271. theme={VictoryTheme.material}
  272. height={200}
  273. padding={{top: 50, left: 30, right: 60, bottom: 50}}>
  274. <VictoryAxis style={axisTheme}/>
  275. <VictoryArea
  276. style={this.areaTheme()}
  277. data={this.state.monthData}
  278. theme={VictoryTheme.material}
  279. labels={({ datum }) => datum.y}
  280. labelComponent={<VictoryLabel dy={10}/>}
  281. />
  282. <VictoryBar
  283. barWidth={28}
  284. data={this.state.monthData}
  285. style={this.barSpacesTheme()}
  286. events={[{
  287. target: "data",
  288. eventHandlers: {
  289. onPress: () => {
  290. return [{
  291. target: "data",
  292. mutation: (props) => {
  293. this.setState({
  294. monthIndex: props.index
  295. });
  296. }
  297. }];
  298. }
  299. }
  300. }]}
  301. />
  302. </VictoryChart>
  303. }
  304. </Svg>
  305. : <Svg height={200}>
  306. <VictoryChart
  307. theme={VictoryTheme.material}
  308. height={200}
  309. padding={{top: 50, left: 30, right: 62, bottom: 50}}>
  310. <VictoryAxis style={axisTheme}/>
  311. <VictoryBar
  312. barWidth={28}
  313. data={this.state.monthData}
  314. style={this.barTheme(this.state.monthIndex)}
  315. events={[{
  316. target: "data",
  317. eventHandlers: {
  318. onPress: () => {
  319. return [{
  320. target: "data",
  321. mutation: (props) => {
  322. this.setState({
  323. monthIndex: props.index
  324. });
  325. }
  326. }];
  327. }
  328. }
  329. }]}
  330. />
  331. </VictoryChart>
  332. </Svg>
  333. }
  334. </View>
  335. }
  336. </View>
  337. </View>
  338. );
  339. }
  340. }
  341. const animate = {
  342. duration: 500,
  343. onLoad: {
  344. duration: 200
  345. }
  346. }
  347. const axisTheme = {
  348. axis: {stroke: "#fff"},
  349. grid: {strokeWidth: 0},
  350. ticks: {size: 0},
  351. tickLabels: {color: '#666', fontSize: 12, padding: 8},
  352. }
  353. const styles = StyleSheet.create({
  354. hide: {
  355. display: 'none'
  356. },
  357. glanceView: {
  358. marginTop: 16,
  359. marginLeft: 16,
  360. marginRight: 16,
  361. marginBottom: 4,
  362. ...ElevationObject(2),
  363. overflow: 'hidden',
  364. borderTopLeftRadius: 6,
  365. borderTopRightRadius: 6,
  366. backgroundColor: colorLight
  367. },
  368. glanceTitle: {
  369. color: textPrimary,
  370. fontSize: 16,
  371. paddingTop: 4,
  372. paddingLeft: 16,
  373. paddingBottom: 4,
  374. backgroundColor: '#C4C8DF'
  375. },
  376. overviewRow: {
  377. paddingTop: 16,
  378. paddingBottom: 10,
  379. alignItems: 'center',
  380. flexDirection: 'row'
  381. },
  382. overviewDivide: {
  383. width: 1,
  384. height: 12,
  385. backgroundColor: '#D9D9D9'
  386. },
  387. valueText: {
  388. color: '#000',
  389. fontSize: 22,
  390. paddingBottom: 8,
  391. textAlign: 'center'
  392. },
  393. titleText: {
  394. color: textPrimary,
  395. fontSize: 14,
  396. textAlign: 'center'
  397. },
  398. subTitleText: {
  399. color: textCancel,
  400. fontSize: 12,
  401. textAlign: 'center'
  402. },
  403. statisticView: {
  404. marginTop: 0,
  405. paddingTop: 16,
  406. backgroundColor: colorLight
  407. },
  408. sectionTitle: {
  409. color: textPrimary,
  410. fontSize: 14,
  411. paddingLeft: 16,
  412. fontWeight: 'bold'
  413. },
  414. statisticTitle: {
  415. color: textPrimary,
  416. fontSize: 14,
  417. paddingTop: 24,
  418. paddingBottom: 8,
  419. textAlign: 'center'
  420. },
  421. statisticChart: {
  422. height: 120
  423. },
  424. linkText: {
  425. ...ui.link,
  426. fontSize: 14,
  427. paddingRight: 16
  428. }
  429. })