index.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <TechCard>
  3. <div class="card-title">检测对象分布</div>
  4. <BaseChart :option="chartOption" :height="180" />
  5. </TechCard>
  6. </template>
  7. <script setup lang="ts">
  8. import { computed } from 'vue'
  9. import TechCard from '../TechCard/index.vue'
  10. defineOptions({ name: 'ObjectDistributionCard' })
  11. interface GuardItem {
  12. guardType: string
  13. count: string | number
  14. }
  15. interface Props {
  16. guardList: GuardItem[]
  17. }
  18. const props = defineProps<Props>()
  19. // 提取分类名称和数量
  20. const categories = computed(() => props.guardList.map((item) => item.guardType))
  21. const counts = computed(() => props.guardList.map((item) => Number(item.count)))
  22. // 配色方案
  23. const colorPalette = ['#f39c12', '#2ecc71', '#3498db', '#e74c3c', '#9b59b6', '#1abc9c']
  24. const chartOption = computed(() => ({
  25. tooltip: {
  26. trigger: 'item',
  27. axisPointer: { type: 'shadow' },
  28. formatter: '{b}: {c} 个',
  29. },
  30. grid: {
  31. left: 40,
  32. right: 20,
  33. top: 20,
  34. bottom: 40,
  35. },
  36. xAxis: {
  37. type: 'category',
  38. data: categories.value,
  39. axisLabel: { color: '#9cc5e0' },
  40. axisTick: { show: false },
  41. axisLine: { show: false },
  42. },
  43. yAxis: {
  44. type: 'value',
  45. axisLabel: { color: '#9cc5e0' },
  46. splitLine: { show: false },
  47. },
  48. series: [
  49. {
  50. type: 'bar',
  51. data: counts.value,
  52. barWidth: 30,
  53. itemStyle: {
  54. color: (params: { dataIndex: number }) => {
  55. return colorPalette[params.dataIndex % colorPalette.length]
  56. },
  57. },
  58. label: {
  59. show: true,
  60. position: 'top',
  61. color: '#00f0ff',
  62. fontSize: 12,
  63. },
  64. },
  65. ],
  66. }))
  67. </script>
  68. <style scoped lang="less">
  69. .card-title {
  70. font-size: 16px;
  71. font-weight: bold;
  72. color: #00f0ff;
  73. margin-bottom: 12px;
  74. text-align: center;
  75. }
  76. </style>