血透系统PC前端

BarChart.vue 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}"></div>
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. require('echarts/theme/macarons') // echarts theme
  7. import { debounce } from '@/utils'
  8. const animationDuration = 6000
  9. export default {
  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: '300px'
  22. },
  23. title:{
  24. type: String,
  25. default: 'chart'
  26. },
  27. options:{
  28. type:Object,
  29. default:function(){
  30. return {}
  31. }
  32. }
  33. },
  34. data() {
  35. return {
  36. chart: null,
  37. chartOptions:{},
  38. }
  39. },
  40. mounted() {
  41. this.initChart()
  42. this.__resizeHanlder = debounce(() => {
  43. if (this.chart) {
  44. this.chart.resize()
  45. }
  46. }, 100)
  47. window.addEventListener('resize', this.__resizeHanlder)
  48. },
  49. beforeDestroy() {
  50. if (!this.chart) {
  51. return
  52. }
  53. window.removeEventListener('resize', this.__resizeHanlder)
  54. this.chart.dispose()
  55. this.chart = null
  56. },
  57. created(){
  58. },
  59. watch:{
  60. "options":function(){
  61. this.chartOptions = this.options;
  62. this.initChart()
  63. this.__resizeHanlder = debounce(() => {
  64. if (this.chart) {
  65. this.chart.resize()
  66. }
  67. }, 100)
  68. window.addEventListener('resize', this.__resizeHanlder)
  69. }
  70. },
  71. methods: {
  72. initChart() {
  73. console.log("this.chartOptions", this.chartOptions);
  74. this.chart = echarts.init(this.$el, 'macarons')
  75. this.chart.setOption({
  76. title: {
  77. text: this.title,
  78. },
  79. tooltip: {
  80. trigger: 'axis',
  81. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  82. type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
  83. }
  84. },
  85. grid: {
  86. left: '2%',
  87. right: '2%',
  88. bottom: '3%',
  89. containLabel: true
  90. },
  91. xAxis: [{
  92. type: 'category',
  93. data: this.chartOptions.xAxis,
  94. axisTick: {
  95. alignWithLabel: true
  96. }
  97. }],
  98. yAxis: [{
  99. type: 'value',
  100. axisTick: {
  101. show: false
  102. }
  103. }],
  104. series: [{
  105. name: '',
  106. type: 'bar',
  107. stack: 'vistors',
  108. barWidth: '60%',
  109. data: this.chartOptions.series,
  110. animationDuration
  111. }]
  112. })
  113. }
  114. }
  115. }
  116. </script>