在后台渲染 JavaFX 2 图表 [英] Rendering JavaFX 2 charts in background

查看:12
本文介绍了在后台渲染 JavaFX 2 图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于数据分析的应用程序.该应用程序的很大一部分是能够根据收集的数据显示图表,并且能够在一个批处理操作中导出大量任务.到目前为止,我一直使用 JFreeChart,但我想使用本机 JavaFX 图表,以便导出的图表在应用程序的屏幕上看起来仍然相同.

I have an application that is used for data analysis. A big part of the application is the ability to be able to show charts based on the data assembled, and to be able to export a large number of tasks in one batch-operation. Up until now I have used JFreeChart, but I would like to use the native JavaFX Charts so that the exported charts will still look the same as they do on-screen in the application.

我使用的是 JavaFX 2.2.1 (jdk 7u6).

I am on JavaFX 2.2.1 (jdk 7u6).

我可以批量生成图表,但这意味着我必须冻结用户界面 (UI),因为这些图表必须在 JavaFX 应用程序线程上呈现.我使用 Platform.runLater(new Runnable() { ... });这个命令,包含在生成图表的代码中.

I am able to generate the charts in one batch, but that means that I have to freeze the User Interface (UI), as these charts have to be rendered on the JavaFX Application Thread. I use the Platform.runLater(new Runnable() { ... }); command for this, wrapped around the code that generates the charts.

如果我将每个单独的图表生成包装到 Platform.runLater(new Runnable() { ... });GUI 不会像以前一样冻结,但我也不会得到任何反馈,因为我无法检测每个单独的图表何时生成(它们在稍后阶段运行,我无法控制何时可能发生,据我所知也没有可用的回调).

If I instead wrap each individual chart generation into a Platform.runLater(new Runnable() { ... }); The GUI doesn't freeze up as before, but I will also get no feedback because I am unable to detect when each of the individual charts are generated (they are ran at a later stage, and I have no control over when that might happen, and there is no callback available as far as I know either).

对于这个特定事件,我想向用户显示一个进度条,并且我希望这个进度条随着实际图表的生成而更新.

For this particular event I would like to show a progress bar to the user, and I want this progress bar to be updated along with the actual chart generation.

有关如何实现这一目标的任何建议或提示?

Any suggestions or hints as to how this can be achieved ?

推荐答案

此问题已在 Oracle JavaFX 论坛帖子中解决:在后台渲染图表.

This question was solved in the Oracle JavaFX forum thread: Render charts in background.

解决方案是使用包含三个子任务的 SaveChartsTask:

The solution was to have a SaveChartsTask with three subtasks:

  1. 在离屏场景图中创建图表.
  2. 为 JavaFX 图像拍摄每个屏幕外图表的快照.
  3. 使用 imageio 例程将 JavaFX 图像导出到文件.
  1. Create charts in an off screen scene graph.
  2. Take snapshots of each off screen chart to an JavaFX image.
  3. Use imageio routines to export the JavaFX images to files.

任务的 workDone 属性允许监控操作的过程.通过将全部工作拆分为子任务,尽可能将工作从 JavaFX 应用程序线程中分离出来,并在必要时使用 CountDownLatches 和 BlockingQueues 来同步操作,UI 能够保持响应.

The workDone properties on tasks allowed the process of the operations to be monitored. By splitting the total work up into subtasks, keeping work off of the JavaFX application thread when possible and making use of CountDownLatches and BlockingQueues as necessary to synchronize operations, the UI was able to remain responsive.

这篇关于在后台渲染 JavaFX 2 图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆