【发布时间】:2010-11-26 05:36:01
【问题描述】:
我有一个应用程序,它每秒大约更新 5 到 50 次变量,我正在寻找某种方法来实时绘制这种变化的连续 XY 图。
尽管更新率如此之高的 JFreeChart 不被推荐,但许多用户仍然表示它适合他们。我尝试使用this 演示并对其进行修改以显示随机变量,但它似乎一直使用 100% 的 CPU 使用率。即使我忽略了这一点,我也不想局限于 JFreeChart 的 ui 类来构建表单(尽管我不确定它的功能到底是什么)。是否可以将它与 Java 的“表单”和下拉菜单集成? (在 VB 中可用)否则,我可以研究其他替代方案吗?
编辑:我是 Swing 的新手,所以我整理了一个代码来测试 JFreeChart 的功能(同时避免使用 JFree 的 ApplicationFrame 类,因为我'我不确定这将如何与 Swing 的组合框和按钮一起使用)。现在,图表正在立即更新,CPU 使用率很高。是否可以使用 new Millisecond() 缓冲该值并可能每秒更新两次?另外,我可以在不中断 JFreeChart 的情况下将其他组件添加到 JFrame 的其余部分吗?我该怎么做? frame.getContentPane().add(new Button("Click")) 似乎覆盖了图表。
package graphtest;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
public class Main {
static TimeSeries ts = new TimeSeries("data", Millisecond.class);
public static void main(String[] args) throws InterruptedException {
gen myGen = new gen();
new Thread(myGen).start();
TimeSeriesCollection dataset = new TimeSeriesCollection(ts);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"GraphTest",
"Time",
"Value",
dataset,
true,
true,
false
);
final XYPlot plot = chart.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000.0);
JFrame frame = new JFrame("GraphTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChartPanel label = new ChartPanel(chart);
frame.getContentPane().add(label);
//Suppose I add combo boxes and buttons here later
frame.pack();
frame.setVisible(true);
}
static class gen implements Runnable {
private Random randGen = new Random();
public void run() {
while(true) {
int num = randGen.nextInt(1000);
System.out.println(num);
ts.addOrUpdate(new Millisecond(), num);
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}
}
【问题讨论】:
标签: java graph real-time jfreechart