CORDA-1197: Take into account last amount submitted when adding data points.

Current logic in `CashWidget` is not handling well updates done in close succession, i.e. less than 1 second.
And such frequent updates do indeed happen, e.g. from: `net.corda.client.jfx.model.ContractStateModel#cashStates`
where `list` is modified twice.

Also use `.toDecimal()` instead of `.quantity`to have amount represented in pounds rather than in pennies.

(cherry picked from commit 952cc35)
This commit is contained in:
Viktor Kolomeyko 2018-03-08 17:23:16 +00:00
parent 198fb4f264
commit af60848da7

View File

@ -11,6 +11,7 @@ import javafx.collections.ObservableList
import javafx.geometry.Insets import javafx.geometry.Insets
import javafx.scene.Parent import javafx.scene.Parent
import javafx.scene.chart.NumberAxis import javafx.scene.chart.NumberAxis
import javafx.scene.chart.XYChart
import javafx.scene.control.* import javafx.scene.control.*
import javafx.scene.input.MouseButton import javafx.scene.input.MouseButton
import javafx.scene.layout.BorderPane import javafx.scene.layout.BorderPane
@ -313,12 +314,26 @@ class CashViewer : CordaView("Cash") {
linechart(null, xAxis, yAxis) { linechart(null, xAxis, yAxis) {
series("USD") { series("USD") {
sumAmount.addListener { _, _, _ -> sumAmount.addListener { _, _, _ ->
val lastAmount = data.last().value?.yValue
val currAmount = sumAmount.value.toDecimal()
val lastTimeStamp = data.last().value?.xValue val lastTimeStamp = data.last().value?.xValue
if (lastTimeStamp == null || System.currentTimeMillis() - lastTimeStamp.toLong() > 1.seconds.toMillis()) { val currentTimeStamp = System.currentTimeMillis()
data(System.currentTimeMillis(), sumAmount.value.quantity)
runInFxApplicationThread { // If amount is not the same - always add a data point.
// Modify data in UI thread. if (lastAmount == null || lastAmount != currAmount) {
if (data.size > 300) data.remove(0, 1) // If update arrived in very close succession to the previous one - kill the last point received to eliminate un-necessary noise on the graph.
if(lastTimeStamp != null && currentTimeStamp - lastTimeStamp.toLong() < 1.seconds.toMillis()) {
data.safelyTransition {
remove(size - 1, size)
}
}
// Add a new data point.
data(currentTimeStamp, currAmount)
// Limit population of data points to make graph painting faster.
data.safelyTransition {
if (size > 300) remove(0, 1)
} }
} }
} }
@ -327,5 +342,12 @@ class CashViewer : CordaView("Cash") {
animated = false animated = false
} }
} }
private fun <X, Y> ObservableList<XYChart.Data<X, Y>>.safelyTransition(block: ObservableList<XYChart.Data<X, Y>>.() -> Unit) {
runInFxApplicationThread {
// Modify data in UI thread to properly propagate to GUI.
this.block()
}
}
} }
} }