Skip to content

Commit ee52f9a

Browse files
Update HDIViz.tsx
1 parent f343f8f commit ee52f9a

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/Apps/CountryApp/HDIViz.tsx

+34-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ const AnimatedLine = styled.line`
3030
stroke-dasharray: 5;
3131
animation: ${animateLines} 4s linear infinite;
3232
`;
33+
function getRanks(array: number[]): number[] {
34+
// Create array of objects with original index and value
35+
const indexed = array.map((value, index) => ({ value, index }));
36+
37+
// Sort by value in descending order
38+
indexed.sort((a, b) => b.value - a.value);
39+
40+
// Initialize ranks array
41+
const ranks: number[] = new Array(array.length);
42+
43+
// Assign ranks
44+
let currentRank = 1;
45+
46+
for (let i = 0; i < indexed.length; i += 1) {
47+
// If this is first element or value is different from previous
48+
if (i === 0 || indexed[i].value !== indexed[i - 1].value) {
49+
currentRank = i + 1;
50+
}
51+
52+
// Assign rank to original position
53+
ranks[indexed[i].index] = currentRank;
54+
}
55+
56+
return ranks;
57+
}
3358

3459
export function HDIViz(props: Props) {
3560
const {
@@ -76,7 +101,9 @@ export function HDIViz(props: Props) {
76101

77102
const dataArray = sortBy(
78103
hdiData.countryData
79-
.filter(d => d.data.findIndex(el => el.year === year) !== -1)
104+
.filter(
105+
(d, i) => d.data.findIndex(el => el.year === year) !== -1 && i < 249,
106+
)
80107
.map(d => d.data[d.data.findIndex(el => el.year === year)]),
81108
d => d.value,
82109
).reverse();
@@ -235,8 +262,12 @@ export function HDIViz(props: Props) {
235262
</span>{' '}
236263
human development category — positioning it at{' '}
237264
<span className='bold'>
238-
{dataArray.findIndex(el => el.value === value) + 1} out of{' '}
239-
{dataArray.length}
265+
{
266+
getRanks(dataArray.map(el => el.value))[
267+
dataArray.findIndex(el => el.value === value)
268+
]
269+
}{' '}
270+
out of {dataArray.length}
240271
</span>{' '}
241272
countries and territories.
242273
</p>

0 commit comments

Comments
 (0)