Overview.js 15 KB

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