-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add spendings per category graphs
- Loading branch information
1 parent
b7ee262
commit 0751737
Showing
8 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
26 changes: 26 additions & 0 deletions
26
my-app/src/routes/groups/graphs/[[id=integer]]/+page.server.ts
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,26 @@ | ||
import { categoryService, groupService, spendingService } from '$lib/server/api'; | ||
import type { PageServerLoad } from './$types'; | ||
|
||
export const load: PageServerLoad = async ({ params, cookies }) => { | ||
const id = Number(params.id) || 0; | ||
const group: Group = id | ||
? await groupService.get(id, cookies) | ||
: { name: '', description: '', id: 0, owner_id: 0, is_archived: false }; | ||
const categories = await categoryService.list(id, cookies); | ||
const spendings = await spendingService.list(id, cookies); | ||
const graphData = computePerCategorySpending(categories, spendings); | ||
return { group, graphData }; | ||
}; | ||
|
||
function computePerCategorySpending(categories: Category[], spendings: Spending[]) { | ||
const categoryMap = new Map<string, number>(categories.map((category) => [category.name, 0])); | ||
spendings.forEach(({ amount }) => { | ||
const randomChoice = Math.floor(Math.random() * categories.length); | ||
const categoryName = categories[randomChoice].name; | ||
const acc = categoryMap.get(categoryName)!; | ||
categoryMap.set(categoryName, acc + amount); | ||
}); | ||
const labels = Array.from(categoryMap.keys()); | ||
const values = Array.from(categoryMap.values()); | ||
return { labels, values }; | ||
} |
53 changes: 53 additions & 0 deletions
53
my-app/src/routes/groups/graphs/[[id=integer]]/+page.svelte
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,53 @@ | ||
<script lang="ts"> | ||
import { title } from '$lib'; | ||
import type { PageData } from './$types'; | ||
import type { Action } from 'svelte/action'; | ||
import { Chart } from 'chart.js/auto'; | ||
export let data: PageData; | ||
export const loadGraph: Action<HTMLCanvasElement> = (canvas) => { | ||
const { labels, values } = data?.graphData; | ||
const label = 'Gastos por categoría'; | ||
// NOTE: this is to have each bar be a different color | ||
const datasets = values.map((v, i) => { | ||
let data = Array(values.length).fill(0); | ||
data[i] = v; | ||
return { label, data }; | ||
}); | ||
const chart = new Chart(canvas, { | ||
type: 'bar', | ||
options: { | ||
plugins: { | ||
legend: { display: false }, | ||
colors: { enabled: true, forceOverride: true } | ||
}, | ||
scales: { x: { stacked: true }, y: { stacked: true } } | ||
}, | ||
data: { labels, datasets } | ||
}); | ||
return { destroy: () => chart.destroy() }; | ||
}; | ||
</script> | ||
|
||
<svelte:head> | ||
<title>{title} - Gráficos de finanzas</title> | ||
</svelte:head> | ||
|
||
<nav aria-label="breadcrumb"> | ||
<ul> | ||
<li><a href="/groups">Grupos</a></li> | ||
<li><a href="/groups/movements/{data.group.id}">{data.group.name}</a></li> | ||
<li>Gráficos de finanzas</li> | ||
</ul> | ||
</nav> | ||
|
||
<header class="row"> | ||
<div> | ||
<h2>Gráficos de finanzas</h2> | ||
<p>{data.group.description}</p> | ||
</div> | ||
</header> | ||
|
||
<div style="width: 600px;"><canvas use:loadGraph></canvas></div> |
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