BarChart.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}" />
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. import resize from './mixins/resize'
  7. export default {
  8. mixins: [resize],
  9. props: {
  10. className: {
  11. type: String,
  12. default: 'chart'
  13. },
  14. width: {
  15. type: String,
  16. default: '100%'
  17. },
  18. height: {
  19. type: String,
  20. default: '350px'
  21. },
  22. chartData: {
  23. type: Object,
  24. required: true
  25. }
  26. },
  27. data() {
  28. return {
  29. chart: null
  30. }
  31. },
  32. watch: {
  33. chartData: {
  34. deep: true,
  35. handler(val) {
  36. this.setOptions(val)
  37. }
  38. }
  39. },
  40. mounted() {
  41. this.$nextTick(() => {
  42. this.initChart()
  43. })
  44. },
  45. beforeDestroy() {
  46. if (!this.chart) {
  47. return
  48. }
  49. this.chart.dispose()
  50. this.chart = null
  51. },
  52. methods: {
  53. initChart() {
  54. this.chart = echarts.init(this.$el)
  55. this.setOptions(this.chartData)
  56. },
  57. setOptions({ labels, datasets } = {}) {
  58. const option = {
  59. tooltip: {
  60. trigger: 'axis',
  61. axisPointer: {
  62. type: 'shadow'
  63. }
  64. },
  65. legend: {
  66. data: ['计划量', '完成量', '完成率'],
  67. right: '4%',
  68. top: '0%'
  69. },
  70. grid: {
  71. left: '3%',
  72. right: '4%',
  73. bottom: '3%',
  74. containLabel: true
  75. },
  76. xAxis: {
  77. type: 'category',
  78. data: labels,
  79. axisLabel: {
  80. interval: 0,
  81. rotate: 30
  82. }
  83. },
  84. yAxis: [
  85. {
  86. type: 'value',
  87. name: '单位/个',
  88. position: 'left',
  89. axisLine: {
  90. show: true,
  91. lineStyle: {
  92. color: '#606266'
  93. }
  94. },
  95. axisLabel: {
  96. formatter: '{value}'
  97. }
  98. },
  99. {
  100. type: 'value',
  101. name: '完成率',
  102. position: 'right',
  103. max: 100,
  104. axisLine: {
  105. show: true,
  106. lineStyle: {
  107. color: '#67C23A'
  108. }
  109. },
  110. axisLabel: {
  111. formatter: '{value}%'
  112. }
  113. }
  114. ],
  115. series: [
  116. {
  117. name: '计划量',
  118. type: 'bar',
  119. data: datasets[0].data,
  120. itemStyle: {
  121. color: '#409EFF'
  122. },
  123. barMaxWidth: 30
  124. },
  125. {
  126. name: '完成量',
  127. type: 'bar',
  128. data: datasets[1].data,
  129. itemStyle: {
  130. color: '#FF9F43'
  131. },
  132. barMaxWidth: 30
  133. },
  134. {
  135. name: '完成率',
  136. type: 'line',
  137. yAxisIndex: 1,
  138. data: datasets[2].data,
  139. itemStyle: {
  140. color: '#67C23A'
  141. },
  142. lineStyle: {
  143. width: 2
  144. },
  145. symbol: 'circle',
  146. symbolSize: 6
  147. }
  148. ]
  149. }
  150. this.chart.setOption(option)
  151. }
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. .chart-container {
  157. position: relative;
  158. width: 100%;
  159. height: 100%;
  160. }
  161. </style>