BarChart.tsx 1.36 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
import { max, min, range } from "d3";
import { scaleLinear, scaleBand } from "d3-scale";
import { BarText } from "../texts";
import { Bar } from "./Bar";

type Props = {
  dataset: number[];
  dimensions: DOMRect | undefined;
};

export const BarChart = ({ dataset, dimensions }: Props) => {
  const margin = { top: 0, right: 0, bottom: 0, left: 0 };
  const width = dimensions?.width || 600;
  const height = dimensions?.height || 300;
  const innerWidth = width - margin.left - margin.right;
  const innerHeight = height - margin.bottom - margin.top;
  console.log("width:", width, "height:", height);

  const xScale = scaleBand<number>()
    .domain(range(dataset.length))
    .rangeRound([0, innerWidth])
    .paddingInner(0.05);
  const yScale = scaleLinear()
    .domain([0, max(dataset) || 0])
    .rangeRound([0, innerHeight]);

  return (
    <div>
      <svg
        width={width}
        height={height}
        // style={{ borderWidth: "2px", borderColor: "black" }}
      >
        <g transform={`translate(${margin.left},${margin.top})`}>
          <Bar
            dataset={dataset}
            xScale={xScale}
            yScale={yScale}
            height={innerHeight}
          />
          <BarText
            dataset={dataset}
            xScale={xScale}
            yScale={yScale}
            height={innerHeight}
          />
        </g>
      </svg>
    </div>
  );
};