javafx line chart
In JavaFX, the LineChart control is used to create a line chart that displays data as a series of points connected by a line. Each data point is represented by a node, which can be customized to display additional information or interact with the user.
Here's an example of how to create a simple line chart in JavaFX:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.stage.Stage; public class LineChartExample extends Application { @Override public void start(Stage stage) { // Define the x and y axis final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); // Create the line chart final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis); // Define the series data XYChart.Series<Number, Number> series = new XYChart.Series<>(); series.getData().add(new XYChart.Data<>(1, 23)); series.getData().add(new XYChart.Data<>(2, 14)); series.getData().add(new XYChart.Data<>(3, 15)); series.getData().add(new XYChart.Data<>(4, 24)); series.getData().add(new XYChart.Data<>(5, 34)); series.getData().add(new XYChart.Data<>(6, 36)); series.getData().add(new XYChart.Data<>(7, 22)); // Add the series to the line chart lineChart.getData().add(series); // Set the chart title and axes labels lineChart.setTitle("Line Chart Example"); xAxis.setLabel("X-axis"); yAxis.setLabel("Y-axis"); // Create the scene and show the chart Scene scene = new Scene(lineChart, 800, 600); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
In this example, we create a LineChart control by providing it with a NumberAxis for both the x and y axes. We then create a series of XYChart.Data objects and add them to the LineChart using the getData() method. Finally, we set the chart title and axes labels, and display the chart in a Scene. When the application is run, the line chart will be displayed with the data points and line connecting them.
JavaFX LineChart control provides a lot of customization options, such as styling the chart and its components, handling user interactions, and supporting animation. With these features, developers can create advanced and interactive line charts for their JavaFX applications.