BarChart.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. const animationDuration = 6000
  9. export default {
  10. mixins: [resize],
  11. props: {
  12. className: {
  13. type: String,
  14. default: 'chart'
  15. },
  16. width: {
  17. type: String,
  18. default: '100%'
  19. },
  20. height: {
  21. type: String,
  22. default: '300px'
  23. }
  24. },
  25. data() {
  26. return {
  27. chart: null
  28. }
  29. },
  30. mounted() {
  31. this.$nextTick(() => {
  32. this.initChart()
  33. })
  34. },
  35. beforeDestroy() {
  36. if (!this.chart) {
  37. return
  38. }
  39. this.chart.dispose()
  40. this.chart = null
  41. },
  42. methods: {
  43. initChart() {
  44. this.chart = echarts.init(this.$el, 'macarons')
  45. this.setOptions(this.chartData)
  46. },
  47. setOptions({ expectedData, actualData,linedata } = {}) {
  48. this.chart.setOption({
  49. tooltip: {
  50. trigger: 'axis',
  51. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  52. type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
  53. }
  54. },
  55. grid: {
  56. top: 10,
  57. left: '2%',
  58. right: '2%',
  59. bottom: '3%',
  60. containLabel: true
  61. },
  62. xAxis: [{
  63. type: 'category',
  64. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  65. axisTick: {
  66. alignWithLabel: true
  67. }
  68. }],
  69. yAxis: [{
  70. type: 'value',
  71. axisTick: {
  72. show: false
  73. }
  74. }],
  75. series: [{
  76. name: 'pageA',
  77. type: 'bar',
  78. stack: 'vistors',
  79. barWidth: '60%',
  80. data: [79, 52, 200, 334, 390, 330, 220],
  81. animationDuration
  82. }, {
  83. name: 'pageB',
  84. type: 'bar',
  85. stack: 'vistors',
  86. barWidth: '60%',
  87. data: [80, 52, 200, 334, 390, 330, 220],
  88. animationDuration
  89. }, {
  90. name: 'pageC',
  91. type: 'bar',
  92. stack: 'vistors',
  93. barWidth: '60%',
  94. data: [30, 52, 200, 334, 390, 330, 220],
  95. animationDuration
  96. }]
  97. })
  98. }
  99. // }
  100. }
  101. }
  102. </script>