Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to reset data in a line chart data series ?

798735Aug 18 2011 — edited Aug 20 2011
I have a line chart with 2 data series as under
 public static ObservableList<XYChart.Series<Double,Double>> lineChartData = FXCollections.observableArrayList(
            new LineChart.Series<Double,Double>("Series 1", FXCollections.observableArrayList(
                new XYChart.Data<Double,Double>(0.0, 1.0),
                new XYChart.Data<Double,Double>(1.2, 1.4),
                new XYChart.Data<Double,Double>(2.2, 1.9),
                new XYChart.Data<Double,Double>(2.7, 2.3),
                new XYChart.Data<Double,Double>(2.9, 0.5)
            )),
            new LineChart.Series<Double,Double>("Series 2", FXCollections.observableArrayList(
                new XYChart.Data<Double,Double>(2.0, 2.2)
            ))
        );
and I need to reset the data present at the zeroth index of the second data series i.e
 XYChart.Data<Double,Double>(2.0, 2.2)
needs to be reset to another x,y value dynamically on click of a button so in the button's action handler I am doing this
  lineChartData.get(1).getData().set(0, new XYChart.Data<Double,Double>(2.6, 2.0));
but unfortunately it is throwing a NullPointerException with JavaFX Beta b38 on my Win Vista machine
java.lang.NullPointerException
at javafx.scene.chart.LineChart.dataItemRemoved(LineChart.java:168)
at javafx.scene.chart.XYChart.dataItemsChanged(XYChart.java:306)
at javafx.scene.chart.XYChart.access$2500(XYChart.java:35)
at javafx.scene.chart.XYChart$Series$1.onChanged(XYChart.java:1111) 
I have filed a bug in the JavaFX Jira over here [http://javafx-jira.kenai.com/browse/RT-15291|http://javafx-jira.kenai.com/browse/RT-15291]

but it was closed as it was not reproducible at their end

I am not sure if there is any workaround for this, I tried clearing the list and adding a new value it works without any problem but it doesn't refresh the chart with the new value

Would like to know whether I am doing anything wrong, and also whether anyone else is facing the same issue or not ?

Below given is the sample program with which I am able to reproduce this issue on my system, execute it and click the reset button and check the stacktrace.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @author abhilshit
 */
public class ResetChartTest extends Application {
    
    public static ObservableList<XYChart.Series<Double,Double>> lineChartData = FXCollections.observableArrayList(
            new LineChart.Series<Double,Double>("Series 1", FXCollections.observableArrayList(
                new XYChart.Data<Double,Double>(0.0, 1.0),
                new XYChart.Data<Double,Double>(1.2, 1.4),
                new XYChart.Data<Double,Double>(2.2, 1.9),
                new XYChart.Data<Double,Double>(2.7, 2.3),
                new XYChart.Data<Double,Double>(2.9, 0.5)
            )),
            new LineChart.Series<Double,Double>("Series 2", FXCollections.observableArrayList(
                new XYChart.Data<Double,Double>(2.0, 2.2)
            ))
        );
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(ResetChartTest.class, args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Reset");
        Group root = new Group();
        Scene scene = new Scene(root, 500, 450, Color.LIGHTGREEN);
        
        NumberAxis xAxis = new NumberAxis("X-Axis", 0, 3, 1);
        NumberAxis yAxis = new NumberAxis("Y-Axis", 0, 3, 1);
       
        LineChart chart = new LineChart(xAxis, yAxis, lineChartData);
        
        
        Button resetButton = new Button();
        resetButton.setText("Reset");
        resetButton.setLayoutX(400);
        resetButton.setLayoutY(350);
        resetButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                lineChartData.get(1).getData().set(0, new XYChart.Data<Double,Double>(2.6, 2.0));
            }
        });
        root.getChildren().
                addAll(chart,resetButton);        
        primaryStage.setScene(scene);
        primaryStage.setVisible(true);
    }
}
Would appreciate any help,suggestion or comments.

Thanks
Abhilshit
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 17 2011
Added on Aug 18 2011
1 comment
1,804 views