JavaFX绘制图表:如何在类中指定值

时间:2016-07-17 22:19:16

标签: java javafx

我试图通过修改http://docs.oracle.com/javafx/2/charts/line-chart.htm中的示例来创建一个可以绘制xy图表的类。代码如下。如何更改数据和标题?如果我设置lineChart.setTitle(title); as lineChart.setTitle(" abc");它会工作,但当我使用变量名称。它没有。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

import java.util.ArrayList;

public class PlotLines extends Application {
private String title;
private String xLab;
private String yLab;
double [][] xy;

public void setTitle(String title) {
    this.title = title;
}

public void setxLab(String xLab) {
    this.xLab = xLab;
}

public void setyLab(String yLab) {
    this.yLab = yLab;
}

public void setxy(double[][] xy) {
    this.xy = xy;
}

public void plot(){
    launch();
}


@Override
public void start(Stage stage) throws Exception {
    //stage.setTitle(title);
    //defining the axes
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel(xLab);
    yAxis.setLabel(yLab);

    //creating the chart
    final LineChart<Number,Number> lineChart =
            new LineChart<Number,Number>(xAxis,yAxis);

    lineChart.setTitle(title);
    //defining a series
    XYChart.Series series = new XYChart.Series();
    series.setName("Data Points");
    //populating the series with data

    //System.out.println(xy.length);

    series.getData().add(new XYChart.Data(1, 23));
    series.getData().add(new XYChart.Data(2, 14));
    series.getData().add(new XYChart.Data(3, 15));
    series.getData().add(new XYChart.Data(4, 24));
    series.getData().add(new XYChart.Data(5, 34));
    series.getData().add(new XYChart.Data(6, 36));
    series.getData().add(new XYChart.Data(7.5, 22));
    series.getData().add(new XYChart.Data(8, 45));
    series.getData().add(new XYChart.Data(9, 43));
    series.getData().add(new XYChart.Data(10, 17));

    series.getData().add(new XYChart.Data(11, 29));
    series.getData().add(new XYChart.Data(12, 25));

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

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

以下代码将此类称为

public class PlotExample  {



public static void main(String[] args) {

    double [][] xy = {{1.,2.},{3.,5.},{6.,7.}};

    PlotLines aa = new PlotLines();
    aa.setTitle("CGM");
    aa.setxLab("Time");
    aa.setyLab("Glucose");
    aa.setxy(xy);

    aa.plot();
}
}

1 个答案:

答案 0 :(得分:1)

您的申请无法正常使用。

您的班级PlotExample采用main方法。在该类中,您可以创建PlotLines的实例,设置值并调用启动应用程序的plot方法。

在您的情况下,应用程序是PlotLines的实例。启动应用程序时会发生什么情况,请参见JavaFX API:

  

JavaFX应用程序的入口点是Application类。无论何时启动应用程序,JavaFX运行时都会按顺序执行以下操作:

     
      
  1. 构造指定Application类的实例
  2.   
  3. 调用init()方法
  4.   
  5. 调用start(javafx.stage.Stage)方法
  6.   
  7. 等待应用程序完成,当发生以下任一情况时会发生:      
        
    • 应用程序调用Platform.exit()
    •   
    • 最后一个窗口已关闭且Platform上的implicitExit属性为true
    •   
  8.   
  9. 调用stop()方法
  10.   

如文档所述,在第一步中构建了一个应用程序实例(这里是PlotLines的一个实例)。此实例的标题和轴标签的值为null PlotLines的原始实例将被忽略。

要解决此问题,您应该先调用launch,然后在应用程序的start方法中构建模型,就像在示例中一样。

对于您的代码,您可以执行以下操作:

public class PlotExample extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        // stage.setTitle(title);
        // defining the axes

        double[][] xy = { { 1., 2. }, { 3., 5. }, { 6., 7. } };

        PlotLines aa = new PlotLines();
        aa.setTitle("CGM");
        aa.setxLab("Time");
        aa.setyLab("Glucose");
        aa.setxy(xy);


        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel(aa.getxLab());
        yAxis.setLabel(aa.getyLab());

        // creating the chart
        final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);

        lineChart.setTitle(aa.getTitle());
        // defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("Data Points");
        // populating the series with data

        // System.out.println(xy.length);

        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        series.getData().add(new XYChart.Data(5, 34));
        series.getData().add(new XYChart.Data(6, 36));
        series.getData().add(new XYChart.Data(7.5, 22));
        series.getData().add(new XYChart.Data(8, 45));
        series.getData().add(new XYChart.Data(9, 43));
        series.getData().add(new XYChart.Data(10, 17));

        series.getData().add(new XYChart.Data(11, 29));
        series.getData().add(new XYChart.Data(12, 25));

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

        stage.setScene(scene);
        stage.show();
    }
}
public class PlotLines {
    private String title;
    private String xLab;
    private String yLab;
    double[][] xy;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setxLab(String xLab) {
        this.xLab = xLab;
    }

    public void setyLab(String yLab) {
        this.yLab = yLab;
    }

    public void setxy(double[][] xy) {
        this.xy = xy;
    }

    public String getTitle() {
        return title;
    }

    public String getxLab() {
        return xLab;
    }

    public String getyLab() {
        return yLab;
    }

    public double[][] getXy() {
        return xy;
    }

}