实时LineChart JavaFx

时间:2014-03-26 14:33:12

标签: javafx arduino real-time linechart scenebuilder

我的GUI出了问题。我正在尝试创建一个显示Arduino微控制器存储的模拟值的Gui。我能够在数组中存储我想要的所有数据。现在我使用SceneBuilder和JavaFx绘制这个模拟信号的数组,我看到真实的信号,但图表是自动缩放和散射。我想有更多类似的东西: JAVAFX中的高级股票线图示例 JavaFX Example

这是我的代码。每次我收到一个可以更新图形的值时,都会调用函数updateGraph1(...)。

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));


}
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

看看这个sample 。可见类别在运行时进行管理,以便只显示最后添加的十个类别。