tho*_*inc 14 java graph real-time jfreechart
我有一个应用程序,每秒更新一次变量大约5到50次,我正在寻找一种实时绘制这种变化的连续XY图的方法.
虽然不建议将JFreeChart用于如此高的更新率,但许多用户仍然认为它适用于它们.我已经尝试使用这个演示并修改它以显示一个随机变量,但似乎一直使用100%的CPU使用率.即使我忽略了这一点,我也不希望被限制在JFreeChart的ui类来构造表单(尽管我不确定它的功能是什么).是否可以将其与Java的"表单"和下拉菜单集成?(如VB中所示)否则,我有什么其他选择吗?
编辑:我是Swing的新手,所以我把一个代码放在一起只是为了测试JFreeChart的功能(同时避免使用JFree的ApplicationFrame类,因为我不确定它如何适用于Swing的组合盒子和按钮).现在,图表正在立即更新,CPU使用率很高.是否可以使用新的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);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的变量快速更新,那么每次更新图表都没有意义.
您是否考虑过缓冲变量,并在不同的线程上刷新图表,比如每5秒?您应该发现JFreeChart可以很好地处理这样的更新速率.
由于JFreeChart是一个普通的桌面库,因此您可以非常轻松地将其与标准Swing应用程序集成.或者,您可以通过Web应用程序使用它来绘制图表(通过渲染为JPEG/PNG等.JFreeChart也可以自动生成图像映射,因此您可以使用鼠标悬停等)
| 归档时间: |
|
| 查看次数: |
31945 次 |
| 最近记录: |