javafx bar chart

www.igift‮moc.aedi‬

In JavaFX, the BarChart control is used to create a bar chart that displays data as a series of horizontal or vertical bars. Each bar represents a data point, with the height or width of the bar representing the value of the data point.

Here's an example of how to create a simple bar chart in JavaFX:

import javafx.application.Application;
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.stage.Stage;

public class BarChartExample extends Application {
    @Override
    public void start(Stage stage) {
        // Define the x and y axis
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();

        // Create the bar chart
        final BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis);

        // Define the series data
        XYChart.Series<String, Number> series = new XYChart.Series<>();
        series.getData().add(new XYChart.Data<>("Jan", 23));
        series.getData().add(new XYChart.Data<>("Feb", 14));
        series.getData().add(new XYChart.Data<>("Mar", 15));
        series.getData().add(new XYChart.Data<>("Apr", 24));
        series.getData().add(new XYChart.Data<>("May", 34));
        series.getData().add(new XYChart.Data<>("Jun", 36));
        series.getData().add(new XYChart.Data<>("Jul", 22));

        // Add the series to the bar chart
        barChart.getData().add(series);

        // Set the chart title and axes labels
        barChart.setTitle("Bar Chart Example");
        xAxis.setLabel("Month");
        yAxis.setLabel("Sales");

        // Create the scene and show the chart
        Scene scene = new Scene(barChart, 800, 600);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

In this example, we create a BarChart control by providing it with a CategoryAxis for the x axis and a NumberAxis for the y axis. We then create a series of XYChart.Data objects and add them to the BarChart 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 bar chart will be displayed with the bars representing the data points.

JavaFX BarChart 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 bar charts for their JavaFX applications.