📜  javafx 图表

📅  最后修改于: 2020-10-14 06:19:06             🧑  作者: Mango

JavaFX图表

通常,购物车可以定义为以符号形式表示数据的图形或图表。

图表主要用于表示大量数据以及数据各部分之间的关系。我们可以创建不同种类的图表来表示不同种类的信息。

在JavaFX中,我们可以使用packagejavafx.scene.chart提供的类来创建图表。

图表类型

图表可以分为以下几种类型。

  • 饼图:在饼图中,圆的扇区用于表示整个信息的不同比例。在JavaFX中,类javafx.scene.chart.PieChart用于处理饼图。我们将在后面详细讨论PieChart。
  • XYChart:在XYChart中,信息绘制在XY(水平和垂直)轴上。 X轴代表一种类型的值,而Y轴代表另一种类型的值。在X和Y图表上绘制的值之间进行映射以显示适当的信息。在JavaFX中,类javafx.scene.chart.XYChart用于处理XYChart。

轴类型

X轴和Y轴可以是以下两种类型之一。

  • 类别轴:类别轴用于表示信息的不同类别。这与值轴不同,因为确切的值未显示在类别轴上。在JavaFX中,类javafx.scene.chart.CategoryAxis表示类别轴。我们只需要实例化此类即可创建类别轴。
  • 数字轴:数字轴用于表示值的确切范围。在JavaFX中,类javafx.scene.chart.NumberAxis表示值轴。我们只需要实例化此类即可创建Number轴。

如何在JavaFX中创建图表?

按照以下说明在JavaFX中创建图表。

配置轴

为了创建Xaxis和Yaxis,我们需要实例化各自的类。但是,此步骤对于饼图不是必需的。使用以下代码创建轴并为其设置属性。

NumberAxis xaxis = new NumberAxis();
CategoryAxis yaxis = new CategoryAxis();
xaxis.setLabel(?X-Axis?);
yaxis.setLabel(?Y-Axis?);  

创建图表

我们只需要实例化相应的类即可创建图表。例如,以下语法将用于创建LineChart。

LineChart linechart = new LineChart(xaxis,yaxis);
linechart.setTitle(?Line Chart Example?); 

将数据传递到图表:

这是该过程中最重要的步骤。为此,请使用以下步骤。

实例化XYChart.Series:

我们需要创建XYChart.Series类的实例,并为该系列设置适当的名称。基本上,系列代表实体类型的信息集。图表中的系列数等于图表中的实体数。实体就是实体,其数据显示在图中。在下面的代码中,我们实例化了XYChart.Series类。

XYChart.Series series = new XYChart.Series();
series.setName(?Value type 1?);   

向系列添加数据:

X轴和Y轴之间需要进行映射,以表示序列的适当信息。通过映射,我们仅意味着将一个轴的值设置为另一轴的值。在代码的以下部分中,我们将添加该系列的数据。

series.getData().add(new XYChart.Data(2010,25)); series.getData().add(new XYChart.Data(2011,15));
series.getData().add(new XYChart.Data(2012,78))
series.getData().add(new XYChart.Data(2013,60));

将序列添加到图表:最后,我们需要将序列添加到图表。在代码的以下部分中,我们已将系列添加到LineChart。

Linechart.getData()。add(series)

配置组和场景

这是所有javafx应用程序中最常见的部分。在这里,我们将创建组并将折线图添加到该组。组对象被传递到场景类构造函数中。场景类对象将传递给setScene方法。为此,请使用以下代码部分。

Group group = new Group();
group.getChildren().add(linechart);
Scene scene = new Scene(group,600,400);
primaryStage.setScene(scene);
primaryStage.showTitle(?Chart Example?);
primaryStage.show();

JavaFX为不同类型的图表提供了不同类型的类。下表描述了包javafx.scene.chart的所有图表类。

SN Chart Name Description
1 Pie Chart In general, Pie chart is a type of the graph or diagram in which the sectors of a circle are used to represent different proportions of the whole information. The angle of the arc of a sector varies according to the percentage of information represented by that sector. In JavaFX, Pie Chart is represented by the class javafx.scene.chart.PieChrt. We need to instantiate this class in order to create pi-chart.
2 Line Chart In general, the Line Chart is defined as the type of graph in which the group of data points called markers are used to display the information. The data points are connected by the straight line segments. In JavaFX, the Line Chart is represented by the class javafx.scene.chart.LineChart
3 Area Chart In general, the area chart is used to display the graphically quantitative data. This basically plots the area for the series of points on a XY Plane. In JavaFX, the class javafx.scene.chart.AreaChart represents the Area Chart
4 Bar Chart In general, A bar chart can be defined as a diagram in which the rectangular bars are used to represent the Numeric data values. The height of the bars varies according to the numeric values. In JavaFX, the classjavafx.scene.chart.BarChart represents the Bar Chart.
5 Bubble Chart A Bubble Chart can be defined as the diagram which is used to display the three dimensional data. Each entity is identified by a bubble which contains three triplets (v1, v2, v3). Two of the triplets are shown by the Bubble’s (X,Y) coordinate while the third one is identified by the radius of the bubble. In JavaFX, the class javafx.scene.chart.BubbleChart represents the Bubble chart. We need to instantiate this class in order to create the Bubble Chart
6 Scatter Chart In Scatter Chart, the data points are scattered along the graph. Each data point displays the mapping between both the axis. It is mainly used to plot the relationship between the two variables of the two axes. In JavaFX, the Scatter Chart is represented by the class javafx.scene.chart.ScatterChart class. We need to instantiate this class in order to create the scatter chart.
7 Stacked Area Chart Stacked area chart is the extension of the basic area charts in which the evolution of the values of several groups on the same graphic is displayed. The area is plotted for all the data points of a certain group. However, the areas of the different groups are shown in the form of a stack but they don’t overlap each other. The stacked area chart is mainly used to trace the comparison between the values of the two groups. In JavaFX, the class javafx.scene.chart.StackedAreaChart is used to represent the stacked area chart. We need to instantiate this class in order to genera a stackedareachart node.
8 Stacked Bar Chart In stacked bar charts, the values of the different groups are displayed on a rectangular bar in the form of a stack. This is mainly used to compare the values of the different groups according to its length plotted on the bar. In JavaFX, the class javafx.scene.chart.StackedBarChart represents the stacked bar chart. We need to instantiate this class to generate a stackedbarchart node.