Search code examples
javafx-2javafxjfreechartjavafx-8

How i can use jfreeChart with Javafx2


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;
    }

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


Solution

  • 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.

    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.

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