Category: JavaFX Charts

  • How to add JavaFX Charts / Graphs : Tutorial

    How to add JavaFX Charts / Graphs : Tutorial

    JavaFX provides a powerful set of Charts/Graphs that can be added very easily to your programs. Frankly, It is even easier than adding a Table in JavaFX. Graphical charts are available in the javafx.scene.chart package.

    Bar Chart

    This video describes how to add Bar Chart to your JavaFX program.

    Bar Chart data can be represented using an XYChart.Series object. All that you have to do is to make a new object and add data to it.

    XYChart.Series set1 = new XYChart.Series<>();

    Data can be added to this set using the code. Here XYChart.Data() takes two parameters. First one is for the X-Axis(Horizontal) and the second one is for Y-Axis(Vertical).

    set1.getData().add(new XYChart.Data("James", 5000));
    set1.getData().add(new XYChart.Data("Alice", 10000));
    set1.getData().add(new XYChart.Data("Alex", 2000));

    Finally, Connect the created series with your JavaFX Bar Chart object using getData().addAll() method.  

    SalaryChart.getData().addAll(set1);

    AddAll() method allows to add more than one series of data to your chart. For example if i have set1,set2 and set3 then i can add all of them by using a comma seperated list.

    SalaryChart.getData().addAll(set1,set2,set3);

    Pie Chart

    JavaFX Pie Chart uses an ObservableList which is very similar to XYSeries.Series we used for Bar Chart. This video explains how to use pie chart.

    ObservableList<PieChart.Data> pieChartData
    = FXCollections.observableArrayList(
    new PieChart.Data("Cars", 13),
    new PieChart.Data("Bikes", 25),
    new PieChart.Data("Buses", 10),
    new PieChart.Data("Cycles", 22));
    pieChart.setData(pieChartData);

    Here i have created an ObservableList and added 4 values. The pie chart takes all of these values and allocate a percentage for each one.

    For eg, Percentage of cars = 13/(13+25+10+22) = 18.5%

    The data then can be associated with the chart using the same code used for Bar chart or using a simple alternative

    pieChart.setData(pieChartData);

    provided, pieChart is the Pie Chart object and pieChartData is the ObservableList.

    Line Chart

    Construction of Line Chart in Java is very much similar to Bar Chart. It takes the same XYChart.Series object.

    XYChart.Series series = new XYChart.Series(); //Make a new XYChart object
    //Add Data
    series.getData().add(new XYChart.Data(“1”, 23));
    series.getData().add(new XYChart.Data(“2”, 14));
    series.getData().add(new XYChart.Data(“3”, 15));

    Finally, associate the data with Line Chart.

    LineChart.getData().addAll(series);

    Area Chart and Scatter Chart

    These two are explained together because, both of these Charts takes same type of data. Yes, the XYChart.Series object. We can use the same example used above.

    XYChart.Series series = new XYChart.Series(); //Make a new XYChart object
    //Add Data
    series.getData().add(new XYChart.Data(“1”, 23));
    series.getData().add(new XYChart.Data(“2”, 14));
    series.getData().add(new XYChart.Data(“3”, 15));

    Finally, associate the data with both charts.

    AreaChart.getData().addAll(series);
    ScatterChart.getData().addAll(series);

    So that’s how we add a chart /Graph to our JavaFX program and i hope you understood these things well enough.

    ———————————————————————————————–
    Thanks to our sponsor
    https://ksaexpats.com/

  • Temperature Monitor Prorgam using JavaFX

    Temperature Monitor Prorgam using JavaFX

    Today i am going to show you how to create a system temperature monitor program in JavaFX. This program use Line Chart from JavaFX library. In order to get the temperature readings, i have given a connection to the system terminal so that i can execute terminal commands within the Java program itself.
    Process proc = Runtime.getRuntime().exec("sensors");
    BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    
    This code will create a new process and will attach to the ‘sensors’ command. This program will work only on linux and the number of temperature readings will change from system to system. Using the process’s input stream we can read the output of the command passed through a Buffered Reader.
    After the establishment of connection with sensors, I have added an infinite loop with 1000ms sleep so that the temperature information will be updated each second. The returned data from process is taken and matched with a pattern [+]…. which will help to select the required temperature information alone. For this purpose, i have made use of Matcher and Pattern class in standard Java Library.
    If you are not familiar with JavaFX line graphs, i recommend reading this article How to Add JavaFX Charts / Graphs Tutorial.
    The processed data is then associated with 4 streams of XYData.Series() data structure to represent 4 different lines on the graph. Extensive CSS styling is used in this program to illustrate the capability of CSS to improve the look and feel of program (if it did :-)). The animations that can be seen on the video is the primitive animation associated with JavaFX LineChart.

     

    Watch the development in Action. Subscribe for more programs and tutorials