HorizontalBarChart.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <div ref="chart" style="width:100%;height:385px;" />
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts';
  6. import { GetDataByName } from '@/api/common'
  7. import draggable from 'vuedraggable'
  8. import Cookies from 'js-cookie'
  9. import { parseTime } from '@/utils/index.js'
  10. import Pagination from '@/components/Pagination'
  11. require('echarts/theme/macarons')
  12. export default {
  13. name: 'HorizontalBarChart',
  14. props: {
  15. chartData: {
  16. type: Object,
  17. required: true
  18. },
  19. // chartOpt: {
  20. // type: Object,
  21. // required: false
  22. // },
  23. },
  24. data() {
  25. return {
  26. chart: null
  27. }
  28. },
  29. watch: {
  30. chartData: {
  31. deep: true,
  32. handler: function (chartData) {
  33. this.renderChart(chartData)
  34. }
  35. },
  36. // schema: {
  37. // deep: true,
  38. // handler: function () {
  39. // this.renderChart(this.data)
  40. // }
  41. // }
  42. },
  43. mounted() {
  44. this.renderChart(this.chartData)
  45. this.$on('resized', this.handleResize)
  46. window.addEventListener('resize', this.handleResize)
  47. },
  48. // created() {
  49. // this.renderChart(this.chartData)
  50. // },
  51. beforeDestroy() {
  52. if (this.chart) {
  53. this.chart.dispose()
  54. }
  55. window.removeEventListener('resize', this.handleResize)
  56. },
  57. methods: {
  58. handleResize() {
  59. if (this.chart) {
  60. this.chart.resize()
  61. }
  62. },
  63. renderChart(chartData) {
  64. const option = {
  65. title: { text: '' },
  66. tooltip: { trigger: 'axis' },
  67. legend: { data:chartData.legendArr, right: 10, show: true, type: 'scroll' },
  68. grid: { top: '15%', left: '8%', right: '8%', containLabel: true },
  69. xAxis: {type: 'value', name: 'kg'},
  70. yAxis: [
  71. { type: 'category',data:chartData.xAxisArr }
  72. ],
  73. series: function (e) {
  74. var serie = [];
  75. for (var i = 0; i < chartData.xAxisArr.length; i++) {
  76. console.log(i)
  77. var item = {
  78. name: chartData.legendArr[i],
  79. data: chartData.dataArr[i],
  80. type: 'bar',
  81. emphasis: { label: { show: true, position: 'inside' } },
  82. }
  83. serie.push(item);
  84. }
  85. console.log(serie)
  86. return serie;
  87. }()
  88. }
  89. setTimeout(() => {
  90. if (!this.chart) {
  91. this.chart = echarts.init(this.$refs.chart, 'macarons')
  92. }
  93. this.chart.clear()
  94. this.chart.setOption(option)
  95. // if (this.chartOpt) {
  96. // this.chart.setOption(this.chartOpt)
  97. // }
  98. this.chart.setOption(this.chartOpt)
  99. }, 0)
  100. }
  101. }
  102. }
  103. </script>