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

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

问题描述

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

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.

虽然 JFreeChart 不推荐用于如此高的更新率,但许多用户仍然表示它对他们有用.我尝试使用 this 演示并修改它以显示随机变量,但它似乎一直在使用 100% 的 CPU 使用率.即使我忽略了这一点,我也不希望受限于 JFreeChart 的 ui 类来构建表单(尽管我不确定它的功能究竟是什么).是否可以将它与 Java 的表单"和下拉菜单集成?(在 VB 中可用)否则,有没有我可以研究的替代方案?

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?

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

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.

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

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.

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

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天全站免登陆