- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
JavaFX Charts
In this part of the JavaFX tutorial, we work with charts. In JavaFX, it is possible to build charts by adding just a few lines of code.
In the following examples, we create a line chart, an area chart, a scatter chart, a bar chart, and a pie chart.
LineChart
A line chart is a basic type of chart which displays information as a series of data points connected by straight line segments. A line chart in JavaFX is created with the javafx.scene.chart.LineChart
.
LineChartEx.java
package com.zetcode; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.layout.HBox; import javafx.stage.Stage; /* * ZetCode JavaFX tutorial * * This program creates a line chart. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: August 2016 */ public class LineChartEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { HBox root = new HBox(); Scene scene = new Scene(root, 450, 330); NumberAxis xAxis = new NumberAxis(); xAxis.setLabel("Age"); NumberAxis yAxis = new NumberAxis(); yAxis.setLabel("Salary (€)"); LineChart lineChart = new LineChart(xAxis, yAxis); lineChart.setTitle("Average salary per age"); XYChart.Series data = new XYChart.Series(); data.setName("2016"); data.getData().add(new XYChart.Data(18, 567)); data.getData().add(new XYChart.Data(20, 612)); data.getData().add(new XYChart.Data(25, 800)); data.getData().add(new XYChart.Data(30, 980)); data.getData().add(new XYChart.Data(40, 1410)); data.getData().add(new XYChart.Data(50, 2350)); lineChart.getData().add(data); root.getChildren().add(lineChart); stage.setTitle("LineChart"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
In the example, we have a line chart showing average salary per age.
NumberAxis xAxis = new NumberAxis(); xAxis.setLabel("Age"); NumberAxis yAxis = new NumberAxis(); yAxis.setLabel("Salary (€)");
Two axes are created with the NumberAxis
. The setLabel()
method sets a description for the axis.
LineChart lineChart = new LineChart(xAxis, yAxis); lineChart.setTitle("Average salary per age");
LineChart
creates a line chart. The setTitle()
method sets a title for the chart.
XYChart.Series data = new XYChart.Series(); data.setName("2016");
A XYChart.Series
provides data series for the chart. A data series is a list of data points. Each data point contains an x value and a y value. The setName()
method gives a series a name. (There may be multiple of series in one chart.)
data.getData().add(new XYChart.Data(18, 567)); data.getData().add(new XYChart.Data(20, 612)); data.getData().add(new XYChart.Data(25, 800)); data.getData().add(new XYChart.Data(30, 980)); data.getData().add(new XYChart.Data(40, 1410)); data.getData().add(new XYChart.Data(50, 2350));
We add data to the data series. XYChart.Data
is a single data item with data for 2 axis charts.
lineChart.getData().add(data);
The data is inserted into the chart.

AreaChart
An area chart displays graphically quantitative data that change over time.
AreaChart.java
package com.zetcode; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.AreaChart; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.layout.HBox; import javafx.stage.Stage; /* * ZetCode JavaFX tutorial * * This program creates an area chart. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: August 2016 */ public class AreaChartEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { HBox root = new HBox(); Scene scene = new Scene(root, 490, 350); CategoryAxis xAxis = new CategoryAxis(); xAxis.setLabel("Time"); NumberAxis yAxis = new NumberAxis(); yAxis.setLabel("Thousand bbl/d"); AreaChart areaChart = new AreaChart(xAxis, yAxis); areaChart.setTitle("Oil consumption"); XYChart.Series data = new XYChart.Series(); data.getData().add(new XYChart.Data("2004", 82502)); data.getData().add(new XYChart.Data("2005", 84026)); data.getData().add(new XYChart.Data("2006", 85007)); data.getData().add(new XYChart.Data("2007", 86216)); data.getData().add(new XYChart.Data("2008", 85559)); data.getData().add(new XYChart.Data("2009", 84491)); data.getData().add(new XYChart.Data("2010", 87672)); data.getData().add(new XYChart.Data("2011", 88575)); data.getData().add(new XYChart.Data("2012", 89837)); data.getData().add(new XYChart.Data("2013", 90701)); areaChart.getData().add(data); areaChart.setLegendVisible(false); root.getChildren().add(areaChart); stage.setTitle("AreaChart"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The example shows an area chart showing world crude oil consumption by year.
AreaChart areaChart = new AreaChart(xAxis, yAxis); areaChart.setTitle("Oil consumption");
An area chart is created with the AreaChart
.
CategoryAxis xAxis = new CategoryAxis(); xAxis.setLabel("Time");
CategoryAxis
works on string categories. We display year strings on this axis.

ScatterChart
A scatter chart is a set of points plotted on a horizontal and vertical axes.
ScatterChartEx.java
package com.zetcode; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.ScatterChart; import javafx.scene.chart.XYChart; import javafx.scene.layout.HBox; import javafx.stage.Stage; /* * ZetCode JavaFX tutorial * * This program creates a scatter chart. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: August 2016 */ public class ScatterChartEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { HBox root = new HBox(); CategoryAxis xAxis = new CategoryAxis(); NumberAxis yAxis = new NumberAxis("USD/kg", 30, 50, 2); ScatterChart scatterChart = new ScatterChart(xAxis, yAxis); XYChart.Series data = new XYChart.Series(); data.getData().add(new XYChart.Data("Mar 14", 43)); data.getData().add(new XYChart.Data("Nov 14", 38.5)); data.getData().add(new XYChart.Data("Jan 15", 41.8)); data.getData().add(new XYChart.Data("Mar 15", 37)); data.getData().add(new XYChart.Data("Dec 15", 33.7)); data.getData().add(new XYChart.Data("Feb 16", 39.8)); scatterChart.getData().add(data); scatterChart.setLegendVisible(false); Scene scene = new Scene(root, 450, 330); root.getChildren().add(scatterChart); stage.setTitle("Gold price"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
In the example, we use the ScatterChart
to display gold prices.
CategoryAxis xAxis = new CategoryAxis();
The x axis is a CategoryAxis
used to display dates.
NumberAxis yAxis = new NumberAxis("USD/kg", 30, 50, 2);
The y axis is a NumberAxis
used to display gold prices. The parameters of the constructor are: axis label, lower bound, upper bound, and tick unit.
XYChart.Series data = new XYChart.Series(); data.getData().add(new XYChart.Data("Mar 14", 43)); ...
A series of data is created with XYChart.Series
and its data items with XYChart.Data
.

Bar chart
A bar chart presents grouped data with rectangular bars with lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally.
BarChartEx.java
package com.zetcode; 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.scene.layout.HBox; import javafx.stage.Stage; /* * ZetCode JavaFX tutorial * * This program creates a bar chart. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: August 2016 */ public class BarChartEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { HBox root = new HBox(); Scene scene = new Scene(root, 480, 330); CategoryAxis xAxis = new CategoryAxis(); NumberAxis yAxis = new NumberAxis(); yAxis.setLabel("Gold medals"); BarChart barChart = new BarChart(xAxis, yAxis); barChart.setTitle("Olympic gold medals in London"); XYChart.Series data = new XYChart.Series(); data.getData().add(new XYChart.Data("USA", 46)); data.getData().add(new XYChart.Data("China", 38)); data.getData().add(new XYChart.Data("UK", 29)); data.getData().add(new XYChart.Data("Russia", 22)); data.getData().add(new XYChart.Data("South Korea", 13)); data.getData().add(new XYChart.Data("Germany", 11)); barChart.getData().add(data); barChart.setLegendVisible(false); root.getChildren().add(barChart); stage.setTitle("BarChart"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
In the example, we use a bar chart to show the number of Olympic gold medals per country in London 2012.
BarChart barChart = new BarChart(xAxis, yAxis);
A bar chart is created with BarChart
.

Pie chart
A pie chart is a circular chart which is divided into slices to illustrate numerical proportion.
PieChartEx.java
package com.zetcode; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.chart.PieChart; import javafx.scene.layout.HBox; import javafx.stage.Stage; /* * ZetCode JavaFX tutorial * * This program creates a pie chart. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: August 2016 */ public class PieChartEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { HBox root = new HBox(); Scene scene = new Scene(root, 450, 330); ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( new PieChart.Data("Apache", 52), new PieChart.Data("Nginx", 31), new PieChart.Data("IIS", 12), new PieChart.Data("LiteSpeed", 2), new PieChart.Data("Google server", 1), new PieChart.Data("Others", 2)); PieChart pieChart = new PieChart(pieChartData); pieChart.setTitle("Web servers market share (2016)"); root.getChildren().add(pieChart); stage.setTitle("PieChart"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The example uses a pie chart to show the market share of web servers.
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( new PieChart.Data("Apache", 52), new PieChart.Data("Nginx", 31), new PieChart.Data("IIS", 12), new PieChart.Data("LiteSpeed", 2), new PieChart.Data("Google server", 1), new PieChart.Data("Others", 2));
Pie chart data items are created with the PieChart.Data
.
PieChart pieChart = new PieChart(pieChartData);
A pie chart is created with the PieChart
class.

In this chapter, we have created a LineChart
, an AreaChart
, a ScatterChart
, a BarChart
, and a PieChart
in JavaFX.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论