LineChart.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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({ expectedData, actualData } = {}) {
  63. this.chart.setOption({
  64. xAxis: {
  65. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  66. boundaryGap: false,
  67. axisTick: {
  68. show: false
  69. },
  70. axisLine: {
  71. lineStyle: {
  72. color: '#666666',
  73. width: 1, //这里是为了突出显示加上的
  74. }
  75. },
  76. },
  77. grid: {
  78. left: 10,
  79. right: 10,
  80. bottom: 20,
  81. top: 30,
  82. containLabel: true
  83. },
  84. tooltip: {
  85. trigger: 'axis',
  86. axisPointer: {
  87. type: 'cross'
  88. },
  89. padding: [5, 10]
  90. },
  91. yAxis: {
  92. axisTick: {
  93. show: true
  94. },
  95. lineStyle: {
  96. type: 'dotted' // 或者'dotted'
  97. },
  98. axisLine: {
  99. lineStyle: {
  100. color: '#666666',
  101. width: 1, //这里是为了突出显示加上的
  102. }
  103. },
  104. },
  105. legend: {
  106. data: []
  107. },
  108. series: [
  109. {
  110. name: 'actual',
  111. smooth: true,
  112. type: 'line',
  113. itemStyle: {
  114. normal: {
  115. color: '##03BF8A',
  116. lineStyle: {
  117. color: '#03BF8A',
  118. width: 2
  119. },
  120. areaStyle: {
  121. color: '#f3f8ff'
  122. }
  123. }
  124. },
  125. data: [12,13,19,39,45,78,13],
  126. animationDuration: 2800,
  127. animationEasing: 'quadraticOut'
  128. }]
  129. })
  130. }
  131. }
  132. }
  133. </script>