Real-time LineChart JavaFx


Question

I have a problem with my GUI. I am trying to create a Gui which displays some analog values stored by the Arduino microcontroller. I am able to store all data I want in an array. Now I am using SceneBuilder and JavaFx to plot the arrays of this analog signal and I see the real signal but the graph is autoscaling and scattering . I would like to have more something like the following: Advanced Stock Line Chart example in JAVAFX JavaFX Example

Here is my code. The function updateGraph1(...) is called everytime that I receive a value which can update the graph.

package de.zft.degreen.view;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ValueAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Gui extends Application implements Runnable, Initializable {

@FXML
private static LineChart<Number,Number> graph1; 

public static XYChart.Series<Number, Number> series;

private ChangeListener changeListener;
private static Scene scene;

public Gui() {
    series = new XYChart.Series<>();
    series.setName("bye");

}

public ChangeListener getChangeListener() {
    return changeListener;
}

public void setChangeListener(ChangeListener changeListener) {
    this.changeListener = changeListener;
}



@Override
public void start(Stage primaryStage) throws Exception {
    System.out.println("Start start");
    primaryStage.setTitle("DEGREEN GUI");
    System.out.println("Before");
    Pane myPane = FXMLLoader.load(getClass().getResource("Gui2.fxml"));
    System.out.println("After");
    System.out.println("Step1");
    scene = new Scene(myPane);
    System.out.println("Step2");
    primaryStage.setScene(scene);
    System.out.println("Step3");
    primaryStage.show();
    System.out.println("End start");
}

private void init(Stage primaryStage){
    System.out.println("Start Init");
}

@FXML
public void updateGraph1(Long long1, int v){
    System.out.println("Start updateGraph1"+series.getName());
    final int q = v;
    final long long2 = long1;
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            add2Series(long2,q,100);
        }
    });
    System.out.println("End updateGraph1");

}
@Override
public void run() {
    System.out.println("Gui Initialized");
    launch();
    System.out.println("Gui Ended");
}

@FXML
public void initialize(URL arg0, ResourceBundle arg1) {
    System.out.println("Start Initialize"+series.getName());
    series = new XYChart.Series<Number,Number>();
    series.setName("Voltage DE1");
    graph1.getData().add(series);
    graph1.setAnimated(false);
    System.out.println("End Initialize"+series.getName());
}
@FXML
protected static void add2Series(Number x, Number y, int displayedSize) {
    int seriesSize = series.getData().size(); 

    if (seriesSize >= displayedSize) {
        series.getData().remove(0);
    }
    series.getData().add(new XYChart.Data<Number,Number>(x,y));


}
}

Someone Can help me please?

1
0
3/26/2014 2:33:11 PM

Take a look at this sample . The visible categories are managed in runtime so that only last ten added are visible.

0
3/27/2014 8:36:44 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon