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.
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.
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).
Finally, Connect the created series with your JavaFX Bar Chart object using getData().addAll() method.
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.
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.
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
provided, pieChart is the Pie Chart object and pieChartData is the ObservableList.
Construction of Line Chart in Java is very much similar to Bar Chart. It takes the same XYChart.Series object.
Finally, associate the data with Line Chart.
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.
Finally, associate the data with both charts.
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