Axis.tsx 5.14 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import React from "react";
import type { AxisScale, AxisDomain } from "d3-axis";

function identity(x: any) {
  return x;
}

/**
 * Instead of a component for each orientation (like AxisLeft, AxisRight),
 * we provide a value from this Orient object. Provide a value, like
 * Orient.left, to the `orient` prop of the Axis component
 * to place the axis on the left.
 */
export enum Orient {
  top = 1,
  right = 2,
  bottom = 3,
  left = 4,
}

function translateX(x: number) {
  return "translate(" + x + ",0)";
}

function translateY(y: number) {
  return "translate(0," + y + ")";
}

/**
 * The axis component. This renders an axis, within a
 * `g` element, for use in a chart.
 */
export const Axis = <Domain extends AxisDomain>({
  scale,
  ticks,
  tickArguments = [],
  tickValues = null,
  tickFormat = null,
  tickSize,
  tickSizeInner = 6,
  tickSizeOuter = 6,
  tickPadding = 3,
  tickTextProps = {},
  tickLineProps = {},
  domainPathProps = {},
  orient = Orient.bottom,
  offset = typeof window !== "undefined" && window.devicePixelRatio > 1
    ? 0
    : 0.5,
}: {
  /** An initialized d3 scale object, like a d3.linearScale */
  scale: AxisScale<Domain>;
  ticks?: any[];
  tickArguments?: any[];
  tickValues?: any[] | null;
  tickFormat?: any;
  tickSize?: number;
  tickSizeInner?: number;
  tickSizeOuter?: number;
  tickPadding?: number;
  /** Additional attributes to add to tick text elements, or null to omit */
  tickTextProps?: React.SVGProps<SVGTextElement> | null;
  /** Additional attributes to add to tick line elements, or null to omit */
  tickLineProps?: React.SVGProps<SVGLineElement> | null;
  /** Additional attributes to the domain path, or null to omit */
  domainPathProps?: React.SVGProps<SVGPathElement> | null;
  offset?: number;
  orient?: Orient;
}) => {
  if (tickSize) {
    tickSizeInner = tickSize;
    tickSizeOuter = tickSize;
  }

  if (ticks) {
    tickArguments = ticks;
  }

  function number(scale: AxisScale<Domain>) {
    return (d: any) => {
      const value = scale(d);
      return value === undefined ? 0 : +value;
    };
  }

  function center(scale: AxisScale<Domain>, offset: number) {
    if (scale.bandwidth) {
      offset = Math.max(0, scale.bandwidth() - offset * 2) / 2;
    }
    if ((scale as any).round()) offset = Math.round(offset);
    return (d: Domain) => {
      const value = scale(d);
      return value === undefined ? 0 : value + offset;
    };
  }

  const k = orient === Orient.top || orient === Orient.left ? -1 : 1,
    x = orient === Orient.left || orient === Orient.right ? "x" : "y",
    transform =
      orient === Orient.top || orient === Orient.bottom
        ? translateX
        : translateY;

  // Rendering
  const values =
      tickValues == null
        ? (scale as any).ticks
          ? (scale as any).ticks.apply(scale, tickArguments)
          : scale.domain()
        : tickValues,
    format =
      tickFormat == null
        ? "tickFormat" in scale
          ? (scale as any).tickFormat.apply(scale, tickArguments)
          : identity
        : tickFormat,
    spacing = Math.max(tickSizeInner, 0) + tickPadding,
    range = scale.range(),
    range0 = +range[0] + offset,
    range1 = +range[range.length - 1] + offset,
    position = (scale.bandwidth ? center : number)(scale.copy(), offset);

  const domainPath =
    orient === Orient.left || orient === Orient.right
      ? tickSizeOuter
        ? "M" +
          k * tickSizeOuter +
          "," +
          range0 +
          "H" +
          offset +
          "V" +
          range1 +
          "H" +
          k * tickSizeOuter
        : "M" + offset + "," + range0 + "V" + range1
      : tickSizeOuter
      ? "M" +
        range0 +
        "," +
        k * tickSizeOuter +
        "V" +
        offset +
        "H" +
        range1 +
        "V" +
        k * tickSizeOuter
      : "M" + range0 + "," + offset + "H" + range1;

  const lineProps = {
    [x + "2"]: k * tickSizeInner,
  };

  const textProps = {
    [x]: k * spacing,
  };

  return (
    <g>
      {values.map((tick: any, i: number) => (
        <g
          className="tick"
          key={i}
          transform={transform(position(tick) + offset)}
        >
          {tickLineProps && (
            <line stroke="currentColor" {...lineProps} {...tickLineProps} />
          )}
          {tickTextProps && (
            <text
              fill="currentColor"
              dy={
                orient === Orient.top
                  ? "0em"
                  : orient === Orient.bottom
                  ? "0.71em"
                  : "0.32em"
              }
              fontSize="10"
              fontFamily="sans-serif"
              textAnchor={
                orient === Orient.right
                  ? "start"
                  : orient === Orient.left
                  ? "end"
                  : "middle"
              }
              {...textProps}
              {...tickTextProps}
            >
              {format(tick)}
            </text>
          )}
        </g>
      ))}
      {domainPathProps && (
        <path
          className="domain"
          stroke="currentColor"
          fill="transparent"
          d={domainPath}
          {...domainPathProps}
        />
      )}
    </g>
  );
};