From 8118cad2e657fbbde91c7afb6a50c3b0df175948 Mon Sep 17 00:00:00 2001 From: Tord Austad Date: Fri, 20 Sep 2024 16:03:51 +0200 Subject: [PATCH] Fix chart showing the data for the wrong case (#1279) * Fix chart showing the data for the wrong case --- frontend/src/Hooks/useProjectChartData.tsx | 90 ++++++++++++---------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/frontend/src/Hooks/useProjectChartData.tsx b/frontend/src/Hooks/useProjectChartData.tsx index c65a5538c..f6a034a0e 100644 --- a/frontend/src/Hooks/useProjectChartData.tsx +++ b/frontend/src/Hooks/useProjectChartData.tsx @@ -26,7 +26,7 @@ export const useProjectChartData = () => { const { project } = useProjectContext() const [rowData, setRowData] = useState() - const [compareCasesTotals, setCompareCasesTotals] = useState() + const [compareCasesTotals, setCompareCasesTotals] = useState() const [npvChartData, setNpvChartData] = useState() const [breakEvenChartData, setBreakEvenChartData] = useState() const [productionProfilesChartData, setProductionProfilesChartData] = useState() @@ -35,44 +35,54 @@ export const useProjectChartData = () => { const [co2IntensityChartData, setCo2IntensityChartData] = useState() const generateAllCharts = () => { - const npvObject: object[] = [] - const breakEvenObject: object[] = [] - const productionProfilesObject: object[] = [] - const investmentProfilesObject: object[] = [] - const totalCo2EmissionsObject: object[] = [] - const co2IntensityObject: object[] = [] - if (compareCasesTotals !== undefined && project) { - for (let i = 0; i < project.cases.length; i += 1) { - npvObject.push({ - cases: project.cases[i].name, - npv: project.cases[i].npv, - }) - breakEvenObject.push({ - cases: project.cases[i].name, - breakEven: project.cases[i].breakEven, - }) - productionProfilesObject.push({ - cases: project.cases[i].name, - oilProduction: compareCasesTotals[i]?.totalOilProduction, - gasProduction: compareCasesTotals[i]?.totalGasProduction, - totalExportedVolumes: compareCasesTotals[i]?.totalExportedVolumes, - }) - investmentProfilesObject.push({ - cases: project.cases[i].name, - offshorePlusOnshoreFacilityCosts: compareCasesTotals[i]?.offshorePlusOnshoreFacilityCosts, - developmentCosts: compareCasesTotals[i]?.developmentWellCosts, - explorationWellCosts: compareCasesTotals[i]?.explorationWellCosts, - }) - totalCo2EmissionsObject.push({ - cases: project.cases[i].name, - totalCO2Emissions: compareCasesTotals[i]?.totalCo2Emissions, - }) - co2IntensityObject.push({ - cases: project.cases[i].name, - cO2Intensity: compareCasesTotals[i]?.co2Intensity, - }) + if (!compareCasesTotals || !project) { return } + + const npvObject = project.cases.map(caseItem => ({ + cases: caseItem.name, + npv: caseItem.npv, + })) + + const breakEvenObject = project.cases.map(caseItem => ({ + cases: caseItem.name, + breakEven: caseItem.breakEven, + })) + + const productionProfilesObject = project.cases.map(caseItem => { + const compareCase = compareCasesTotals.find(c => c.caseId === caseItem.id) + return { + cases: caseItem.name, + oilProduction: compareCase?.totalOilProduction, + gasProduction: compareCase?.totalGasProduction, + totalExportedVolumes: compareCase?.totalExportedVolumes, } - } + }) + + const investmentProfilesObject = project.cases.map(caseItem => { + const compareCase = compareCasesTotals.find(c => c.caseId === caseItem.id) + return { + cases: caseItem.name, + offshorePlusOnshoreFacilityCosts: compareCase?.offshorePlusOnshoreFacilityCosts, + developmentCosts: compareCase?.developmentWellCosts, + explorationWellCosts: compareCase?.explorationWellCosts, + } + }) + + const totalCo2EmissionsObject = project.cases.map(caseItem => { + const compareCase = compareCasesTotals.find(c => c.caseId === caseItem.id) + return { + cases: caseItem.name, + totalCO2Emissions: compareCase?.totalCo2Emissions, + } + }) + + const co2IntensityObject = project.cases.map(caseItem => { + const compareCase = compareCasesTotals.find(c => c.caseId === caseItem.id) + return { + cases: caseItem.name, + cO2Intensity: compareCase?.co2Intensity, + } + }) + setNpvChartData(npvObject) setBreakEvenChartData(breakEvenObject) setProductionProfilesChartData(productionProfilesObject) @@ -93,8 +103,8 @@ export const useProjectChartData = () => { id: c.id!, cases: c.name ?? "", description: c.description ?? "", - npv: Math.round(c.npv ?? 0 * 1) / 1 ?? 0, - breakEven: Math.round(c.breakEven ?? 0 * 1) / 1 ?? 0, + npv: Math.round(c.npv ?? 0 * 1) / 1, + breakEven: Math.round(c.breakEven ?? 0 * 1) / 1, oilProduction: Math.round(matchingCase.totalOilProduction * 10) / 10, gasProduction: Math.round(matchingCase.totalGasProduction * 10) / 10, totalExportedVolumes: Math.round(matchingCase.totalExportedVolumes * 10) / 10,