-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathAxisLinear.tsx
269 lines (244 loc) · 8.02 KB
/
AxisLinear.tsx
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import React from 'react'
import { Axis, AxisLinear } from '../types'
import { translate } from '../utils/Utils'
import useChartContext from '../utils/chartContext'
//
import useMeasure from './AxisLinear.useMeasure'
export default function AxisLinearComp<TDatum>(axis: Axis<TDatum>) {
const [showRotated, setShowRotated] = React.useState(false)
const { getOptions, gridDimensions, width, height } =
useChartContext<TDatum>()
const { dark, showDebugAxes } = getOptions()
const elRef = React.useRef<SVGGElement>(null)
useMeasure({
axis,
elRef,
gridDimensions,
showRotated,
setShowRotated,
})
const renderAxis = (isOuter: boolean) => {
const isRotated = !isOuter && showRotated
const scale = isOuter ? axis.outerScale : axis.scale
const [rangeStart, rangeEnd] = scale.range()
const getTicks = () => {
const anyAxis = axis as AxisLinear<TDatum>
if ((anyAxis as AxisLinear<TDatum>).outerScale.ticks!) {
if (typeof anyAxis.tickCount === 'number') {
return anyAxis.outerScale.ticks(anyAxis.tickCount)
}
const autoSpacing = anyAxis.isVertical ? 40 : 80
const range = anyAxis.outerScale.range()
const num = Math.abs(range[1] - range[0]) / autoSpacing
return anyAxis.outerScale.ticks(num)
}
return anyAxis.outerScale.domain()
}
const resolvedHeight = isOuter ? height : gridDimensions.height
const resolvedWidth = isOuter ? width : gridDimensions.width
const [lineFrom, lineTo] =
axis.position === 'left'
? [
{ x: 0, y: rangeStart },
{ x: 0, y: rangeEnd },
]
: axis.position === 'right'
? [
{ x: resolvedWidth, y: rangeStart },
{ x: resolvedWidth, y: rangeEnd },
]
: axis.position === 'top'
? [
{ x: rangeStart, y: 0 },
{ x: rangeEnd, y: 0 },
]
: [
{ x: rangeStart, y: resolvedHeight },
{ x: rangeEnd, y: resolvedHeight },
]
const ticks = getTicks().map(tick => {
const px = getTickPx(scale, tick)
const [from, to, gridTo] =
axis.position === 'left'
? [
{ x: 0, y: px },
{ x: -8, y: px },
{ x: resolvedWidth, y: px },
]
: axis.position === 'right'
? [
{ x: resolvedWidth, y: px },
{ x: resolvedWidth + 8, y: px },
{ x: 0, y: px },
]
: axis.position === 'top'
? [
{ x: px, y: 0 },
{ x: px, y: -8 },
{ x: px, y: resolvedHeight },
]
: [
{ x: px, y: resolvedHeight },
{ x: px, y: resolvedHeight + 8 },
{ x: px, y: 0 },
]
return {
value: tick,
from,
to,
gridTo,
}
})
// For vertical axis find height, for horizontal width
// if not enough ticks, default to 0
const bandDim =
ticks.length > 1
? axis.isVertical
? ticks[0].from.y - ticks[1].from.y
: ticks[1].from.x - ticks[0].from.x
: 0
return (
<g
key={`Axis-Group ${isOuter ? 'outer' : 'inner'}`}
className={`Axis-Group ${isOuter ? 'outer' : 'inner'}`}
style={{
transform: isOuter
? undefined
: translate(gridDimensions.left, gridDimensions.top),
}}
>
<g
className={`Axis`}
style={{
...(isOuter
? {
opacity: showDebugAxes ? 0.5 : 0,
pointerEvents: 'none',
}
: {
opacity: 1,
pointerEvents: 'all',
}),
}}
>
<g className="domainAndTicks">
<line
className="domain"
x1={lineFrom.x}
y1={lineFrom.y}
x2={lineTo.x}
y2={lineTo.y}
stroke={dark ? 'rgba(255,255,255, .2)' : 'rgba(0,0,0, .2)'}
/>
{ticks.map((tick, i) => {
let { x: tickLabelX, y: tickLabelY } = tick.to
if (axis.position === 'top') {
tickLabelY -= 5
} else if (axis.position === 'bottom') {
tickLabelY += 5
} else if (axis.position === 'left') {
tickLabelX -= 5
} else if (axis.position === 'right') {
tickLabelX += 5
}
return (
<g key={`vx-tick-${tick}-${i}`} className={'tick'}>
{!isOuter ? (
<line
x1={tick.from.x}
y1={tick.from.y}
x2={tick.to.x}
y2={tick.to.y}
stroke={
dark ? 'rgba(255,255,255, .2)' : 'rgba(0,0,0, .2)'
}
/>
) : null}
<text
className="tickLabel"
style={{
fontSize: 10,
fill: dark ? 'rgba(255,255,255, .7)' : 'rgba(0,0,0, .7)',
dominantBaseline: isRotated
? 'central'
: axis.position === 'bottom'
? 'hanging'
: axis.position === 'top'
? 'alphabetic'
: 'central',
textAnchor: isRotated
? 'end'
: axis.position === 'right'
? 'start'
: axis.position === 'left'
? 'end'
: 'middle',
}}
transform={`translate(${tickLabelX}, ${tickLabelY}) rotate(${
isRotated ? (axis.position === 'top' ? 60 : -60) : 0
})`}
>
{(axis as AxisLinear<any>).formatters.scale(
tick.value as number
)}
</text>
</g>
)
})}
</g>
{axis.alternatingBackgroundColor ? (
<g className="background-bands">
{ticks.map((tick, i) => {
return !isOuter && i % 2 ? (
<rect
key={`vx-band-${tick}-${i}`}
fill={axis.alternatingBackgroundColor}
// the shift to the left by bandDim is so that each band is drawn "to the left" of the tick (none will overflow)
x={axis.isVertical ? tick.from.x : tick.to.x - bandDim}
y={axis.isVertical ? tick.from.y : tick.gridTo.y}
width={axis.isVertical ? tick.gridTo.x : bandDim}
height={axis.isVertical ? bandDim : tick.from.y}
></rect>
) : null
})}
</g>
) : null}
<g className="grid">
{ticks.map((tick, i) => {
return (
<g key={`vx-tick-${tick}-${i}`} className={'tick'}>
{(axis.showGrid ?? true) && !isOuter ? (
<line
x1={tick.from.x}
y1={tick.from.y}
x2={tick.gridTo.x}
y2={tick.gridTo.y}
stroke={
dark ? 'rgba(255,255,255, .05)' : 'rgba(0,0,0, .05'
}
/>
) : null}
</g>
)
})}
</g>
</g>
</g>
)
}
return axis.show ? (
<g ref={elRef}>
{renderAxis(false)}
{renderAxis(true)}
</g>
) : null
}
function getTickPx<TDatum>(scale: Axis<TDatum>['scale'], value: any) {
let px = scale(value) ?? NaN
// @ts-ignore
if (scale.bandwidth) {
// @ts-ignore
return px + scale.bandwidth() / 2
}
return px
}