Chart.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template >
  2. <div ref="chart" style="height: 200px;"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. export default {
  7. name: 'ChartComponent',
  8. props: {
  9. items: Array,
  10. label: {
  11. type: Array,
  12. default: () => []
  13. }
  14. },
  15. data() {
  16. return {
  17. chart: null
  18. }
  19. },
  20. mounted() {
  21. // 初始化表格
  22. this.init()
  23. },
  24. watch: {
  25. items: {
  26. handler() {
  27. this.reload()
  28. },
  29. deep: true
  30. },
  31. label: {
  32. handler() {
  33. console.log('-------',this.label)
  34. this.chart.setOption({
  35. xAxis: {
  36. data: this.label
  37. }
  38. });
  39. },
  40. deep: true
  41. }
  42. },
  43. methods: {
  44. init() {
  45. this.chart = echarts.init(this.$refs.chart)
  46. this.chart.setOption({
  47. tooltip: {
  48. trigger: "axis",
  49. formatter: "{b} : {c}",
  50. axisPointer: {
  51. // 坐标轴指示器,坐标轴触发有效
  52. type: "shadow", // 默认为直线,可选为:'line' | 'shadow'
  53. },
  54. backgroundColor: "rgba(0,224,255,0.46)",
  55. borderColor: "rgba(0,213,255, .6)",
  56. textStyle: {
  57. color: '#fff',
  58. }
  59. },
  60. xAxis: {
  61. type: 'category',
  62. axisLine: {
  63. show: false,
  64. },
  65. axisTick: {
  66. //y轴刻度线
  67. show: false,
  68. },
  69. axisLabel: {
  70. color: '#fff',
  71. fontSize: 8
  72. },
  73. data: this.label
  74. },
  75. yAxis: {
  76. type: 'value',
  77. //坐标轴
  78. axisLine: {
  79. show: false,
  80. },
  81. axisLabel: {
  82. color: '#fff'
  83. },
  84. axisTick: {
  85. //y轴刻度线
  86. show: false,
  87. },
  88. //分格线
  89. splitLine: {
  90. lineStyle: {
  91. color: "#4784e8",
  92. type: 'dashed'
  93. },
  94. },
  95. },
  96. grid: {
  97. right: '8px',
  98. top: '20px'
  99. },
  100. series: [
  101. {
  102. data: this.items,
  103. type: 'bar',
  104. barWidth: 10,
  105. barGap: 20,
  106. showBackground: true,
  107. backgroundStyle: {
  108. color: 'rgba(180, 180, 180, 0.2)'
  109. },
  110. itemStyle: {
  111. normal: {
  112. color: '#0085FF'
  113. },
  114. emphasis: {
  115. color: '#00E0FF'
  116. }
  117. }
  118. }
  119. ]
  120. })
  121. this.chart
  122. },
  123. reload() {
  124. // 重载数据
  125. if (this.chart) {
  126. this.chart.setOption({
  127. series: [
  128. {
  129. data: this.items
  130. }
  131. ]
  132. });
  133. }
  134. }
  135. }
  136. }
  137. </script>
  138. <style scoped lang='less'>
  139. </style>