BarChartWithAnimation.tsx 1.12 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
import { max, min, range } from "d3";
import { scaleLinear, scaleBand } from "d3-scale";
import { BarText } from "../texts";
import { BarWithAnimation } from "./BarWithAnimation";

type Props = {
  dataset: number[];
};
export const BarChartWithAnimation = ({ dataset }: Props) => {
  const margin = { top: 20, right: 30, bottom: 20, left: 30 };
  const width = window.innerWidth - margin.left - margin.right;
  const height = 300 - margin.bottom - margin.top;

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

  return (
    <svg
      width={width + margin.left + margin.right}
      height={height + margin.bottom + margin.top}
      style={{ borderWidth: "2px", borderColor: "black" }}
    >
      <BarWithAnimation
        dataset={dataset}
        xScale={xScale}
        yScale={yScale}
        height={height}
      />
      <BarText
        dataset={dataset}
        xScale={xScale}
        yScale={yScale}
        height={height}
      />
    </svg>
  );
};