TrendChart.tsx 2.29 KB
export function TrendChart() {
  const data = [30, 42, 38, 55, 60, 48, 72, 80, 68, 90, 85, 98];
  const labels = [
    '1月',
    '2月',
    '3月',
    '4月',
    '5月',
    '6月',
    '7月',
    '8月',
    '9月',
    '10月',
    '11月',
    '12月',
  ];
  const w = 720;
  const h = 240;
  const padL = 16;
  const padR = 16;
  const padT = 18;
  const padB = 28;
  const max = 100;
  const step = (w - padL - padR) / (data.length - 1);
  const px = (i: number) => padL + i * step;
  const py = (v: number) => padT + (1 - v / max) * (h - padT - padB);
  const line = data
    .map((v, i) => `${i ? 'L' : 'M'}${px(i).toFixed(1)} ${py(v).toFixed(1)}`)
    .join(' ');
  const area = `${line} L${px(data.length - 1).toFixed(1)} ${(h - padB).toFixed(1)} L${px(0).toFixed(1)} ${(h - padB).toFixed(1)} Z`;
  const grid = [0, 25, 50, 75, 100];
  return (
    <svg viewBox={`0 0 ${w} ${h}`} width="100%" style={{ display: 'block' }}>
      <defs>
        <linearGradient id="ujArea" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#2B5CE6" stopOpacity="0.28" />
          <stop offset="100%" stopColor="#2B5CE6" stopOpacity="0" />
        </linearGradient>
      </defs>
      {grid.map((g) => (
        <g key={g}>
          <line x1={padL} x2={w - padR} y1={py(g)} y2={py(g)} stroke="#EEF1F6" strokeWidth="1" />
          <text x={2} y={py(g) + 4} fontSize="10" fill="#AAB3C2">
            {g}
          </text>
        </g>
      ))}
      <path d={area} fill="url(#ujArea)" />
      <path
        d={line}
        fill="none"
        stroke="#2B5CE6"
        strokeWidth="2.6"
        strokeLinejoin="round"
        strokeLinecap="round"
      />
      {data.map((v, i) =>
        i === data.length - 1 ? (
          <g key={i}>
            <circle cx={px(i)} cy={py(v)} r="5" fill="#fff" stroke="#2B5CE6" strokeWidth="2.6" />
            <text
              x={px(i)}
              y={py(v) - 12}
              fontSize="12"
              fontWeight={700}
              fill="#2B5CE6"
              textAnchor="middle"
            >
              {v}
            </text>
          </g>
        ) : null,
      )}
      {labels.map((lb, i) => (
        <text key={lb} x={px(i)} y={h - 8} fontSize="10" fill="#AAB3C2" textAnchor="middle">
          {i % 2 === 0 ? lb : ''}
        </text>
      ))}
    </svg>
  );
}