LineChart.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}" />
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. require('echarts/theme/macarons') // echarts theme
  7. import resize from './mixins/resize'
  8. export default {
  9. mixins: [resize],
  10. props: {
  11. className: {
  12. type: String,
  13. default: 'chart'
  14. },
  15. width: {
  16. type: String,
  17. default: '100%'
  18. },
  19. height: {
  20. type: String,
  21. default: '300px'
  22. },
  23. autoResize: {
  24. type: Boolean,
  25. default: true
  26. },
  27. chartData: {
  28. type: Object,
  29. required: true
  30. }
  31. },
  32. data() {
  33. return {
  34. chart: null
  35. }
  36. },
  37. watch: {
  38. chartData: {
  39. deep: true,
  40. handler(val) {
  41. this.setOptions(val)
  42. }
  43. }
  44. },
  45. mounted() {
  46. this.$nextTick(() => {
  47. this.initChart()
  48. })
  49. },
  50. beforeDestroy() {
  51. if (!this.chart) {
  52. return
  53. }
  54. this.chart.dispose()
  55. this.chart = null
  56. },
  57. methods: {
  58. initChart() {
  59. this.chart = echarts.init(this.$el, 'macarons')
  60. this.setOptions(this.chartData)
  61. },
  62. setOptions({ x, y1,y2 } = {}) {
  63. console.log(x,y1,y2)
  64. this.chart.setOption({
  65. xAxis: {
  66. data: x,
  67. boundaryGap: false,
  68. axisTick: {
  69. show: false
  70. },
  71. axisLine: {
  72. lineStyle: {
  73. color: '#666666',
  74. width: 1, //这里是为了突出显示加上的
  75. }
  76. },
  77. },
  78. grid: {
  79. left: 10,
  80. right: 10,
  81. bottom: 20,
  82. top: 30,
  83. containLabel: true
  84. },
  85. tooltip: {
  86. trigger: 'axis',
  87. axisPointer: {
  88. type: 'cross'
  89. },
  90. padding: [5, 10]
  91. },
  92. yAxis: {
  93. axisTick: {
  94. show: true
  95. },
  96. lineStyle: {
  97. type: 'dotted' // 或者'dotted'
  98. },
  99. axisLine: {
  100. lineStyle: {
  101. color: '#666666',
  102. width: 1, //这里是为了突出显示加上的
  103. }
  104. },
  105. },
  106. legend: {
  107. data: []
  108. },
  109. series: [{
  110. name: 'expected', itemStyle: {
  111. normal: {
  112. color: '#FFAB2B',
  113. lineStyle: {
  114. color: '#FFAB2B',
  115. width: 2
  116. },
  117. areaStyle: {
  118. color: '#FFAB2B',
  119. opacity: 0.2,
  120. }
  121. }
  122. },
  123. smooth: true,
  124. type: 'line',
  125. data: y1,
  126. animationDuration: 2800,
  127. animationEasing: '离岗告警'
  128. },
  129. {
  130. name: 'actual',
  131. smooth: true,
  132. type: 'line',
  133. itemStyle: {
  134. normal: {
  135. color: '##03BF8A',
  136. lineStyle: {
  137. color: '#03BF8A',
  138. width: 2
  139. },
  140. areaStyle: {
  141. color: '#f3f8ff'
  142. }
  143. }
  144. },
  145. data: y2,
  146. animationDuration: 2800,
  147. animationEasing: '玩手机告警'
  148. }]
  149. })
  150. }
  151. }
  152. }
  153. </script>