From 6bc7239a3c1e90b92cc4fd4ff395e1fd331c1585 Mon Sep 17 00:00:00 2001 From: Sophia Mersmann Date: Mon, 10 Jun 2024 15:46:02 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(line=20legend)=20improve=20perf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marcel Gerber 🤖 style: prettify code --- .../grapher/src/lineLegend/LineLegend.tsx | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/@ourworldindata/grapher/src/lineLegend/LineLegend.tsx b/packages/@ourworldindata/grapher/src/lineLegend/LineLegend.tsx index 79c896e768d..504fd65f808 100644 --- a/packages/@ourworldindata/grapher/src/lineLegend/LineLegend.tsx +++ b/packages/@ourworldindata/grapher/src/lineLegend/LineLegend.tsx @@ -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