Skip to content

Commit c1eac5d

Browse files
committed
better treated case when a country won no medal
1 parent 0b6a317 commit c1eac5d

2 files changed

Lines changed: 105 additions & 4 deletions

File tree

website/js/fair-distribution.js

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
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"];
@@ -786,6 +787,15 @@
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) {
@@ -805,9 +815,21 @@
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
@@ -1002,4 +1024,56 @@
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
})();

website/styles.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,33 @@ p {
11691169
font-weight: 700;
11701170
}
11711171

1172+
.fair-prediction-toggle {
1173+
cursor: pointer;
1174+
font-family: "Elms Sans", sans-serif;
1175+
}
1176+
1177+
.fair-whatif-panel {
1178+
margin-top: 12px;
1179+
padding: 14px;
1180+
border-radius: 12px;
1181+
background: rgba(255, 209, 102, 0.08);
1182+
border: 1px solid rgba(255, 209, 102, 0.35);
1183+
}
1184+
1185+
.fair-whatif-title {
1186+
font-size: 16px;
1187+
font-weight: 800;
1188+
color: #ffd166;
1189+
margin-bottom: 6px;
1190+
}
1191+
1192+
.fair-whatif-text {
1193+
font-size: 13px;
1194+
color: rgba(255,255,255,0.8);
1195+
margin-bottom: 10px;
1196+
line-height: 1.25;
1197+
}
1198+
11721199
/* =============================================
11731200
Footer
11741201
=========================================== */

0 commit comments

Comments
 (0)