Donut.tsx 1.73 KB
import { dist } from '../tools/data';

export function Donut() {
  const total = dist.reduce((s, d) => s + d.value, 0);
  const r = 52;
  const c = 2 * Math.PI * r;
  let offset = 0;
  const circles = dist.map((d) => {
    const len = (d.value / total) * c;
    const el = (
      <circle
        key={d.label}
        cx={66}
        cy={66}
        r={r}
        fill="none"
        stroke={d.color}
        strokeWidth="14"
        strokeDasharray={`${len} ${c - len}`}
        strokeDashoffset={-offset}
        transform="rotate(-90 66 66)"
      />
    );
    offset += len;
    return el;
  });
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 20 }}>
      <svg width={132} height={132} viewBox="0 0 132 132">
        <circle cx={66} cy={66} r={r} fill="none" stroke="#F0F2F7" strokeWidth="14" />
        {circles}
        <text x={66} y={62} fontSize="22" fontWeight={700} fill="#1D2533" textAnchor="middle">
          {total}
        </text>
        <text x={66} y={80} fontSize="11" fill="#8A94A6" textAnchor="middle">
          门店总数
        </text>
      </svg>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
        {dist.map((d) => (
          <div key={d.label} style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <span style={{ width: 9, height: 9, borderRadius: 3, background: d.color }} />
            <span style={{ fontSize: 13, color: '#4A5568', minWidth: 72 }}>{d.label}</span>
            <span style={{ fontSize: 13, fontWeight: 600, color: '#1D2533' }}>{d.value}</span>
            <span style={{ fontSize: 12, color: '#AAB3C2' }}>
              {Math.round((d.value / total) * 100)}%
            </span>
          </div>
        ))}
      </div>
    </div>
  );
}