AxisScaleLinear.tsx 923 Bytes
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React, { useMemo } from "react";
import { ScaleLinear, scaleLinear } from "d3-scale";

type Props = {
  x: number;
  y: number;
  scale: ScaleLinear<number, number>;
};

export const AxisScaleLinear = ({ x = 0, y = 0, scale }: Props) => {
  const orient = "bottom";

  const ticks = useMemo(
    () =>
      scale.ticks().map((value) => ({
        value,
        xOffset: scale(value),
      })),
    []
  );

  // console.log("ticks:", ticks);

  const range = scale.range();

  return (
    <g fontSize="10px" textAnchor="middle" transform={`translate(${x}, ${y})`}>
      <path d={`M ${range[0]} 0.5 H ${range[1]}`} stroke="currentColor" />
      {ticks.map(({ value, xOffset }) => (
        <g key={value} transform={`translate(${xOffset}, 0)`}>
          <line y2={6} stroke="currentColor" />
          <text key={value} dy="1.5em">
            {value}
          </text>
        </g>
      ))}
    </g>
  );
};