Javafx 简明教程

JavaFX - Bubble Chart

泡泡图用于绘制三维数据;第三维将用泡泡的大小(半径)来表示。

A bubble chart is used to plant three-dimensional data; the third dimension will be represented by the size (radius) of the bubble.

以下是描述完成工作的泡泡图。

The following is a Bubble chart depicting the work done.

bubble chart

Bubble Chart in JavaFX

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

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

要生成一个 JavaFX 中的泡泡图,请按照以下步骤操作。

To generate a bubble chart in JavaFX, follow the steps given below.

Step 1: Defining the Axis

定义泡泡图的 X 和 Y 轴,并为其设置标签。在我们的示例中,X 轴表示年龄,Y 轴表示重量。同时,泡泡的半径表示完成的工作。

Define the X and Y axis of the bubble chart and set labels to them. In our example, X axis represents the age, Y axis represents the weight. While, the radius of the bubble represents the work done.

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

      //Defining Y axis
      NumberAxis yAxis = new NumberAxis(20, 100, 10);
      yAxis.setLabel("Weight");
   }
 }

Step 2: Creating the Bubble Chart

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

Create a line chart by instantiating the class named BubbleChart 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 Bubble chart
BubbleChart bubbleChart = new BubbleChart(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.setName("work");

series.getData().add(new XYChart.Data(10,30,4));
series.getData().add(new XYChart.Data(25,40,5));
series.getData().add(new XYChart.Data(40,50,9));
series.getData().add(new XYChart.Data(55,60,7));
series.getData().add(new XYChart.Data(70,70,9));
series.getData().add(new XYChart.Data(85,80,6));

Step 4: Add Data to the Bubble Chart

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

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

//Setting the data to bar chart
bubbleChart.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.

将上一步骤中创建的 BubbleChart(节点)对象作为 Group 类构造函数的参数传递。应该这样做以便将其添加到群组中,如下所示:

Pass the BubbleChart (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(bubbleChart);

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

我们来考虑不同的人及其年龄、体重和工作能力。工作能力可以视为作为图表中以气泡方式绘制的小时数。

Let us consider different persons along with their age, weight and work capacities. The work capacity can be treated as the number of hours that is plotted as bubbles in the chart.

WEIGHT

AGE

30

40

50

60

70

80

10

4

WORK

25

5

40

6

55

8

70

9

85

15

以下是使用 JavaFX 生成泡泡图的 Java 程序,它上面描述的数据进行描绘。

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

将此代码保存在一个文件内,文件名是 BubbleChartExample.java

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

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

public class BubbleChartExample extends Application {
   @Override
   public void start(Stage stage) {
      //Defining the axes
      NumberAxis xAxis = new NumberAxis(0, 100, 10);
      xAxis.setLabel("Age");

      NumberAxis yAxis = new NumberAxis(20, 100, 10);
      yAxis.setLabel("Weight");

      //Creating the Bubble chart
      BubbleChart bubbleChart = new BubbleChart(xAxis, yAxis);

      //Prepare XYChart.Series objects by setting data
      XYChart.Series series = new XYChart.Series();
      series.setName("work");

      series.getData().add(new XYChart.Data(10,30,4));
      series.getData().add(new XYChart.Data(25,40,5));
      series.getData().add(new XYChart.Data(40,50,9));
      series.getData().add(new XYChart.Data(55,60,7));
      series.getData().add(new XYChart.Data(70,70,9));
      series.getData().add(new XYChart.Data(85,80,6));

      //Setting the data to bar chart
      bubbleChart.getData().add(series);

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

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

      //Setting title to the Stage
      stage.setTitle("Bubble 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 BubbleChartExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls BubbleChartExample

在执行之后,上述程序会生成一个 JavaFX 窗口,其中显示了一个泡泡图(如下所示)。

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

bubblechart example

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 bubble chart, depicting the above data using JavaFX.

将此代码保存在一个文件内,文件名是 BubbleChartSchools.java

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

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

public class BubbleChartSchools 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(20, 320, 20);
      yAxis.setLabel("No.of schools");

      //Creating the Bubble chart
      BubbleChart bubblechart = new BubbleChart(xAxis, yAxis);

      //Prepare XYChart.Series objects by setting data
      XYChart.Series series = new XYChart.Series();
      series.setName("No of schools in an year");
      // Add a third coordinate representing the radius of bubble
      series.getData().add(new XYChart.Data(1970, 25, 1));
      series.getData().add(new XYChart.Data(1980, 30, 2));
      series.getData().add(new XYChart.Data(1990, 60, 3));
      series.getData().add(new XYChart.Data(2000, 120, 4));
      series.getData().add(new XYChart.Data(2013, 240, 5));
      series.getData().add(new XYChart.Data(2014, 300, 6));

      //Setting the data to bubble chart
      bubblechart.getData().add(series);

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

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

      //Setting title to the Stage
      stage.setTitle("Bubble 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 BubbleChartSchools.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls BubbleChartSchools

在执行之后,上述程序会生成一个 JavaFX 窗口,其中显示了一个泡泡图(如下所示)。

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

bubblechart schools