Skip to content

Commit b467be8

Browse files
Graph visual fixes & perf improvement
1 parent caeeaac commit b467be8

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

client/src/components/sidebar/game/quick-line-chart.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ export const QuickLineChart: React.FC<LineChartProps> = ({
4040

4141
setCanvasResolution(canvas, width, height, resolution)
4242

43-
const max = Math.max(...data.map((d) => Math.max(d.team0, d.team1)))
44-
const { xScale, yScale, innerWidth, innerHeight } = getAxes(width, height, margin, { x: data.length, y: max })
43+
let maxX = -9999999
44+
let maxY = -9999999
45+
for (const d of data) {
46+
maxX = Math.max(maxX, d.round)
47+
maxY = Math.max(maxY, Math.max(d.team0, d.team1))
48+
}
49+
50+
const { xScale, yScale, innerWidth, innerHeight } = getAxes(width, height, margin, { x: maxX, y: maxY })
4551

4652
context.clearRect(0, 0, width, height)
4753

@@ -65,11 +71,11 @@ export const QuickLineChart: React.FC<LineChartProps> = ({
6571
height,
6672
margin,
6773
{
68-
range: { min: 0, max: data.length },
74+
range: { min: 0, max: maxX },
6975
options: { textColor: 'white', lineColor: 'white' }
7076
},
7177
{
72-
range: { min: 0, max: max },
78+
range: { min: 0, max: maxY },
7379
options: { textColor: 'white', lineColor: 'white' }
7480
}
7581
)
Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
import React from 'react'
2-
import assert from 'assert'
32
import { useRound } from '../../../playback/GameRunner'
43
import Round from '../../../playback/Round'
54
import { LineChartDataPoint, QuickLineChart } from './quick-line-chart'
5+
import { TeamRoundStat } from '../../../playback/RoundStat'
66

77
interface Props {
88
active: boolean
9-
property: string
9+
property: keyof TeamRoundStat
1010
propertyDisplayName: string
1111
}
12-
function hasKey<O extends Object>(obj: O, key: PropertyKey): key is keyof O {
13-
return key in obj
14-
}
1512

16-
function getChartData(round: Round, property: string): LineChartDataPoint[] {
17-
const values = [0, 1].map((index) =>
18-
round.match.stats.map((roundStat) => {
19-
const teamStat = roundStat.getTeamStat(round.match.game.teams[index])
20-
assert(hasKey(teamStat, property), `TeamStat missing property '${property}' when rendering chart`)
21-
return teamStat[property]
13+
function getChartData(round: Round, property: keyof TeamRoundStat): LineChartDataPoint[] {
14+
const teams = round.match.game.teams
15+
16+
const result: LineChartDataPoint[] = []
17+
18+
// Sparser graph as datapoints increase
19+
const interval = Math.ceil(round.roundNumber / 500)
20+
21+
for (let i = 0; i < round.roundNumber; i += interval) {
22+
const roundStat = round.match.stats[i]
23+
24+
const team0Stat = roundStat.getTeamStat(teams[0])
25+
const team1Stat = roundStat.getTeamStat(teams[1])
26+
27+
result.push({
28+
round: i + 1,
29+
team0: team0Stat[property] as number,
30+
team1: team1Stat[property] as number
2231
})
23-
)
32+
}
2433

25-
return values[0].slice(0, round.roundNumber).map((value, index) => {
26-
return {
27-
round: index + 1,
28-
team0: value as number,
29-
team1: values[1][index] as number
30-
}
31-
})
34+
return result
3235
}
3336

3437
export const ResourceGraph: React.FC<Props> = (props: Props) => {
@@ -38,7 +41,7 @@ export const ResourceGraph: React.FC<Props> = (props: Props) => {
3841
return (
3942
<div className="mt-2 w-full">
4043
<h2 className="mx-auto text-center mb-2">{props.propertyDisplayName}</h2>
41-
<QuickLineChart data={data} width={350} height={170} margin={{ top: 2, right: 20, bottom: 17, left: 30 }} />
44+
<QuickLineChart data={data} width={350} height={170} margin={{ top: 2, right: 20, bottom: 17, left: 40 }} />
4245
</div>
4346
)
4447
}

0 commit comments

Comments
 (0)