Javafx 简明教程
JavaFX - Charts
通常情况下,图表是一种数据的图形化表示形式。有许多种图表可用于表示数据,例如条形图、饼图、折线图、散点图等。
In general, a chart is a graphical representation of data. There are various kinds of charts to represent data such as Bar Chart, Pie Chart, Line Chart, Scatter Chart, etc.
JavaFX 为各种饼图和 XY 图表提供支持。绘制在 XY 平面上的图表包括面积图、条形图、气泡图、折线图、散点图、堆积面积图、堆积条形图等。
JavaFX Provides support for various Pie Charts and XY Charts. The charts that are represented on an XY–plane include AreaChart, BarChart, BubbleChart, LineChart, ScatterChart, StackedAreaChart, StackedBarChart, etc.
每个图表都由一个类表示,所有这些图表都属于 javafx.scene.chart 包。名为 Chart 的类是 JavaFX 中所有图表的基类,而 XYChart 是在 XY 平面上绘制的所有图表的基类。
Each chart is represented by a class and all these charts belongs to the package javafx.scene.chart. The class named Chart is the base class of all the charts in JavaFX and the XYChart is base class of all those charts that are drawn on the XY–plane.
Creating a Chart in JavaFX
要在 JavaFX 中创建图表,你需要执行以下步骤 −
To create a chart in JavaFX, you need to perform the following steps −
-
Define the axis of the chart
-
Instantiate the respective class
-
Prepare and pass data to the chart
Instantiating the Respective Class
要创建一个图表,请实例化其各自的类。例如,如果你要创建一个折线图,你需要如下实例化名为 LineChart 的类:
To create a chart, instantiate its respective class. For example, if you want to create a line chart, you need to instantiate the class named LineChart as follows −
LineChart linechart = new LineChart(xAxis, yAxis);
如上文代码所示,在实例化时,你需要分别传递表示图表的 X 轴和 Y 轴的两个对象。
As observed in the above code, while instantiating, you need to pass two objects representing the X and Y axis of the chart respectively.
Defining the Axis
一般来说,图表轴可以表示为以下内容:
In general, the axis of the charts can be represented by −
-
Numbers such as Population, Age and
-
Categories such as Days in a Week, Countries.
在 JavaFX 中,一个轴是一个抽象类,表示 X 轴或 Y 轴。它有两个子类来定义每种类型的轴,即 CategoryAxis 和 NumberAxis ,如下图所示:
In JavaFX, an axis is an abstract class representing X or Y axis. It has two subclasses to define each type of axis, namely CategoryAxis and NumberAxis as shown in the following diagram −
Category Axis:
Category Axis:
通过实例化此类,你可以定义(创建)一个 X 轴或 Y 轴,每个值都表示一个类别。你可以如下所示实例化此类来定义一个类别轴:
By instantiating this class, you can define (create) an X or Y axis along which each value represents a category. You can define a Category axis by instantiating this class as shown below −
CategoryAxis xAxis = new CategoryAxis();
对此轴,你需要设置类别的列表并将标签设置为如下所示:
To this axis, you need set the list of categories and label to the axis as shown below −
//setting the list of categories.
xAxis.setCategories(FXCollections.<String>observableArrayList
(Arrays.asList("n ame1", "name2"....)));
//Setting label to the axis
xAxis.setLabel("name of the axis ");
NumberAxis:
NumberAxis:
通过实例化此类,你可以定义(创建)一个 X 轴或 Y 轴,其中每个值都表示一个数字。你可以与该轴一同使用任何数字类型,例如 Long、Double、BigDecimal 等。你可以如下实例化此类来定义一个数字轴:
By instantiating this class, you can define (create) an X or Y axis along which each value represents a Numerical value. You can use any Number type with this Axis, Long, Double, BigDecimal, etc. You can define a Number axis by instantiating this class as follows −
//Defining the axis
NumberAxis yAxis = new NumberAxis();
//Setting labelto the axis
yAxis.setLabel("name of the axis");
Passing Data to XY Charts
所有 XY 图表都表示在 XY 平面中。要绘制图表中的一组点,我们需要指定一系列 XY 坐标。
All the XY charts are represented along the XY plane. To plot a set of points in a chart, we need to specify a series of XY coordinates.
javafx.scene.chart 包中的 <X,Y> 类是一个类,使用该类,你可以将数据发送到图表中。此类包含一个命名的序列的可观察列表。你可以按如下所示使用 XYChart.Series 类的 getData() 方法获取此列表:
The <X,Y> class of the javafx.scene.chart package is a class using which, you can send data to a chart. This class holds an observable list of named series. You can get this list using the getData() method of XYChart.Series class as shown below −
ObservableList list = series.getData();
其中,系列是 XYChart.Series 类的对象。你可以使用 add() 方法将数据添加到此列表,如下所示:
Where, series is the object of the XYChart.Series class. You can add data to this list using the add() method as follows −
list.add(new XYChart.Data(x-axis data, y-axis data));
这两行可以一起写入,如下所示:
These two lines can be written together as shown below −
series.getData().add(new XYChart.Data(x-axis data, y-axis data));
Types of Charts in JavaFX
下表对 JavaFX 提供的不同图表(类)进行了说明:
The following table gives a description of various charts (classes) provided by JavaFX −
S.No |
Chart & Description |
1 |
Pie ChartA pie-chart is a representation of values as slices of a circle with different colors. These slices are labeled and the values corresponding to each slice is represented in the chart. In JavaFX, a pie chart is represented by a class named PieChart. This class belongs to the package javafx.scene.chart. |
2 |
Line ChartA line chart or line graph displays information as a series of data points (markers) connected by straight line segments. Line Chart shows how the data changes at equal time frequency. 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. |
3 |
Area ChartArea charts are used to draw area based charts. It plots the area between the given series of points and the axis. In general, this chart is used to compare two quantities. In JavaFX, an Area chart is represented by a class named AreaChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a AreaChart node in JavaFX. |
4 |
Bar ChartA bar chart is used to represent grouped data using rectangular bars. The length of these bars depicts the values. The bars in the bar chart can be plotted vertically or horizontally. In JavaFX, a Bar chart is represented by a class named BarChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a BarChart node in JavaFX. |
5 |
Bubble ChartA bubble chart is used to plat three-dimensional data. The third dimension will be represented by the size (radius) of the bubble. 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 a BubbleChart node in JavaFX. |
6 |
Scatter ChartA 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. 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. |
7 |
Stacked Area ChartIn JavaFX, a Stacked Area chart is represented by a class named StackedAreaChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create an StackedAreaChart node in JavaFX. |
8 |
Stacked Bar ChartIn JavaFX, a Stacked Bar chart is represented by a class named StackedBarChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a StackedBarChart node in JavaFX. |