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