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

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

Jul 29, 2023 pm 05:41 PM
javafx chart component data visualization

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles