本文介绍了我如何使用jfreeChart与Javafx2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于JavaFX2应用程序,我必须显示不同类型的图表。我正在使用jfreeChart创建图表和下面给出的代码:

For a JavaFX2 Application I have to show different types of Charts.I am using jfreeChart for Charts creation and code given below:

public static JFreeChart generatePieChart() {
        DefaultPieDataset dataSet = new DefaultPieDataset();
        dataSet.setValue("China", 25);
        dataSet.setValue("India", 25);
        dataSet.setValue("United States", 50);
        JFreeChart chart = ChartFactory.createPieChart(
                "World Population by countries", dataSet, true, true, false);

        return chart;
    }

这会返回一个图表对象。如何将它与我的JavaFx节点如HBox等集成?

This returns me a chart object. How can I integrate this with my JavaFx Node like HBox etc.?

推荐答案

JFreeChart是面向Swing的 - 而JFreeChart类本身它不是Swing组件,通常显示在org.jfree.chart.ChartPanel上,它是javax.swing.JPanel的子类。它使用ava.awt.Graphics2D对象来绘制自己。

JFreeChart is Swing-oriented - while the JFreeChart class itself is not a Swing component, it is typically displayed on a org.jfree.chart.ChartPanel, which is a subclass of javax.swing.JPanel. It uses a ava.awt.Graphics2D object to draw itself.

我想你可以使用JFreeChart的createBufferedImage()方法来获取BufferedImage,然后使用SwingFXUtils将它转换为javafx.scene.image.WritableImage,但是你会失去图表的交互性。

I suppose that you could use the createBufferedImage() method of JFreeChart to get a BufferedImage, and then use SwingFXUtils to convert it to a javafx.scene.image.WritableImage, but then you'd lose the interactive nature of the chart.

不幸的是,目前还没有似乎是在JavaFX中嵌入Swing组件的任何方法

Unfortunately, at the moment, there doesn't seem to be any way to embed a Swing component in JavaFX

这篇关于我如何使用jfreeChart与Javafx2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:26