1717 populationAdjustment : 100 ,
1818 gdpAdjustment : 100 ,
1919 rankingSeason : "Summer" ,
20- rankingYear : null
20+ rankingYear : null ,
21+ expandedPredictionCountry : null
2122 } ;
2223
2324 const medalOrder = [ "Gold" , "Silver" , "Bronze" ] ;
786787 grid . append ( "div" ) . html ( renderCountryCard ( state . country1 , data1 , maxScale ) ) ;
787788 grid . append ( "div" ) . html ( renderCountryCard ( state . country2 , data2 , maxScale ) ) ;
788789 renderRankingTable ( ) ;
790+
791+ d3 . selectAll ( ".fair-prediction-toggle" ) . on ( "click" , function ( ) {
792+ const country = this . dataset . country ;
793+
794+ state . expandedPredictionCountry =
795+ state . expandedPredictionCountry === country ? null : country ;
796+
797+ render ( ) ;
798+ } ) ;
789799 }
790800
791801 function renderCountryCard ( country , data , maxScale ) {
805815
806816 ${ allFairZero ? `
807817 <div class="fair-prediction-wrapper">
808- <div class="fair-prediction-banner">
809- How would you predict their performance if they had the same GDP and population as the other countries?
810- </div>
818+ ${ state . expandedPredictionCountry === country ? `
819+ <div class="fair-whatif-panel">
820+ <div class="fair-whatif-title">What if conditions were different?</div>
821+ <div class="fair-whatif-text">
822+ Adjust the Population and GDP sliders below to explore how changes in resources could affect this country's expected medal performance.
823+ </div>
824+
825+ ${ renderWhatIfPrediction ( country ) }
826+ </div>
827+ ` : `
828+ <button class="fair-prediction-banner fair-prediction-toggle"
829+ data-country="${ country } ">
830+ This country won no medals. Click to simulate a possible medal expectation.
831+ </button>
832+ ` }
811833 </div>
812834 ` : "" }
813835
10021024 </div>
10031025 ` ;
10041026 }
1027+
1028+ function renderWhatIfPrediction ( country ) {
1029+ const indicators = getCountryIndicators ( country ) ;
1030+ const means = getMeanIndicators ( ) ;
1031+
1032+ const allCountries = getAvailableCountries ( state . season , state . year ) ;
1033+
1034+ const medalWinningCountries = allCountries . filter ( c => {
1035+ const medals = getMedalCounts ( c ) ;
1036+ return medals . Gold + medals . Silver + medals . Bronze > 0 ;
1037+ } ) ;
1038+
1039+ const average = {
1040+ Gold : d3 . mean ( medalWinningCountries , c => getMedalCounts ( c ) . Gold ) || 0 ,
1041+ Silver : d3 . mean ( medalWinningCountries , c => getMedalCounts ( c ) . Silver ) || 0 ,
1042+ Bronze : d3 . mean ( medalWinningCountries , c => getMedalCounts ( c ) . Bronze ) || 0
1043+ } ;
1044+
1045+ const populationRatio =
1046+ indicators . population && means . meanPopulation
1047+ ? indicators . population / means . meanPopulation
1048+ : 1 ;
1049+
1050+ const gdpRatio =
1051+ indicators . gdp && means . meanGDP
1052+ ? indicators . gdp / means . meanGDP
1053+ : 1 ;
1054+
1055+ const populationEffect = Math . pow ( populationRatio , state . populationAdjustment / 100 ) ;
1056+ const gdpEffect = Math . pow ( gdpRatio , state . gdpAdjustment / 100 ) ;
1057+
1058+ const factor = populationEffect * gdpEffect ;
1059+
1060+ return `
1061+ <div class="fair-whatif-bars">
1062+ ${ medalOrder . map ( medal => {
1063+ const value = average [ medal ] * factor ;
1064+
1065+ return `
1066+ <div class="fair-medal-row">
1067+ <div class="fair-medal-label">${ medal } </div>
1068+ <div class="fair-bar-bg">
1069+ <div class="fair-bar-fill"
1070+ style="width:${ Math . min ( 100 , value * 12 ) } %; background:${ medalColors [ medal ] } "></div>
1071+ </div>
1072+ <div class="fair-medal-value">${ formatFair ( value ) } </div>
1073+ </div>
1074+ ` ;
1075+ } ) . join ( "" ) }
1076+ </div>
1077+ ` ;
1078+ }
10051079} ) ( ) ;
0 commit comments