Contrast.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}" />
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. require('echarts/theme/macarons') // echarts theme
  7. import resize from './mixins/resize'
  8. import { debounce } from '@/utils'
  9. // eslint-disable-next-line no-unused-vars
  10. const animationDuration = 6000
  11. export default {
  12. mixins: [resize],
  13. props: {
  14. className: {
  15. type: String,
  16. default: 'chart'
  17. },
  18. width: {
  19. type: String,
  20. default: '100%'
  21. },
  22. height: {
  23. type: String,
  24. default: '350px'
  25. },
  26. autoResize: {
  27. type: Boolean,
  28. default: true
  29. },
  30. contrastData: {
  31. type: Object,
  32. required: true
  33. }
  34. },
  35. data() {
  36. return {
  37. chart: null
  38. }
  39. },
  40. watch: {
  41. contrastData: {
  42. deep: true,
  43. handler(val) {
  44. this.setOptions(val)
  45. console.log('watch拿到的val', val)
  46. }
  47. }
  48. },
  49. mounted() {
  50. this.$nextTick(() => {
  51. this.initChart()
  52. })
  53. },
  54. beforeDestroy() {
  55. if (!this.chart) {
  56. return
  57. }
  58. this.chart.dispose()
  59. this.chart = null
  60. },
  61. methods: {
  62. initChart() {
  63. this.chart = echarts.init(this.$el, 'macarons')
  64. this.setOptions(this.contrastData)
  65. // console.log('methods拿到的pieDeptData', this.pieDeptData)
  66. },
  67. setOptions({ lastData, thisData, text, thisMinData, days } = {}) {
  68. this.chart.setOption({
  69. title: {
  70. text: text + '月维修同期对比',
  71. subtext: ''
  72. },
  73. tooltip: {
  74. trigger: 'axis'
  75. },
  76. legend: {
  77. data: ['去年', '今年']
  78. },
  79. toolbox: {
  80. show: true,
  81. feature: {
  82. dataZoom: {
  83. yAxisIndex: 'none'
  84. },
  85. dataView: { readOnly: false },
  86. magicType: { type: ['line', 'bar'] },
  87. restore: {},
  88. saveAsImage: {}
  89. }
  90. },
  91. xAxis: {
  92. type: 'category',
  93. name: '日期',
  94. boundaryGap: false,
  95. data: days
  96. },
  97. yAxis: {
  98. type: 'value',
  99. name: '元',
  100. axisLabel: {
  101. formatter: '{value}'
  102. }
  103. },
  104. grid: [
  105. { top: '25%', left: '15%' }
  106. ],
  107. series: [
  108. {
  109. name: '去年',
  110. type: 'line',
  111. // data: [11, 11, 15, 13, 12, 13, 10, 9, 11, 3],
  112. data: lastData,
  113. markPoint: {
  114. data: [
  115. // { type: 'max', name: '最大值' },
  116. // { type: 'min', name: '最小值' }
  117. ]
  118. },
  119. markLine: {
  120. data: [
  121. { type: 'average', name: '平均值' }
  122. ]
  123. }
  124. },
  125. {
  126. name: '今年',
  127. type: 'line',
  128. // data: [1, 5, 2, 5, 3, 2, 0, 9, 6, 7],
  129. data: thisData,
  130. markPoint: {
  131. data: [
  132. // { name: '周最低', value: 5, xAxis: 1, yAxis: 2 }
  133. // { type: 'max', name: '最大值' },
  134. // { type: 'min', name: '最小值' }
  135. ]
  136. },
  137. markLine: {
  138. data: [
  139. { type: 'average', name: '平均值' },
  140. [{
  141. symbol: 'none',
  142. x: '90%',
  143. yAxis: 'max'
  144. }, {
  145. symbol: 'circle',
  146. label: {
  147. normal: {
  148. position: 'start',
  149. formatter: '最大值'
  150. }
  151. },
  152. type: 'max',
  153. name: '最高点'
  154. }]
  155. ]
  156. }
  157. }
  158. ]
  159. })
  160. }
  161. }
  162. }
  163. </script>