使用JFreeChart< java>将动态折线图添加到现有JFrame中的JPanel中. [英] Adding dynamic line chart to JPanel in existing JFrame using JFreeChart <java>

查看:54
本文介绍了使用JFreeChart< java>将动态折线图添加到现有JFrame中的JPanel中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个应用程序,当我在JFrame应用程序中单击开始"时,将显示最大",最小"和平均ping".我制作了一个白框,以便可以容纳一个动态变化的折线图,该折线图将与我的统计信息位于同一窗口中,并且将以与我的统计信息相同的速率(1秒)进行更新.

I made an application that would show me Max, Minimum, and Average ping when I click "start" in my JFrame application. I made a white box so I could fit a dynamically changing line graph that will be in the same window as my stats, and will update at the same rate as my stats (1 second).

无论我用Google多少钱,每个人似乎都了解JFreeChart示例代码.我根本不了解如何实现这一点.其他教程仅在独立的窗口中将图形显示为独立图形.

No matter how much I google, everybody seems to understand the JFreeChart sample code. I do not understand how to implement that at all. Other tutorials show just the graph as a standalone in its own window.

我希望有人能帮助我.这是我到目前为止的JFrame代码:

I hope somebody can help me. Here is my code for the JFrame as of now:

 private void startButtonMouseClicked(java.awt.event.MouseEvent evt) {                                         
   String host = hostName.getText();


    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    exec.scheduleAtFixedRate(new Runnable(){
        @Override
        public void run(){

            try { 
                NetworkAnalyzer.pingCheck(host);

            } catch (IOException ex) {
                Logger.getLogger(NetworkAnalyzerwindow.class.getName()).log(Level.SEVERE, null, ex);

            }
        }
    }, 0, 1, TimeUnit.SECONDS);

这是我的pingCheck:

And here is my pingCheck:

public class NetworkAnalyzer {
static int count=0;
static long max_time = 0;
static long min_time = 0;
static long avg_time = 0;

public static void pingCheck(String host) throws IOException{
String time = "";
//command to execute
 String pingCmd = "ping " + host + " -t " + "-n 1"; 

 //gets runtime to execute command
 Runtime runtime = Runtime.getRuntime(); 
 try {
     Process process = runtime.exec(pingCmd);

     //gets inputstream to read the output of the cocmmand
     BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

     //read outputs
     String inputLine = in.readLine();
     while((inputLine != null)) {
         if (inputLine.length() > 0 && inputLine.contains("time")){
             time = inputLine.substring(inputLine.indexOf("time"));
             break;
         }
         inputLine = in.readLine();
     } 
    /* if(inputLine.contains("timed")){
         System.out.print("Request timed out. Try another domain:");

         pingCheck(time);
     }*/ 

         time = time.replaceAll("[^0-9]+", " ");
         time = time.substring(0, Math.min(time.length(), 3));
         time = time.replaceAll(" ", "");
         System.out.println("ping:" + time);
         //ping calcs


         count++;
         Long ping;
         ping = Long.parseLong(time);
         //for avg                  
         //max ping


         if( count == 1 || ping >= max_time)
             max_time = ping;

         //min ping
         if(count == 1 || ping <= min_time)
             min_time = ping;
         //avg ping


         if (count > 0)
             avg_time = (avg_time*(count-1)+ping)/count;


         NetworkAnalyzerwindow.maxPing.setText("Max: " + max_time + "ms");
         NetworkAnalyzerwindow.minPing.setText("Min: " + min_time + "ms");
         NetworkAnalyzerwindow.avgPing.setText("Avg: " + avg_time + "ms"); 

 } catch(IOException | NumberFormatException ex){
     JOptionPane.showMessageDialog(null, ex);
 }


}

我只想添加一个动态变化的图形,该图形将使用ping值并对它们进行图形化.有人真的可以帮助我,却不能将我链接到仅显示如何自己制作图形的教程之一.

I just want to add a dynamically changing graph that would take the ping values and graph them. Can somebody actually help me and not link me to one of the tutorials that only shows how to make a graph by itself.

这是应用程序在运行时的外观(我希望在白框中显示图形.我可以将框变大):

Here is what the app looks like when running(I would like the graph in the white box. I could make the box bigger):

http://imgur.com/kgYCoW2

推荐答案

使用ProcessBuilder(在在此处显示)执行 SwingWorkerdoInBackground()实现中的ping命令.解析输出,然后结果publish().更新您的process()实现中的数据集,图表将相应地进行更新. 此处.

Using ProcessBuilder, shown here, execute the ping command in your doInBackground() implementation of a SwingWorker. Parse the output, and publish() the results. Update the dataset in your process() implementation, and the chart will update itself in response. An example using JFreeChart is shown here.

您能再解释一下吗?

Can you explain a bit more?

从第一个示例开始,替换

ProcessBuilder pb = new ProcessBuilder("ls", "-lR", "/");

使用

ProcessBuilder pb = new ProcessBuilder("ping", "-c", "3", "example.com");

可以方便地显示ping的标准输出.在概述中,第二个示例中的process()可能看起来像这样:

to get a convenient display of the standard output of ping. In outline, your process() in the second example might look like this:

@Override
protected void process(java.util.List<String> messages) {
    for (String message : messages) {
        textArea.append(message + "\n");
        // parse x, y from message 
        series.add(x, y);
    }
}

这篇关于使用JFreeChart&lt; java&gt;将动态折线图添加到现有JFrame中的JPanel中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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