Skip to content

Commit

Permalink
🐛 fix cost-metric coloring to take reversed values
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkjrs committed Mar 27, 2024
1 parent 2516d15 commit a2259e2
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/lib/coerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ export function computeChange(data: (string | number | null)[]) {
}
return 0;
}
export function computeChangeType(changeAmount: number) {
export function computeChangeType(
changeAmount: number,
isOpposite: boolean = false
) {
if (changeAmount !== 0) {
return changeAmount < 0 ? 'decrease' : 'increase';
if (!isOpposite) {
return changeAmount < 0 ? 'decrease' : 'increase';
}
return changeAmount < 0 ? 'increase' : 'decrease';
}
return 'same';
}
Expand Down Expand Up @@ -183,7 +189,7 @@ export function prepareChartData(chartJsData: {
stat: cpmStat,
icon: EnvelopeOpenIcon,
change: computeChange(chartJsData.cpm).toFixed(2),
changeType: computeChangeType(computeChange(chartJsData.cpm)),
changeType: computeChangeType(computeChange(chartJsData.cpm), true),
chartData: {
labels: chartJsData.updatedTime,
data: chartJsData.cpm,
Expand All @@ -207,7 +213,7 @@ export function prepareChartData(chartJsData: {
stat: cpcStat,
icon: CursorArrowRaysIcon,
change: computeChange(chartJsData.cpc).toFixed(2),
changeType: computeChangeType(computeChange(chartJsData.cpc)),
changeType: computeChangeType(computeChange(chartJsData.cpc), true),
chartData: {
labels: chartJsData.updatedTime,
data: chartJsData.cpc,
Expand All @@ -219,7 +225,7 @@ export function prepareChartData(chartJsData: {
stat: cpvStat,
icon: VideoCameraIcon,
change: computeChange(chartJsData.cpv).toFixed(2),
changeType: computeChangeType(computeChange(chartJsData.cpv)),
changeType: computeChangeType(computeChange(chartJsData.cpv), true),
chartData: {
labels: chartJsData.updatedTime,
data: chartJsData.cpv,
Expand Down Expand Up @@ -354,7 +360,10 @@ export function aggregateChartData(
maximumFractionDigits: 2,
minimumFractionDigits: 0,
}),
changeType: computeChangeType(change),
changeType: computeChangeType(
change,
['CPC', 'CPV', 'CPM'].includes(localMetric)
),
chartData: { ...aggregateChartData },
};
}
Expand Down

0 comments on commit a2259e2

Please sign in to comment.