gpt4 book ai didi

对象的 Javafx 线图

转载 作者:行者123 更新时间:2023-11-30 10:41:57 25 4
gpt4 key购买 nike

我有一个对象 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)

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

最佳答案

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

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

}

}

关于对象的 Javafx 线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com