Overview.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 'react-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. this.stopRefresh();
  115. })
  116. });
  117. }).catch(err => {
  118. toastShort(err);
  119. this.stopRefresh();
  120. });
  121. }
  122. stopRefresh() {
  123. if (this.props.refreshed) {
  124. this.props.refreshed();
  125. }
  126. this.refreshing = false;
  127. setTimeout(() => {
  128. Dialog.dismissLoading();
  129. }, 1000);
  130. }
  131. barTheme = (active) => ({
  132. data: {
  133. fill: ({datum, index}) => {
  134. return (datum.y > 0 ? (index == active ? colorAccent : chartThemes) : "#999999");
  135. }
  136. },
  137. labels: {
  138. fontSize: 12,
  139. fill: ({index}) => (index == active ? "#000" : textSecondary)
  140. }
  141. })
  142. barSpacesTheme = () => ({
  143. data: {
  144. fill: "transparent",
  145. },
  146. labels: {
  147. fontSize: 12,
  148. fill: textSecondary
  149. }
  150. })
  151. areaTheme = () => ({
  152. data: {
  153. fill: chartThemes,
  154. fillOpacity: 0.7,
  155. stroke: colorPrimary,
  156. strokeWidth: 1
  157. },
  158. labels: {
  159. fontSize: 1,
  160. fill: "transparent"//textSecondary
  161. }
  162. })
  163. getFill = ({datum, index}) => (datum.y > 0 ? (index == this.state.monthIndex ? colorAccent : chartThemes) : "#999999");
  164. render() {
  165. return (
  166. <View style={this.props.shown ? ui.flex1 : styles.hide}>
  167. { this.props.atAglance &&
  168. <View style={styles.glanceView}>
  169. <TextView style={styles.glanceTitle}>{$t('wallet.atAglance')}</TextView>
  170. <View style={styles.overviewRow}>
  171. <View style={ui.flex1}>
  172. <TextView style={styles.valueText}>{this.state.glanceData.averageCharge ?? 0}</TextView>
  173. <TextView style={styles.titleText}>kWh/{$t('wallet.perWeek')}</TextView>
  174. <TextView style={styles.subTitleText}>{$t('wallet.averageCharge')}</TextView>
  175. </View>
  176. <View style={ui.flex1}>
  177. <TextView style={styles.valueText}>{this.state.glanceData.averageSpend ?? 0}</TextView>
  178. <TextView style={styles.titleText}>{currency}/{$t('wallet.perWeek')}</TextView>
  179. <TextView style={styles.subTitleText}>{$t('wallet.averageSpend')}</TextView>
  180. </View>
  181. <View style={ui.flex1}>
  182. <TextView style={styles.valueText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}</TextView>
  183. <TextView style={styles.titleText}>{$t('wallet.perHrWeek')}</TextView>
  184. <TextView style={styles.subTitleText}>{$t('wallet.averageTime')}</TextView>
  185. </View>
  186. </View>
  187. </View>
  188. }
  189. <View style={styles.statisticView}>
  190. <View style={ui.flexcw}>
  191. <TextView style={styles.sectionTitle}>{$t('wallet.forWeekOf')}</TextView>
  192. {/* <Text style={styles.linkText}>1st Jan to 8th Jan </Text> */}
  193. </View>
  194. <View style={styles.overviewRow}>
  195. <View style={ui.flex1}>
  196. {/* <Text style={styles.valueText}>{this.state.glanceData.averageCharge ?? 0}</Text> */}
  197. <TextView style={styles.titleText}>{this.state.glanceData.averageCharge ?? 0} kWh{/*$t('wallet.perWeek')*/}</TextView>
  198. <TextView style={styles.subTitleText}>{$t('wallet.averageCharge')}</TextView>
  199. </View>
  200. <View style={styles.overviewDivide}></View>
  201. <View style={ui.flex1}>
  202. {/* <Text style={styles.valueText}>{this.state.glanceData.averageSpend ?? 0}</Text> */}
  203. <TextView style={styles.titleText}>{this.state.glanceData.currencySymbol} {this.state.glanceData.averageSpend ?? 0}{/*$t('wallet.perWeek')*/}</TextView>
  204. <TextView style={styles.subTitleText}>{$t('wallet.averageSpend')}</TextView>
  205. </View>
  206. <View style={styles.overviewDivide}></View>
  207. <View style={ui.flex1}>
  208. {/* <Text style={styles.valueText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}</Text> */}
  209. <TextView style={styles.titleText}>{utils.hour2HHmm(this.state.glanceData.averageTime)}{/*$t('wallet.perWeek')*/}</TextView>
  210. <TextView style={styles.subTitleText}>{$t('wallet.averageTime')}</TextView>
  211. </View>
  212. </View>
  213. </View>
  214. <View style={ui.flex1}>
  215. <View style={styles.statisticView}>
  216. <TextView style={styles.sectionTitle}>{$t('wallet.statistics4week')}</TextView>
  217. <TextView style={styles.statisticTitle}>{this.state.weekdayData[this.state.weekIndex]?.title}</TextView>
  218. <Svg height={200}>
  219. { this.state.chartReady &&
  220. <VictoryChart
  221. theme={VictoryTheme.material}
  222. animate={animate}
  223. height={200}
  224. padding={{top: 50, left: 32, right: 60, bottom: 50}}>
  225. <VictoryAxis style={axisTheme}/>
  226. <VictoryBar
  227. barWidth={25}
  228. style={this.barTheme(this.state.weekIndex)}
  229. data={this.state.weekdayData}
  230. events={[{
  231. target: "data",
  232. eventHandlers: {
  233. onPress: () => {
  234. return [{
  235. target: "data",
  236. mutation: (props) => {
  237. this.setState({
  238. weekIndex: props.index
  239. });
  240. }
  241. }];
  242. }
  243. }
  244. }]}
  245. />
  246. </VictoryChart>
  247. }
  248. </Svg>
  249. </View>
  250. { this.state.skeleton &&
  251. <View style={styles.statisticView}>
  252. <TextView style={styles.sectionTitle}>{$t('wallet.statistics4HalfYear')}</TextView>
  253. <View style={{height: 200}}></View>
  254. </View>
  255. }
  256. { this.state.monthData.length > 0 &&
  257. <View style={styles.statisticView}>
  258. <TextView style={styles.sectionTitle}>{$t('wallet.statistics4HalfYear')}</TextView>
  259. <TextView style={styles.statisticTitle}>{this.state.monthData[this.state.monthIndex].title}</TextView>
  260. {/* <svg style={{height: 0}}>
  261. <defs>
  262. <linearGradient id="myGradient" gradientUnits="userSpaceOnUse">
  263. <stop stopColor={chartThemes} stopOpacity="0.8"/>
  264. <stop offset="1" stopColor={chartThemes} stopOpacity="0"/>
  265. </linearGradient>
  266. </defs>
  267. </svg> */}
  268. { this.enableChartArea
  269. ? <Svg height={200}>
  270. { this.state.chartReady &&
  271. <VictoryChart
  272. theme={VictoryTheme.material}
  273. height={200}
  274. padding={{top: 50, left: 30, right: 60, bottom: 50}}>
  275. <VictoryAxis style={axisTheme}/>
  276. <VictoryArea
  277. style={this.areaTheme()}
  278. data={this.state.monthData}
  279. theme={VictoryTheme.material}
  280. labels={({ datum }) => datum.y}
  281. labelComponent={<VictoryLabel dy={10}/>}
  282. />
  283. <VictoryBar
  284. barWidth={28}
  285. data={this.state.monthData}
  286. style={this.barSpacesTheme()}
  287. events={[{
  288. target: "data",
  289. eventHandlers: {
  290. onPress: () => {
  291. return [{
  292. target: "data",
  293. mutation: (props) => {
  294. this.setState({
  295. monthIndex: props.index
  296. });
  297. }
  298. }];
  299. }
  300. }
  301. }]}
  302. />
  303. </VictoryChart>
  304. }
  305. </Svg>
  306. : <Svg height={200}>
  307. <VictoryChart
  308. theme={VictoryTheme.material}
  309. height={200}
  310. padding={{top: 50, left: 30, right: 62, bottom: 50}}>
  311. <VictoryAxis style={axisTheme}/>
  312. <VictoryBar
  313. barWidth={28}
  314. data={this.state.monthData}
  315. style={this.barTheme(this.state.monthIndex)}
  316. events={[{
  317. target: "data",
  318. eventHandlers: {
  319. onPress: () => {
  320. return [{
  321. target: "data",
  322. mutation: (props) => {
  323. this.setState({
  324. monthIndex: props.index
  325. });
  326. }
  327. }];
  328. }
  329. }
  330. }]}
  331. />
  332. </VictoryChart>
  333. </Svg>
  334. }
  335. </View>
  336. }
  337. </View>
  338. </View>
  339. );
  340. }
  341. }
  342. const animate = {
  343. duration: 500,
  344. onLoad: {
  345. duration: 200
  346. }
  347. }
  348. const axisTheme = {
  349. axis: {stroke: "#fff"},
  350. grid: {strokeWidth: 0},
  351. ticks: {size: 0},
  352. tickLabels: {color: '#666', fontSize: 12, padding: 8},
  353. }
  354. const styles = StyleSheet.create({
  355. hide: {
  356. display: 'none'
  357. },
  358. glanceView: {
  359. marginTop: 16,
  360. marginLeft: 16,
  361. marginRight: 16,
  362. marginBottom: 4,
  363. ...ElevationObject(2),
  364. overflow: 'hidden',
  365. borderTopLeftRadius: 6,
  366. borderTopRightRadius: 6,
  367. backgroundColor: colorLight
  368. },
  369. glanceTitle: {
  370. color: textPrimary,
  371. fontSize: 16,
  372. paddingTop: 4,
  373. paddingLeft: 16,
  374. paddingBottom: 4,
  375. backgroundColor: '#C4C8DF'
  376. },
  377. overviewRow: {
  378. paddingTop: 16,
  379. paddingBottom: 10,
  380. alignItems: 'center',
  381. flexDirection: 'row'
  382. },
  383. overviewDivide: {
  384. width: 1,
  385. height: 12,
  386. backgroundColor: '#D9D9D9'
  387. },
  388. valueText: {
  389. color: '#000',
  390. fontSize: 22,
  391. paddingBottom: 8,
  392. textAlign: 'center'
  393. },
  394. titleText: {
  395. color: textPrimary,
  396. fontSize: 14,
  397. textAlign: 'center'
  398. },
  399. subTitleText: {
  400. color: textCancel,
  401. fontSize: 12,
  402. textAlign: 'center'
  403. },
  404. statisticView: {
  405. marginTop: 0,
  406. paddingTop: 16,
  407. backgroundColor: colorLight
  408. },
  409. sectionTitle: {
  410. color: textPrimary,
  411. fontSize: 14,
  412. paddingLeft: 16,
  413. fontWeight: 'bold'
  414. },
  415. statisticTitle: {
  416. color: textPrimary,
  417. fontSize: 14,
  418. paddingTop: 24,
  419. paddingBottom: 8,
  420. textAlign: 'center'
  421. },
  422. statisticChart: {
  423. height: 120
  424. },
  425. linkText: {
  426. ...ui.link,
  427. fontSize: 14,
  428. paddingRight: 16
  429. }
  430. })