简体   繁体   中英

How generate dynamic graph from massive Java

I have a job in the construction of the dynamic graphics in real time, for example, I found a JFreeChart link below will bring, but I can't figure out how can I use this example, can tell where to place the array with data and then for example to make the derivation of the odds-cycle delay of 3 seeundy and build a graph using data from the array.

http://www.java2s.com/Code/Java/Chart/JFreeChartDynamicDataDemo.htm

In particular, Where must I write cycle for and with what to see the value from massive on the graph?

The MVC pattern lies at the core of dynamic charts in JFreeChart— if you update the model , the listening view will update itself accordingly. In the example cited, a Swing Timer evokes the implementation of ActionListener to periodically add() a new value to the TimeSeries . Your code would do likewise. A similar approach is taken here with a DynamicTimeSeriesCollection . To process data in the background, use a SwingWoker , illustrated here .

JFreeChart implements org.jfree.ui.Drawable so you can create your custom JPanel, override public void paint(Graphics g) and in this method you can create JFreeChart, then you can call method draw of JfreeChart object with arg Graphic.

When data will change repaint this JPanel

below is example as you wish:

/** Your custom JPanel */
public class MyPanel extends JPanel{
Random random =new Random(System.currentTimeMillis()); //this is only to simulate change data

public MyPanel(){
    //simulation change data
    new Thread(){
        public void run(){
            while(true){
            try {
                Thread.sleep(3000l);//in every 3 sec refresh
                Thread.yield();     // release processor 
                repaint();          //repaint panel with new data
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            }
        }
    }.start();
}

public void paint(Graphics g){
    //paint panel
    super.paint(g);
    // create chart
    JFreeChart lineChart = ChartFactory.createLineChart(
             "My Title",
             "Years","Number of Schools",
             createDataset(),
             PlotOrientation.VERTICAL,
             true,true,false);
    //draw chart on panel
    lineChart.draw((Graphics2D) g, this.getVisibleRect());
}

/** create data for chart */
private DefaultCategoryDataset createDataset( )
   {
      DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
      dataset.addValue( random.nextInt(100) , "schools" , "1970" );
      dataset.addValue( random.nextInt(100) , "schools" , "1980" );
      dataset.addValue( random.nextInt(100) , "schools" ,  "1990" );
      dataset.addValue( random.nextInt(100) , "schools" , "2000" );
      dataset.addValue( random.nextInt(100) , "schools" , "2010" );
      dataset.addValue( random.nextInt(100) , "schools" , "2014" );
      return dataset;
   }
}

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