channel-chart.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <a-spin :loading="loading" style="width: 100%; height: 100%">
  3. <a-card
  4. class="general-card"
  5. :header-style="{ paddingBottom: '0' }"
  6. :body-style="{
  7. padding: '20px',
  8. height: 'calc(100% - 40px)',
  9. }"
  10. :style="{ height: '100%' }"
  11. >
  12. <template #title>
  13. {{ $t('workplace.channelChart') }}
  14. </template>
  15. <div class="chart-box" style="height: 100%">
  16. <Chart :option="chartOptionOne" />
  17. <!-- <Chart width="50%" :option="chartOptionTwo" /> -->
  18. </div>
  19. </a-card>
  20. </a-spin>
  21. </template>
  22. <script lang="ts" setup>
  23. import useLoading from '@/hooks/loading';
  24. import useChartOption from '@/hooks/chart-option';
  25. import { ref } from 'vue';
  26. import { useI18n } from 'vue-i18n';
  27. import { queryPolicyList, PolicyParams } from '@/api/list';
  28. const { t } = useI18n();
  29. const { loading, setLoading } = useLoading();
  30. const chartData1 = ref<number>(0);
  31. const chartData2 = ref<number>(0);
  32. const countSB = ref<number>(0);
  33. const countTD = ref<number>(0);
  34. const splitLineLength = '30%';
  35. const splitLineDistance = -2;
  36. const splitLineWidth = 10;
  37. function toPercentage(hold: number, total: number) {
  38. if (total === 0) {
  39. return 0; // 避免除以零的情况
  40. }
  41. return ((hold / total) * 100).toFixed(0); // 四舍五入保留两位小数
  42. }
  43. const chartOptionFn = (count: any, label: string, data: any) => {
  44. return (isDark: any) => {
  45. const fontColor = isDark ? '#ccc' : '#fff';
  46. return {
  47. title: {
  48. text: `{a|${toPercentage(data.value, count.value)}%}\n{b|${t(
  49. 'workplace.zxs'
  50. )}: }{d|${data.value} }\n{c|${t(label)}: }{d|${count.value} }`,
  51. x: 'center',
  52. y: 'center',
  53. textStyle: {
  54. rich: {
  55. a: {
  56. fontSize: 30,
  57. color: fontColor,
  58. fontWeight: 'bold',
  59. },
  60. b: {
  61. fontSize: 12,
  62. color: fontColor,
  63. padding: [10, 0, 0, 0],
  64. },
  65. c: {
  66. fontSize: 12,
  67. color: fontColor,
  68. padding: [6, 0, 0, 0],
  69. },
  70. d: {
  71. fontSize: 12,
  72. color: 'rgb(51, 122, 183)',
  73. fontWeight: 'bold',
  74. padding: [6, 0, 0, 0],
  75. },
  76. },
  77. },
  78. },
  79. series: [
  80. {
  81. type: 'pie',
  82. radius: ['86%', '70%'],
  83. silent: true,
  84. clockwise: true,
  85. startAngle: -90,
  86. z: 0,
  87. zlevel: 0,
  88. data: [
  89. {
  90. value: data.value,
  91. itemStyle: {
  92. color: {
  93. type: 'linear',
  94. x: 0,
  95. y: 0,
  96. x2: 0,
  97. y2: 1,
  98. colorStops: [
  99. {
  100. offset: 0,
  101. color: 'rgba(89, 113, 196, 0.6)', // 0% 处的颜色
  102. },
  103. {
  104. offset: 1,
  105. color: 'rgba(89, 113, 196, 1)', // 100% 处的颜色
  106. },
  107. ],
  108. global: false, // 缺省为 false
  109. },
  110. },
  111. label: {
  112. show: false,
  113. },
  114. },
  115. {
  116. value:
  117. count.value - data.value === 0 ? 0 : count.value - data.value,
  118. label: {
  119. show: false,
  120. },
  121. itemStyle: {
  122. color: isDark ? 'rgba(197, 197, 197, 1)' : '#0c336b',
  123. },
  124. },
  125. ],
  126. },
  127. {
  128. name: '',
  129. type: 'gauge',
  130. radius: '95%',
  131. center: ['50%', '50%'],
  132. startAngle: 0,
  133. endAngle: 360,
  134. splitNumber: 30,
  135. axisTick: {
  136. show: false,
  137. },
  138. splitLine: {
  139. length: splitLineLength,
  140. distance: splitLineDistance,
  141. lineStyle: {
  142. width: splitLineWidth,
  143. color: isDark ? 'rgba(35, 35, 36, 1)' : '#051F44',
  144. },
  145. },
  146. axisLabel: {
  147. show: false,
  148. },
  149. pointer: {
  150. show: false,
  151. },
  152. axisLine: {
  153. show: false,
  154. },
  155. },
  156. ],
  157. };
  158. };
  159. };
  160. const { chartOption: chartOptionOne } = useChartOption(
  161. chartOptionFn(countSB, 'workplace.zsb', chartData1)
  162. );
  163. // const { chartOption: chartOptionTwo } = useChartOption(
  164. // chartOptionFn(countTD, 'workplace.ztd', chartData2)
  165. // );
  166. const fetchData = async (
  167. params: PolicyParams = {
  168. current: 1,
  169. pageSize: 20,
  170. Status: 'All',
  171. }
  172. ) => {
  173. setLoading(true);
  174. try {
  175. const { data } = await queryPolicyList(params);
  176. const res = Array.isArray(data) ? data : [];
  177. countSB.value = res.length;
  178. countTD.value = res.length;
  179. const zxs = res.filter((item) => item.status === 'Online').length;
  180. chartData1.value = zxs;
  181. chartData2.value = zxs;
  182. setLoading(false);
  183. } catch (err) {
  184. // you can report use errorHandler or other
  185. } finally {
  186. setLoading(false);
  187. }
  188. };
  189. fetchData();
  190. </script>
  191. <style scoped lang="scss"></style>