我试图使用名为“jfreechart”的库来绘制我的饼图。这里的问题是我无法整合结果witsh在我的窗口中包含2个JPanel的不同Jframe中。最后“Statistiques”应该包含不在另一个窗口中的饼图。
这是饼图的类
public class PieChart extends JFrame {
private static final long serialVersionUID = 1L;
public PieChart(String applicationTitle, String chartTitle) {
super(applicationTitle);
PieDataset dataset = createDataset();
JFreeChart chart = createChart(dataset, chartTitle);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private PieDataset createDataset() {
DefaultPieDataset result = new DefaultPieDataset();
result.setValue("Tunis", 29);
result.setValue("Ariana", 20);
result.setValue("Sousse", 51);
return result;
}
private JFreeChart createChart(PieDataset dataset, String title) {
JFreeChart chart = ChartFactory.createPieChart3D(title,dataset,true,true,false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart;
}
}
答案 0 :(得分:3)
将JFrame
放在JPanel
中是不可能的,但您可以将饼图放在JPanel
中。
PieDataset dataset = createDataset();
JFreeChart chart = createChart(dataset, chartTitle);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
yourPanel.add(chartPanel); // this line is new