JavaFX示例创建具有多个系列的面积图
面积图接受一系列数据点(x,y)作为输入值,使用一条线连接它们,并映射所获得的线和轴之间的区域。在JavaFX中,您可以通过实例化javafx.scene.chart.AreaChart类来创建面积图。
在实例化此类时,必须传递Axis类的两个对象,它们代表x轴和y轴(作为构造函数的参数)。由于Axis类是抽象的,因此您需要传递其具体子类的对象NumberNumber(用于数字值)或CategoryAxis(字符串值)。
多个系列的面积图
该XYChart.Data 类表示图表中的数据点,你可以通过实例这个类来创建一个数据点。
XYChart.Data dataPoint1 = new XYChart.Data(x-value, y-value) XYChart.Data dataPoint2 = new XYChart.Data(x-value, y-value) XYChart.Data dataPoint3 = new XYChart.Data(x-value, y-value)
一旦创建了所有必需的数据点,就可以创建实例化XYChart.Series类并将其添加数据所需的序列。
XYChart.Series series = XYChart.Series series.getData().add(dataPoint1); series.getData().add(dataPoint2); series.getData().add(dataPoint3);
您可以根据需要创建任意数量的此类序列。
示例
以下JavaFX示例演示了多个面积图的创建。
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.chart.AreaChart; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.layout.StackPane; public class AreaCharts_Multiple extends Application { public void start(Stage stage) { //定义x和y轴 CategoryAxis xAxis = new CategoryAxis(); NumberAxis yAxis = new NumberAxis(); //设置轴的标签 xAxis.setLabel("Month"); yAxis.setLabel("Temperature(°C)"); //创建面积图 AreaChart<String, Number> areaChart = new AreaChart<String, Number>(xAxis, yAxis); //准备系列的数据点1- XYChart.Series series1 = new XYChart.Series(); series1.getData().add(new XYChart.Data("Jan", 7.0)); series1.getData().add(new XYChart.Data("Feb", 6.9)); series1.getData().add(new XYChart.Data("March", 9.5)); series1.getData().add(new XYChart.Data("April", 14.5)); series1.getData().add(new XYChart.Data("May", 18.2)); series1.getData().add(new XYChart.Data("June", 21.5)); series1.getData().add(new XYChart.Data("July", 25.2)); series1.getData().add(new XYChart.Data("August", 26.5)); series1.getData().add(new XYChart.Data("Sep", 23.3)); series1.getData().add(new XYChart.Data("Oct", 18.3)); series1.getData().add(new XYChart.Data("Nov", 13.9)); series1.getData().add(new XYChart.Data("Dec", 9.6)); //准备系列2的数据点 XYChart.Series series2 = new XYChart.Series(); series2.getData().add(new XYChart.Data("Jan", -0.2)); series2.getData().add(new XYChart.Data("Feb", 0.8)); series2.getData().add(new XYChart.Data("March", 5.7)); series2.getData().add(new XYChart.Data("April", 11.3)); series2.getData().add(new XYChart.Data("May", 17.0)); series2.getData().add(new XYChart.Data("June", 22.0)); series2.getData().add(new XYChart.Data("July", 24.8)); series2.getData().add(new XYChart.Data("August", 24.1)); series2.getData().add(new XYChart.Data("Sep", 20.1)); series2.getData().add(new XYChart.Data("Oct", 14.1)); series2.getData().add(new XYChart.Data("Nov", 8.6)); series2.getData().add(new XYChart.Data("Dec", 2.5)); //准备系列的数据点3- XYChart.Series series3 = new XYChart.Series(); series3.getData().add(new XYChart.Data("Jan", 3.9)); series3.getData().add(new XYChart.Data("Feb", 4.2)); series3.getData().add(new XYChart.Data("March", 5.7)); series3.getData().add(new XYChart.Data("April", 8.5)); series3.getData().add(new XYChart.Data("May", 11.9)); series3.getData().add(new XYChart.Data("June", 15.2)); series3.getData().add(new XYChart.Data("July", 17.0)); series3.getData().add(new XYChart.Data("August", 16.6)); series3.getData().add(new XYChart.Data("Sep", 14.2)); series3.getData().add(new XYChart.Data("Oct", 10.3)); series3.getData().add(new XYChart.Data("Nov", 6.6)); series3.getData().add(new XYChart.Data("Dec", 4.8)); //设置所有系列的名称 series1.setName("Tokyo"); series2.setName("New York"); series3.setName("London"); //将数据设置为面积图 areaChart.getData().addAll(series1, series2, series3); //创建一个堆栈窗格来保存图表 StackPane pane = new StackPane(areaChart); pane.setPadding(new Insets(15, 15, 15, 15)); pane.setStyle("-fx-background-color: BEIGE"); //设置场景 Scene scene = new Scene(pane, 595, 350); stage.setTitle("Area Chart"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果