将JFreeChart添加到JPanel中

时间:2011-10-30 14:47:49

标签: java swing user-interface jpanel jfreechart

如果我有我的Jpanel和我的JFreeChart。如何将此图表添加到JPanel中?

XYSeries series = new XYSeries("XYGraph");
   series.add(1, 1);
   series.add(1, 2);
   series.add(2, 1);
   series.add(3, 9);
   series.add(4, 10);

// Add the series to your data set
   XYSeriesCollection dataset = new XYSeriesCollection();
   dataset.addSeries(series);

// Generate the graph
   JFreeChart chart = ChartFactory.createXYLineChart(
   "XY Chart", // Title
   "x-axis", // x-axis Label
   "y-axis", // y-axis Label
   dataset, // Dataset
   PlotOrientation.VERTICAL, // Plot Orientation
   true, // Show Legend
   true, // Use tooltips
   false // Configure chart to generate URLs?
);

现在,如何在我的JPanle中添加图表?

4 个答案:

答案 0 :(得分:21)

来自旧java forums thread

JPanel jPanel1 = new JPanel();
jPanel1.setLayout(new java.awt.BorderLayout());
 ..
ChartPanel CP = new ChartPanel(chart);
.. 
jPanel1.add(CP,BorderLayout.CENTER);
jPanel1.validate();

答案 1 :(得分:4)

这是一个很好的教程,可以引导您:How to embed JFreeChart in JPanel

答案 2 :(得分:1)

JFreeChart chart = new JFreeChart("Cos(x) and Cos^2(x) versus x", parent);
ChartPanel myChart = new ChartPanel(chart);
myChart.setMouseWheelEnabled(true);
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.add(myChart,BorderLayout.CENTER);
jPanel1.validate();

答案 3 :(得分:0)

我遇到了同样的问题,但我找到了解决这个问题的方法。

JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 102, 51));
		panel.setBounds(50, 64, 955, 888);
		frame.getContentPane().add(panel);
		panel.setLayout(null); //Absolute Layout

XYSeries series = new XYSeries("XY Chart");
		XYSeriesCollection dataset = new XYSeriesCollection(series);
		JFreeChart chart = ChartFactory.createTimeSeriesChart("Testing Chart", "Date", "Average Profit", dataset);
		
		ChartPanel chartPanel = new ChartPanel((JFreeChart) null);
		chartPanelH2S.setChart(chart);
		chartPanelH2S.setBounds(39, 193, 419, 309);
		panel.add(chartPanel);
frame.setVisible(true);

希望这有帮助