EchartsComponent.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import React, { Component } from 'react'
  2. import { Dimensions, Text, View } from 'react-native'
  3. import Echarts from 'native-echarts'
  4. import { PieOptions } from './PieConfig'
  5. import { ScaleUtil, accSub } from '../../utils/utils'
  6. const { width, height } = Dimensions.get('window')
  7. const color = [
  8. '#FF7772',
  9. '#FF9C68',
  10. '#FFD75B',
  11. '#93E05C',
  12. '#56DBD4',
  13. '#65BEFF',
  14. '#84A2FF',
  15. ]
  16. class EchartsComponent extends Component {
  17. constructor(props) {
  18. super(props)
  19. this.state = { pieOptions: PieOptions, option: {} }
  20. }
  21. componentWillReceiveProps(nextProps) {
  22. if (
  23. nextProps.data.proTitle == true &&
  24. nextProps.data.creditBalance !== this.props.data.creditBalance &&
  25. nextProps.data.creditBalance
  26. ) {
  27. // if (
  28. // nextProps.parentState.activeSection ||
  29. // nextProps.parentState.dateActive
  30. // ) {
  31. this.index = Math.round(Math.random() * color.length)
  32. const { creditBalance, creditLimit } = nextProps.data
  33. let pieOptions = JSON.parse(JSON.stringify(this.state.pieOptions)),
  34. creditBalanceIf = creditBalance
  35. // let Balance = Quota - Occupancy;
  36. if (creditBalance <= 0) {
  37. creditBalanceIf = 0
  38. }
  39. if (
  40. ScaleUtil(creditBalance, CURRENCY.currencyAmountScale).toString()
  41. .length >= 10 &&
  42. ScaleUtil(creditBalance, CURRENCY.currencyAmountScale).toString()
  43. .length < 12
  44. ) {
  45. pieOptions.series[0].radius = ['80%', '90%']
  46. } else if (
  47. ScaleUtil(creditBalance, CURRENCY.currencyAmountScale).toString()
  48. .length >= 12
  49. ) {
  50. pieOptions.series[0].radius = ['90%', '100%']
  51. }
  52. pieOptions.series[0].data = [
  53. ScaleUtil(creditLimit, CURRENCY.currencyAmountScale),
  54. ScaleUtil(
  55. accSub(creditLimit, creditBalanceIf),
  56. CURRENCY.currencyAmountScale
  57. ),
  58. ]
  59. pieOptions.series[0].label.normal.formatter = `${
  60. CURRENCY.currencySign
  61. }${ScaleUtil(creditBalance, CURRENCY.currencyAmountScale)}`
  62. pieOptions.series[0].label.normal.textStyle.fontSize =
  63. this.props.fontSize || 20
  64. if (this.props.color && Array.isArray(this.props.color)) {
  65. pieOptions.color = this.props.color
  66. } else {
  67. pieOptions.color[0] = color[0]
  68. }
  69. this.setState({ option: pieOptions })
  70. }
  71. }
  72. componentDidMount() {
  73. if (this.props.data) {
  74. let colorIndex = this.props.index % 7
  75. const { creditBalance, creditLimit } = this.props.data
  76. let pieOptions = JSON.parse(JSON.stringify(this.state.pieOptions)),
  77. creditBalanceIf = creditBalance
  78. // let Balance = Quota - Occupancy;
  79. if (creditBalance <= 0) {
  80. creditBalanceIf = 0
  81. }
  82. if (
  83. ScaleUtil(creditBalance, CURRENCY.currencyAmountScale).toString()
  84. .length >= 10 &&
  85. ScaleUtil(creditBalance, CURRENCY.currencyAmountScale).toString()
  86. .length < 12
  87. ) {
  88. pieOptions.series[0].radius = ['80%', '90%']
  89. } else if (
  90. ScaleUtil(creditBalance, CURRENCY.currencyAmountScale).toString()
  91. .length >= 12
  92. ) {
  93. pieOptions.series[0].radius = ['90%', '100%']
  94. }
  95. pieOptions.series[0].data = [
  96. ScaleUtil(creditLimit, CURRENCY.currencyAmountScale),
  97. ScaleUtil(
  98. accSub(creditLimit, creditBalanceIf),
  99. CURRENCY.currencyAmountScale
  100. ),
  101. ]
  102. pieOptions.series[0].label.normal.formatter = `${
  103. CURRENCY.currencySign
  104. }${ScaleUtil(creditBalance, CURRENCY.currencyAmountScale)}`
  105. pieOptions.series[0].label.normal.textStyle.fontSize =
  106. this.props.fontSize || 20
  107. if (this.props.color && Array.isArray(this.props.color)) {
  108. pieOptions.color = this.props.color
  109. } else {
  110. pieOptions.color[0] = color[colorIndex || 0]
  111. }
  112. this.setState({ option: pieOptions })
  113. }
  114. }
  115. render() {
  116. return (
  117. <View
  118. style={{
  119. alignSelf: 'center',
  120. }}
  121. >
  122. <Echarts
  123. option={this.state.option}
  124. width={width}
  125. height={this.props.height || 160}
  126. />
  127. <View
  128. style={{
  129. position: 'absolute',
  130. alignSelf: 'center',
  131. bottom: this.props.bottom || '22%',
  132. }}
  133. >
  134. <Text
  135. style={{
  136. fontSize: 14,
  137. lineHeight: 20,
  138. letterSpacing: 0.17,
  139. color: '#999',
  140. }}
  141. >
  142. {this.props.bottomText}
  143. </Text>
  144. </View>
  145. </View>
  146. )
  147. }
  148. }
  149. export default EchartsComponent