Pie.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <div ref="chartRef" :style="{ height, width }"></div>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, PropType, ref, Ref, watchEffect, reactive, watch } from 'vue';
  6. import { useECharts } from '/@/hooks/web/useECharts';
  7. import { cloneDeep } from 'lodash-es';
  8. export default defineComponent({
  9. name: 'Pie',
  10. props: {
  11. chartData: {
  12. type: Array,
  13. default: () => [],
  14. },
  15. size: {
  16. type: Object,
  17. default: () => {},
  18. },
  19. option: {
  20. type: Object,
  21. default: () => ({}),
  22. },
  23. width: {
  24. type: String as PropType<string>,
  25. default: '100%',
  26. },
  27. height: {
  28. type: String as PropType<string>,
  29. default: 'calc(100vh - 78px)',
  30. },
  31. },
  32. emits: ['click'],
  33. setup(props, { emit }) {
  34. const chartRef = ref<HTMLDivElement | null>(null);
  35. const { setOptions, getInstance, resize } = useECharts(chartRef as Ref<HTMLDivElement>);
  36. const option = reactive({
  37. tooltip: {
  38. formatter: '{b} ({c})',
  39. },
  40. series: [
  41. {
  42. type: 'pie',
  43. radius: '72%',
  44. center: ['50%', '55%'],
  45. data: [],
  46. labelLine: { show: true },
  47. label: {
  48. show: true,
  49. formatter: '{b} \n ({d}%)',
  50. color: '#B1B9D3',
  51. },
  52. },
  53. ],
  54. });
  55. watchEffect(() => {
  56. props.chartData && initCharts();
  57. });
  58. /**
  59. * 监听拖拽大小变化
  60. */
  61. watch(
  62. () => props.size,
  63. () => {
  64. resize();
  65. },
  66. {
  67. immediate: true,
  68. }
  69. );
  70. function initCharts() {
  71. if (props.option) {
  72. Object.assign(option, cloneDeep(props.option));
  73. }
  74. option.series[0].data = props.chartData;
  75. setOptions(option);
  76. resize();
  77. getInstance()?.off('click', onClick);
  78. getInstance()?.on('click', onClick);
  79. }
  80. function onClick(params) {
  81. emit('click', params);
  82. }
  83. return { chartRef };
  84. },
  85. });
  86. </script>