gpt4 book ai didi

java - 更新 JFreeChart 上的数据

转载 作者:行者123 更新时间:2023-11-30 05:37:55 26 4
gpt4 key购买 nike

JFreeChart 没有像我预期的那样更新。

我有下面的代码。有 2 个面板,一个带有增量按钮,一个带有显示数据的图表。

每次按下+1按钮时,都会更新数据;但图表未显示新数据。

提前非常感谢您的帮助。

public class GUItest3 extends JFrame  {
private JPanel evoTabbedPanel, outputPanel;
float tempCtr;

public GUItest3(){
tempCtr = 0;
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
JButton incrementorButton = new JButton ("+1");
incrementorButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
tempCtr++;}
});
toolBar.add(incrementorButton);
evoTabbedPanel = new JPanel();
evoTabbedPanel.add(toolBar);
MyGraphPanel myGraphPanel = new MyGraphPanel();
outputPanel = new JPanel();
outputPanel.setLayout(new GridBagLayout());
outputPanel.add(myGraphPanel);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Generations", evoTabbedPanel);
tabbedPane.addTab("Output", outputPanel);
getContentPane().add(tabbedPane);
pack();
setVisible(true);
}

class MyGraphPanel extends JPanel {
JFreeChart myChart;
ChartPanel chartPanel;
XYSeriesCollection myDataset;

private MyGraphPanel(){
XYSeries myData = new XYSeries( "My data" );
myData.add(1.0, 1.0);
myData.add(2.0, tempCtr);

myDataset = new XYSeriesCollection();
myDataset.addSeries(myData);
myChart = ChartFactory.createXYLineChart("My chart", "X axis", "Y axis",
myDataset, PlotOrientation.VERTICAL, true, true, false);

chartPanel = new ChartPanel(myChart);
this.add(chartPanel);
}
}
}

最佳答案

您的 ActionListener 更新 tempCtr,但您需要更新图表的数据集才能更新图表:

@Override
public void actionPerformed(ActionEvent e) {
XYSeries mySeries = myGraphPanel.myDataset.getSeries(0);
mySeries.addOrUpdate(tempCtr, Math.pow(2, tempCtr));
tempCtr++;
}

下面的完整示例说明了单击按钮两次后的结果;它重点关注基本问题并说明了一些附加要点:

  • event dispatch thread 上构造和操作 Swing GUI 对象.

  • 覆盖封闭JPanel上的getPreferredSize(),如图here ,以确定封闭面板的初始尺寸。

  • 使用带有默认约束的 GridLayoutGridBagLayout,以允许图表在调整封闭容器大小时进行调整。

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class GUItest3 {

float tempCtr;

private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyGraphPanel myGraphPanel = new MyGraphPanel();
f.add(myGraphPanel, BorderLayout.CENTER);
f.add(new JButton(new AbstractAction("+1") {
@Override
public void actionPerformed(ActionEvent e) {
XYSeries mySeries = myGraphPanel.myDataset.getSeries(0);
mySeries.addOrUpdate(tempCtr, Math.pow(2, tempCtr));
tempCtr++;
}
}), BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

class MyGraphPanel extends JPanel {

JFreeChart myChart;
ChartPanel chartPanel;
XYSeriesCollection myDataset;

private MyGraphPanel() {
setLayout(new GridLayout());
XYSeries myData = new XYSeries("My data");
myData.add(tempCtr, tempCtr++);
myData.add(tempCtr, tempCtr++);
myDataset = new XYSeriesCollection();
myDataset.addSeries(myData);
myChart = ChartFactory.createXYLineChart("My chart", "X axis", "Y axis",
myDataset, PlotOrientation.VERTICAL, true, true, false);
chartPanel = new ChartPanel(myChart);
this.add(chartPanel);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(512, 256);
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new GUItest3()::display);
}
}

关于java - 更新 JFreeChart 上的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56245969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com