BarChart2.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div :id="id" :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. id: {
  15. type: String,
  16. default: "chart"
  17. },
  18. width: {
  19. type: String,
  20. default: "100%"
  21. },
  22. height: {
  23. type: String,
  24. default: "400px"
  25. },
  26. data: {
  27. type: Object,
  28. default: {}
  29. },
  30. },
  31. data() {
  32. return {
  33. chart: null
  34. };
  35. },
  36. mounted() {
  37. this.$nextTick(() => {
  38. this.initChart();
  39. })
  40. },
  41. beforeDestroy() {
  42. if (!this.chart) {
  43. return;
  44. }
  45. this.chart.dispose();
  46. this.chart = null;
  47. },
  48. methods: {
  49. initChart() {
  50. let that = this;
  51. this.chart = echarts.init(document.getElementById(this.id));
  52. let seriesData = [], legendData = [];
  53. that.data.seriesData.forEach(function(item,index){
  54. let series = {
  55. name: '',
  56. type: 'bar',
  57. barWidth: '40%',
  58. data:[]
  59. }
  60. item.stack ? series.stack = item.stack : '';
  61. item.data ? series.data = [].concat(item.data) : '';
  62. if(item.name){
  63. series.name = item.name;
  64. legendData.push(item.name);
  65. }
  66. seriesData.push(series)
  67. })
  68. let option = {
  69. tooltip: {
  70. trigger: 'axis',
  71. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  72. type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
  73. }
  74. },
  75. color:that.data.color,
  76. legend: {
  77. left: "center",
  78. top: 10,
  79. data: legendData
  80. },
  81. grid: {
  82. top: '25%',
  83. left: '2%',
  84. bottom: '3%',
  85. containLabel: true
  86. },
  87. xAxis: [{
  88. type: 'category',
  89. data: that.data.xAxisData,
  90. axisTick: {
  91. show:false
  92. },
  93. axisLine: {
  94. show: false
  95. },
  96. }],
  97. yAxis: [{
  98. type: 'value',
  99. name: that.data.yAxisName,
  100. axisTick: {
  101. show: false
  102. },
  103. axisLine: {
  104. show: false
  105. },
  106. splitLine: {
  107. lineStyle:{
  108. color:'#EFEFEF',
  109. },
  110. },
  111. }],
  112. series: seriesData,
  113. };
  114. this.chart.setOption(option);
  115. }
  116. }
  117. };
  118. </script>