Use the new JavaFX chart component in Java 13 for 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(); } }
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(); } }
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(); } }
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!

Cloud computing significantly improves Java's platform independence. 1) Java code is compiled into bytecode and executed by the JVM on different operating systems to ensure cross-platform operation. 2) Use Docker and Kubernetes to deploy Java applications to improve portability and scalability.

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.

JRE is the environment in which Java applications run, and its function is to enable Java programs to run on different operating systems without recompiling. The working principle of JRE includes JVM executing bytecode, class library provides predefined classes and methods, configuration files and resource files to set up the running environment.

JVM ensures efficient Java programs run through automatic memory management and garbage collection. 1) Memory allocation: Allocate memory in the heap for new objects. 2) Reference count: Track object references and detect garbage. 3) Garbage recycling: Use the tag-clear, tag-tidy or copy algorithm to recycle objects that are no longer referenced.

Start Spring using IntelliJIDEAUltimate version...

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...

Java...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.