将 JFreeChart 添加到 JPanel 中

发布于 2024-12-12 17:28:18 字数 713 浏览 1 评论 0原文

如果我有一个 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 中添加图表?

if i have a my Jpanel and a my JFreeChart. How can I add this Chart in to the 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?
);

and now, how can i add chart in my JPanle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

善良天后 2024-12-19 17:28:18

来自旧的 Java 论坛主题< /a>

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

From an old 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();
内心激荡 2024-12-19 17:28:18

这是一个很好的教程,可以进一步引导您:如何将 JFreeChart 嵌入 JPanel

Here is a good tutorial to lead you further : How to embed JFreeChart in JPanel

£噩梦荏苒 2024-12-19 17:28:18
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();
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();
皇甫轩 2024-12-19 17:28:18

我遇到了同样的问题,但我能够找到解决此问题的方法。

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

希望这有帮助

I was having the same problem but I was able to identify a way to solve this.

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

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文