Overview.js 14 KB

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