EchartMap.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import Taro, { Component } from '@tarojs/taro'
  2. import EChart from '@EChart/index'
  3. import './EchartMap.scss'
  4. import * as echarts from '@EChart/echarts'
  5. import { View } from '@tarojs/components'
  6. export default class EchartLine extends Component {
  7. constructor (props) {
  8. super(props)
  9. this.state = {
  10. exportedImg: '',
  11. cList: [
  12. { name: '凤凰州', value: 'fhz'},
  13. { name: '红中区', value: 'hzq'},
  14. { name: '红角州', value: 'hjz'},
  15. { name: '东湖区', value: 'dhq'},
  16. { name: '西湖区', value: 'xhq'},
  17. { name: '青云谱', value: 'qyp'},
  18. { name: '象湖新城', value: 'xhxc'},
  19. ],
  20. isAllSelect: false,
  21. }
  22. }
  23. componentWillMount () {
  24. this.props.onRef(this)
  25. }
  26. setChartRef = node => {
  27. this.chart = node
  28. }
  29. componentDidMount() {
  30. const dictData = Taro.getStorageSync('dictData')
  31. const dictAreaArr = dictData.area_type || []
  32. let cList = dictAreaArr.map(item => {
  33. return {
  34. name: item.key,
  35. value: item.val,
  36. }
  37. })
  38. this.setState({
  39. cList
  40. })
  41. this.changeChart()
  42. }
  43. changeChart () {
  44. const curOption = {
  45. series: [
  46. {
  47. name: '测试',
  48. type: 'map',
  49. map: 'test',
  50. top: 10,
  51. right: 10,
  52. bottom: 10,
  53. left: 10,
  54. layoutCenter: ["50%", "50%"],
  55. layoutSize: "100%",
  56. label: {
  57. show: true,
  58. color: '#666',
  59. fontSize: 8
  60. },
  61. itemStyle: {
  62. areaColor: '#c1d7ef',
  63. borderColor: '#eff3f6',
  64. borderWidth: 2
  65. },
  66. selectedMode: 'multiple',
  67. emphasis: {
  68. label: {
  69. show: true,
  70. color: '#fff',
  71. // backgroundColor: '#369af7',
  72. // padding: 6,
  73. // borderRadius: 6,
  74. // borderWidth: 2
  75. },
  76. itemStyle: {
  77. areaColor: '#369af7'
  78. },
  79. },
  80. select: {
  81. label: {
  82. show: true,
  83. color: '#fff'
  84. },
  85. itemStyle: {
  86. areaColor: '#999'
  87. },
  88. },
  89. data: this.cList,
  90. // 自定义名称映射
  91. // nameMap: {
  92. // 'fhz': '凤凰州',
  93. // 'hzq': '红中区',
  94. // 'hjz': '红角州',
  95. // 'dhq': '东湖区',
  96. // 'xhq': '西湖区',
  97. // 'qyp': '青云谱',
  98. // 'xhxc': '象湖新城',
  99. // 'xh': '象湖',
  100. // }
  101. }
  102. ]
  103. }
  104. setTimeout(() => {
  105. if (!this.chart) return
  106. const geoJson = require('./test.json')
  107. echarts.registerMap('test', geoJson)
  108. this.chart.setOption(curOption)
  109. const curChart = this.chart.chart
  110. curChart.on('mapselectchanged', (params) => {
  111. console.log(params)
  112. })
  113. // curChart.on('click', (params) => {
  114. // console.log(params)
  115. // curChart.dispatchAction({
  116. // type: 'mapSelect',
  117. // dataIndex: 1,
  118. // // type: 'highlight',
  119. // // seriesIndex: 0,
  120. // // dataIndex: 1
  121. // })
  122. // // console.log(this.chart)
  123. // })
  124. }, 500)
  125. }
  126. testHandle () {
  127. const curChart = this.chart.chart
  128. const { cList, isAllSelect } = this.state
  129. if (isAllSelect) {
  130. cList.forEach((item, index) => {
  131. curChart.dispatchAction({
  132. type: 'mapUnSelect',
  133. dataIndex: index,
  134. })
  135. })
  136. this.setState({
  137. isAllSelect: false
  138. })
  139. } else {
  140. cList.forEach((item, index) => {
  141. curChart.dispatchAction({
  142. type: 'mapSelect',
  143. // name: '凤凰州',
  144. dataIndex: index,
  145. })
  146. })
  147. this.setState({
  148. isAllSelect: true
  149. })
  150. }
  151. }
  152. renderSelectMap () {
  153. const { isAllSelect } = this.state
  154. return (
  155. <View className="">
  156. <View style="position: absolute;top: 0;left: 0;" onClick={this.testHandle.bind(this)}>{isAllSelect ? '全部取消' : '全部选中'}</View>
  157. </View>
  158. )
  159. }
  160. render () {
  161. const { exportedImg } = this.state
  162. return (
  163. <View style="height: 400px">
  164. {
  165. exportedImg
  166. ? <Image className="icon" src={exportedImg} style="width: 100%;height: 100%;"></Image>
  167. : <EChart ref={this.setChartRef} echarts={echarts} />
  168. }
  169. {this.renderSelectMap()}
  170. </View>
  171. )
  172. }
  173. }