KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > demo > graph > SwingDemo


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.demo.graph;
26
27 import org.jrobin.core.*;
28 import org.jrobin.graph.RrdGraphDef;
29 import org.jrobin.graph.RrdGraph;
30
31 import javax.swing.*;
32 import java.io.IOException JavaDoc;
33 import java.awt.*;
34 import java.awt.event.ComponentAdapter JavaDoc;
35 import java.awt.event.ComponentEvent JavaDoc;
36 import java.util.Date JavaDoc;
37
38 /**
39  * <p>Swing demonstration of the minmax graph.</p>
40  *
41  * @author Arne Vandamme (cobralord@jrobin.org)
42  */

43 public class SwingDemo
44 {
45     static JFrame frame = null;
46     static SwingDemoPanel demoPanel = null;
47     static RrdGraph graph = null;
48     static RrdGraphDef gDef = null;
49
50     static final String JavaDoc rrd = "SwingDemo.rrd";
51     static final long START = Util.getTimestamp( 2004, 1, 1 );
52
53     static Date JavaDoc end = new Date JavaDoc(START);
54
55     static void prepareRrd() throws IOException JavaDoc, RrdException
56     {
57         RrdDef rrdDef = new RrdDef( rrd, START - 300, 300 );
58         rrdDef.addDatasource("a", "GAUGE", 600, Double.NaN, Double.NaN);
59         rrdDef.addArchive("AVERAGE", 0.5, 1, 300);
60         rrdDef.addArchive("MIN", 0.5, 6, 300);
61         rrdDef.addArchive("MAX", 0.5, 6, 300);
62
63         RrdDb rrdDb = new RrdDb( rrdDef, RrdBackendFactory.getFactory("MEMORY") );
64         rrdDb.close();
65     }
66
67     static void prepareFrame() throws RrdException
68     {
69         gDef = new RrdGraphDef();
70         gDef.setImageBorder( Color.WHITE, 0 );
71         gDef.setTitle("JRobin Swing minmax demo");
72         gDef.setVerticalLabel( "value" );
73         gDef.setTimeAxisLabel( "time" );
74         gDef.datasource("a", rrd, "a", "AVERAGE", "MEMORY" );
75         gDef.datasource("b", rrd, "a", "MIN", "MEMORY" );
76         gDef.datasource("c", rrd, "a", "MAX", "MEMORY");
77         gDef.datasource( "avg", "a", "AVERAGE" );
78         gDef.area("a", Color.decode("0xb6e4"), "real");
79         gDef.line("b", Color.decode("0x22e9"), "min", 2 );
80         gDef.line("c", Color.decode("0xee22"), "max", 2 );
81         gDef.line("avg", Color.RED, "Average" );
82         gDef.time( "@l@lTime period: @t", "MMM dd, yyyy HH:mm:ss", START );
83         gDef.time( "to @t@l", "HH:mm:ss", end );
84         gDef.time("@lGenerated: @t@c", "HH:mm:ss" );
85
86         // create graph finally
87
graph = new RrdGraph(gDef);
88
89         // Create JFrame
90
frame = new JFrame( "JRobin Swing Demo" );
91         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
92
93         demoPanel = new SwingDemoPanel( graph );
94         frame.getContentPane().add( demoPanel );
95
96         frame.pack();
97
98         frame.addComponentListener(new ComponentAdapter JavaDoc() {
99             public void componentResized(ComponentEvent JavaDoc e) {
100                 Dimension d = frame.getContentPane().getSize();
101                 demoPanel.setGraphDimension(d);
102             }
103         });
104         frame.setBounds( 10, 10, 504, 303 );
105         frame.show();
106     }
107
108     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
109     {
110         prepareRrd();
111
112         prepareFrame();
113
114         // Let's update the RRD and generate the graphs many times
115
RrdDb rrdDb = new RrdDb( rrd, RrdBackendFactory.getFactory("MEMORY") );
116         Sample sample = rrdDb.createSample();
117
118         long t = 0, start, stop, generationTime;
119
120         // First we do 2 hours worth of 5 minute updates
121
for ( int i = 1; i < 25; i++ )
122         {
123             start = System.currentTimeMillis();
124
125             t = START + i*300;
126
127             // Update the rrd
128
sample.setTime( t );
129             sample.setValue("a", Math.sin(t / 3000.0) * 50 + 50);
130             sample.update();
131
132             // Set custom graph settings
133
gDef.setTimePeriod( START, t );
134             end.setTime( t *1000 );
135
136             // Regenerate the graph
137
frame.repaint();
138
139             stop = System.currentTimeMillis();
140             generationTime = stop - start;
141
142             // Sleep if necessary, don't update more than once per second
143
if ( generationTime < 1000 )
144                 Thread.sleep( 1000 - generationTime );
145         }
146
147         // Now we do some more updates, but we move per hour
148
for ( int i = 0; i < 22; i++ )
149         {
150             start = System.currentTimeMillis();
151
152             for ( int j = 1; j < 13; j++ )
153             {
154                 t = t + 300;
155
156                 // Update the rrd
157
sample.setTime( t );
158                 sample.setValue("a", Math.sin(t / 3000.0) * 50 + 50);
159                 sample.update();
160             }
161
162             // Set custom graph settings
163
gDef.setTimePeriod( START, t );
164             end.setTime( t * 1000 );
165
166             // Regenerate the graph
167
frame.repaint();
168
169             stop = System.currentTimeMillis();
170             generationTime = stop - start;
171
172             // Sleep if necessary, don't update more than once per second
173
if ( generationTime < 1000 )
174                 Thread.sleep( 1000 - generationTime );
175         }
176     }
177 }
178
Popular Tags