声脉桥梁云监控平台

LineChart.vue 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}"></div>
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. require('echarts/theme/macarons') // echarts theme
  7. import { debounce } from '@/utils'
  8. export default {
  9. props: {
  10. className: {
  11. type: String,
  12. default: 'chart'
  13. },
  14. width: {
  15. type: String,
  16. default: '100%'
  17. },
  18. height: {
  19. type: String,
  20. default: '350px'
  21. },
  22. autoResize: {
  23. type: Boolean,
  24. default: true
  25. },
  26. chartData: {
  27. type: Object
  28. }
  29. },
  30. data() {
  31. return {
  32. chart: null
  33. }
  34. },
  35. mounted() {
  36. this.initChart()
  37. if (this.autoResize) {
  38. this.__resizeHanlder = debounce(() => {
  39. if (this.chart) {
  40. this.chart.resize()
  41. }
  42. }, 100)
  43. window.addEventListener('resize', this.__resizeHanlder)
  44. }
  45. // 监听侧边栏的变化
  46. const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
  47. sidebarElm.addEventListener('transitionend', this.__resizeHanlder)
  48. },
  49. beforeDestroy() {
  50. if (!this.chart) {
  51. return
  52. }
  53. if (this.autoResize) {
  54. window.removeEventListener('resize', this.__resizeHanlder)
  55. }
  56. const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
  57. sidebarElm.removeEventListener('transitionend', this.__resizeHanlder)
  58. this.chart.dispose()
  59. this.chart = null
  60. },
  61. watch: {
  62. chartData: {
  63. deep: true,
  64. handler(val) {
  65. this.setOptions(val)
  66. }
  67. }
  68. },
  69. methods: {
  70. setOptions({ expectedData, actualData } = {}) {
  71. this.chart.setOption({
  72. xAxis: {
  73. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  74. boundaryGap: false,
  75. axisTick: {
  76. show: false
  77. }
  78. },
  79. grid: {
  80. left: 10,
  81. right: 10,
  82. bottom: 20,
  83. top: 30,
  84. containLabel: true
  85. },
  86. tooltip: {
  87. trigger: 'axis',
  88. axisPointer: {
  89. type: 'cross'
  90. },
  91. padding: [5, 10]
  92. },
  93. yAxis: {
  94. axisTick: {
  95. show: false
  96. }
  97. },
  98. legend: {
  99. data: ['expected', 'actual']
  100. },
  101. series: [{
  102. name: 'expected', itemStyle: {
  103. normal: {
  104. color: '#FF005A',
  105. lineStyle: {
  106. color: '#FF005A',
  107. width: 2
  108. }
  109. }
  110. },
  111. smooth: true,
  112. type: 'line',
  113. data: expectedData,
  114. animationDuration: 2800,
  115. animationEasing: 'cubicInOut'
  116. },
  117. {
  118. name: 'actual',
  119. smooth: true,
  120. type: 'line',
  121. itemStyle: {
  122. normal: {
  123. color: '#3888fa',
  124. lineStyle: {
  125. color: '#3888fa',
  126. width: 2
  127. },
  128. areaStyle: {
  129. color: '#f3f8ff'
  130. }
  131. }
  132. },
  133. data: actualData,
  134. animationDuration: 2800,
  135. animationEasing: 'quadraticOut'
  136. }]
  137. })
  138. },
  139. initChart() {
  140. this.chart = echarts.init(this.$el, 'macarons')
  141. this.setOptions(this.chartData)
  142. }
  143. }
  144. }
  145. </script>