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

查看:115
本文介绍了在后台渲染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. 将每个离屏图表的快照拍摄到JavaFX图片。

  2. 使用 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天全站免登陆