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; package no.ntnu.idatt1002.demo.controller;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
...@@ -20,6 +23,7 @@ import javafx.stage.Stage; ...@@ -20,6 +23,7 @@ import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Budget.BudgetItem; import no.ntnu.idatt1002.demo.data.Budget.BudgetItem;
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget; import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory; import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
...@@ -70,14 +74,16 @@ public class BudgetController implements FinanceController { ...@@ -70,14 +74,16 @@ public class BudgetController implements FinanceController {
@FXML @FXML
private ObservableList<BudgetItem> budgetList; 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. * 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 @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 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 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 //TODO make budget item throw exception with negative amount
...@@ -88,29 +94,42 @@ public class BudgetController implements FinanceController { ...@@ -88,29 +94,42 @@ public class BudgetController implements FinanceController {
amountCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount")); amountCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount"));
descriptionCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription")); 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 //Initialize registers and tableview
general = loadBudgetDataFromFile("Budget");
budgetList = FXCollections.observableArrayList(general.getBudgetItems());
budgetTableView.setItems(budgetList);
formatDatePicker(); formatDatePicker();
//createBudgetPieChart();
//Initialize sum field under the tableview //Initialize sum field under the tableview
//sum.setText(String.valueOf(general.totalSum())); //sum.setText(String.valueOf(general.totalSum()));
} }
private ObservableList<PieChart.Data> createBudgetPieChart() throws IllegalArgumentException { //TODO DOESNT WORK IF BUDGETITEM HAS NO BUDGET private List<ExpenseCategory> getChosenBudgetCategories() { //todo could be moved to generalbudget
try { return Arrays.stream(ExpenseCategory.values()).toList().
return FXCollections.observableArrayList( stream().filter(expenseCategory -> general.hasBudgetCategory(expenseCategory)).toList();
new Data("Food", general.getBudgetItem(ExpenseCategory.FOOD).getBudgetAmount()), }
new Data("Books", general.getBudgetItem(ExpenseCategory.BOOKS).getBudgetAmount()), private ObservableList<PieChart.Data> createBudgetPieChart() throws IllegalArgumentException { //
new Data("Clothes", ObservableList<PieChart.Data> budgetData = FXCollections.observableArrayList();
general.getBudgetItem(ExpenseCategory.CLOTHES).getBudgetAmount()), List<ExpenseCategory> chosenCategories = getChosenBudgetCategories();
new Data("Other", general.getBudgetItem(ExpenseCategory.OTHER).getBudgetAmount()) for (ExpenseCategory category : chosenCategories) {
); budgetData.add(new Data(category.toString().substring(0, 1).toUpperCase().
} catch(IllegalArgumentException iae) { concat(category.toString().substring(1)),
return FXCollections.observableArrayList(); 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