我有一个对象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;
}
我想生成这些元素的LineChart
,其中时间戳在X中,值在Y中。但我在互联网上找不到任何关于此的教程(也许我正在尝试做一些不可能的事情)。
对我来说最好的情况是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)
能够将其整合到场景中。
答案 0 :(得分:1)
我设法让解决方案正常工作,我使用控制器将这些值作为属性传递(即使我没有,我只想将其作为属性来确保我仍然可以在此方法之外访问它)
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);
}
}