简体   繁体   中英

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

该图当前的外观

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM