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.part package help you to set up the look and feel of the chart.
  • Use the action instance variable of the Data class 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: January 2010
[Return to How-To's Home]



Rate This Article
Leave a Comment or Suggest a Topic

Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies—your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.

 

English
日本語
한국어
简体中文
русский
Português do Brasil