QualificationRateChart.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div ref="chartRef" class="qualification-rate-chart"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts';
  6. export default {
  7. name: 'QualificationRateChart',
  8. props: {
  9. data: {
  10. type: Object,
  11. default: () => ({
  12. productNames: [],
  13. rates: []
  14. })
  15. }
  16. },
  17. data() {
  18. return {
  19. chartInstance: null
  20. };
  21. },
  22. watch: {
  23. data: {
  24. handler() {
  25. this.updateChart();
  26. },
  27. deep: true
  28. }
  29. },
  30. mounted() {
  31. this.initChart();
  32. window.addEventListener('resize', this.handleResize);
  33. },
  34. beforeDestroy() {
  35. this.destroyChart();
  36. window.removeEventListener('resize', this.handleResize);
  37. },
  38. methods: {
  39. initChart() {
  40. const chartDom = this.$refs.chartRef;
  41. if (!chartDom) return;
  42. this.chartInstance = echarts.init(chartDom);
  43. this.updateChart();
  44. },
  45. updateChart() {
  46. if (!this.chartInstance || !this.data || !this.data.productNames || !this.data.rates) return;
  47. const { productNames, rates } = this.data;
  48. if (productNames.length === 0 || rates.length === 0) return;
  49. const option = {
  50. backgroundColor: 'transparent',
  51. grid: { left: '2%', right: '2%', bottom: '12%', top: '10%', containLabel: true },
  52. tooltip: {
  53. trigger: 'axis',
  54. formatter: '{b}: {c}',
  55. backgroundColor: '#fff',
  56. borderColor: '#2068b8',
  57. borderWidth: 1,
  58. padding: 10,
  59. textStyle: { color: '#000', fontSize: 14 },
  60. axisPointer: {
  61. type: 'line',
  62. axis: 'x',
  63. lineStyle: {
  64. color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
  65. { offset: 0, color: 'rgb(31, 56, 109,1)' },
  66. { offset: 1, color: 'rgb(1, 83, 255,1)' }
  67. ]),
  68. width: 1.5,
  69. type: 'solid'
  70. }
  71. }
  72. },
  73. xAxis: {
  74. type: 'category',
  75. data: productNames,
  76. axisTick: { show: true, lineStyle: { color: '#203454' }, alignWithLabel: true },
  77. axisLabel: { color: '#ccc', fontSize: 10, align: 'center', offset: 10 },
  78. axisLine: { lineStyle: { color: '#203454' } }
  79. },
  80. yAxis: {
  81. type: 'value',
  82. // min: 0,
  83. // max: 100,
  84. axisLabel: { color: '#ccc', fontSize: 12, formatter: '{value}%' },
  85. axisLine: { lineStyle: { color: 'rgba(255, 255, 255, 0.3)' } },
  86. splitLine: { lineStyle: { color: 'rgba(255, 255, 255, 0.1)' } }
  87. },
  88. series: [{
  89. name: '合格率',
  90. type: 'line',
  91. data: rates,
  92. symbol: 'circle',
  93. symbolSize: 8,
  94. smooth: true,
  95. itemStyle: { color: '#FFF' },
  96. lineStyle: { color: 'rgba(39, 102, 203)', width: 2 },
  97. emphasis: {
  98. itemStyle: { color: '#fff', borderColor: '#14cdc9', borderWidth: 2 }
  99. }
  100. }]
  101. };
  102. this.chartInstance.setOption(option);
  103. },
  104. destroyChart() {
  105. if (this.chartInstance) {
  106. this.chartInstance.dispose();
  107. this.chartInstance = null;
  108. }
  109. },
  110. handleResize() {
  111. if (this.chartInstance) {
  112. this.chartInstance.resize();
  113. }
  114. }
  115. }
  116. };
  117. </script>
  118. <style scoped>
  119. .qualification-rate-chart {
  120. width: 100%;
  121. height: 100%;
  122. }
  123. </style>