Building Tabunganku Homepage by Implementing Sveltekit and Chart.js
This is how I implement the front page of Tabunganku using Sveltekit framework with the help of Chart.js
2023-07-31 · 11 min · Willy Setiawan
Table of Contents
I have completed the implementation of most of the main page features. It can load recent transactions and create a pie chart based on these transactions. Furthermore, the page displays both the total earnings and expense of the day. There is one feature that I have not completed yet, and that is the ability to view transactions on a date other than the current date.
Here’s what it looks like if there is available data:
To ensure good accessibility, I use Accessible Colors to adjust the red and green text colors. As for the chart color, Chart.js automatically handles the setup.
There are 3 functions provided when the page loads: getTodayDate, getTransactions, and getCategories. getTransactions is a function to get a transaction list based on date. getCategories is a function to a user’s category. load function will return result from getTransactions and getCategories.
with name as category name and type is the category type. categoryMap is used to display the category name and to determine the color in the transaction table.
In this code snippet, it creates a map named categoryAmountMap containing category name as the key and the total amount as its value. In this example, there is only one data in categoryAmountMap: { 'Test' => 100000 }. totalEarning and totalExpense calculation process also happens here.
existingCategoryArray stores keys (categories) from categoryAmountMap, and perCategoryAmountArray stores the total of each category. Both variables play a role in supplying data for the pie chart. In this example, the value inside existingCategoryArray is ['Test'] and the value inside perCategoryAmountArray is [100000].
This is the configuration for the pie chart itself. In this configuration, the pie chart is made to be responsive. The legend is placed at the bottom of the chart, and has font size of 14. The chart title is Portfolio.
onMount is a function that is called when a component is initialized. In this case, when the page loads all the HTML component, it will provide dateString, totalEarning, totalExpense, and initialize myChart variable.
This is the code to display top part of the page. It displays the total earning, current date string, and total expense value. All the values are sourced from their respective variables in the script before. There are previous date and next date buttons, but they are not implemented yet.
1
2
3
4
5
6
7
8
9
<divclass="dashboard__item dashboard__item--full"><divclass="card"><divclass="chart"><canvasbind:this={portfolio}width={500}height={300}> Your browser does not support the canvas element.
</canvas></div></div></div>
This is where the pie chart is located, using the portfolio variable set up in the script. The initial width is 500, and height of 300. There is a fallback text if a browser does not support canvas element.
This is the HTML code to display the transaction table. If there is no transaction, the table will not be displayed.
In tbody, iterate through the transactions data, utilizing categoryMap to determine both the category name and the representation of amount.
After completing the basic feature, there are several things that I believe can be improved:
The ability to view past transactions is crucial for effectively analyzing spending trends, but it hasn’t been implemented yet. I will try to implement this feature soon. I was busy improving the table layout, but the result was disappointing.
Fixing the table layout. I have to find a way to make the table layout consistent with the overall design.
Updating the backend to return the result in a specific format. Currently, the frontend calls backend twice to get transactions and categories. By processing the data in the backend instead, we can reduce the API call to 1.
I think some of the implementations in +page.server.js can be moved to +page.js so it doesn’t run on the server.
The pie chart can be turned into a reusable component.
Support for another currency, because currently Tabunganku only has support for IDR currency.
The sidebar menu can be improved, but it’s a concern for the future.
P.S. I can’t guarantee I will do all of these!
I will post about the backend implementation next. I have finished the backend before creating the frontend part.