-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMovesByRating.tsx
More file actions
309 lines (289 loc) · 9.83 KB
/
MovesByRating.tsx
File metadata and controls
309 lines (289 loc) · 9.83 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import {
Area,
XAxis,
YAxis,
Legend,
Tooltip,
AreaChart,
CartesianGrid,
ResponsiveContainer,
} from 'recharts'
import { useContext } from 'react'
import { ColorSanMapping } from 'src/types'
import { WindowSizeContext } from 'src/contexts'
interface Props {
moves: { [key: string]: number }[] | undefined
colorSanMapping: ColorSanMapping
isHomePage?: boolean
}
export const MovesByRating: React.FC<Props> = ({
moves,
colorSanMapping,
isHomePage = false,
}: Props) => {
const { isMobile } = useContext(WindowSizeContext)
const maxValue = moves
? Math.max(
...moves.flatMap((move) =>
Object.entries(move)
.filter(([key]) => key !== 'rating')
.map(([, value]) => value as number),
),
)
: 100
const domainMax = maxValue > 60 ? 100 : 60
const domain = [0, domainMax]
return (
<div
id="analysis-moves-by-rating"
className="flex h-64 w-full flex-col bg-transparent md:h-full md:rounded"
>
<h2 className="p-3 text-base text-primary md:text-sm xl:text-base">
Moves by Rating
</h2>
<ResponsiveContainer width="100%" height="90%">
<AreaChart
data={moves}
margin={{
left: 0,
right: isMobile ? 40 : 50,
bottom: 0,
top: isMobile ? 5 : 0,
}}
>
<CartesianGrid strokeDasharray="3 3" stroke="#3C3C3C" />
<XAxis
dataKey="rating"
axisLine={false}
tick={{
fill: 'white',
fontSize: 11,
}}
tickMargin={4}
ticks={
isMobile
? [600, 1000, 1400, 1800, 2200, 2600]
: [
600, 800, 1000, 1200, 1400, 1600, 1800, 2000, 2200, 2400,
2600,
]
}
/>
<YAxis
yAxisId="left"
orientation="left"
axisLine={false}
domain={domain}
tick={{
fill: 'white',
fontSize: 11,
}}
label={{
value: 'Maia Probability',
angle: -90,
fill: '#FE7F6D',
position: 'insideLeft',
dy: 46,
offset: 15,
fontWeight: 600,
fontSize: isHomePage ? 12 : 14,
}}
tickCount={isMobile ? 4 : 5}
tickMargin={isMobile ? 1 : 2}
tickLine={false}
tickFormatter={(value) => `${value}%`}
/>
<defs>
{moves &&
Object.keys(moves[0]).map((move, i) => {
if (move === 'rating') {
return null
}
return (
<linearGradient
key={`color${move}`}
id={`color${move}`}
x1="0"
y1="0"
x2="0"
y2="1"
>
<stop
offset="5%"
stopColor={colorSanMapping[move]?.color ?? '#fff'}
stopOpacity={0.5}
/>
<stop
offset="95%"
stopColor={colorSanMapping[move]?.color ?? '#fff'}
stopOpacity={0}
/>
</linearGradient>
)
})}
</defs>
{moves &&
// First, collect all the end points and sort them by y-position
(() => {
const lastIndex = moves.length - 1
// Define the type for end points
interface EndPoint {
move: string
value: number
san: string
color: string
yPosition?: number // Actual y-coordinate after rendering
adjustment?: number
}
const endPoints = Object.keys(moves[0])
.filter((move) => move !== 'rating')
.map((move) => {
const value = moves[lastIndex][move] as number
return {
move,
value,
san: colorSanMapping[move]?.san || move,
color: colorSanMapping[move]?.color ?? '#fff',
} as EndPoint
})
.sort((a, b) => a.value - b.value) // Sort by value (y-position)
// Return the original map function with adjusted positions
return Object.keys(moves[0]).map((move, i) => {
if (move === 'rating') {
return null
}
const endPoint = endPoints.find((ep) => ep.move === move)
const san = endPoint?.san || move
return (
<Area
key={i}
yAxisId="left"
dataKey={move}
dot={{
r: isMobile ? 2 : 3,
stroke: colorSanMapping[move]?.color ?? '#fff',
strokeWidth: isMobile ? 2 : 3,
}}
stroke={colorSanMapping[move]?.color ?? '#fff'}
fill={`url(#color${move})`}
strokeWidth={isMobile ? 2 : 3}
animationDuration={300}
name={san}
label={(props: {
x: number
y: number
index: number
width: number
height: number
}) => {
if (props.index !== lastIndex) return null
if (endPoint) {
endPoint.yPosition = props.y
}
const positionedEndPoints = endPoints.filter(
(ep) => ep.yPosition !== undefined,
)
positionedEndPoints.sort(
(a, b) => (a.yPosition || 0) - (b.yPosition || 0),
)
const currentIndex = positionedEndPoints.findIndex(
(ep) => ep.move === move,
)
let adjustment = 0
const minLabelHeight = isMobile ? 14 : 16
if (currentIndex > 0) {
const prevEndPoint =
positionedEndPoints[currentIndex - 1]
const prevY = prevEndPoint.yPosition || 0
const prevAdjustment = prevEndPoint.adjustment || 0
const adjustedPrevY = prevY - prevAdjustment
if (props.y - adjustedPrevY < minLabelHeight) {
adjustment =
minLabelHeight - (props.y - adjustedPrevY) + 2
}
}
if (endPoint) {
endPoint.adjustment = adjustment
}
return (
<text
x={props.x + (isMobile ? 6 : 10)}
y={props.y - adjustment}
dy={isMobile ? 3 : 4}
fontSize={11}
fontWeight={600}
fill={colorSanMapping[move]?.color ?? '#fff'}
textAnchor="start"
>
{san}
</text>
)
}}
/>
)
})
})()}
<Tooltip
content={({ payload }) => {
return (
<div
className="flex w-32 flex-col rounded-md border border-glass-border pb-2 text-white/90"
style={{
background:
'radial-gradient(ellipse 110% 90% at 20% 10%, rgba(239, 68, 68, 0.10) 0%, rgba(239, 68, 68, 0.06) 35%, transparent 75%), #171214',
}}
>
<div className="flex px-3 py-2">
{payload ? (
<p className="text-sm">{payload[0]?.payload.rating}</p>
) : null}
</div>
{payload?.map((point) => {
const san = point.name
const prob = Math.round((point.value as number) * 10) / 10
return (
<div
key={san}
className="flex items-center justify-between px-3"
>
<p
style={{
color: point.color ?? '#fff',
}}
className="text-xs"
>
{san}
</p>
<p
style={{
color: point.color ?? '#fff',
}}
className="font-mono text-xs"
>
{prob}%
</p>
</div>
)
})}
</div>
)
}}
/>
<Legend
align="right"
verticalAlign="top"
wrapperStyle={{
top: isMobile ? -10 : -14,
right: isMobile ? 10 : 20,
fontSize: 14,
}}
iconSize={0}
formatter={(value) => {
return colorSanMapping[value as string]?.san ?? value
}}
/>
</AreaChart>
</ResponsiveContainer>
</div>
)
}