我有一个对象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
的ArrayList
作为参数的DataEntry
构造函数,因为我想将此图表集成到场景中。以下是我走了多远,但是我无法执行我的程序。
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);
}
}
如果这不可能,我想知道是否有一种基于参数创建LineChart的方法,例如
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);
}
}
关于java - Java FX LineChart对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38310389/