如何每5秒将数据添加到JFree XY图表中? [英] How to add data into a JFree XY chart every 5 seconds?

查看:50
本文介绍了如何每5秒将数据添加到JFree XY图表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以访问一个数据库,该数据库每5秒返回一个位置的温度和该位置的时间.
我有一个在x轴上绘制时间的想法.
也许通过使用java swing定时器,我将能够每5秒将数据添加到图形中.
但是,我不知道该怎么实现,因为我考虑过在createDataset()中添加一个计时器,但是由于它返回了一个数据集,所以我将无法实现它.
知道如何每5秒将数据添加到图形中吗?
这是我的代码:

I have access to a database that returns the temperature of a location and time of that location every 5 seconds.
I have an idea of plotting the time on the x-axis.
And probably by using the java swing timer I would be able to add data into the graph every 5 seconds.
However, I do not know how to implement that because I thought of adding a timer in createDataset( ) but since it returns a dataset, I won't be able to achieve it.
Any idea how I would be able to add data into the graph every 5 seconds?
Here is my code:

import java.awt.Color; 
import java.awt.BasicStroke; 

import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.data.xy.XYSeriesCollection; 
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

public class XYLineChart_AWT extends ApplicationFrame {

public XYLineChart_AWT( String applicationTitle, String chartTitle ) {
  super(applicationTitle);
  JFreeChart xylineChart = ChartFactory.createXYLineChart(
     chartTitle ,
     "Time" ,
     "Temperature" ,
     createDataset() ,
     PlotOrientation.VERTICAL ,
     true , true , false);

  ChartPanel chartPanel = new ChartPanel( xylineChart );
  chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
  final XYPlot plot = xylineChart.getXYPlot( );

  XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
  renderer.setSeriesPaint( 0 , Color.RED );
  renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
  plot.setRenderer( renderer ); 
  setContentPane( chartPanel ); 
}

private XYDataset createDataset( ) {
  final XYSeries temp = new XYSeries( "Temperature" );  
  //time = getTime(); //returns a float time in seconds.milliseconds
  //temperature = getTemp(); //returns a number temperature 
  //I want to add data into temp every 5 seconds but i don't know how to do it        
  temp.add( 1.0 , 1.0 );          
  temp.add( 2.0 , 4.0 );          
  temp.add( 3.0 , 3.0 );                  

  final XYSeriesCollection dataset = new XYSeriesCollection( );          
  dataset.addSeries( temp );
  return dataset;
}

public static void main( String[ ] args ) {
  XYLineChart_AWT chart = new XYLineChart_AWT("Temp",
     "Temperature of some location");
  chart.pack( );          
  RefineryUtilities.centerFrameOnScreen( chart );          
  chart.setVisible( true ); 
}
}

推荐答案

除了在您的 createDataset()方法中放置一个计时器外,您还可以从修改您的JFreeChart的main方法中生成一个新线程每5秒收集一次数据.

Rather than putting a timer in your createDataset() method you can instead spawn a new thread from your main method that modifies your JFreeChart dataset every 5 seconds.

例如,您可以执行以下操作:

For example you could do it something like this:

public static void main( String[ ] args ) {
  XYLineChart_AWT chart = new XYLineChart_AWT("Temp",
     "Temperature of some location");
  chart.pack( );          
  RefineryUtilities.centerFrameOnScreen( chart );          
  chart.setVisible( true ); 

  //now make your timer
  int delay = 5000; //milliseconds
  ActionListener timerAction = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //some code here to get and modify your dataset so it can be updated
          // ----
          // ----
          //now apply your new dataset to your JFreeChart
          xylineChart.getXYPlot().setDataset(myNewDataset);
      }
  };
  new Timer(delay, timerAction).start();
}

请记住添加一些代码以删除数据集中的旧条目,以使图表保持可读性,并且时间轴上的所有值在不同数据集之间保持相同的距离,例如,确保不超过24个项目(一次绘制2分钟的数据.

Remember to add some code to remove old entries in your dataset so that the chart remains readable and all the values on the Time axis remain the same distance apart between different datasets, for example make sure there are no more than 24 items (2 minutes of data) plotted at a time.

这篇关于如何每5秒将数据添加到JFree XY图表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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