PicEcharts.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React from "react";
  2. import EChart, {type EChartProps} from "../echarts";
  3. // 饼图特定属性接口
  4. export type PieChartProps = {
  5. // 饼图数据
  6. data: { value: number; name: string }[];
  7. // 饼图标题
  8. title?: string;
  9. // 饼图半径配置
  10. radius?: [string, string];
  11. // 饼图颜色配置
  12. colors?: string[];
  13. // 其他EChart通用属性
  14. } & Omit<EChartProps, "option">;
  15. const PieChart = ({
  16. data,
  17. title = "统计数据",
  18. radius = ["40%", "70%"],
  19. colors,
  20. ...props
  21. }: PieChartProps) => {
  22. // 默认饼图配置
  23. const defaultOption = {
  24. // title: {
  25. // text: title,
  26. // left: "center",
  27. // textStyle: {
  28. // color: "#fff", // 标题文字颜色
  29. // },
  30. // },
  31. tooltip: {
  32. trigger: "item",
  33. formatter: "{a} <br/>{b}: {c} ({d}%)",
  34. },
  35. legend: {
  36. orient: "vertical",
  37. top:20,
  38. right: 10,
  39. textStyle: {
  40. color: "#fff", // 图例文字颜色
  41. },
  42. },
  43. series: [
  44. {
  45. name: "统计",
  46. type: "pie",
  47. radius: radius,
  48. // 添加center属性控制饼图位置,[left, top],百分比值相对于容器
  49. center: ['40%', '50%'], // 调整第一个值(左右位置),越小越靠左
  50. avoidLabelOverlap: false,
  51. itemStyle: {
  52. borderRadius: 10,
  53. borderColor: "#3a3899", // 饼图块边框颜色
  54. borderWidth: 2,
  55. },
  56. label: {
  57. show: false,
  58. position: "center",
  59. color: "#fff",
  60. },
  61. emphasis: {
  62. label: {
  63. show: true,
  64. fontSize: 16,
  65. fontWeight: "bold",
  66. color: "#fff",
  67. },
  68. },
  69. labelLine: {
  70. show: false,
  71. },
  72. data: data,
  73. color: colors || ["#f5222d", "#faad14", "#52c41a", "#1890ff", "#722ed1"],
  74. },
  75. ],
  76. };
  77. return <EChart option={defaultOption} {...props} />;
  78. };
  79. export default PieChart;