Skip to content
Snippets Groups Projects
Commit d46b3591 authored by Harry Linrui XU's avatar Harry Linrui XU
Browse files

Implemented pie charts display in BudgetController

parent c2399581
No related branches found
No related tags found
3 merge requests!43Merging frontend-testing into master,!38"Made progressbar dynamic in accordance to spending. Added balance field....,!37Made the sub progress bars respond to changes in expense
Pipeline #214826 passed
package no.ntnu.idatt1002.demo.controller;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
......@@ -20,6 +23,7 @@ import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Budget.BudgetItem;
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
......@@ -70,14 +74,16 @@ public class BudgetController implements FinanceController {
@FXML
private ObservableList<BudgetItem> budgetList;
@FXML
private PieChart budgetPieChart;
/**
* Initializes the budget register, the observable budget list and the tableview, along with the values of the dropbox used for filtering the tableview.
* @throws IOException If there occurs any exception when loading the budget register from a file.
*/
@FXML
public void initialize() throws IOException {
public void initialize() {
//TODO if budget is not empty (HAS VALUES) -> make uneditable -> EVENT FILTER TO CONTEXT MENU
//TODO disable return to main menu when creating budget because this is the same view as when you create budeget
//TODO make budget item throw exception with negative amount
......@@ -88,29 +94,42 @@ public class BudgetController implements FinanceController {
amountCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount"));
descriptionCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription"));
try {
general = loadBudgetDataFromFile("Budget");
budgetList = FXCollections.observableArrayList(general.getBudgetItems());
budgetTableView.setItems(budgetList);
refreshPieChart();
} catch(IOException ioe) {
sum.setText("Invalid budget file format");
} catch(IllegalArgumentException iae) {
sum.setText("Cannot load pie charts");
}
//Initialize registers and tableview
general = loadBudgetDataFromFile("Budget");
budgetList = FXCollections.observableArrayList(general.getBudgetItems());
budgetTableView.setItems(budgetList);
formatDatePicker();
//createBudgetPieChart();
//Initialize sum field under the tableview
//sum.setText(String.valueOf(general.totalSum()));
}
private ObservableList<PieChart.Data> createBudgetPieChart() throws IllegalArgumentException { //TODO DOESNT WORK IF BUDGETITEM HAS NO BUDGET
try {
return FXCollections.observableArrayList(
new Data("Food", general.getBudgetItem(ExpenseCategory.FOOD).getBudgetAmount()),
new Data("Books", general.getBudgetItem(ExpenseCategory.BOOKS).getBudgetAmount()),
new Data("Clothes",
general.getBudgetItem(ExpenseCategory.CLOTHES).getBudgetAmount()),
new Data("Other", general.getBudgetItem(ExpenseCategory.OTHER).getBudgetAmount())
);
} catch(IllegalArgumentException iae) {
return FXCollections.observableArrayList();
private List<ExpenseCategory> getChosenBudgetCategories() { //todo could be moved to generalbudget
return Arrays.stream(ExpenseCategory.values()).toList().
stream().filter(expenseCategory -> general.hasBudgetCategory(expenseCategory)).toList();
}
private ObservableList<PieChart.Data> createBudgetPieChart() throws IllegalArgumentException { //
ObservableList<PieChart.Data> budgetData = FXCollections.observableArrayList();
List<ExpenseCategory> chosenCategories = getChosenBudgetCategories();
for (ExpenseCategory category : chosenCategories) {
budgetData.add(new Data(category.toString().substring(0, 1).toUpperCase().
concat(category.toString().substring(1)),
general.getBudgetItem(category).getBudgetAmount()));
}
return budgetData;
}
private void refreshPieChart() {
this.budgetPieChart.setData(createBudgetPieChart());
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment