line-echarts.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="echarts-content">
  3. <div ref="echarts" class="line-echarts"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import echarts from 'echarts'
  8. import loopEchart from '../mixins/index'
  9. import resize from '@/components/Charts/mixins/resize'
  10. export default {
  11. mixins: [loopEchart, resize],
  12. props: {
  13. echartsName: {
  14. type: String,
  15. default: () => '机构新增趋势'
  16. },
  17. optionData: {
  18. type: Object,
  19. default: () => ({})
  20. }
  21. },
  22. data() {
  23. return {
  24. chart: null
  25. }
  26. },
  27. watch: {
  28. optionData: {
  29. handler(val) {
  30. if (val) {
  31. clearInterval(this.timeTicket)
  32. this.timeTicket = null
  33. this.initChart()
  34. }
  35. },
  36. deep: true
  37. }
  38. },
  39. methods: {
  40. getXAxis() {
  41. return this.optionData['个人机构']?.map(e => e.time)
  42. },
  43. getLegend() {
  44. const a = []
  45. for (const key in this.optionData) {
  46. a.push(key)
  47. }
  48. return a
  49. },
  50. getSeriesData() {
  51. const a = []
  52. for (const key in this.optionData) {
  53. const b = {}
  54. b['name'] = key
  55. b['type'] = 'line'
  56. b['stack'] = 'Total'
  57. b['data'] = this.optionData[key]?.map(e => e.num)
  58. a.push(b)
  59. }
  60. return a
  61. },
  62. initChart() {
  63. const myEchart = this.$refs['echarts']
  64. this.chart = echarts.init(myEchart)
  65. const option = {
  66. title: {
  67. text: this.echartsName
  68. },
  69. tooltip: {
  70. trigger: 'axis',
  71. axisPointer: {
  72. type: 'cross' // 提示框样式为十字准星
  73. }
  74. },
  75. legend: {
  76. data: this.getLegend()
  77. },
  78. grid: {
  79. left: '3%',
  80. right: '4%',
  81. bottom: '3%',
  82. containLabel: true
  83. },
  84. toolbox: {
  85. },
  86. xAxis: {
  87. type: 'category',
  88. boundaryGap: false,
  89. data: this.getXAxis()
  90. },
  91. yAxis: {
  92. type: 'value'
  93. },
  94. series: this.getSeriesData()
  95. }
  96. this.chart && this.chart.setOption(option)
  97. // this.chart && this.handleChartLineLoop(option, this.chart)
  98. },
  99. handleMouseOver(e, callback) {
  100. if (e) {
  101. const myEchart = this.$refs['echarts']
  102. myEchart.addEventListener('mouseover', callback)
  103. }
  104. },
  105. handleMouseOut(e, callback) {
  106. if (e) {
  107. const myEchart = this.$refs['echarts']
  108. myEchart.addEventListener('mouseout', callback)
  109. }
  110. }
  111. }
  112. }
  113. </script>
  114. <style lang="scss" scoped>
  115. .echarts-content {
  116. width: 100%;
  117. height: 350px;
  118. border-radius: 12px;
  119. border: 1px solid #ccc;
  120. box-shadow: 0 0 4px #ccc;
  121. overflow: hidden;
  122. padding: 10px;
  123. margin-bottom: 14px;
  124. .title {
  125. display: flex;
  126. align-items: center;
  127. }
  128. .line-echarts {
  129. width: 100%;
  130. height: 100%;
  131. }
  132. }
  133. </style>