1 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 ; 33 import java.awt.*; 34 import java.awt.event.ComponentAdapter ; 35 import java.awt.event.ComponentEvent ; 36 import java.util.Date ; 37 38 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 rrd = "SwingDemo.rrd"; 51 static final long START = Util.getTimestamp( 2004, 1, 1 ); 52 53 static Date end = new Date (START); 54 55 static void prepareRrd() throws IOException , 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 graph = new RrdGraph(gDef); 88 89 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 () { 99 public void componentResized(ComponentEvent 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 [] args ) throws Exception 109 { 110 prepareRrd(); 111 112 prepareFrame(); 113 114 RrdDb rrdDb = new RrdDb( rrd, RrdBackendFactory.getFactory("MEMORY") ); 116 Sample sample = rrdDb.createSample(); 117 118 long t = 0, start, stop, generationTime; 119 120 for ( int i = 1; i < 25; i++ ) 122 { 123 start = System.currentTimeMillis(); 124 125 t = START + i*300; 126 127 sample.setTime( t ); 129 sample.setValue("a", Math.sin(t / 3000.0) * 50 + 50); 130 sample.update(); 131 132 gDef.setTimePeriod( START, t ); 134 end.setTime( t *1000 ); 135 136 frame.repaint(); 138 139 stop = System.currentTimeMillis(); 140 generationTime = stop - start; 141 142 if ( generationTime < 1000 ) 144 Thread.sleep( 1000 - generationTime ); 145 } 146 147 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 sample.setTime( t ); 158 sample.setValue("a", Math.sin(t / 3000.0) * 50 + 50); 159 sample.update(); 160 } 161 162 gDef.setTimePeriod( START, t ); 164 end.setTime( t * 1000 ); 165 166 frame.repaint(); 168 169 stop = System.currentTimeMillis(); 170 generationTime = stop - start; 171 172 if ( generationTime < 1000 ) 174 Thread.sleep( 1000 - generationTime ); 175 } 176 } 177 } 178 | Popular Tags |