如何让定时器 class 在 Java 中每 10 秒加载一个 jfreechart?
How to make Timer class to load a jfreechart every 10 seconds in Java?
我有一个使用 jfreechart API 显示网站登录的 JFrame。这是一个条形图,数据集来自 SQL 数据库。
java 项目加载图表,图表显示当前网站登录。由于数据集每秒都在变化,条形图应该 dynamic.I 使用计时器 class 来做到这一点。它 运行 每 10 秒调用一次 loadLogonChart()。到目前为止我没有问题。然而问题在于它确实会在 10 秒内更新图表。然而,20 秒后,当我 运行 形式 first.Then 时,它会加载值,这个过程一遍又一遍地重复。我使用的代码如下。我不知道它有什么问题。
public final class MainForm extends javax.swing.JFrame implements ActionListener{
private static int AUTO_UPDATE_CHARTS = 10000;
public MainForm() throws SQLException {
//Timer for the barchart
Timer timerChart = new Timer(AUTO_UPDATE_CHARTS, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
loadLogonChart();
} catch (SQLException ex) {
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
timerChart.start();
//LOAD THE FORM
loadForm();
}
public void loadForm() throws SQLException{
initComponents();
loadLogonChart();
}
public void loadLogonChart() throws SQLException{
//database class that I need for the barchart datasets
Database db = new Database();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
JFreeChart chart = ChartFactory.createBarChart("Web Site Activity Chart", "Days", "# of visits", dataset, PlotOrientation.VERTICAL, false, true, false);
CategoryPlot catPlot = chart.getCategoryPlot();
try {
dataset.setValue(db.showMonday(), "logon", "Monday");
dataset.setValue(db.showTuesday(), "logon", "Tuesday");
dataset.setValue(db.showWednesday(), "logon", "Wednesday");
dataset.setValue(db.showThursday(), "logon", "Thursday");
dataset.setValue(db.showFriday(), "logon", "Friday");
dataset.setValue(db.showSaturday(), "logon", "Saturday");
dataset.setValue(db.showSunday(), "logon", "Sunday");
chart = ChartFactory.createBarChart("Web Site Activity Chart", "Days", "# of visits", dataset, PlotOrientation.VERTICAL, false, true, false);
ChartPanel myChart = new ChartPanel(chart);
myChart.setMouseWheelEnabled(true);
BarRenderer renderer = (BarRenderer) catPlot.getRenderer();
DecimalFormat decimalFormat = new DecimalFormat("##.##");
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",decimalFormat));
catPlot.setRenderer(renderer);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT));
renderer.setItemLabelsVisible(true);
chart.getCategoryPlot().setRenderer(renderer);
//renderer.setBaseItemLabelGenerator(CategoryItemLabelGenerator generator)
chartPanel.setLayout(new java.awt.BorderLayout());
chartPanel.add(myChart,BorderLayout.CENTER);
chartPanel.revalidate();
chartPanel.validate();
} catch (SQLException ex) {
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
chartPanel.add(myChart,BorderLayout.CENTER);
chartPanel.revalidate();
chartPanel.validate();
在我看来,您似乎一直在尝试向 chartPanel 添加不同的组件。那么添加一个组件不会删除以前的组件。
Swing 绘制最先添加的最后一个组件,因此旧面板将显示在新面板之上。尝试:
chartPanel.removeAll();
chartPanel.add(myChart,BorderLayout.CENTER);
chartPanel.revalidate();
chartPanel.validate(); // not needed since you do revalidate
chartPanel.repaint();
我有一个使用 jfreechart API 显示网站登录的 JFrame。这是一个条形图,数据集来自 SQL 数据库。
java 项目加载图表,图表显示当前网站登录。由于数据集每秒都在变化,条形图应该 dynamic.I 使用计时器 class 来做到这一点。它 运行 每 10 秒调用一次 loadLogonChart()。到目前为止我没有问题。然而问题在于它确实会在 10 秒内更新图表。然而,20 秒后,当我 运行 形式 first.Then 时,它会加载值,这个过程一遍又一遍地重复。我使用的代码如下。我不知道它有什么问题。
public final class MainForm extends javax.swing.JFrame implements ActionListener{
private static int AUTO_UPDATE_CHARTS = 10000;
public MainForm() throws SQLException {
//Timer for the barchart
Timer timerChart = new Timer(AUTO_UPDATE_CHARTS, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
loadLogonChart();
} catch (SQLException ex) {
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
timerChart.start();
//LOAD THE FORM
loadForm();
}
public void loadForm() throws SQLException{
initComponents();
loadLogonChart();
}
public void loadLogonChart() throws SQLException{
//database class that I need for the barchart datasets
Database db = new Database();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
JFreeChart chart = ChartFactory.createBarChart("Web Site Activity Chart", "Days", "# of visits", dataset, PlotOrientation.VERTICAL, false, true, false);
CategoryPlot catPlot = chart.getCategoryPlot();
try {
dataset.setValue(db.showMonday(), "logon", "Monday");
dataset.setValue(db.showTuesday(), "logon", "Tuesday");
dataset.setValue(db.showWednesday(), "logon", "Wednesday");
dataset.setValue(db.showThursday(), "logon", "Thursday");
dataset.setValue(db.showFriday(), "logon", "Friday");
dataset.setValue(db.showSaturday(), "logon", "Saturday");
dataset.setValue(db.showSunday(), "logon", "Sunday");
chart = ChartFactory.createBarChart("Web Site Activity Chart", "Days", "# of visits", dataset, PlotOrientation.VERTICAL, false, true, false);
ChartPanel myChart = new ChartPanel(chart);
myChart.setMouseWheelEnabled(true);
BarRenderer renderer = (BarRenderer) catPlot.getRenderer();
DecimalFormat decimalFormat = new DecimalFormat("##.##");
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",decimalFormat));
catPlot.setRenderer(renderer);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT));
renderer.setItemLabelsVisible(true);
chart.getCategoryPlot().setRenderer(renderer);
//renderer.setBaseItemLabelGenerator(CategoryItemLabelGenerator generator)
chartPanel.setLayout(new java.awt.BorderLayout());
chartPanel.add(myChart,BorderLayout.CENTER);
chartPanel.revalidate();
chartPanel.validate();
} catch (SQLException ex) {
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
chartPanel.add(myChart,BorderLayout.CENTER);
chartPanel.revalidate();
chartPanel.validate();
在我看来,您似乎一直在尝试向 chartPanel 添加不同的组件。那么添加一个组件不会删除以前的组件。
Swing 绘制最先添加的最后一个组件,因此旧面板将显示在新面板之上。尝试:
chartPanel.removeAll();
chartPanel.add(myChart,BorderLayout.CENTER);
chartPanel.revalidate();
chartPanel.validate(); // not needed since you do revalidate
chartPanel.repaint();