如何在 JavaFX 选项卡中显示 JFreeChart?

How to display a JFreeChart in a JavaFX tab?

我正在尝试在我的 JavaFX 应用程序中显示 JFreeChart PieChart。如果有帮助,我正在使用 SceneBuilder。下面是我创建要添加到初始化方法的图表的代码:

private static PieDataset createDataset() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    AnnonceService sa = AnnonceService.getInstance();
    Annonce a = sa.last();
    dataset.setValue("Les offres",a.getNbr_o());
    dataset.setValue("Les demandes", a.getNbr_d());        
    return dataset;
}

private static JFreeChart createChart(PieDataset dataset) {
    JFreeChart chart = ChartFactory.createPieChart(
        "Les offres par rapport aux demandes", dataset);
    return chart;
}

从这个完整的 example, I constructed a TabPane 开始,包含三个新标签。每个 Tab 包含一个新的 ChartViewer,每个 ChartViewer 包含一个不同名称的饼图。最后,我通过使用 TabPane 作为其根节点创建 Scene 来设置 Stage

TabPane tabPane = new TabPane(
    new Tab("Tab 1", new ChartViewer(createChart("One"))),
    new Tab("Tab 2", new ChartViewer(createChart("Two"))),
    new Tab("Tab 3", new ChartViewer(createChart("Three")))
);
stage.setScene(new Scene(tabPane));

经测试:

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.fx.ChartViewer;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 * @see 
 * @see 
 */
public class PieChartFX extends Application {

    private static PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("A", 0.8);
        dataset.setValue("B", 9.4);
        dataset.setValue("C", 0.1);
        dataset.setValue("D", 89.5);
        dataset.setValue("E", 0.2);
        dataset.setValue("F", 0.0);
        return dataset;
    }

    private static JFreeChart createChart(String name) {
        PieDataset dataset = createDataset();
        JFreeChart chart = ChartFactory.createPieChart(
            name, dataset, false, true, false);
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setOutlineVisible(false);
        plot.setSectionPaint("A", Color.RED);
        plot.setSectionPaint("B", Color.BLUE);
        plot.setSectionPaint("C", Color.GREEN);
        plot.setSectionPaint("D", Color.YELLOW);
        plot.setSectionPaint("E", Color.CYAN);
        plot.setLabelFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
        // Custom labels 
        PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
            "{0}: {2}", new DecimalFormat("0"), new DecimalFormat("0.0%"));
        plot.setLabelGenerator(gen);
        return chart;
    }

    @Override
    public void start(Stage stage) {
        TabPane tabPane = new TabPane(
            new Tab("Tab 1", new ChartViewer(createChart("One"))),
            new Tab("Tab 2", new ChartViewer(createChart("Two"))),
            new Tab("Tab 3", new ChartViewer(createChart("Three")))
        );
        stage.setScene(new Scene(tabPane));
        stage.setTitle("JFreeChart: PieChartFX");
        stage.setWidth(600);
        stage.setHeight(400);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}