diff --git a/src/routes/heatmaps/+page.svelte b/src/routes/heatmaps/+page.svelte index a048192f..4cd78fa5 100644 --- a/src/routes/heatmaps/+page.svelte +++ b/src/routes/heatmaps/+page.svelte @@ -15,11 +15,11 @@ export let data; let cloudFrontUrl = import.meta.env.VITE_IMAGE_URL; let displayIndex = 'S&P500'; -let lowestSumCategory = null; -let highestSumCategory = null; -let lowestSum = Infinity; -let highestSum = -Infinity; +let lowestAvg = Infinity; +let lowestAvgCategory = ''; +let highestAvg = -Infinity; +let highestAvgCategory = ''; let rawData; @@ -100,26 +100,24 @@ function getLevelOption() { } function plotData(fontSize) { - lowestSum = Infinity; - highestSum = -Infinity; + rawData.forEach(category => { - let sum = category?.children?.reduce((acc, stock) => acc + stock?.changesPercentage, 0); - - if (sum < lowestSum) { - lowestSum = sum; - lowestSumCategory = category.name; - } - }); + if (category?.children && category.children.length > 0) { + let sum = category.children.reduce((acc, stock) => acc + (stock?.changesPercentage || 0), 0); + let avg = sum / category.children.length; - rawData?.forEach(category => { - let sum = category?.children?.reduce((acc, stock) => acc + stock?.changesPercentage, 0); - - if (sum > highestSum) { - highestSum = sum; - highestSumCategory = category.name; + if (avg < lowestAvg) { + lowestAvg = avg; + lowestAvgCategory = category.name; + } + + if (avg > highestAvg) { + highestAvg = avg; + highestAvgCategory = category.name; + } } - }); +}); options = { @@ -376,8 +374,8 @@ $: {
- Today, took the lead as the {displayIndex} largest loser, marking a cumulative return of {lowestSum?.toFixed(2)}%, - while surged ahead as the top performer with an impressive cumulative return of {highestSum?.toFixed(2)}%. + Today, took the lead as the {displayIndex} largest loser, marking a average return of {lowestAvg?.toFixed(2)}%, + while surged ahead as the top performer with an impressive average return of {highestAvg?.toFixed(2)}%.