playBackChart.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <div ref="preResultRef" :style="{ height: height, width: width }"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. export default {
  7. name: 'PlayBackChart',
  8. props: {
  9. // chartRef: {
  10. // type: String,
  11. // required: true
  12. // },
  13. width: {
  14. type: String,
  15. default: '100%'
  16. },
  17. height: {
  18. type: String,
  19. default: '400px'
  20. },
  21. /**
  22. * chartData包括:
  23. * title:''
  24. * legendData:[]
  25. * xAxisData:{}
  26. * yAxisData:{}
  27. * seriesData:{}
  28. */
  29. chartData: {
  30. type: Object,
  31. required: true
  32. }
  33. },
  34. data() {
  35. return {
  36. chart: null
  37. }
  38. },
  39. watch: {
  40. chartData: {
  41. deep: true,
  42. handler(val) {
  43. // this.initChart()
  44. this.$nextTick(() => {
  45. this.initChart()
  46. this.setOptions(val)
  47. })
  48. }
  49. }
  50. },
  51. mounted() {
  52. this.$nextTick(() => {
  53. this.initChart()
  54. })
  55. },
  56. beforeDestroy() {
  57. if (!this.chart) {
  58. return
  59. }
  60. this.chart.dispose()
  61. this.chart = null
  62. },
  63. computed: {},
  64. methods: {
  65. initChart() {
  66. if (this.chart) {
  67. this.chart.dispose()
  68. this.chart = null
  69. }
  70. // console.log('66', this.chartRef)
  71. this.chart = echarts.init(this.$refs.preResultRef)
  72. // 监听 dataZoom 事件
  73. this.chart.on('dataZoom', (params) => {
  74. const startValue = this.chartData.xAxisData[params.batch[0].startValue]
  75. const endValue = this.chartData.xAxisData[params.batch[0].endValue]
  76. this.$emit('data-zoom', [startValue, endValue])
  77. })
  78. this.setOptions(this.chartData)
  79. },
  80. setOptions({ title, legendData, xAxisData, yAxisData, seriesData } = {}) {
  81. this.chart.setOption({
  82. title: {
  83. text: title
  84. },
  85. tooltip: {
  86. trigger: 'axis'
  87. },
  88. legend: {
  89. // data: legendData,
  90. textStyle: {
  91. //图例文字的样式
  92. color: '#fff'
  93. }
  94. },
  95. toolbox: {
  96. show: true,
  97. feature: {
  98. dataZoom: {
  99. yAxisIndex: 'none'
  100. },
  101. restore: {},
  102. saveAsImage: {}
  103. }
  104. },
  105. xAxis: {
  106. type: 'category',
  107. data: xAxisData
  108. },
  109. yAxis: {
  110. type: 'value'
  111. // data: yAxisData
  112. },
  113. dataZoom: [
  114. {
  115. type: 'slider',
  116. start: 0,
  117. end: 100
  118. },
  119. {
  120. type: 'inside',
  121. start: 0,
  122. end: 100
  123. }
  124. ],
  125. // series: series
  126. series: seriesData
  127. })
  128. }
  129. }
  130. }
  131. </script>
  132. <style lang="scss" scoped></style>