Search in sources :

Example 81 with Transaction

use of jgnash.engine.Transaction in project jgnash by ccavanaugh.

the class IncomeExpensePayeePieChartDialogController method createPieDataSet.

private ObservableList<PieChart.Data>[] createPieDataSet(@NotNull final Account account) {
    @SuppressWarnings("unchecked") final ObservableList<PieChart.Data>[] chartData = (ObservableList<PieChart.Data>[]) new ObservableList[2];
    chartData[CREDIT] = FXCollections.observableArrayList();
    chartData[DEBIT] = FXCollections.observableArrayList();
    final Map<String, BigDecimal> names = new HashMap<>();
    final List<TranTuple> list = getTransactions(account, new ArrayList<>(), startDatePicker.getValue(), endDatePicker.getValue());
    final CurrencyNode currency = account.getCurrencyNode();
    // Create a list of predicates
    final List<Predicate<Transaction>> predicates = getPayeeTextFields().stream().filter(textField -> !textField.getText().isEmpty()).map(textField -> new PayeePredicate(textField.getText(), Options.regexForFiltersProperty().get())).collect(Collectors.toList());
    // Iterate through the list and add up filtered payees
    for (final TranTuple tranTuple : list) {
        final String payee = tranTuple.transaction.getPayee();
        BigDecimal sum = tranTuple.transaction.getAmount(tranTuple.account);
        sum = sum.multiply(tranTuple.account.getCurrencyNode().getExchangeRate(currency));
        boolean keep = false;
        if (predicates.isEmpty()) {
            keep = true;
        } else {
            for (final Predicate<Transaction> predicate : predicates) {
                if (predicate.test(tranTuple.transaction)) {
                    keep = true;
                    break;
                }
            }
        }
        if (keep) {
            if (names.containsKey(payee)) {
                sum = sum.add(names.get(payee));
            }
            names.put(payee, sum);
        }
    }
    final Map<String, BigDecimal> sortedNames = CollectionUtils.sortMapByValue(names);
    for (final Map.Entry<String, BigDecimal> entry : sortedNames.entrySet()) {
        final PieChart.Data data = new PieChart.Data(truncateString(entry.getKey()), entry.getValue().abs().doubleValue());
        // nodes are created lazily.  Set the user data (Account) after the node is created
        data.nodeProperty().addListener((observable, oldValue, newValue) -> newValue.setUserData(entry.getKey()));
        if (entry.getValue().signum() == -1) {
            chartData[DEBIT].add(data);
        } else {
            chartData[CREDIT].add(data);
        }
    }
    return chartData;
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) DoughnutChart(jgnash.uifx.control.DoughnutChart) Scene(javafx.scene.Scene) Engine(jgnash.engine.Engine) TextInputControl(javafx.scene.control.TextInputControl) Transaction(jgnash.engine.Transaction) EngineFactory(jgnash.engine.EngineFactory) FXCollections(javafx.collections.FXCollections) HashMap(java.util.HashMap) VBox(javafx.scene.layout.VBox) Side(javafx.geometry.Side) AccountComboBox(jgnash.uifx.control.AccountComboBox) NumberFormat(java.text.NumberFormat) ArrayList(java.util.ArrayList) EncodeDecode(jgnash.util.EncodeDecode) BigDecimal(java.math.BigDecimal) ResourceBundle(java.util.ResourceBundle) Map(java.util.Map) Tooltip(javafx.scene.control.Tooltip) GridPane(javafx.scene.layout.GridPane) CurrencyNode(jgnash.engine.CurrencyNode) ObjectProperty(javafx.beans.property.ObjectProperty) TextField(javafx.scene.control.TextField) InjectFXML(jgnash.uifx.util.InjectFXML) TitledPane(javafx.scene.control.TitledPane) NotNull(jgnash.util.NotNull) Comparators(jgnash.engine.Comparators) Predicate(java.util.function.Predicate) Collectors(java.util.stream.Collectors) Preferences(java.util.prefs.Preferences) Objects(java.util.Objects) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) PieChart(javafx.scene.chart.PieChart) List(java.util.List) CommodityFormat(jgnash.text.CommodityFormat) Stage(javafx.stage.Stage) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) LocalDate(java.time.LocalDate) Account(jgnash.engine.Account) ObservableList(javafx.collections.ObservableList) PayeePredicate(jgnash.util.function.PayeePredicate) ChangeListener(javafx.beans.value.ChangeListener) Collections(java.util.Collections) DatePickerEx(jgnash.uifx.control.DatePickerEx) CollectionUtils(jgnash.util.CollectionUtils) Options(jgnash.uifx.Options) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal) Predicate(java.util.function.Predicate) PayeePredicate(jgnash.util.function.PayeePredicate) PieChart(javafx.scene.chart.PieChart) Transaction(jgnash.engine.Transaction) ObservableList(javafx.collections.ObservableList) PayeePredicate(jgnash.util.function.PayeePredicate) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Transaction (jgnash.engine.Transaction)81 InvestmentTransaction (jgnash.engine.InvestmentTransaction)39 Account (jgnash.engine.Account)27 BigDecimal (java.math.BigDecimal)24 Engine (jgnash.engine.Engine)22 TransactionEntry (jgnash.engine.TransactionEntry)18 NotNull (jgnash.util.NotNull)13 List (java.util.List)10 LocalDate (java.time.LocalDate)9 FXML (javafx.fxml.FXML)9 AbstractInvestmentTransactionEntry (jgnash.engine.AbstractInvestmentTransactionEntry)9 ArrayList (java.util.ArrayList)7 TransactionType (jgnash.engine.TransactionType)7 ResourceBundle (java.util.ResourceBundle)6 TransactionFactory (jgnash.engine.TransactionFactory)6 NumberFormat (java.text.NumberFormat)5 Objects (java.util.Objects)5 ChangeListener (javafx.beans.value.ChangeListener)5 EngineFactory (jgnash.engine.EngineFactory)5 Message (jgnash.engine.message.Message)5