Home > Java > javaTutorial > body text

Use the new JavaFX chart component in Java 13 for data visualization

WBOY
Release: 2023-07-29 17:41:54
Original
1902 people have browsed it

Use the new JavaFX chart component in Java 13 for data visualization

Introduction:
Data visualization is the process of presenting data as charts, graphs, and other visual elements. Through data visualization, we can better understand and interpret data and discover patterns and relationships hidden behind the data. JavaFX is a powerful Java library that contains many components for creating interactive and creative user interfaces. In Java 13, JavaFX introduces some new chart components to make data visualization easier and more flexible. In this article, we'll cover how to use JavaFX's new Chart component for data visualization and provide some sample code.

1. Environment Settings
First, we need to make sure we have installed Java 13 and JavaFX. Then, we need to introduce the JavaFX library into the Java project. The JavaFX library can be downloaded via Maven or manually and added to the classpath in the project.

2. Create a basic JavaFX application
Before using JavaFX’s new chart component, we first create a basic JavaFX application. The following is a simple JavaFX application template:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class DataVisualizationApp extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("数据可视化应用");
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
Copy after login

This is the simplest JavaFX application, which creates an empty Stage (Stage) and sets a background of 800x600 pixels (Scene) . We can add other JavaFX components to this scene to implement our data visualization.

3. Use LineChart to draw a line chart
One of the new chart components of JavaFX is LineChart, which can display continuous lines between a set of data points. Here is sample code for how to use LineChart to draw a line chart:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class DataVisualizationApp extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("数据可视化应用");

        // 创建x轴和y轴
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("X轴");
        yAxis.setLabel("Y轴");

        // 创建LineChart并设置数据
        final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
        lineChart.setTitle("折线图");
        XYChart.Series<Number, Number> series = new XYChart.Series<>();
        series.setName("数据系列");
        series.setData(FXCollections.observableArrayList(
                new XYChart.Data<>(1, 23),
                new XYChart.Data<>(2, 14),
                new XYChart.Data<>(3, 15),
                new XYChart.Data<>(4, 24),
                new XYChart.Data<>(5, 34)
        ));

        lineChart.getData().add(series);

        StackPane root = new StackPane();
        root.getChildren().add(lineChart);

        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
Copy after login

This code example creates a LineChart and adds a data series to the chart. You can specify the x and y coordinates of the data points by adding XYChart.Data to the series. In the example, we create a series of 5 data points and add it to a LineChart for display.

4. Use BarChart to draw histograms
In addition to line charts, we can also use one of JavaFX's new chart components, namely BarChart, to draw histograms. Here is sample code for how to use BarChart to draw a bar chart:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class DataVisualizationApp extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("数据可视化应用");

        // 创建x轴和y轴
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("X轴");
        yAxis.setLabel("Y轴");

        // 创建BarChart并设置数据
        final BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis);
        barChart.setTitle("柱状图");
        XYChart.Series<String, Number> series = new XYChart.Series<>();
        series.setName("数据系列");
        series.setData(FXCollections.observableArrayList(
                new XYChart.Data<>("一月", 23),
                new XYChart.Data<>("二月", 14),
                new XYChart.Data<>("三月", 15),
                new XYChart.Data<>("四月", 24),
                new XYChart.Data<>("五月", 34)
        ));

        barChart.getData().add(series);

        StackPane root = new StackPane();
        root.getChildren().add(barChart);

        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
Copy after login

This code example creates a BarChart and adds a data series to the chart. You can use XYChart.Data to specify the name (x-axis) and height (y-axis) of each bar chart. In the example, we create a data series containing 5 histograms and add them to the BarChart for display.

Conclusion:
With JavaFX’s new chart component, data visualization becomes easier and more flexible. We can use LineChart to draw line charts, BarChart to draw bar charts, and add multiple data series to each chart. These sample codes can serve as the basis for us to start using the JavaFX chart component, and we can further customize and extend it according to actual needs. I hope this article helps you get started with data visualization using JavaFX's new chart component.

The above is the detailed content of Use the new JavaFX chart component in Java 13 for data visualization. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template