classify.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <hc-charts ref="classifyCharts" :option="classifyChartsOption" dark />
  3. </template>
  4. <script setup>
  5. import { onMounted, ref, watch } from 'vue'
  6. const props = defineProps({
  7. datas: {
  8. type: Array,
  9. default: () => ([]),
  10. },
  11. })
  12. //渲染完成
  13. onMounted(() => {
  14. setClassifyChartsOption(props.datas)
  15. })
  16. //监听值变化
  17. watch(() => props.datas, (val) => {
  18. setClassifyChartsOption(val)
  19. })
  20. //处理数据
  21. const classifyCharts = ref(null)
  22. const classifyChartsOption = ref({})
  23. const setClassifyChartsOption = (data) => {
  24. const colors = ['#F9D949', '#EE8031', '#54B8BC']
  25. classifyChartsOption.value = {
  26. backgroundColor: '',
  27. color: colors,
  28. tooltip: {
  29. trigger: 'item',
  30. },
  31. legend: {
  32. top: '5%',
  33. left: 'center',
  34. },
  35. series: [
  36. {
  37. name: '档案分类占比统计',
  38. type: 'pie',
  39. radius: '50%',
  40. data: data.map((item, index) => {
  41. item.label = {
  42. color: colors[index],
  43. formatter(param) {
  44. const percent = ' (' + param.percent * 2 + '%)'
  45. return `${item.name}${percent}\n${item.value}`
  46. },
  47. }
  48. return item
  49. }),
  50. label: {
  51. alignTo: 'edge',
  52. minMargin: 25,
  53. edgeDistance: 25,
  54. lineHeight: 25,
  55. },
  56. labelLine: {
  57. length: 15,
  58. length2: 0,
  59. maxSurfaceAngle: 80,
  60. },
  61. labelLayout: function (params) {
  62. const isLeft = params.labelRect.x < classifyCharts.value?.getWidth() / 2
  63. const points = params.labelLinePoints
  64. points[2][0] = isLeft ? params.labelRect.x : params.labelRect.x + params.labelRect.width
  65. return { labelLinePoints: points }
  66. },
  67. },
  68. ],
  69. }
  70. }
  71. </script>