I'm using BarChart and I've a very strange problem. First time I display data they are displayed correclty in the chart, but next times the reference axes moves.
Thi is a test case to reproduce the problem:
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.chart.Chart">
<children>
<BarChart fx:id="chart">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</BarChart>
</children>
</AnchorPane>
Chart.java
package application.chart;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.stage.Stage;
public class Chart extends Application implements Initializable {
@FXML
private BarChart<String, Integer> chart;
private ObjectProperty<ObservableList<Series<String, Integer>>> seriesProperty = new SimpleObjectProperty<ObservableList<Series<String, Integer>>>();
public static void main(String[] args) {
Application.launch(Chart.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World!");
Parent root = FXMLLoader.load(getClass().getResource("Chart.fxml"));
primaryStage.setScene(new Scene(root, 700, 500));
primaryStage.show();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
chart.dataProperty().bind(seriesProperty);
loadRemoteData();
}
private void loadRemoteData() {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
System.out.println("Attesa....");
Thread.sleep(1500);
Series<String, Integer> serie = new Series<String, Integer>();
List<Data<String, Integer>> valori = new ArrayList<>();
Data<String, Integer> data1 = null, data2 = null, data3 = null;
data1 = new XYChart.Data<String, Integer>("Column1", 1);
data2 = new XYChart.Data<String, Integer>("Column2", 5);
data3 = new XYChart.Data<String, Integer>("Column3", 0);
valori.add(data1);
valori.add(data2);
valori.add(data3);
serie.setData(FXCollections.observableArrayList(valori));
final List<Series<String, Integer>> lista = new ArrayList<Series<String, Integer>>();
lista.add(serie);
// Aggiorno la proprietà su cui il grafico esegue il binding
Platform.runLater(() -> seriesProperty.set(FXCollections.observableArrayList(lista)));
System.out.println("Attesa....");
Thread.sleep(1500);
serie = new Series<String, Integer>();
valori = new ArrayList<>();
data1 = new XYChart.Data<String, Integer>("Column1", 0);
data2 = new XYChart.Data<String, Integer>("Column2", 0);
data3 = new XYChart.Data<String, Integer>("Column3", 0);
valori.add(data1);
valori.add(data2);
valori.add(data3);
serie.setData(FXCollections.observableArrayList(valori));
lista.clear();
lista.add(serie);
// Aggiorno la proprietà su cui il grafico esegue il binding
Platform.runLater(() -> seriesProperty.set(FXCollections.observableArrayList(lista)));
System.out.println("Attesa....");
Thread.sleep(1500);
serie = new Series<String, Integer>();
valori = new ArrayList<>();
data1 = new XYChart.Data<String, Integer>("Column1", 1);
data2 = new XYChart.Data<String, Integer>("Column2", 1);
data3 = new XYChart.Data<String, Integer>("Column3", 1);
valori.add(data1);
valori.add(data2);
valori.add(data3);
serie.setData(FXCollections.observableArrayList(valori));
lista.clear();
lista.add(serie);
// Aggiorno la proprietà su cui il grafico esegue il binding
Platform.runLater(() -> seriesProperty.set(FXCollections.observableArrayList(lista)));
return null;
}
};
new Thread(task).start();
}
}
When you run the example youll see 3 refresh of the chat: first time are displayed values 1,5,0, then 0,0,0 and then should be displayed 1,1,1 but as you can see in the image you'll see 0.9,0.9,0.9.
http://snag.gy/XG0A6.jpg
Is this a bug?
Thanks
Thi is a test case to reproduce the problem:
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.chart.Chart">
<children>
<BarChart fx:id="chart">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</BarChart>
</children>
</AnchorPane>
Chart.java
package application.chart;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.stage.Stage;
public class Chart extends Application implements Initializable {
@FXML
private BarChart<String, Integer> chart;
private ObjectProperty<ObservableList<Series<String, Integer>>> seriesProperty = new SimpleObjectProperty<ObservableList<Series<String, Integer>>>();
public static void main(String[] args) {
Application.launch(Chart.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World!");
Parent root = FXMLLoader.load(getClass().getResource("Chart.fxml"));
primaryStage.setScene(new Scene(root, 700, 500));
primaryStage.show();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
chart.dataProperty().bind(seriesProperty);
loadRemoteData();
}
private void loadRemoteData() {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
System.out.println("Attesa....");
Thread.sleep(1500);
Series<String, Integer> serie = new Series<String, Integer>();
List<Data<String, Integer>> valori = new ArrayList<>();
Data<String, Integer> data1 = null, data2 = null, data3 = null;
data1 = new XYChart.Data<String, Integer>("Column1", 1);
data2 = new XYChart.Data<String, Integer>("Column2", 5);
data3 = new XYChart.Data<String, Integer>("Column3", 0);
valori.add(data1);
valori.add(data2);
valori.add(data3);
serie.setData(FXCollections.observableArrayList(valori));
final List<Series<String, Integer>> lista = new ArrayList<Series<String, Integer>>();
lista.add(serie);
// Aggiorno la proprietà su cui il grafico esegue il binding
Platform.runLater(() -> seriesProperty.set(FXCollections.observableArrayList(lista)));
System.out.println("Attesa....");
Thread.sleep(1500);
serie = new Series<String, Integer>();
valori = new ArrayList<>();
data1 = new XYChart.Data<String, Integer>("Column1", 0);
data2 = new XYChart.Data<String, Integer>("Column2", 0);
data3 = new XYChart.Data<String, Integer>("Column3", 0);
valori.add(data1);
valori.add(data2);
valori.add(data3);
serie.setData(FXCollections.observableArrayList(valori));
lista.clear();
lista.add(serie);
// Aggiorno la proprietà su cui il grafico esegue il binding
Platform.runLater(() -> seriesProperty.set(FXCollections.observableArrayList(lista)));
System.out.println("Attesa....");
Thread.sleep(1500);
serie = new Series<String, Integer>();
valori = new ArrayList<>();
data1 = new XYChart.Data<String, Integer>("Column1", 1);
data2 = new XYChart.Data<String, Integer>("Column2", 1);
data3 = new XYChart.Data<String, Integer>("Column3", 1);
valori.add(data1);
valori.add(data2);
valori.add(data3);
serie.setData(FXCollections.observableArrayList(valori));
lista.clear();
lista.add(serie);
// Aggiorno la proprietà su cui il grafico esegue il binding
Platform.runLater(() -> seriesProperty.set(FXCollections.observableArrayList(lista)));
return null;
}
};
new Thread(task).start();
}
}
When you run the example youll see 3 refresh of the chat: first time are displayed values 1,5,0, then 0,0,0 and then should be displayed 1,1,1 but as you can see in the image you'll see 0.9,0.9,0.9.
http://snag.gy/XG0A6.jpg
Is this a bug?
Thanks