-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5efd33
commit 939bcce
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/components/client/campaigns/CampaignPublicExpensesChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React from 'react' | ||
import { observer } from 'mobx-react' | ||
import { useTranslation } from 'next-i18next' | ||
import { | ||
Chart as ChartJS, | ||
CategoryScale, | ||
LinearScale, | ||
BarElement, | ||
Title, | ||
Colors, | ||
Tooltip, | ||
Legend, | ||
TooltipItem, | ||
} from 'chart.js' | ||
import { Bar } from 'react-chartjs-2' | ||
|
||
import { useCampaignApprovedExpensesList } from 'common/hooks/expenses' | ||
import { fromMoney, moneyPublic, toMoney } from 'common/util/money' | ||
|
||
ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Colors, Tooltip, Legend) | ||
|
||
type ExpenseDataset = { | ||
type: string | ||
total: number | ||
} | ||
|
||
type Props = { | ||
slug: string | ||
reachedAmount: number | ||
currency?: string | ||
} | ||
|
||
export default observer(function CampaignPublicExpensesChart({ | ||
slug, | ||
reachedAmount, | ||
currency, | ||
}: Props) { | ||
const { t } = useTranslation('') | ||
const { data: campaignExpenses } = useCampaignApprovedExpensesList(slug) | ||
|
||
const expenses: ExpenseDataset[] = [] | ||
|
||
campaignExpenses?.forEach(({ type, amount }) => { | ||
const exists = expenses.find((e) => e.type === type) | ||
if (exists) exists.total += fromMoney(amount) | ||
else expenses.push({ type, total: fromMoney(amount) }) | ||
}) | ||
|
||
expenses.sort((a, b) => b.total - a.total) | ||
|
||
const options = { | ||
indexAxis: 'y' as const, | ||
scales: { | ||
x: { | ||
stacked: true, | ||
min: 0, | ||
max: fromMoney(reachedAmount), | ||
}, | ||
y: { | ||
stacked: true, | ||
display: false, | ||
}, | ||
}, | ||
elements: { | ||
bar: { | ||
borderWidth: 1, | ||
}, | ||
}, | ||
responsive: true, | ||
plugins: { | ||
legend: { | ||
position: 'bottom' as const, | ||
}, | ||
colors: { | ||
enabled: true, | ||
}, | ||
tooltip: { | ||
callbacks: { | ||
label: (context: TooltipItem<'bar'>) => | ||
` ${context.dataset.label + ':' || ''} ${moneyPublic( | ||
toMoney(context.parsed.x), | ||
currency, | ||
)}`, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const data = { | ||
labels: [''], | ||
datasets: expenses.map((expense) => { | ||
return { | ||
label: t('expenses:field-types.' + expense.type), | ||
data: [expense.total], | ||
} | ||
}), | ||
} | ||
|
||
return <Bar options={options} height={80} data={data} /> | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters