Animation Line Chart : Chart « Advanced Graphics « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Advanced Graphics » Chart 




Animation Line Chart
Animation Line Chart
 
import java.awt.*;
import java.applet.*;
import java.net.URL;
import java.util.*;
import graph.*;
/*************************************************************************
**
**    Applet example2
**                                              Version 1.0   January 1996
**
**************************************************************************
**    Copyright (C) 1996 Leigh Brookshaw
**
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License as published by
**    the Free Software Foundation; either version 2 of the License, or
**    (at your option) any later version.
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**************************************************************************
**
**    This is a simple applet that creates a Scroll Chart using the 
**    Graph2D class library
**
*************************************************************************/

public class example2 extends Applet {

      Graph2D graph;
      Label title;
      DataSet data1 = new DataSet();
      CalculateData cd1;
      Axis    yaxis_right;
/*
**    In milliseconds how often do we want to add a new data point.
*/
      int period      = 500;
/*
**    Maximum number of points to display before scrolling the data
*/
      int maximum        = 25;
      URL markersURL;
/*
**    Initialize the applet. The Parameters passed are the title of the plot
**    the marker file to use and the update period in milliseconds
*/
      public void init() {
        int i;
        int j;

/*
**      Get the passed parameters
*/
        String st       = "TITLE";
        String mfile    = "marker.txt";
        period   = 10;//Integer.parseInt(getParameter("PERIOD"));
/*
**      Create the Graph instance and modify the default behaviour
*/
        graph = new Graph2D();
        graph.zerocolor = new Color(0,255,0);
        graph.borderTop    = 50;
        graph.borderBottom = 50;
/*
**      Create the Title
*/
        title = new Label(st, Label.CENTER);
        title.setFont(new Font("TimesRoman",Font.PLAIN,25));

        setLayoutnew BorderLayout() );
        add("North",  title);
        add("Center", graph);
/*
**      Load a file containing Marker definitions
*/
        try {
           markersURL = this.getClass().getResource(mfile);
           graph.setMarkers(new Markers(markersURL));
        catch(Exception e) {
           System.out.println("Failed to create Marker URL!");
        }
 

/*
**      Modify the default Data behaviour
*/
        data1.linecolor   = new Color(255,0,0);
        data1.marker      = 1;
        data1.markercolor = new Color(100,100,255);
/*
**      Setup the Axis. Attach it to the Graph2D instance, and attach the data
**      to it.
*/
        yaxis_right = graph.createAxis(Axis.RIGHT);
        yaxis_right.attachDataSet(data1);
        yaxis_right.setLabelFont(new Font("Helvetica",Font.PLAIN,20));

        graph.attachDataSet(data1);

/*
**      Calculate the data asynchronously using a new Thread.
*/
        cd1 = new CalculateData(data1,graph,period,maximum);
        cd1.start();

      }

      public static void main(String[] a){
         javax.swing.JFrame f = new javax.swing.JFrame();
         Applet app = new example2();
         app.init();
         
         f.getContentPane().add (app);

         f.pack();
         f.setSize (new Dimension (500500));
         f.setVisible(true);
      }

}


/*
** Here is the Thread class to calculate the data and append it to the existing
** data set.
** This class is easily modified to get the data from a server 
** or the local machine. Currently the data is Random.
*/

class CalculateData extends Thread {
//      local copy of the update period in milliseconds
        int p = 1000;
        DataSet d;
        Graph2D g;
//      local copy of the maximum number of points.
        int m = 25;

        Random random = new Random();


        public CalculateData(DataSet ds, Graph2D g, int p, int m)
        {
            this.d = ds;
            this.g = g;
            if(p > 100   this.p = p;
            if(m > 0     this.m = m;
  }

        public void run() {
              int i =0;
              double data[] new double[2];
              int count = 0;

              if(d == nullreturn;

              setPriority(Thread.MIN_PRIORITY);


              while(true) {

                   count++;

                   if(count >= md.delete(0,0);

                   data[1(2.0*random.nextDouble() 1.0)*15.0;
                   data[0= count;

                   try {
                        d.append(data,1);
          }
                   catch (Exception e) {
                        System.out.println("Error appending Data!");
                   }

                   d.yaxis.maximum = 15.0;
                   d.yaxis.minimum = -15.0;

//                 To avoid flashing try and repaint halfway through the sleep

                   g.repaint(p/2);

                   try {    sleep(p)}
                   catch(Exception e) { }

       }

       }


}



           
         
  














Graph-AnimationLineChart.zip( 195 k)
Related examples in the same category
1.Chart based on Graph libraryChart based on Graph library
2.Scroll ChartScroll Chart
3.Pie ChartPie Chart
4.A rudimentary chart libraryA rudimentary chart library
5.A rudimentary bar chart classA rudimentary bar chart class
6.bar-graph drawable
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.