Skip to content

Commit

Permalink
✨ (line legend) improve perf
Browse files Browse the repository at this point in the history
Co-authored-by: Marcel Gerber <mgerber59@gmail.com>

🤖 style: prettify code
  • Loading branch information
sophiamersmann committed Jun 11, 2024
1 parent 77d1791 commit 6bc7239
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/@ourworldindata/grapher/src/lineLegend/LineLegend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,22 @@ export class LineLegend extends React.Component<{
})

// pick the candidate with the highest score
const maxCandidate = maxBy(candidateScores, (s) => s[1])![0]
const picked = maybePickCandidate(maxCandidate)

// if the highest scoring candidate doesn't fit,
// remove it from the candidates and continue
if (!picked) candidates.delete(maxCandidate)
// that fits into the available space
let picked = false
while (!picked && candidateScores.length > 0) {
const maxCandidateArr = maxBy(candidateScores, (s) => s[1])!
const maxCandidate = maxCandidateArr[0]
picked = maybePickCandidate(maxCandidate)

// if the highest scoring candidate doesn't fit,
// remove it from the candidates and continue
if (!picked) {
candidates.delete(maxCandidate)

const cIndex = candidateScores.indexOf(maxCandidateArr)
if (cIndex > -1) candidateScores.splice(cIndex, 1)
}
}
}

return sortedKeepSeries
Expand Down

0 comments on commit 6bc7239

Please sign in to comment.