ExecutionChart.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div ref="chartContainer" style="width: 100%; height: 300px"></div>
  3. </template>
  4. <script setup lang="ts">
  5. import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
  6. import * as echarts from 'echarts'
  7. interface ChartData {
  8. categories: string[]
  9. successful: number[]
  10. failed: number[]
  11. }
  12. interface Props {
  13. data: ChartData
  14. title?: string
  15. }
  16. const props = withDefaults(defineProps<Props>(), {
  17. title: '执行统计'
  18. })
  19. const chartContainer = ref<HTMLElement>()
  20. let chart: echarts.ECharts | null = null
  21. const initChart = () => {
  22. if (!chartContainer.value) return
  23. chart = echarts.init(chartContainer.value)
  24. const option: echarts.EChartsOption = {
  25. tooltip: {
  26. trigger: 'axis',
  27. axisPointer: {
  28. type: 'shadow'
  29. }
  30. },
  31. legend: {
  32. data: ['Successful', 'Failed'],
  33. top: 0
  34. },
  35. xAxis: {
  36. type: 'category',
  37. data: props.data.categories,
  38. boundaryGap: true
  39. },
  40. yAxis: {
  41. type: 'value',
  42. splitLine: {
  43. lineStyle: {
  44. color: '#f0f0f0'
  45. }
  46. }
  47. },
  48. series: [
  49. {
  50. name: 'Successful',
  51. data: props.data.successful,
  52. type: 'bar',
  53. itemStyle: {
  54. color: '#13c2c2'
  55. },
  56. barWidth: '30%',
  57. barGap: '30%'
  58. },
  59. {
  60. name: 'Failed',
  61. data: props.data.failed,
  62. type: 'bar',
  63. itemStyle: {
  64. color: '#ff6b6b'
  65. },
  66. barWidth: '30%',
  67. barGap: '30%'
  68. }
  69. ],
  70. grid: {
  71. left: '3%',
  72. right: '4%',
  73. bottom: '3%',
  74. top: '50px',
  75. containLabel: true
  76. }
  77. }
  78. chart.setOption(option)
  79. }
  80. const handleResize = () => {
  81. chart?.resize()
  82. }
  83. watch(
  84. () => props.data,
  85. () => {
  86. if (chart) {
  87. const option: echarts.EChartsOption = {
  88. xAxis: {
  89. data: props.data.categories
  90. },
  91. series: [
  92. {
  93. data: props.data.successful
  94. },
  95. {
  96. data: props.data.failed
  97. }
  98. ]
  99. }
  100. chart.setOption(option)
  101. }
  102. },
  103. { deep: true }
  104. )
  105. onMounted(() => {
  106. initChart()
  107. window.addEventListener('resize', handleResize)
  108. })
  109. onBeforeUnmount(() => {
  110. window.removeEventListener('resize', handleResize)
  111. chart?.dispose()
  112. })
  113. </script>