Java 对象的 FX 折线图

Java FX LineChart of Objects

我有一个对象 DataEntry 定义如下:

public class DataEntry {

private long timestamp;
private double value;

public DataEntry(long timestamp, double value) {
    this.timestamp = timestamp;
    this.value = value;
}

public long getTimeStamp() {
    return timestamp;
}

public double getValue() {
    return value;
}

我想用 X 中的时间戳和 Y 中的值生成这些元素的 LineChart。但是我在 Internet 上找不到任何关于此的教程(也许我正在尝试做不可能的事)。

对我来说最好的情况是 LineChart 构造函数,它将 DataEntryArrayList 作为参数,因为我想将此图表集成到场景中。

以下是我走了多远,但我无法执行我的程序。

public class DataEntryChart extends Application {

private String title;
private ArrayList<DataEntry> entries;

public DataEntryChart(String title, ArrayList<DataEntry> entries){
    this.title=title;
    this.entries=entries;
}

@Override public void start(Stage stage) {
    stage.setTitle(this.title);
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Date");       

    final LineChart<Number,Number> lineChart = 
            new LineChart<Number,Number>(xAxis,yAxis);               

    Series series = new XYChart.Series<Long,Double>();      

    for(DataEntry d : this.entries){
        series.getData().add(new XYChart.Data(d.getTimeStamp(),d.getValue()));
    }

    Scene scene  = new Scene(lineChart,800,600);
    lineChart.getData().add(series);


    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    launch(args);
 }
}

如果这不可能,我想知道是否有一种方法可以根据参数创建折线图,例如

new LineChart(int[] x, int[]y)

并且能够将其集成到一个场景中。

我设法让一个解决方案起作用,我使用控制器将这些值作为属性传递(即使我可以不这样做,我只是想将其作为属性以确保我仍然可以在此之外访问它方法)

public class GraphListener {

    ArrayList<DataEntry> entries = new ArrayList<DataEntry>();

    @FXML
    private Button updateButton;
    @FXML
    private ComboBox<String> propertiesCombo;
    @FXML
    private Button generateButton;
    @FXML
    private LineChart<Number, Number> dataChart;
    @FXML
    private TextArea statsTextArea;

    @FXML
    protected void handleUpdateButtonAction(ActionEvent event) {
        HBaseClient client = new HBaseClient();
        client.makeConnection();
        ArrayList<String> properties = client.getColumnName("demo");

        propertiesCombo.getItems().clear();
        propertiesCombo.getItems().addAll(properties);

        client.disconnect();
    }

    @FXML
    protected void handleGenerateButtonAction(ActionEvent event) {
        // Retrieving data
        String property = propertiesCombo.getSelectionModel().getSelectedItem();

        HBaseClient client = new HBaseClient();
        client.makeConnection();
        this.entries = client.scan("demo", "data", property);
        client.disconnect();

        // Filling the text area
        statsTextArea.clear();
        statsTextArea.appendText("Number of elements : " + handler.numberOfElements()+"\n");
        statsTextArea.appendText("Mean : " + handler.mean()+"\n");
        statsTextArea.appendText("Geometric mean : " + handler.geometricMean()+"\n");
        statsTextArea.appendText("Minimal value : " + handler.min()+"\n");
        statsTextArea.appendText("Maximal value : " + handler.max()+"\n");

        // Generating the graph

        dataChart.setTitle("Evolution of "+property);


        Series<Number, Number> series = new XYChart.Series<Number, Number>();
        series.setName(property);
        ArrayList<Long> timestamps = new ArrayList<Long>();
        for (DataEntry d : this.entries) {
            series.getData().add(new Data<Number, Number>(d.getTimeStamp(), d.getValue()));
            timestamps.add(d.getTimeStamp());
        }
        dataChart.getXAxis().setLabel("Timestamp");
        dataChart.getYAxis().setLabel("Values");
        dataChart.getData().add(series);

    }

}