PieChart.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!-- 饼图 -->
  2. <template>
  3. <el-card>
  4. <template #header> 产品分类饼图 </template>
  5. <div :id="id" :class="className" :style="{ height, width }" />
  6. </el-card>
  7. </template>
  8. <script setup lang="ts">
  9. import * as echarts from 'echarts';
  10. const props = defineProps({
  11. id: {
  12. type: String,
  13. default: 'pieChart'
  14. },
  15. className: {
  16. type: String,
  17. default: ''
  18. },
  19. width: {
  20. type: String,
  21. default: '200px',
  22. required: true
  23. },
  24. height: {
  25. type: String,
  26. default: '200px',
  27. required: true
  28. }
  29. });
  30. const options = {
  31. grid: {
  32. left: '2%',
  33. right: '2%',
  34. bottom: '10%',
  35. containLabel: true
  36. },
  37. legend: {
  38. top: 'bottom',
  39. textStyle: {
  40. color: '#999'
  41. }
  42. },
  43. series: [
  44. {
  45. name: 'Nightingale Chart',
  46. type: 'pie',
  47. radius: [50, 130],
  48. center: ['50%', '50%'],
  49. roseType: 'area',
  50. itemStyle: {
  51. borderRadius: 1,
  52. color: function (params: any) {
  53. //自定义颜色
  54. const colorList = ['#409EFF', '#67C23A', '#E6A23C', '#F56C6C'];
  55. return colorList[params.dataIndex];
  56. }
  57. },
  58. data: [
  59. { value: 26, name: '家用电器' },
  60. { value: 27, name: '户外运动' },
  61. { value: 24, name: '汽车用品' },
  62. { value: 23, name: '手机数码' }
  63. ]
  64. }
  65. ]
  66. };
  67. onMounted(() => {
  68. const chart = echarts.init(
  69. document.getElementById(props.id) as HTMLDivElement
  70. );
  71. chart.setOption(options);
  72. window.addEventListener('resize', () => {
  73. chart.resize();
  74. });
  75. });
  76. </script>