11import React from 'react'
2- import assert from 'assert'
32import { useRound } from '../../../playback/GameRunner'
43import Round from '../../../playback/Round'
54import { LineChartDataPoint , QuickLineChart } from './quick-line-chart'
5+ import { TeamRoundStat } from '../../../playback/RoundStat'
66
77interface 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
3437export 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