Javafx 简明教程

JavaFX - Line Chart

折线图或折线图将信息显示为一系列通过直线段连接的数据点(标记)。折线图显示了数据如何以相等的时间频率变化。

A line chart or line graph displays information as a series of data points (markers) connected by straight line segments. A Line Chart shows how the data changes at equal time frequency.

下面是一个折线图,描述了不同年份的学校数量。

Following is a Line chart depicting the number of schools in different years.

line chart

Line Chart in JavaFX

在 JavaFX 中,折线图由名为 LineChart 的类表示。此类属于 javafx.scene.chart 包。通过实例化此类,可以在 JavaFX 中创建一个 LineChart 节点。

In JavaFX, a line chart is represented by a class named LineChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a LineChart node in JavaFX.

要在 JavaFX 中生成折线图,应按照以下步骤操作。

To generate a line chart in JavaFX, you should follow the steps given below.

Step 1: Defining the axis

在 Application 类的 start() 方法中定义折线图的 X 轴和 Y 轴,并为其设置标签。在我们的示例中,X 轴表示从 1960 年到 2020 年的年份,主要刻度标记每十年出现一次。

Define the X and Y axis of the line chart and set labels to them, within the start() method of Application class. In our example, the X axis represent the years starting from 1960 to 2020 having major tick mark at every ten years.

public class ClassName extends Application {
   @Override
   public void start(Stage primaryStage) throws Exception {
      //Defining X axis
      NumberAxis xAxis = new NumberAxis(1960, 2020, 10);
      xAxis.setLabel("Years");

      //Defining y axis
      NumberAxis yAxis = new NumberAxis(0, 350, 50);
      yAxis.setLabel("No.of schools");
   }
}

Step 2: Creating the Line Chart

通过实例化包 javafx.scene.chart 中名为 LineChart 的类创建一个折线图。向此类的构造函数传递在上一步骤中创建的表示 X 和 Y 轴的对象。

Create a line chart by instantiating the class named LineChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step.

LineChart linechart = new LineChart(xAxis, yAxis);

Step 3: Preparing the Data

实例化 XYChart.Series 类。然后将数据(一系列 x 和 y 坐标)添加到此类的 Observable 列表中,如下所示 −

Instantiate the XYChart.Series class. Then add the data (a series of, x and y coordinates) to the Observable list of this class as follows −

XYChart.Series series = new XYChart.Series();
series.setName("No of schools in an year");

series.getData().add(new XYChart.Data(1970, 15));
series.getData().add(new XYChart.Data(1980, 30));
series.getData().add(new XYChart.Data(1990, 60));
series.getData().add(new XYChart.Data(2000, 120));
series.getData().add(new XYChart.Data(2013, 240));
series.getData().add(new XYChart.Data(2014, 300));

Step 4: Add Data to the Line Chart

将上一步骤中准备的数据系列添加到折线图中,如下所示 −

Add the data series prepared in the previous step to the line chart as follows −

//Setting the data to Line chart
linechart.getData().add(series);

Step 5: Creating a Group Object

start() 方法中,通过实例化名为 Group 的类创建组对象。这属于包 javafx.scene

In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene.

将上一步骤中创建的 LineChart(节点)对象作为参数传递给 Group 类的构造函数。应该这样做以按照如下方式将其添加到组中 −

Pass the LineChart (node) object, created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows −

Group root = new Group(linechart);

Step 6: Launching Application

最后,按照下面给出的步骤正确启动应用程序 −

Lastly, follow the given steps below to launch the application properly −

  1. Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.

  2. Then, set the title to the stage using the setTitle() method of the Stage class.

  3. Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  4. Display the contents of the scene using the method named show().

  5. Lastly, the application is launched with the help of the launch() method.

Example

下表描绘了从 1970 年到 2014 年区域内的学校数量。

The following table depicts the number of schools that were in an area from the year 1970 to 2014.

Year

Number of Schools

1970

15

1980

30

1990

60

2000

120

2013

240

2014

300

下面是一个使用 JavaFX 生成折线图以描述上述数据的 Java 程序。

Following is a Java program which generates a line chart, depicting the above data, using JavaFX.

使用名称 LineChartExample.java 将此代码保存在文件中。

Save this code in a file with the name LineChartExample.java.

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

public class LineChartExample extends Application {
   @Override
   public void start(Stage stage) {
      //Defining the x axis
      NumberAxis xAxis = new NumberAxis(1960, 2020, 10);
      xAxis.setLabel("Years");

      //Defining the y axis
      NumberAxis yAxis = new NumberAxis   (0, 350, 50);
      yAxis.setLabel("No.of schools");

      //Creating the line chart
      LineChart linechart = new LineChart(xAxis, yAxis);

      //Prepare XYChart.Series objects by setting data
      XYChart.Series series = new XYChart.Series();
      series.setName("No of schools in an year");

      series.getData().add(new XYChart.Data(1970, 15));
      series.getData().add(new XYChart.Data(1980, 30));
      series.getData().add(new XYChart.Data(1990, 60));
      series.getData().add(new XYChart.Data(2000, 120));
      series.getData().add(new XYChart.Data(2013, 240));
      series.getData().add(new XYChart.Data(2014, 300));

      //Setting the data to Line chart
      linechart.getData().add(series);

      //Creating a Group object
      Group root = new Group(linechart);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 400);

      //Setting title to the Stage
      stage.setTitle("Line Chart");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls LineChartExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls LineChartExample

在执行上述程序时,它将生成一个 JavaFX 窗口,显示折线图,如下所示。

On executing, the above program generates a JavaFX window displaying a line chart as shown below.

line chart example

Example

下表说明了每个月售出的电子设备数量。

The following table illustrates the number of electronic items that were in sold per month.

Item

Number of Sales (Per Month)

Laptop

176

TV

30

Mobile

540

Smart Watch

250

MacBook

60

在另一个示例中,使用 JavaFX 生成了描绘上述数据的折线图。使用名称 LineChartItems.java 将此代码保存在文件中。

In another example, a line chart depicting the above data, is generated using JavaFX. Save this code in a file with the name LineChartItems.java.

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

public class LineChartItems extends Application {
   @Override
   public void start(Stage stage) {
      //Defining the x axis
      CategoryAxis xAxis = new CategoryAxis();
      xAxis.setLabel("Items");

      //Defining the y axis
      NumberAxis yAxis = new NumberAxis();
      yAxis.setLabel("Sales (per month)");

      //Creating the line chart
      LineChart linechart = new LineChart(xAxis, yAxis);

      //Prepare XYChart.Series objects by setting data
      XYChart.Series series = new XYChart.Series();
      series.setName("Items sold per month");

      series.getData().add(new XYChart.Data("Laptop", 176));
      series.getData().add(new XYChart.Data("TV", 30));
      series.getData().add(new XYChart.Data("Mobile", 540));
      series.getData().add(new XYChart.Data("Smart Watch", 250));
      series.getData().add(new XYChart.Data("MacBook", 60));

      //Creating a scene object
      Scene scene = new Scene(linechart, 600, 400);
	  //Setting the data to Line chart
      linechart.getData().add(series);

      //Setting title to the Stage
      stage.setTitle("Line Chart");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}