ChargeTrendChart.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div ref="chart" style="width: 100%; height: 100%"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. export default {
  7. name: 'ChargeTrendChart',
  8. props: {
  9. data: {
  10. type: Array,
  11. default: () => []
  12. }
  13. },
  14. data() {
  15. return {
  16. chart: null
  17. }
  18. },
  19. watch: {
  20. data: {
  21. handler(val) {
  22. this.initChart()
  23. },
  24. deep: true
  25. }
  26. },
  27. mounted() {
  28. this.initChart()
  29. window.addEventListener('resize', this.resizeChart)
  30. },
  31. beforeDestroy() {
  32. if (this.chart) {
  33. this.chart.dispose()
  34. }
  35. window.removeEventListener('resize', this.resizeChart)
  36. },
  37. methods: {
  38. initChart() {
  39. if (!this.chart) {
  40. this.chart = echarts.init(this.$refs.chart)
  41. }
  42. const option = {
  43. tooltip: {
  44. trigger: 'axis',
  45. axisPointer: {
  46. type: 'cross',
  47. label: {
  48. backgroundColor: '#6a7985'
  49. }
  50. }
  51. },
  52. legend: {
  53. data: ['充值金额']
  54. },
  55. grid: {
  56. left: '3%',
  57. right: '4%',
  58. bottom: '3%',
  59. containLabel: true
  60. },
  61. xAxis: {
  62. type: 'category',
  63. boundaryGap: false,
  64. data: this.data.map(item => item.day)
  65. },
  66. yAxis: {
  67. type: 'value',
  68. name: '金额',
  69. axisLabel: {
  70. formatter: '{value} 元'
  71. }
  72. },
  73. series: [
  74. {
  75. name: '充值金额',
  76. type: 'line',
  77. stack: 'Total',
  78. areaStyle: {
  79. opacity: 0.3
  80. },
  81. emphasis: {
  82. focus: 'series'
  83. },
  84. data: this.data.map(item => item.rechargePayAmount || 0)
  85. }
  86. ]
  87. }
  88. this.chart.setOption(option)
  89. },
  90. resizeChart() {
  91. if (this.chart) {
  92. this.chart.resize()
  93. }
  94. }
  95. }
  96. }
  97. </script>