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");
}
}
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);
}
}
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));
}
}
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);
}
Aggregations