How to add data into a JFree XY chart every 5 seconds?

一世执手 提交于 2019-12-25 09:46:04

问题


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

回答1:


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

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.



来源:https://stackoverflow.com/questions/44016187/how-to-add-data-into-a-jfree-xy-chart-every-5-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!