当前位置:   article > 正文

echarts 柱状图--柱体的点击事件_echart 柱状图点击事件

echart 柱状图点击事件

 

  1. <template>
  2. <el-card class="current-sort-line-ratio-div">
  3. <div class="current-sort-line-ratio-title-div">
  4. <span>{{ `各分类线路本周期${CurClickRatioCard.label1}` }}</span>
  5. <el-tooltip class="item" effect="dark" placement="bottom">
  6. <i class="el-icon-info"></i>
  7. <div slot="content">{{ `自本周期线路下发日开始统计线路累计的${CurClickRatioCard.label1}` }}</div>
  8. </el-tooltip>
  9. <el-button v-if="!showBarChartRef" class="btn-div" size="small" @click="handleClick">返回上层</el-button>
  10. </div>
  11. <el-divider></el-divider>
  12. <div v-show="showBarChartRef" id="BarChartRef" class="bar-chart-div"></div>
  13. <div v-show="!showBarChartRef" id="clickBarChartRef" class="bar-chart-div"></div>
  14. </el-card>
  15. </template>
  16. <script>
  17. import echarts from 'echarts'
  18. import { barOntimeRateByType, getOntimeRateOfRegionalControl } from '@/api/agingSystem/agingSystem'
  19. export default {
  20. props: {
  21. formDataObj: { type: Object, default: () => ({}) },
  22. CurClickRatioCard: {
  23. type: Object,
  24. default: () => ({
  25. index: 1,
  26. isClick: true,
  27. value1: 'onTimeRate',
  28. label1: '准点率',
  29. value2: 'onTimeRateDiffRate',
  30. label2: '基比',
  31. value3: 'basicOnTimeRate',
  32. label3: '基准准点率',
  33. }),
  34. },
  35. },
  36. data() {
  37. return {
  38. chartObj: null,
  39. clickChartObj: null,
  40. dataList: [],
  41. showBarChartRef: true,
  42. }
  43. },
  44. watch: {
  45. formDataObj() {
  46. this.chartObj.clear()
  47. this.showBarChartRef = true
  48. this.asyncGetListEtaTaskScrollProp(this.CurClickRatioCard)
  49. },
  50. CurClickRatioCard(val) {
  51. this.asyncGetListEtaTaskScrollProp(val)
  52. },
  53. },
  54. mounted() {
  55. this.chartObj = echarts.init(document.getElementById('BarChartRef'))
  56. },
  57. methods: {
  58. handleClick() {
  59. this.showBarChartRef = true
  60. },
  61. asyncGetListEtaTaskScrollProp(clickData) {
  62. const params = {
  63. taskAreaCode: this.formDataObj.taskAreaCode,
  64. updateTime: this.formDataObj.updateTime,
  65. }
  66. barOntimeRateByType(params)
  67. .then(res => {
  68. this.showBarChartRef = true
  69. this.dataList = res.data.reduce((s, i) => {
  70. s.push([i.splitTagNewType, parseFloat(i[clickData.value3]), parseFloat(i[clickData.value1])])
  71. return s
  72. }, [])
  73. const option = this.barChartOptions(this.dataList)
  74. this.chartObj.setOption(option)
  75. this.clickBarChartOptions()
  76. })
  77. .catch(e => {
  78. console.log(e)
  79. })
  80. },
  81. formatterDiQuData(row, basicValue, newValue) {
  82. return [
  83. [row[1].splitTagNew, parseFloat(row[1][basicValue]), parseFloat(row[1][newValue])],
  84. [row[0].splitTagNew, parseFloat(row[0][basicValue]), parseFloat(row[0][newValue])],
  85. ]
  86. },
  87. clickBarChartOptions() {
  88. this.chartObj.off('click')
  89. this.chartObj.on('click', params => {
  90. if (this.clickChartObj) {
  91. this.clickChartObj.clear()
  92. }
  93. this.showBarChartRef = false
  94. if (params.name == '地区管控') {
  95. const params = {
  96. taskAreaCode: this.formDataObj.taskAreaCode,
  97. updateTime: this.formDataObj.updateTime,
  98. }
  99. getOntimeRateOfRegionalControl(params).then(res => {
  100. let clickData
  101. if (res.success && res.data.length) {
  102. clickData = this.formatterDiQuData(
  103. res.data,
  104. this.CurClickRatioCard.value3,
  105. this.CurClickRatioCard.value1,
  106. )
  107. this.$nextTick(() => {
  108. this.clickChartObj = echarts.init(document.getElementById('clickBarChartRef'))
  109. const row = this.barChartOptions(clickData)
  110. this.clickChartObj.setOption(row)
  111. })
  112. }
  113. })
  114. } else {
  115. this.$nextTick(() => {
  116. this.clickChartObj = echarts.init(document.getElementById('clickBarChartRef'))
  117. const row = this.barChartOptions([params.data])
  118. this.clickChartObj.setOption(row)
  119. })
  120. }
  121. })
  122. },
  123. barChartOptions(rowData) {
  124. return {
  125. color: ['#50D9EB', '#2196F3'],
  126. legend: {
  127. itemGap: 40,
  128. itemWidth: 14,
  129. itemHeight: 14,
  130. right: 40,
  131. textStyle: {
  132. fontSize: 14,
  133. color: '#333',
  134. },
  135. selectedMode: false, //控制是否可以通过点击图例改变系列的显示状态
  136. },
  137. tooltip: {
  138. trigger: 'axis',
  139. axisPointer: {
  140. type: 'none',
  141. },
  142. textStyle: {
  143. color: '#fff',
  144. fontSize: 14,
  145. },
  146. backgroundColor: '#373F57',
  147. itemWidth: '20px',
  148. },
  149. grid: {
  150. left: '6%',
  151. },
  152. dataset: {
  153. source: [['product', '基准', '最新'], ...rowData],
  154. },
  155. xAxis: {
  156. type: 'category',
  157. axisLine: {
  158. show: true,
  159. lineStyle: {
  160. color: '#666',
  161. },
  162. },
  163. axisTick: {
  164. show: false,
  165. },
  166. axisPointer: {
  167. type: 'line',
  168. },
  169. },
  170. yAxis: {
  171. type: 'value',
  172. name: '占比',
  173. max: 100,
  174. splitLine: {
  175. show: false,
  176. },
  177. axisLine: {
  178. show: true,
  179. lineStyle: {
  180. color: '#666',
  181. },
  182. },
  183. axisTick: {
  184. show: true,
  185. },
  186. splitNumber: 5,
  187. minInterval: 20,
  188. // maxInterval: 20,
  189. axisLabel: {
  190. formatter: function(val) {
  191. return val + '%'
  192. },
  193. },
  194. },
  195. series: [
  196. {
  197. type: 'bar',
  198. barWidth: 30,
  199. itemStyle: {
  200. normal: {
  201. label: {
  202. textStyle: {
  203. fontSize: 12,
  204. fontWeight: 400,
  205. },
  206. },
  207. },
  208. },
  209. },
  210. {
  211. type: 'bar',
  212. barWidth: 30,
  213. itemStyle: {
  214. normal: {
  215. label: {
  216. textStyle: {
  217. fontSize: 12,
  218. fontWeight: 400,
  219. },
  220. },
  221. },
  222. },
  223. },
  224. ],
  225. }
  226. },
  227. },
  228. }
  229. </script>
  230. <style lang="scss" scoped>
  231. .current-sort-line-ratio-div {
  232. width: 49.6%;
  233. .current-sort-line-ratio-title-div {
  234. position: relative;
  235. font-family: PingFangSC-Medium;
  236. font-size: 16px;
  237. color: #333333;
  238. font-weight: 700;
  239. .el-icon-info {
  240. color: #2970ff;
  241. }
  242. .btn-div {
  243. position: absolute;
  244. right: 10px;
  245. font-family: PingFangSC-Regular;
  246. font-size: 14px;
  247. color: #333333;
  248. }
  249. }
  250. .el-divider--horizontal {
  251. margin: 18px 0;
  252. }
  253. .bar-chart-div {
  254. margin-top: 20px;
  255. height: 350px;
  256. }
  257. }
  258. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/549804
推荐阅读
  

闽ICP备14008679号