Search in sources :

Example 6 with Tag

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

the class TransactionTagPieChartDialogController 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 = NumericFormats.getFullCommodityFormat(defaultCurrency);
        final ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
        final Map<Tag, BigDecimal> balanceMap = new HashMap<>();
        // Iterate through all the Tags in use
        for (final Tag tag : engine.getTagsInUse()) {
            BigDecimal balance = new BigDecimal(BigInteger.ZERO);
            for (final Account child : a.getChildren()) {
                balance = balance.add(getSumForTag(tag, child));
            }
            if (balance.compareTo(BigDecimal.ZERO) != 0) {
                balanceMap.put(tag, balance);
            }
        }
        // Sum of all the balances
        final BigDecimal total = balanceMap.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        // Iterate and crate each pie slice
        for (final Map.Entry<Tag, BigDecimal> entry : balanceMap.entrySet()) {
            final Tag tag = entry.getKey();
            final BigDecimal balance = entry.getValue();
            final String label = tag.getName() + " - " + numberFormat.format(balance.doubleValue());
            // protect against a div by zero caused by net zero of income and expense
            double value = total.compareTo(BigDecimal.ZERO) == 0 ? 0 : balance.divide(total, MathContext.DECIMAL64).multiply(ONE_HUNDRED).doubleValue();
            final PieChart.Data data = new PieChart.Data(label, value);
            // nodes are created lazily.  Set the user data (Tag) after the node is created
            data.nodeProperty().addListener((observable, oldValue, newValue) -> newValue.setUserData(tag));
            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((((Tag) data.getNode().getUserData()).getName() + " - " + percentFormat.format(data.getPieValue() / 100d)))));
        pieChart.setTitle(resources.getString("Title.TransactionTagPieChart"));
        pieChart.centerTitleProperty().set(accountComboBox.getValue().getName());
        pieChart.centerSubTitleProperty().set(numberFormat.format(total));
    } else {
        pieChart.setData(FXCollections.emptyObservableList());
        pieChart.setTitle("No Data");
    }
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Account(jgnash.engine.Account) HashMap(java.util.HashMap) Tooltip(javafx.scene.control.Tooltip) BigDecimal(java.math.BigDecimal) PieChart(javafx.scene.chart.PieChart) Tag(jgnash.engine.Tag) HashMap(java.util.HashMap) Map(java.util.Map) Engine(jgnash.engine.Engine) NumberFormat(java.text.NumberFormat)

Example 7 with Tag

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

the class TagManagerDialogController method handleSaveAction.

@FXML
private void handleSaveAction() {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    final Tag tag = tagListView.getSelectionModel().getSelectedItem();
    if (tag != null) {
        tag.setName(nameField.getText());
        tag.setDescription(descriptionTextArea.getText());
        tag.setShape(iconCombo.getValue().getUnicode());
        tag.setColor(EncodeDecode.colorStringToLong(colorPicker.getValue().toString()));
        engine.updateTag(tag);
    }
}
Also used : Tag(jgnash.engine.Tag) Engine(jgnash.engine.Engine) InjectFXML(jgnash.uifx.util.InjectFXML) FXML(javafx.fxml.FXML)

Example 8 with Tag

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

the class TagManagerDialogController method loadTagListView.

private void loadTagListView() {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    lockedMap.clear();
    final List<Tag> tagList = new ArrayList<>(engine.getTags());
    Collections.sort(tagList);
    tagListView.getItems().setAll(tagList);
    final Set<Tag> usedTags = engine.getTagsInUse();
    for (final Tag tag : tagList) {
        lockedMap.put(tag, usedTags.contains(tag));
    }
}
Also used : ArrayList(java.util.ArrayList) Tag(jgnash.engine.Tag) Engine(jgnash.engine.Engine)

Example 9 with Tag

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

the class TagManagerDialogController method handleNewAction.

@FXML
private void handleNewAction() {
    final Tag tag = new Tag();
    tag.setName(resources.getString("Word.NewTag"));
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    engine.addTag(tag);
}
Also used : Tag(jgnash.engine.Tag) Engine(jgnash.engine.Engine) InjectFXML(jgnash.uifx.util.InjectFXML) FXML(javafx.fxml.FXML)

Aggregations

Tag (jgnash.engine.Tag)9 Engine (jgnash.engine.Engine)6 FXML (javafx.fxml.FXML)4 InjectFXML (jgnash.uifx.util.InjectFXML)3 ArrayList (java.util.ArrayList)2 Tooltip (javafx.scene.control.Tooltip)2 BigDecimal (java.math.BigDecimal)1 NumberFormat (java.text.NumberFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Node (javafx.scene.Node)1 PieChart (javafx.scene.chart.PieChart)1 CheckBox (javafx.scene.control.CheckBox)1 Label (javafx.scene.control.Label)1 Account (jgnash.engine.Account)1 CurrencyNode (jgnash.engine.CurrencyNode)1 TransactionEntry (jgnash.engine.TransactionEntry)1 MaterialDesignLabel (jgnash.uifx.resource.font.MaterialDesignLabel)1