utxo bar chart initial commit

This commit is contained in:
Craig Raw 2020-06-26 18:34:35 +02:00
parent 07458d3dff
commit 766afae9c1
4 changed files with 84 additions and 1 deletions

View file

@ -5,17 +5,26 @@ import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.UtxosTreeTable;
import com.sparrowwallet.sparrow.event.WalletHistoryChangedEvent;
import com.sparrowwallet.sparrow.event.WalletNodesChangedEvent;
import javafx.collections.ListChangeListener;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.XYChart;
import java.net.URL;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
public class UtxosController extends WalletFormController implements Initializable {
@FXML
private UtxosTreeTable utxosTable;
@FXML
private BarChart<String, Number> utxosChart;
@Override
public void initialize(URL location, ResourceBundle resources) {
EventManager.get().register(this);
@ -24,12 +33,60 @@ public class UtxosController extends WalletFormController implements Initializab
@Override
public void initializeView() {
utxosTable.initialize(getWalletForm().getWalletUtxosEntry());
initializeChart(getWalletForm().getWalletUtxosEntry());
}
private void initializeChart(WalletUtxosEntry walletUtxosEntry) {
XYChart.Series<String, Number> utxoSeries = new XYChart.Series<>();
List<XYChart.Data<String, Number>> utxoDataList = walletUtxosEntry.getChildren().stream()
.map(UtxoData::new)
.map(data -> new XYChart.Data<>(data.name, data.value, data.entry))
.collect(Collectors.toList());
utxoSeries.getData().addAll(utxoDataList);
walletUtxosEntry.getChildren().forEach(entry -> entry.labelProperty().addListener((observable, oldValue, newValue) -> {
Optional<XYChart.Data<String, Number>> optData = utxoSeries.getData().stream().filter(data -> data.getExtraValue().equals(entry)).findFirst();
if(optData.isPresent()) {
int index = utxoSeries.getData().indexOf(optData.get());
utxoSeries.getData().set(index, new XYChart.Data<>(newValue, optData.get().getYValue(), optData.get().getExtraValue()));
}
}));
utxoSeries.getData().sort((o1, o2) -> (int) (o2.getYValue().longValue() - o1.getYValue().longValue()));
utxosChart.getData().clear();
utxosChart.getData().add(utxoSeries);
walletUtxosEntry.getChildren().addListener((ListChangeListener<Entry>) change -> {
while(change.next()) {
if(change.wasAdded()) {
List<XYChart.Data<String, Number>> addedList = change.getAddedSubList().stream().map(UtxoData::new).map(data -> new XYChart.Data<>(data.name, data.value, data.entry)).collect(Collectors.toList());
utxoSeries.getData().addAll(addedList);
utxoSeries.getData().sort((o1, o2) -> (int) (o2.getYValue().longValue() - o1.getYValue().longValue()));
change.getAddedSubList().forEach(entry -> entry.labelProperty().addListener((observable, oldValue, newValue) -> {
Optional<XYChart.Data<String, Number>> optData = utxoSeries.getData().stream().filter(data -> data.getExtraValue().equals(entry)).findFirst();
if(optData.isPresent()) {
int index = utxoSeries.getData().indexOf(optData.get());
utxoSeries.getData().set(index, new XYChart.Data<>(newValue, optData.get().getYValue(), optData.get().getExtraValue()));
}
}));
}
if(change.wasRemoved()) {
change.getRemoved().forEach(entry -> {
UtxoData utxoData = new UtxoData(entry);
Optional<XYChart.Data<String, Number>> optRemovedData = utxoSeries.getData().stream().filter(data -> data.getExtraValue().equals(utxoData.entry)).findFirst();
optRemovedData.ifPresent(removedData -> utxoSeries.getData().remove(removedData));
});
}
}
});
}
@Subscribe
public void walletNodesChanged(WalletNodesChangedEvent event) {
if(event.getWallet().equals(walletForm.getWallet())) {
utxosTable.updateAll(getWalletForm().getWalletUtxosEntry());
initializeChart(getWalletForm().getWalletUtxosEntry());
}
}
@ -39,4 +96,16 @@ public class UtxosController extends WalletFormController implements Initializab
utxosTable.updateHistory(event.getHistoryChangedNodes());
}
}
private static class UtxoData {
public final Entry entry;
public final String name;
public final Number value;
public UtxoData(Entry entry) {
this.entry = entry;
this.name = entry.getLabel() != null && !entry.getLabel().isEmpty() ? entry.getLabel() : ((UtxoEntry)entry).getDescription();
this.value = entry.getValue();
}
}
}

View file

@ -39,6 +39,7 @@ public class WalletUtxosEntry extends Entry {
utxoEntry.setDuplicateAddress(true);
} else {
addressMap.put(address, utxoEntry);
utxoEntry.setDuplicateAddress(false);
}
}
}

View file

@ -2,4 +2,8 @@
-fx-font-weight: bold;
-fx-font-size: 1.2em;
-fx-padding: 10 0 10 0;
}
.chart-bar {
-fx-bar-fill: rgba(105, 108, 119, 0.6);
}

View file

@ -9,6 +9,8 @@
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.BarChart?>
<?import com.sparrowwallet.sparrow.control.UtxosTreeTable?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<GridPane hgap="10.0" vgap="10.0" stylesheets="@utxos.css, @wallet.css, @../general.css" styleClass="wallet-pane" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sparrowwallet.sparrow.wallet.UtxosController">
<padding>
<Insets left="25.0" right="25.0" top="15.0" bottom="25.0" />
@ -30,7 +32,14 @@
</BorderPane>
<BorderPane GridPane.columnIndex="0" GridPane.rowIndex="1">
<center>
<!-- <BarChart fx:id="changeTable" /> -->
<BarChart fx:id="utxosChart" legendVisible="false" verticalGridLinesVisible="false" animated="false">
<xAxis>
<CategoryAxis side="BOTTOM" animated="false" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</BarChart>
</center>
</BorderPane>
</GridPane>