a2318fcee890d4ab62a4f1be8844708cdb7bea55.svn-base 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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: '350px'
  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. console.log('watch拿到的val', val)
  43. }
  44. }
  45. },
  46. mounted() {
  47. this.$nextTick(() => {
  48. this.initChart()
  49. })
  50. },
  51. beforeDestroy() {
  52. if (!this.chart) {
  53. return
  54. }
  55. this.chart.dispose()
  56. this.chart = null
  57. },
  58. methods: {
  59. initChart() {
  60. this.chart = echarts.init(this.$el, 'macarons')
  61. this.setOptions(this.chartData)
  62. console.log('methods拿到的this.chartData', this.chartData)
  63. },
  64. setOptions({ expectedData, actualData } = {}) {
  65. console.log('setOptions拿到的expectedData', expectedData)
  66. this.chart.setOption({
  67. xAxis: {
  68. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  69. boundaryGap: false,
  70. axisTick: {
  71. show: false
  72. }
  73. },
  74. grid: {
  75. left: 10,
  76. right: 10,
  77. bottom: 20,
  78. top: 30,
  79. containLabel: true
  80. },
  81. tooltip: {
  82. trigger: 'axis',
  83. axisPointer: {
  84. type: 'cross'
  85. },
  86. padding: [5, 10]
  87. },
  88. yAxis: {
  89. axisTick: {
  90. show: false
  91. }
  92. },
  93. legend: {
  94. data: ['expected', 'actual']
  95. },
  96. series: [{
  97. name: 'expected', itemStyle: {
  98. normal: {
  99. color: '#FF005A',
  100. lineStyle: {
  101. color: '#FF005A',
  102. width: 2
  103. }
  104. }
  105. },
  106. smooth: true,
  107. type: 'line',
  108. data: expectedData,
  109. animationDuration: 2800,
  110. animationEasing: 'cubicInOut'
  111. },
  112. {
  113. name: 'actual',
  114. smooth: true,
  115. type: 'line',
  116. itemStyle: {
  117. normal: {
  118. color: '#3888fa',
  119. lineStyle: {
  120. color: '#3888fa',
  121. width: 2
  122. },
  123. areaStyle: {
  124. color: '#f3f8ff'
  125. }
  126. }
  127. },
  128. data: actualData,
  129. animationDuration: 2800,
  130. animationEasing: 'quadraticOut'
  131. }]
  132. })
  133. }
  134. }
  135. }
  136. </script>