第6部分的主题

  • 创建一个统计图显示生日的分布。

生日统计

在AddressApp中所有人员都有生日。当我们人员庆祝他们生日的时候,如果有一些生日的统计不是会更好。

我们使用柱状图,包含每个月的一个条形。每个条形显示在指定月份中有多少人需要过生日。

统计FXML视图

  1. ch.makery.address.view包中我们开始创建一个BirthdayStatistics.fxml(*右击包|New|other..|New FXML Document*) 

  2. 在Scene Builder中打开BirthdayStatistics.fxml文件。

  3. 选择根节点AnchorPane。在*Layout*组中设置*Pref Width*为620,*Pref Height*为450。

  4. 添加BarChartAnchorPane中。

  5. 右击BarChart并且选择*Fit to Parent*。

  6. 保存fxml文件,进入到Eclipse中,F5刷新项目。

在我们返回到Scene Builder之前,我们首先创建控制器,并且在我们的MainApp中准备好一切。

统计控制器

在view包 ch.makery.address.view中创建一个Java类,称为BirthdayStatisticsController.java

在开始解释之前,让我们看下整个控制器类。

BirthdayStatisticsController.java

package ch.makery.address.view;

import java.text.DateFormatSymbols;
import java.util.Arrays;
import java.util.List;
import java.util.Locale; import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.XYChart;
import ch.makery.address.model.Person; /**
* The controller for the birthday statistics view.
*
* @author Marco Jakob
*/
public class BirthdayStatisticsController { @FXML
private BarChart<String, Integer> barChart; @FXML
private CategoryAxis xAxis; private ObservableList<String> monthNames = FXCollections.observableArrayList(); /**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
// Get an array with the English month names.
String[] months = DateFormatSymbols.getInstance(Locale.ENGLISH).getMonths();
// Convert it to a list and add it to our ObservableList of months.
monthNames.addAll(Arrays.asList(months)); // Assign the month names as categories for the horizontal axis.
xAxis.setCategories(monthNames);
} /**
* Sets the persons to show the statistics for.
*
* @param persons
*/
public void setPersonData(List<Person> persons) {
// Count the number of people having their birthday in a specific month.
int[] monthCounter = new int[12];
for (Person p : persons) {
int month = p.getBirthday().getMonthValue() - 1;
monthCounter[month]++;
} XYChart.Series<String, Integer> series = new XYChart.Series<>(); // Create a XYChart.Data object for each month. Add it to the series.
for (int i = 0; i < monthCounter.length; i++) {
series.getData().add(new XYChart.Data<>(monthNames.get(i), monthCounter[i]));
} barChart.getData().add(series);
}
}

控制器如何工作

  1. 控制器需要从FXML文件中访问两个元素:

    • barChar:它有StringInteger类型。String用于x轴上的月份,Integer用于指定月份中人员的数量。
    • xAxis:我们使用它添加月字符串
  2. initialize() 方法使用所有月的列表填充x-axis

  3. setPersonData(…)方法将由MainApp访问,设置人员数据。它遍历所有人员,统计出每个月生日的人数。然后它为每个月添加XYChart.Data到数据序列中。每个XYChart.Data对象在图表中表示一个条形。


连接视图和控制器

  1. 在Scene Builder中打开BirthdayStatistics.fxml

  2. Controller组中设置BirthdayStatisticsController为控制器。

  3. 选择BarChart,并且选择barChar作为fx:id属性(在*Code*组中)

  4. 选择CategoryAxis,并且选择xAxis作为fx:id属性。

  5. 你可以添加一个标题给BarChar(在*Properties*组中)进一步修饰。


连接View/Controller和MainApp

我们为*生日统计*使用与*编辑人员对话框*相同的机制,一个简单的弹出对话框。

添加下面的方法到MainApp类中

/**
* Opens a dialog to show birthday statistics.
*/
public void showBirthdayStatistics() {
try {
// Load the fxml file and create a new stage for the popup.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/BirthdayStatistics.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Birthday Statistics");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene); // Set the persons into the controller.
BirthdayStatisticsController controller = loader.getController();
controller.setPersonData(personData); dialogStage.show(); } catch (IOException e) {
e.printStackTrace();
}
}

一切设置完毕,但是我们没有任何东西实际上调用新的showBirthdayStatistics()方法。幸运的是我们已经在RootLayout.fxml中有一个菜单,它可以用于这个目的。

显示生日统计菜单

RootLayoutController中添加下面的方法,它将处理*显示生日统计*菜单项的用户点击。

/**
* Opens the birthday statistics.
*/
@FXML
private void handleShowBirthdayStatistics() {
mainApp.showBirthdayStatistics();
}

现在,使用Scene Builder打开RootLayout.fxml文件。创建Staticstic 菜单,带有一个Show Statistcs MenuItem:

选择Show Statistics MenuItem,并且选择handleShowBirthdayStatistics作为On Action(在*Code*组中)。

进入到Eclipse,刷新项目,测试它


JavaFX Chart的更多信息

更多信息的一个好地方是官方Oracle教程,使用JavaFX Chart.

下一步做什么?

在最后的教程第7部分 中,我们将最后部署我们的应用(例如:打包并且发布应用到我们的用户)

--------------------- 本文来自 jobbible 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/moshenglv/article/details/82877735?utm_source=copy

【JavaFx教程】第六部分:统计图的更多相关文章

  1. Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler

    上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK>我们讲述了如何使用Senparc.Weixin.MP SDK ...

  2. Docker入门教程(六)另外的15个Docker命令

    Docker入门教程(六)另外的15个Docker命令 [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第六篇,继续介绍Docker命令.之前的第二篇文章 ...

  3. 无废话ExtJs 入门教程十六[页面布局:Layout]

    无废话ExtJs 入门教程十六[页面布局:Layout] extjs技术交流,欢迎加群(201926085) 首先解释什么是布局: 来自百度词典的官方解释:◎ 布局 bùjú: [distributi ...

  4. HMM 自学教程(六)维特比算法

    本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在 HMM 学习最佳范例,这是针对 国外网站上一个 HMM 教程 的翻译,作者功底很深,翻译得很精彩 ...

  5. Ocelot简易教程(六)之重写配置文件存储方式并优化响应数据

    本来这篇文章在昨天晚上就能发布的,悲剧的是写了两三千字的文章居然没保存,结果我懵逼了.今天重新来写这篇文章.今天我们就一起来探讨下如何重写Ocelot配置文件的存储方式以及获取方式. 作者:依乐祝 原 ...

  6. [转]PostgreSQL教程(十六):系统视图详解

    这篇文章主要介绍了PostgreSQL教程(十六):系统视图详解,本文讲解了pg_tables.pg_indexes.pg_views.pg_user.pg_roles.pg_rules.pg_set ...

  7. iOS 11开发教程(六)iOS11Main.storyboard文件编辑界面

    iOS 11开发教程(六)iOS11Main.storyboard文件编辑界面 在1.2.2小节中提到过编辑界面(Interface builder),编辑界面是用来设计用户界面的,单击打开Main. ...

  8. Netty4.x中文教程系列(六) 从头开始Bootstrap

    Netty4.x中文教程系列(六) 从头开始Bootstrap 其实自从中文教程系列(五)一直不知道自己到底想些什么.加上忙着工作上出现了一些问题.本来想就这么放弃维护了.没想到有朋友和我说百度搜索推 ...

  9. Photoshop入门教程(六):通道

    学习心得:当大部分人听到通道.心里可能会有一种很害怕的感觉,因为“通道”并不像“图层”这样易于理解,望而生畏.”通道“的本质其实是存储图片的信息,把一张图片比作一个为网站,那么通道就是网站的后台,存储 ...

  10. 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

随机推荐

  1. 【BZOJ1052】 [HAOI2007]覆盖问题

    BZOJ1052 [HAOI2007]覆盖问题 前言 小清新思维题. 最近肯定需要一些思维题挽救我这种碰到题目只会模板的菜鸡. 这题腾空出世? Solution 考虑一下我们二分答案怎么做? 首先转换 ...

  2. Android------TabLayout的使用

    https://www.jianshu.com/p/2b2bb6be83a8 主要放在 -------> Design库中的TabLayout的使用. margin和padding的区别 外边距 ...

  3. Keras学习笔记——Hello Keras

    最近几年,随着AlphaGo的崛起,深度学习开始出现在各个领域,比如无人车.图像识别.物体检测.推荐系统.语音识别.聊天问答等等.因此具备深度学习的知识并能应用实践,已经成为很多开发者包括博主本人的下 ...

  4. Web安全测试学习手册-业务逻辑测试

    i春秋作家:Vulkey_Chen 首先感谢朋友倾璇的邀请 http://payloads.online/archivers/2018-03-21/1 ,参与了<web安全测试学习手册>的 ...

  5. JSX 和 template 随想

    就目前而言,我用到的前端页面开发框架主要有两种:以JSX为主的react和以template为主的vue. 虽然这两种方式各有千秋,但我其实更偏爱template多一些.为什么? 相较于灵活的JSX, ...

  6. flask框架--cookie,session

    今天我又给大家分享一下怎么用flask框架来实现像淘宝购物车一样存储数据,并且把存储的数据删除,这个方法可以用两个方法都可以做成,一个是cookie,另一个是session. session是依赖于c ...

  7. vue项目警告There are multiple modules with names that only differ in casing

    执行npm run dev后出现了警告提示: warning in ./src/components/Public/yearSelectCell.vue There are multiple modu ...

  8. Flask 初印象

    Flask 出生于2010年,集中了其他python web框架的长处,定位于微小项目上. 特点 1 内置开发服务器和调试器 2 于Python单元测试功能无缝衔接 Flask框架提供了一个与Pyth ...

  9. fastjson的JSONArray转化为泛型列表

    背景:一个复杂结构体内部可能有array的数据,例如:{name:"test",cities:[{name:"shanghai",area:1,code:200 ...

  10. Android之混淆心得与亲身体验

    project.properties 中设置 proguard.config=proguard-project.txt proguard-project.txt  中设置 -optimizationp ...