OverviewV2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * 钱包概述页面优化版
  3. * @邠心vbe on 2024/03/27
  4. */
  5. import React, { Component } from 'react';
  6. import { View, StyleSheet, Text, Pressable } from 'react-native';
  7. import { ElevationObject } from '../../components/Button';
  8. import apiWallet from '../../api/apiWallet';
  9. import utils from '../../utils/utils';
  10. import TextView from '../../components/TextView';
  11. import Dialog from '../../components/Dialog';
  12. const chartThemes = "#A6EB7C"; //配置柱状图颜色
  13. export default class OverviewV2 extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. skeleton: true,
  18. glanceData: {},
  19. monthData: [],
  20. weekdayData: [],
  21. weekIndex: 0,
  22. monthIndex: 0,
  23. maxWeek: 0,
  24. maxMonth: 0
  25. };
  26. this.nowDataString = new Date().toDateString()
  27. this.refreshing = false;
  28. }
  29. componentDidMount() {
  30. console.log("概述", this.props);
  31. if (!this.props.skeleton) {
  32. this.refreshing = true;
  33. //Dialog.showProgressDialog();
  34. this.getOverview();
  35. }
  36. }
  37. componentDidUpdate() {
  38. if (this.props.shown && !this.props.skeleton) {
  39. if (this.props.refresh && !this.refreshing) {
  40. this.refreshing = true;
  41. this.getOverview();
  42. } else if (this.state.skeleton && !this.refreshing) {
  43. this.refreshing = true;
  44. this.getOverview();
  45. }
  46. }
  47. }
  48. getOverview() {
  49. apiWallet.getOverviewData().then(res => {
  50. var glanceData = {}
  51. var weekdayData = []
  52. var monthData = []
  53. var weekIndex = 0
  54. var monthIndex = 0
  55. let maxWeek = 0;
  56. let maxMonth = 0;
  57. if (res.data) {
  58. if (res.data.atAGlance) {
  59. glanceData = res.data.atAGlance
  60. }
  61. if (res.data.statisticsForThisWeek) {
  62. res.data.statisticsForThisWeek.forEach((item, index) => {
  63. if (this.nowDataString.indexOf(item.x) >= 0) {
  64. weekIndex = index;
  65. }
  66. if (item.y > maxWeek) {
  67. maxWeek = item.y;
  68. }
  69. //item.y += index + 2;
  70. item.label = currency + item.y;
  71. item.title = item.dateTimeStr + ' | ' + item.label + ' | ' + item.power + 'kW';
  72. weekdayData.push(item);
  73. });
  74. }
  75. if (res.data.pastSixMonths) {
  76. res.data.pastSixMonths.forEach((item, index) => {
  77. if (this.nowDataString.indexOf(item.x) >= 0) {
  78. monthIndex = index;
  79. }
  80. //item.y += index + 3;
  81. if (item.y > maxMonth) {
  82. maxMonth = item.y;
  83. }
  84. item.label = currency + item.y;
  85. item.title = item.x + ' | ' + item.label + ' | ' + item.power + 'kW';
  86. monthData.push(item);
  87. });
  88. }
  89. }
  90. this.setState({
  91. glanceData: glanceData,
  92. weekIndex: weekIndex,
  93. monthIndex: monthIndex,
  94. weekdayData: weekdayData,
  95. monthData: monthData,
  96. maxWeek: maxWeek,
  97. maxMonth: maxMonth
  98. }, () => {
  99. this.setState({
  100. skeleton: false
  101. }, () => {
  102. this.stopRefresh();
  103. })
  104. });
  105. }).catch(err => {
  106. toastShort(err);
  107. this.stopRefresh();
  108. });
  109. }
  110. stopRefresh() {
  111. if (this.props.refreshed) {
  112. this.props.refreshed();
  113. }
  114. this.refreshing = false;
  115. Dialog.dismissLoading();
  116. }
  117. changeWeek(index) {
  118. this.setState({
  119. weekIndex: index
  120. });
  121. }
  122. changeMonth(index) {
  123. this.setState({
  124. monthIndex: index
  125. });
  126. }
  127. getWeekHeight(y) {
  128. if (y) {
  129. let r = y / this.state.maxWeek * 100;
  130. return Math.ceil(r);
  131. } else {
  132. return 1;
  133. }
  134. }
  135. getMonthHeight(y) {
  136. if (y) {
  137. let r = y / this.state.maxMonth * 100;
  138. return Math.ceil(r);
  139. } else {
  140. return 1;
  141. }
  142. }
  143. render() {
  144. return (
  145. <View style={this.props.shown ? ui.flex1 : styles.hide}>
  146. { this.props.atAglance &&
  147. <View style={styles.glanceView}>
  148. <TextView style={styles.glanceTitle}>{$t('wallet.atAglance')}</TextView>
  149. <View style={styles.overviewRow}>
  150. <View style={ui.flex1}>
  151. <TextView style={styles.valueText}>{this.state.glanceData.averageCharge ?? 0}</TextView>
  152. <TextView style={styles.titleText}>kWh/{$t('wallet.perWeek')}</TextView>
  153. <TextView style={styles.subTitleText}>{$t('wallet.averageCharge')}</TextView>
  154. </View>
  155. <View style={ui.flex1}>
  156. <TextView style={styles.valueText}>{this.state.glanceData.averageSpend ?? 0}</TextView>
  157. <TextView style={styles.titleText}>{currency}/{$t('wallet.perWeek')}</TextView>
  158. <TextView style={styles.subTitleText}>{$t('wallet.averageSpend')}</TextView>
  159. </View>
  160. <View style={ui.flex1}>
  161. <TextView style={styles.valueText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}</TextView>
  162. <TextView style={styles.titleText}>{$t('wallet.perHrWeek')}</TextView>
  163. <TextView style={styles.subTitleText}>{$t('wallet.averageTime')}</TextView>
  164. </View>
  165. </View>
  166. </View>
  167. }
  168. <View style={styles.statisticView}>
  169. <View style={ui.flexcw}>
  170. <TextView style={this.props.isTabPage ? styles.sectionTitleV2 : styles.sectionTitle}>{$t('wallet.forWeekOf')}</TextView>
  171. {/* <Text style={styles.linkText}>1st Jan to 8th Jan </Text> */}
  172. </View>
  173. <View style={styles.overviewRow}>
  174. <View style={ui.flex1}>
  175. {/* <Text style={styles.valueText}>{this.state.glanceData.averageCharge ?? 0}</Text> */}
  176. <TextView style={styles.titleText}>{this.state.glanceData.averageCharge ?? 0} kWh{/*$t('wallet.perWeek')*/}</TextView>
  177. <TextView style={styles.subTitleText}>{$t('wallet.averageCharge')}</TextView>
  178. </View>
  179. <View style={styles.overviewDivide}></View>
  180. <View style={ui.flex1}>
  181. {/* <Text style={styles.valueText}>{this.state.glanceData.averageSpend ?? 0}</Text> */}
  182. <TextView style={styles.titleText}>{this.state.glanceData.currencySymbol} {this.state.glanceData.averageSpend ?? 0}{/*$t('wallet.perWeek')*/}</TextView>
  183. <TextView style={styles.subTitleText}>{$t('wallet.averageSpend')}</TextView>
  184. </View>
  185. <View style={styles.overviewDivide}></View>
  186. <View style={ui.flex1}>
  187. {/* <Text style={styles.valueText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}</Text> */}
  188. <TextView style={styles.titleText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}{/*$t('wallet.perWeek')*/}</TextView>
  189. <TextView style={styles.subTitleText}>{$t('wallet.averageTime')}</TextView>
  190. </View>
  191. </View>
  192. </View>
  193. <View style={ui.flex1}>
  194. <View style={styles.statisticView}>
  195. <TextView style={this.props.isTabPage ? styles.sectionTitleV2 : styles.sectionTitle}>{$t('wallet.statistics4week')}</TextView>
  196. <TextView style={styles.statisticTitle}>{this.state.weekdayData[this.state.weekIndex]?.title}</TextView>
  197. <View style={styles.barChartView}>
  198. { this.state.weekdayData.map((item, index) => (
  199. <View
  200. style={styles.barChartItem}
  201. key={index}>
  202. <Text
  203. style={styles.barLabelText}
  204. onPress={() => this.changeWeek(index)}>
  205. {item.label}
  206. </Text>
  207. <Pressable
  208. style={[
  209. styles.barChartValue,
  210. {
  211. height: this.getWeekHeight(item.y),
  212. opacity: index == this.state.weekIndex ? 1 : 0.6
  213. }
  214. ]}
  215. onPress={() => this.changeWeek(index)}
  216. />
  217. <Text style={styles.barLabelText}>{item.x}</Text>
  218. </View>
  219. ))}
  220. </View>
  221. </View>
  222. <View style={styles.statisticView}>
  223. <TextView style={this.props.isTabPage ? styles.sectionTitleV2 : styles.sectionTitle}>{$t('wallet.statistics4HalfYear')}</TextView>
  224. <TextView style={styles.statisticTitle}>{this.state.monthData[this.state.monthIndex]?.title}</TextView>
  225. <View style={styles.barChartView}>
  226. { this.state.monthData.map((item, index) => (
  227. <View
  228. style={styles.barChartItem}
  229. key={index}>
  230. <Text
  231. style={styles.barLabelText}
  232. onPress={() => this.changeMonth(index)}>
  233. {item.label}
  234. </Text>
  235. <Pressable
  236. style={[
  237. styles.barChartValue,
  238. {
  239. height: this.getMonthHeight(item.y),
  240. opacity: index == this.state.monthIndex ? 1 : 0.6
  241. }
  242. ]}
  243. onPress={() => this.changeMonth(index)}
  244. />
  245. <Text style={styles.barLabelText}>{item.x}</Text>
  246. </View>
  247. ))}
  248. </View>
  249. </View>
  250. </View>
  251. </View>
  252. );
  253. }
  254. }
  255. const animate = {
  256. duration: 500,
  257. onLoad: {
  258. duration: 200
  259. }
  260. }
  261. const axisTheme = {
  262. axis: {stroke: "#fff"},
  263. grid: {strokeWidth: 0},
  264. ticks: {size: 0},
  265. tickLabels: {color: '#666', fontSize: 12, padding: 8},
  266. }
  267. const styles = StyleSheet.create({
  268. hide: {
  269. display: 'none'
  270. },
  271. glanceView: {
  272. marginTop: 16,
  273. marginLeft: 16,
  274. marginRight: 16,
  275. marginBottom: 4,
  276. ...ElevationObject(2),
  277. overflow: 'hidden',
  278. borderTopLeftRadius: 6,
  279. borderTopRightRadius: 6,
  280. backgroundColor: colorLight
  281. },
  282. glanceTitle: {
  283. color: textPrimary,
  284. fontSize: 16,
  285. paddingTop: 4,
  286. paddingLeft: 16,
  287. paddingBottom: 4,
  288. backgroundColor: '#C4C8DF'
  289. },
  290. overviewRow: {
  291. paddingTop: 16,
  292. paddingBottom: 10,
  293. alignItems: 'center',
  294. flexDirection: 'row'
  295. },
  296. overviewDivide: {
  297. width: 1,
  298. height: 12,
  299. backgroundColor: '#D9D9D9'
  300. },
  301. valueText: {
  302. color: '#000',
  303. fontSize: 22,
  304. paddingBottom: 8,
  305. textAlign: 'center'
  306. },
  307. titleText: {
  308. color: textPrimary,
  309. fontSize: 14,
  310. textAlign: 'center'
  311. },
  312. subTitleText: {
  313. color: textCancel,
  314. fontSize: 12,
  315. textAlign: 'center'
  316. },
  317. statisticView: {
  318. marginTop: 0,
  319. paddingBottom: 16,
  320. backgroundColor: colorLight
  321. },
  322. sectionTitle: {
  323. color: textPrimary,
  324. fontSize: 14,
  325. paddingLeft: 16,
  326. fontWeight: 'bold'
  327. },
  328. sectionTitleV2: {
  329. flex: 1,
  330. color: textPrimary,
  331. fontSize: 14,
  332. marginLeft: 16,
  333. marginRight: 16,
  334. paddingBottom: 4,
  335. fontWeight: 'bold',
  336. textTransform: 'uppercase',
  337. borderBottomWidth: 1,
  338. borderBottomColor: colorPrimary
  339. },
  340. statisticTitle: {
  341. color: textPrimary,
  342. fontSize: 14,
  343. paddingTop: 24,
  344. paddingBottom: 8,
  345. textAlign: 'center'
  346. },
  347. statisticChart: {
  348. height: 120
  349. },
  350. linkText: {
  351. ...ui.link,
  352. fontSize: 14,
  353. paddingRight: 16
  354. },
  355. barChartView: {
  356. height: 200,
  357. padding: 16,
  358. alignItems: 'flex-end',
  359. flexDirection: 'row',
  360. justifyContent: 'space-between'
  361. },
  362. barChartItem: {
  363. alignItems: 'center'
  364. },
  365. barLabelText: {
  366. padding: 4,
  367. fontSize: 12,
  368. textAlign: 'center'
  369. },
  370. barChartValue: {
  371. width: 24,
  372. minHeight: 1,
  373. backgroundColor: colorAccent
  374. }
  375. })