How-To's > How do I create charts and graphs?
Use one of the six kinds of charts available in the javafx.scene.chart package to create a chart object:
| AreaChart | ![]() |
BarChart | ![]() |
| BubbleChart | ![]() |
LineChart | ![]() |
| PieChart | ![]() |
ScatterChart | ![]() |
A chart might contain one or several series of data. Use the corresponding classes to specify the data series for the particular type of chart.
The following code fragment constructs a simple pie chart by using the PieChart and PieChart.Data classes.
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.chart.*;
def pieChart = PieChart {
title: "Sample Pie"
data: [
PieChart.Data { label: "Apples" value: 34 }
PieChart.Data { label: "Oranges" value: 27 }
PieChart.Data { label: "Bananas" value: 16 }
PieChart.Data { label: "Grapes" value: 50 }
PieChart.Data { label: "Cherries" value: 6 }
PieChart.Data { label: "Raspberry" value: 7 }
]
}
Stage {
title: "Pie Chart"
scene: Scene{
width: 540
height: 410
content: pieChart
} //Scene
}//Stage
Tips
- The classes that reside in the
javafx.scene.chart.partpackage help you to set up the look and feel of the chart. - Use the
actioninstance variable of theDataclass to assign the specific behavior to the data item when it is clicked on. This variable adds interactivity to charts.
action:function(){ Alert.inform("Clicked on Austria=25601.34") }
Examples
API Documentation
Last Updated: April 2010
[Return to How-To's Home]





