Java中的实时图形 [英] Real-time graphing in Java

查看:155
本文介绍了Java中的实时图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序每秒更新大约5到50次的变量,我正在寻找某种方式来实时绘制这种变化的连续XY图。



尽管不推荐使用JFreeChart来获得如此高的更新率,但许多用户仍然认为它适合他们。我试过使用这个演示,并修改它以显示一个随机变量,但它似乎总是占用100%的CPU使用量。即使我忽略了这一点,我也不希望仅限于JFreeChart的UI类来构造表单(尽管我不确定它的功能究竟是什么)。是否有可能将它与Java的表单和下拉菜单集成? (如在VB中可用)否则,有什么替代方案可以看看?



编辑:我是Swing的新手,所以我已经编写了一段代码来测试JFreeChart的功能(同时避免使用ApplicationFrame类的JFree,因为我不确定这将如何与Swing的组合框和按钮配合使用)。目前,该图表正在立即更新,并且CPU使用率很高。是否有可能用新的毫秒()缓冲该值并更新它可能每秒两次?另外,我可以在不中断JFreeChart的情况下将其他组件添加到JFrame的其余部分吗?我会怎么做? frame.getContentPane()。add(new Button(Click))似乎会覆盖图形。

  package graphtest; 

import java.util.Random;
import javax.swing.JFrame;
导入org.jfree.chart.ChartFactory;
导入org.jfree.chart.ChartPanel;
导入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;
导入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
);
最终的XYPlot图= 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标签=新ChartPanel(图表);
frame.getContentPane()。add(label);
//假设我在后面添加组合框和按钮

frame.pack();
frame.setVisible(true);


static class gen实现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);
尝试{
Thread.sleep(20);
} catch(InterruptedException ex){
System.out.println(ex);
}
}
}
}

}


解决方案

如果您的变量正在更新,那么每次更新图表都没有意义。

你有没有想过缓冲变量的变化,并在不同的线程上刷新图表,比如说每5秒?您应该发现JFreeChart可以很好地处理这种更新率。

由于JFreeChart是一个普通的桌面库,因此您可以非常轻松地将它与标准的Swing应用程序集成。或者,您可以使用它来通过Web应用程序进行图表绘制(通过渲染为JPEG / PNG等。JFreeChart也可以自动生成图像映射,因此您可以使用鼠标悬停等)。

I have an application which updates a variable about between 5 to 50 times a second and I am looking for some way of drawing a continuous XY plot of this change in real-time.

Though JFreeChart is not recommended for such a high update rate, many users still say that it works for them. I've tried using this demo and modified it to display a random variable, but it seems to use up 100% CPU usage all the time. Even if I ignore that, I do not want to be restricted to JFreeChart's ui class for constructing forms (though I'm not sure what its capabilities are exactly). Would it be possible to integrate it with Java's "forms" and drop-down menus? (as are available in VB) Otherwise, are there any alternatives I could look into?

EDIT: I'm new to Swing, so I've put together a code just to test the functionality of JFreeChart with it (while avoiding the use of the ApplicationFrame class of JFree since I'm not sure how that will work with Swing's combo boxes and buttons). Right now, the graph is being updated immediately and CPU usage is high. Would it be possible to buffer the value with new Millisecond() and update it maybe twice a second? Also, can I add other components to the rest of the JFrame without disrupting JFreeChart? How would I do that? frame.getContentPane().add(new Button("Click")) seems to overwrite the graph.

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

}

解决方案

If your variable is updating that fast, there's no point in updating a chart every time.

Have you thought about buffering the variable changes, and refreshing the chart on a different thread, say, every 5s ? You should find that JFreeChart can handle such update rates well.

Since JFreeChart is a normal desktop library, you can integrate it with a standard Swing application very easily. Or, you can use it to chart via a web application (by rendering to a JPEG/PNG etc. JFreeChart can generate image maps automatically as well, so you can use mouseovers etc.)

这篇关于Java中的实时图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆