categories-percent2.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <a-spin :loading="loading" style="min-width: 350px">
  3. <a-card class="general-card" :header-style="{ paddingBottom: '0' }">
  4. <template #title>
  5. <template v-if="record.accountName !== ''">
  6. {{ record.accountName }}
  7. </template>
  8. API调用记录 -
  9. {{
  10. dayjs(record.report_date).format(
  11. record.isLeaf ? 'YYYY-MM-DD HH:mm' : 'YYYY-MM-DD'
  12. )
  13. }}
  14. </template>
  15. <Chart height="200px" :option="chartOption" />
  16. </a-card>
  17. </a-spin>
  18. </template>
  19. <script lang="ts" setup>
  20. import dayjs from 'dayjs';
  21. import useLoading from '@/hooks/loading';
  22. import { defineProps, ref } from 'vue';
  23. import {
  24. TkReportRecord,
  25. TkReportParams,
  26. queryTkReportChartData,
  27. ChartDataResponse,
  28. } from '@/api/taobao';
  29. // 使用 defineProps 来定义并接收 props
  30. const props = defineProps<{
  31. record: TkReportRecord;
  32. }>();
  33. const { loading, setLoading } = useLoading(true);
  34. const chartOption = ref<any>(null);
  35. const generateCoolColor = (data: ChartDataResponse[], value: number): string => {
  36. const colorRange = [
  37. '#21CCFF', // blue-10
  38. '#1793FF', // blue-9
  39. '#1A88F6', // blue-8
  40. '#1D7DEB', // blue-7
  41. '#2172E1', // blue-6
  42. '#2467D6', // blue-5
  43. '#275CCB', // blue-4
  44. '#2B51BF', // blue-3
  45. '#2E47B4', // blue-2
  46. '#313CA9', // blue-1
  47. ];
  48. // 计算数据中的最大值和最小值
  49. const maxValue = Math.max(...data.map(item => item.value));
  50. const minValue = Math.min(...data.map(item => item.value));
  51. return colorRange[Math.round((value - minValue) / (maxValue - minValue) * (colorRange.length - 1))];
  52. };
  53. const setChartOption = (data: ChartDataResponse[]) => {
  54. return {
  55. legend: {
  56. left: 'center',
  57. data: data.map((item) => item.name),
  58. bottom: 0,
  59. icon: 'circle',
  60. itemWidth: 8,
  61. textStyle: {
  62. color: '#4E5969',
  63. },
  64. itemStyle: {
  65. borderWidth: 0,
  66. },
  67. },
  68. tooltip: {
  69. show: true,
  70. trigger: 'item',
  71. },
  72. series: [
  73. {
  74. type: 'pie',
  75. radius: ['30%', '50%'],
  76. center: ['50%', '40%'],
  77. label: {
  78. formatter: '{d}%',
  79. fontSize: 14,
  80. color: '#4E5969',
  81. },
  82. itemStyle: {
  83. borderColor: '#fff',
  84. borderWidth: 1,
  85. },
  86. data: data.map((item) => ({
  87. value: item.value,
  88. name: item.name,
  89. itemStyle: {
  90. color: generateCoolColor(data, item.value),
  91. },
  92. })),
  93. },
  94. ],
  95. };
  96. };
  97. const fetchData = async (
  98. params: TkReportParams = {
  99. current: 1,
  100. pageSize: 0,
  101. }
  102. ) => {
  103. setLoading(true);
  104. try {
  105. const { data } = await queryTkReportChartData(params);
  106. chartOption.value = setChartOption(data);
  107. } catch (err) {
  108. console.log(err);
  109. // you can report use errorHandler or other
  110. } finally {
  111. setLoading(false);
  112. }
  113. };
  114. const search = () => {
  115. fetchData({
  116. accountId: props.record.accountId,
  117. accountName: props.record.accountName,
  118. isLeaf: props.record.isLeaf,
  119. query_date: [props.record.report_date],
  120. } as unknown as TkReportParams);
  121. };
  122. search();
  123. </script>
  124. <style scoped lang="less">
  125. .popover-content {
  126. width: 600px;
  127. }
  128. </style>