Skip to content

Instantly share code, notes, and snippets.

@christophe-dooapp
Last active December 18, 2015 19:00
Show Gist options
  • Save christophe-dooapp/5830210 to your computer and use it in GitHub Desktop.
Save christophe-dooapp/5830210 to your computer and use it in GitHub Desktop.
package com.dooapp.FXBinding.model;
import com.dooapp.FXBinding.FXDoubleBinding;
import javafx.beans.binding.DoubleBinding;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.XYChart;
/**
* A simple demo class
* Created at 20/06/13 14:44.<br>
*
* @author Christophe DUFOUR
*/
public class Foo {
/**
* A list of Data values
*/
private ObservableList<XYChart.Data<Number, Number>> values = FXCollections.observableArrayList(new XYChart.Data<Number, Number>(0.3d, 0.3d), new XYChart.Data<Number, Number>(0.1d, 0.1d), new XYChart.Data<Number, Number>(0.2d, 0.2d));
/**
* A list of Data values
*/
public ObservableList<XYChart.Data<Number, Number>> values() {
return values;
}
/**
* the average value of all XValue of Data in values
*
* @see #values()
*/
private DoubleBinding averageBinding = new FXDoubleBinding() {
/**
* This method is called every time the binding is becoming invalid.
*/
@Override
protected void configure() {
addObservable(values());
for (XYChart.Data<Number, Number> d : values()) {
addObservable(d.XValueProperty());
}
}
@Override
protected double compute() {
double result = 0;
if (values().isEmpty()) {
return result;
}
for (XYChart.Data<Number, Number> d : values()) {
result += d.getXValue().doubleValue();
}
return result / values().size();
}
};
/**
* the average value of all double in values
*
* @see #values()
*/
public DoubleBinding averageBinding() {
return averageBinding;
}
/**
* Just a main to show a demo
*
* @param args args
*/
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println(foo.averageBinding().get()); //Should print 0.2
foo.values().add(new XYChart.Data<Number, Number>(-3d, -3d));
System.out.println(foo.averageBinding().get()); //Should print -0.6
foo.values().get(0).setXValue(30d);
System.out.println(foo.averageBinding().get()); //Should print 6.825
foo.values.get(3).setXValue(10d);
System.out.println(foo.averageBinding().get()); //Should print 10.075
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment