use of jgnash.engine.Account in project jgnash by ccavanaugh.
the class IncomeExpensePayeePieChartDialogController method updateCharts.
private void updateCharts() {
final Account account = accountComboBox.getValue();
if (account != null) {
final ObservableList<PieChart.Data>[] chartData = createPieDataSet(account);
creditPieChart.setData(chartData[CREDIT]);
debitPieChart.setData(chartData[DEBIT]);
final NumberFormat numberFormat = CommodityFormat.getFullNumberFormat(account.getCurrencyNode());
// Calculate the totals for percentage value
final double creditTotal = chartData[CREDIT].parallelStream().mapToDouble(PieChart.Data::getPieValue).sum();
final double debitTotal = chartData[DEBIT].parallelStream().mapToDouble(PieChart.Data::getPieValue).sum();
final NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMaximumFractionDigits(1);
percentFormat.setMinimumFractionDigits(1);
// Install tooltips on the data after it has been added to the chart
creditPieChart.getData().forEach(data -> Tooltip.install(data.getNode(), new Tooltip((data.getNode().getUserData() + "\n" + numberFormat.format(data.getPieValue()) + "(" + percentFormat.format(data.getPieValue() / creditTotal)) + ")")));
// Install tooltips on the data after it has been added to the chart
debitPieChart.getData().forEach(data -> Tooltip.install(data.getNode(), new Tooltip(((data.getNode().getUserData()) + "\n" + numberFormat.format(data.getPieValue()) + "(" + percentFormat.format(data.getPieValue() / debitTotal)) + ")")));
creditPieChart.centerSubTitleProperty().set(numberFormat.format(creditTotal));
debitPieChart.centerSubTitleProperty().set(numberFormat.format(debitTotal));
} else {
creditPieChart.setData(FXCollections.emptyObservableList());
creditPieChart.setTitle("No Data");
debitPieChart.setData(FXCollections.emptyObservableList());
debitPieChart.setTitle("No Data");
}
}
use of jgnash.engine.Account in project jgnash by ccavanaugh.
the class IncomeExpensePayeePieChartDialogController method getTransactions.
private List<TranTuple> getTransactions(final Account account, final List<TranTuple> transactions, final LocalDate startDate, final LocalDate endDate) {
for (final Transaction transaction : account.getTransactions(startDate, endDate)) {
TranTuple tuple = new TranTuple(account, transaction);
transactions.add(tuple);
}
for (final Account child : account.getChildren(Comparators.getAccountByCode())) {
getTransactions(child, transactions, startDate, endDate);
}
return transactions;
}
use of jgnash.engine.Account in project jgnash by ccavanaugh.
the class IncomeExpensePieChartDialogController method setParameters.
void setParameters(final AccountType accountType, final LocalDate startDate, final LocalDate endDate) {
//PK: dates can be directly set
startDatePicker.setValue(startDate);
endDatePicker.setValue(endDate);
//PK: We assume that the root of all income or expense accounts is one account under the root. Select that
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
for (final Account comboAccount : accountComboBox.getItems()) {
if ((accountType == comboAccount.getAccountType()) && (comboAccount.getParent() == engine.getRootAccount())) {
accountComboBox.setValue(comboAccount);
break;
}
}
}
use of jgnash.engine.Account in project jgnash by ccavanaugh.
the class IncomeExpensePieChartDialogController method updateChart.
private void updateChart() {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
final Account a = accountComboBox.getValue();
if (a != null) {
final CurrencyNode defaultCurrency = a.getCurrencyNode();
final NumberFormat numberFormat = CommodityFormat.getFullNumberFormat(defaultCurrency);
final ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
double total = a.getTreeBalance(startDatePicker.getValue(), endDatePicker.getValue(), defaultCurrency).doubleValue();
for (final Account child : a.getChildren()) {
double balance = child.getTreeBalance(startDatePicker.getValue(), endDatePicker.getValue(), defaultCurrency).doubleValue();
if (balance > 0 || balance < 0) {
final String label = child.getName() + " - " + numberFormat.format(balance);
final PieChart.Data data = new PieChart.Data(label, balance / total * 100);
// nodes are created lazily. Set the user data (Account) after the node is created
data.nodeProperty().addListener((observable, oldValue, newValue) -> newValue.setUserData(child));
pieChartData.add(data);
}
}
pieChart.setData(pieChartData);
final NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMaximumFractionDigits(1);
percentFormat.setMinimumFractionDigits(1);
// Install tooltips on the data after it has been added to the chart
pieChart.getData().forEach(data -> Tooltip.install(data.getNode(), new Tooltip((((Account) data.getNode().getUserData()).getName() + " - " + percentFormat.format(data.getPieValue() / 100d)))));
// Indicate the node can be clicked on to zoom into the next account level
for (final PieChart.Data data : pieChart.getData()) {
data.getNode().setOnMouseEntered(event -> {
final Account account = (Account) data.getNode().getUserData();
if (account.isParent()) {
data.getNode().setCursor(CustomCursor.getZoomInCursor());
} else {
data.getNode().setCursor(Cursor.DEFAULT);
}
nodeFocused = true;
});
data.getNode().setOnMouseExited(event -> nodeFocused = false);
// zoom in on click if this is a parent account
data.getNode().setOnMouseClicked(event -> {
if (data.getNode().getUserData() != null) {
if (((Account) data.getNode().getUserData()).isParent()) {
accountComboBox.setValue((Account) data.getNode().getUserData());
}
}
});
}
final String title;
// pick an appropriate title
if (a.getAccountType() == AccountType.EXPENSE) {
title = resources.getString("Title.PercentExpense");
} else if (a.getAccountType() == AccountType.INCOME) {
title = resources.getString("Title.PercentIncome");
} else {
title = resources.getString("Title.PercentDist");
}
pieChart.setTitle(title);
pieChart.centerTitleProperty().set(accountComboBox.getValue().getName());
pieChart.centerSubTitleProperty().set(numberFormat.format(total));
// abs() on all values won't work if children aren't of uniform sign,
// then again, this chart is not right to display those trees
//boolean negate = total != null && total.signum() < 0;
} else {
pieChart.setData(FXCollections.emptyObservableList());
pieChart.setTitle("No Data");
}
}
use of jgnash.engine.Account in project jgnash by ccavanaugh.
the class IncomeExpensePieChartDialogController method initialize.
@FXML
public void initialize() {
// Respect animation preference
pieChart.animatedProperty().set(Options.animationsEnabledProperty().get());
accountComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null && newValue.getParent().getAccountType() != AccountType.ROOT) {
pieChart.setCursor(CustomCursor.getZoomOutCursor());
} else {
pieChart.setCursor(Cursor.DEFAULT);
}
});
final Preferences preferences = Preferences.userNodeForPackage(IncomeExpensePieChartDialogController.class).node("IncomeExpensePieChart");
accountComboBox.setPredicate(new ParentAccountPredicate());
if (preferences.get(LAST_ACCOUNT, null) != null) {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
final Account account = engine.getAccountByUuid(preferences.get(LAST_ACCOUNT, null));
if (account != null) {
accountComboBox.setValue(account);
}
}
startDatePicker.setValue(endDatePicker.getValue().minusYears(1));
final ChangeListener<Object> listener = (observable, oldValue, newValue) -> {
if (newValue != null) {
updateChart();
preferences.put(LAST_ACCOUNT, accountComboBox.getValue().getUuid());
}
};
accountComboBox.valueProperty().addListener(listener);
startDatePicker.valueProperty().addListener(listener);
endDatePicker.valueProperty().addListener(listener);
pieChart.setLegendSide(Side.BOTTOM);
// zoom out
pieChart.setOnMouseClicked(event -> {
if (!nodeFocused && accountComboBox.getValue().getParent().getAccountType() != AccountType.ROOT) {
accountComboBox.setValue(accountComboBox.getValue().getParent());
}
});
// Push the initial load to the end of the platform thread for better startup and nicer visual effect
Platform.runLater(this::updateChart);
}
Aggregations