-1

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?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
VasyPupkin
  • 153
  • 1
  • 14
  • 1
    *"this is code"* What is question? Note that *"I have a job.."* leads into a requirement (for you). What is your question (for us)? – Andrew Thompson Feb 15 '16 at 09:39
  • Where i must write cycle for and with what to see the value from massive on the graph – VasyPupkin Feb 15 '16 at 09:42
  • 1
    So the question is *"Where must I write cycle for and with what to see the value from massive on the graph?"* (I'm not sure I even understand that, but at least it is a question..). – Andrew Thompson Feb 15 '16 at 09:45
  • Please don't simply cut and paste 14 your old code; edit your question to include a [mcve] that shows your current approach. – trashgod Feb 15 '16 at 11:13

2 Answers2

4

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

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;
   }
}
Victor1125
  • 642
  • 5
  • 16
  • 1
    [org.jfree.ui.Drawable](http://www.jfree.org/jcommon/api/org/jfree/ui/Drawable.html) is irrelevant in this context. – trashgod Feb 15 '16 at 11:11
  • as above. create your class eg `class MyChartPanel extends JPanel` override method `public void paint(Graphics g)`. In this method cast Graphics to Graphics2D. Create JFreeChart object as in your example. Using method draw(). I think you must create class extending `java.awt.geom.Rectangle2D` which get size of your JPanel. – Victor1125 Feb 15 '16 at 11:29
  • 3
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). `ChartPanel` already does this for you. – trashgod Feb 15 '16 at 12:39