Javafx 简明教程
JavaFX - Scatter Chart
散点图是一种图形,它使用绘制在笛卡尔平面中的两个变量的值。它通常用于找出两个变量之间的关系。
A scatterplot is a type of graph which uses values from two variables plotted in a Cartesian plane. It is usually used to find out the relationship between two variables.
以下是按面积和重量绘制的散点图。
Following is a Scatter chart plotted between area and weight.
Scatter Chart in JavaFX
在 JavaFX 中,散点图由名为 ScatterChart 的类表示。此类属于 javafx.scene.chart 包。通过实例化此类,可以在 JavaFX 中创建 ScatterChart 节点。
In JavaFX, a Scatter chart is represented by a class named ScatterChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a ScatterChart node in JavaFX.
要生成 JavaFX 中的面积图,请按照以下步骤操作。
To generate an area chart in JavaFX, follow the steps given below.
Step 1: Defining the Axis
在应用程序类的 start() 方法中定义面积图的 X 和 Y 轴,并为其设置标签。在我们的示例中,X 轴表示面积,Y 轴表示重量。
Define the X and Y axis of the area chart and set labels to them in start() method of Application class. In our example, X axis represents area and the Y axis represents weights.
public class ClassName extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//Defining the x axis
NumberAxis xAxis = new NumberAxis(0, 12, 3);
xAxis.setLabel("Area");
//Defining the y axis
NumberAxis yAxis = new NumberAxis(0, 16, 4);
yAxis.setLabel("Weight");
}
}
Step 2: Creating the Scatter Chart
通过实例化包 javafx.scene.chart 中名为 ScatterChart 的类创建折线图。将表示在上一步骤中创建的 X 轴和 Y 轴的对象传递给此类的构造函数。
Create a line chart by instantiating the class named ScatterChart 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.
//Creating the Scatter chart
ScatterChart<String, Number> scatterChart = new ScatterChart(xAxis, yAxis);
Step 3: Preparing the Data
实例化 XYChart.Series 类并将数据(系列、x 和 y 坐标)添加到此类的可观察列表中,如下所示 −
Instantiate the XYChart.Series class and add the data (a series of, x and y coordinates) to the Observable list of this class as follows −
//Prepare XYChart.Series objects by setting data
XYChart.Series series = new XYChart.Series();
series.getData().add(new XYChart.Data(8, 12));
series.getData().add(new XYChart.Data(4, 5.5));
series.getData().add(new XYChart.Data(11, 14));
series.getData().add(new XYChart.Data(4, 5));
series.getData().add(new XYChart.Data(3, 3.5));
series.getData().add(new XYChart.Data(6.5, 7));
Step 4: Add Data to the Scatter Chart
将上一步骤中准备的数据系列添加到散点图,如下所示 −
Add the data series prepared in the previous step to the scatter chart as follows −
//Setting the data to scatter chart
scatterChart.getData().addAll(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.
将上一步骤中创建的 ScatterChart(节点)对象作为参数传递给 Group 类的构造函数。应这样做,以便将其添加到组中,如下所示 −
Pass the ScatterChart (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(scatterChart);
Step 6: Launching Application
最后,按照下面给出的步骤正确启动应用程序 −
Lastly, follow the given steps below to launch the application properly −
-
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.
-
Then, set the title to the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example
下表包含按面积和重量绘制的示例数据。
The following table contains sample data plotted between area and weight.
Area |
Weight |
8 |
12 |
4 |
5.5 |
11 |
14 |
4 |
5 |
3 |
3.5 |
6.5 |
7 |
以下是使用 JavaFX 生成描绘以上数据的散点图的 Java 程序。
Following is a Java program which generates a scatter chart depicting the above data using JavaFX.
将此代码保存在名为 ScatterChartExample.java 的文件中。
Save this code in a file with the name ScatterChartExample.java.
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
public class ScatterChartExample extends Application {
@Override
public void start(Stage stage) {
//Defining the axes
NumberAxis xAxis = new NumberAxis(0, 12, 3);
xAxis.setLabel("Area");
NumberAxis yAxis = new NumberAxis(0, 16, 4);
yAxis.setLabel("Weight");
//Creating the Scatter chart
ScatterChart<String, Number> scatterChart =
new ScatterChart(xAxis, yAxis);
//Prepare XYChart.Series objects by setting data
XYChart.Series series = new XYChart.Series();
series.getData().add(new XYChart.Data(8, 12));
series.getData().add(new XYChart.Data(4, 5.5));
series.getData().add(new XYChart.Data(11, 14));
series.getData().add(new XYChart.Data(4, 5));
series.getData().add(new XYChart.Data(3, 3.5));
series.getData().add(new XYChart.Data(6.5, 7));
//Setting the data to scatter chart
scatterChart.getData().addAll(series);
//Creating a Group object
Group root = new Group(scatterChart);
//Creating a scene object
Scene scene = new Scene(root, 600, 400);
//Setting title to the Stage
stage.setTitle("Scatter 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 ScatterChartExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ScatterChartExample
在执行时,上述程序生成一个 JavaFX 窗口,显示如下所示的散点图。
On executing, the above program generates a JavaFX window displaying a scatter chart as shown below.
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 生成描绘以上数据的散点图的 Java 程序。
Following is a Java program which generates a scatter chart depicting the above data using JavaFX.
将此代码保存在名为 ScatterChartSales.java 的文件中。
Save this code in a file with the name ScatterChartSales.java.
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
public class ScatterChartExample extends Application {
@Override
public void start(Stage stage) {
//Defining the axes
CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("Items");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("Sales (per month)");
//Creating the Scatter chart
ScatterChart scatterChart =
new ScatterChart(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));
//Setting the data to scatter chart
scatterChart.getData().addAll(series);
//Creating a Group object
Group root = new Group(scatterChart);
//Creating a scene object
Scene scene = new Scene(root, 600, 400);
//Setting title to the Stage
stage.setTitle("Scatter 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);
}
}