Java FX对象线条图
我有一个对象Java FX对象线条图,java,javafx,linechart,Java,Javafx,Linechart,我有一个对象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 timesta
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
构造函数,它将DataEntry
的ArrayList
作为参数,因为我想将此图表集成到场景中
下面是我走了多远,但我无法执行我的程序
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);
}
}
并且能够将其集成到场景中。我设法使解决方案起作用,我使用控制器将这些值作为属性传递(即使我可以不使用这些值,但我只想将其作为属性,以确保我在该方法之外仍然可以访问它)
公共类GraphListener{
ArrayList条目=新的ArrayList();
@FXML
私有按钮更新按钮;
@FXML
私有组合框属性Combo;
@FXML
专用按钮生成按钮;
@FXML
私有线形图数据图;
@FXML
私人文本区statsTextArea;
@FXML
受保护的无效handleUpdateButtonAction(ActionEvent事件){
HBaseClient client=新的HBaseClient();
client.makeConnection();
ArrayList属性=client.getColumnName(“demo”);
propertiesCombo.getItems().clear();
propertiesCombo.getItems().addAll(属性);
client.disconnect();
}
@FXML
受保护的无效handleGenerateButtonAction(ActionEvent事件){
//检索数据
String property=propertiesCombo.getSelectionModel().getSelectedItem();
HBaseClient client=新的HBaseClient();
client.makeConnection();
this.entries=client.scan(“演示”、“数据”、属性);
client.disconnect();
//填充文本区域
statsTextArea.clear();
statsTextArea.appendText(“元素数:“+handler.numberOfElements()+”\n”);
statsTextArea.appendText(“平均值:“+handler.Mean()+”\n”);
statsTextArea.appendText(“几何平均值:“+handler.geometricMean()+”\n”);
statsTextArea.appendText(“最小值:“+handler.min()+”\n”);
statsTextArea.appendText(“最大值:“+handler.max()+”\n”);
//生成图形
dataChart.setTitle(““+财产的演变”);
Series系列=新的XYChart.Series();
series.setName(属性);
ArrayList时间戳=新的ArrayList();
for(数据条目d:this.entries){
series.getData().add(新数据(d.getTimeStamp(),d.getValue());
add(d.getTimeStamp());
}
dataChart.getXAxis().setLabel(“时间戳”);
dataChart.getYAxis().setLabel(“值”);
dataChart.getData().add(系列);
}
}
应用程序不应具有参数化构造函数,启动器将无法构造应用程序,因为它不知道要向构造函数提供哪些值。输入数据(标题和条目)来自哪里?如果是命令行,那么您应该在应用程序中使用(可能在start()方法中)来检索命令行数据。这些参数来自我的应用程序,比如说,我想用给定数据的所有条目构建一个图表,如果可能的话,我想使用类似DataEntryChart(“温度”)的东西来构建此图表,arrayList)但这没有任何意义。您提供的DataEntryChart类扩展了应用程序,因此该类就是您的应用程序。在DataEntryChart应用程序中,当应用程序启动时,除了通过命令行参数(您当前未使用)之外,无法为图表提供输入数据。好的,我明白您的意思,但是有没有办法在不使用命令行的情况下生成带参数的图形,并将此场景放到舞台上?
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);
}
}