diff --git a/src/lib/lg-dashboard.ts b/src/lib/lg-dashboard.ts index d6ef8b2..cb9b3ab 100644 --- a/src/lib/lg-dashboard.ts +++ b/src/lib/lg-dashboard.ts @@ -423,6 +423,14 @@ export const calcGapBannerRow = (after: BannerRow, before: BannerRow): BannerRow const round1 = (n: number) => Math.round(n * 10) / 10; +const calcCagr = (after: number, before: number, years:number): string => { + const power = Math.pow(after / before, 1 / years); + const cagr = power - 1; + const cagr100 = cagr * 100; + + return String(round1(cagr100)); +}; + export const calcCagrRow = (after: ProductRow, before: ProductRow): ProductRow => { const years = 2; // 24 -> 26 const out: ProductRow = { @@ -433,12 +441,13 @@ export const calcCagrRow = (after: ProductRow, before: ProductRow): ProductRow = // total은 참고용으로만: (26/24) CAGR % out.total = - Number(before?.total ?? "0") > 0 ? String(round1((Math.pow(Number(after?.total ?? "0") / Number(before?.total ?? "0"), 1 / years) - 1) * 100)) : "0"; + Number(before?.total ?? "0") > 0 ? calcCagr(Number(after?.total ?? "0"), Number(before?.total ?? "0"), years) : "0"; for (const k of PRODUCT_KEYS) { const vBefore = before? before[k] ?? "0" : "0"; const vAfter = after? after[k] ?? "0" : "0"; - out[k] = Number(vBefore) > 0 ? String(round1((Math.pow(Number(vAfter) / Number(vBefore), 1 / years) - 1) * 100)) : "0"; + console.log(k, vBefore, vAfter); + out[k] = Number(vBefore) > 0 ? calcCagr(Number(vAfter), Number(vBefore), years) : "0"; } return out; }; @@ -478,7 +487,7 @@ export const withCagrRow = ( rows: T[], calcCagrRow: (last: T, first: T) => T ): T[] => { - const first = rows.at(0); + const first = rows.at(1); const last = rows.at(-1); if (!first || !last) return rows; return [...rows, calcCagrRow(last, first)];