From: Arne V. <cob...@us...> - 2004-07-11 22:07:04
|
Update of /cvsroot/jrobin/src/org/jrobin/demo/graph In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7621/org/jrobin/demo/graph Added Files: GraphTemplate.java SwingDemo.java SwingDemoPanel.java Log Message: JRobin 1.4.0 - Added SwingDemo demo application - Added GraphTemplate demo application --- NEW FILE: SwingDemo.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...); * * (C) Copyright 2003, by Sasa Markovic. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.demo.graph; import org.jrobin.core.*; import org.jrobin.graph.RrdGraphDef; import org.jrobin.graph.RrdGraph; import javax.swing.*; import java.io.IOException; import java.awt.*; import java.util.Date; /** * <p>Swing demonstration of the minmax graph.</p> * * @author Arne Vandamme (cob...@jr...) */ public class SwingDemo { static JFrame frame = null; static JPanel demoPanel = null; static RrdGraph graph = null; static RrdGraphDef gDef = null; static final String rrd = "SwingDemo.rrd"; static final long START = Util.getTimestamp( 2004, 1, 1 ); static Date end = new Date(START); static void prepareRrd() throws IOException, RrdException { RrdDef rrdDef = new RrdDef( rrd, START - 300, 300 ); rrdDef.addDatasource("a", "GAUGE", 600, Double.NaN, Double.NaN); rrdDef.addArchive("AVERAGE", 0.5, 1, 300); rrdDef.addArchive("MIN", 0.5, 6, 300); rrdDef.addArchive("MAX", 0.5, 6, 300); RrdDb rrdDb = new RrdDb( rrdDef, RrdBackendFactory.getFactory("MEMORY") ); rrdDb.close(); } static void prepareFrame() throws RrdException { gDef = new RrdGraphDef(); gDef.setImageBorder( Color.WHITE, 0 ); gDef.setTitle("JRobin Swing minmax demo"); gDef.setVerticalLabel( "value" ); gDef.setTimeAxisLabel( "time" ); gDef.datasource("a", rrd, "a", "AVERAGE", "MEMORY" ); gDef.datasource("b", rrd, "a", "MIN", "MEMORY" ); gDef.datasource("c", rrd, "a", "MAX", "MEMORY"); gDef.datasource( "avg", "a", "AVERAGE" ); gDef.area("a", Color.decode("0xb6e4"), "real"); gDef.line("b", Color.decode("0x22e9"), "min", 2 ); gDef.line("c", Color.decode("0xee22"), "max", 2 ); gDef.line("avg", Color.RED, "Average" ); gDef.time( "@l@lTime period: @t", "MMM dd, yyyy HH:mm:ss", START ); gDef.time( "to @t@l", "HH:mm:ss", end ); gDef.time("@l@lGenerated: @t@c", "HH:mm:ss" ); // create graph finally graph = new RrdGraph(gDef); // Create JFrame frame = new JFrame( "JRobin Swing Demo" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); demoPanel = new SwingDemoPanel( graph ); frame.getContentPane().add( demoPanel ); frame.pack(); frame.setBounds( 10, 10, 504, 303 ); frame.show(); } public static void main( String[] args ) throws Exception { prepareRrd(); prepareFrame(); // Let's update the RRD and generate the graphs many times RrdDb rrdDb = new RrdDb( rrd, RrdBackendFactory.getFactory("MEMORY") ); Sample sample = rrdDb.createSample(); long t = 0, start, stop, generationTime; // First we do 2 hours worth of 5 minute updates for ( int i = 1; i < 25; i++ ) { start = System.currentTimeMillis(); t = START + i*300; // Update the rrd sample.setTime( t ); sample.setValue("a", Math.sin(t / 3000.0) * 50 + 50); sample.update(); // Set custom graph settings gDef.setTimePeriod( START, t ); end.setTime( t *1000 ); // Regenerate the graph frame.repaint(); stop = System.currentTimeMillis(); generationTime = stop - start; // Sleep if necessary, don't update more than once per second if ( generationTime < 1000 ) Thread.sleep( 1000 - generationTime ); } // Now we do some more updates, but we move per hour for ( int i = 0; i < 22; i++ ) { start = System.currentTimeMillis(); for ( int j = 1; j < 13; j++ ) { t = t + 300; // Update the rrd sample.setTime( t ); sample.setValue("a", Math.sin(t / 3000.0) * 50 + 50); sample.update(); } // Set custom graph settings gDef.setTimePeriod( START, t ); end.setTime( t * 1000 ); // Regenerate the graph frame.repaint(); stop = System.currentTimeMillis(); generationTime = stop - start; // Sleep if necessary, don't update more than once per second if ( generationTime < 1000 ) Thread.sleep( 1000 - generationTime ); } } } --- NEW FILE: GraphTemplate.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...); * * (C) Copyright 2003, by Sasa Markovic. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.demo.graph; import org.jrobin.graph.RrdGraphDefTemplate; import org.jrobin.graph.RrdGraph; import org.jrobin.core.RrdException; import java.io.File; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; /** * <p>Simple command line application that allows you to generate a graph * from a RrdGraphDefTemplate. Pretty straightforward use.</p> * * @author Arne Vandamme (cob...@jr...) */ public class GraphTemplate { private static String format = "gif"; private static int width = 0; // Auto scale private static int height = 0; // Auto scale private static float quality = 1.0f; // JPEG quality private static String templateFile, imageName; private static void die( String msg ) { System.err.println( msg ); System.exit( -1 ); } private static void parseArguments( String[] args ) { int rpos = args.length - 1; // Last two arguments should be templateFile and imageName imageName = args[rpos--]; templateFile = args[rpos]; // Remaining number of parameters should be even if ( rpos % 2 > 0 ) die( "Invalid number of arguments." ); for ( int i = 0; i < rpos; i += 2 ) { String arg = args[i]; String val = args[i + 1]; try { if ( arg.equalsIgnoreCase("-img") ) format = val; else if ( arg.equalsIgnoreCase("-w") ) width = Integer.parseInt(val); else if ( arg.equalsIgnoreCase("-h") ) height = Integer.parseInt(val); else if ( arg.equalsIgnoreCase("-q") ) quality = Float.parseFloat(val); } catch ( Exception e ) { die( "Error with option '" + arg + "': " + e.getMessage() ); } } } private static String readVariable( BufferedReader in, String name ) throws IOException { System.out.print( "Variable '" + name + "' = " ); return in.readLine(); } public static void main( String[] args ) { if ( args.length < 2 ) { System.out.println( "Usage: GraphTemplate [-img (png|gif|jpg)] [-w width] [-h height] [-q jpegQuality] <template_file> <image_name>" ); System.exit(0); } parseArguments( args ); try { // -- Read the RrdGraphDefTemplate (XML format) System.out.println( ">>> Reading XML template" ); RrdGraphDefTemplate template = new RrdGraphDefTemplate( new File(templateFile) ); System.out.println( ">>> Setting template variables" ); if ( template.hasVariables() ) { BufferedReader in = new BufferedReader( new InputStreamReader(System.in) ); String[] variables = template.getVariables(); for ( int i = 0; i < variables.length; i++ ) template.setVariable( variables[i], readVariable( in, variables[i] ) ); } System.out.println( ">>> Generating graph..." ); long start = System.currentTimeMillis(); RrdGraph graph = new RrdGraph( template.getRrdGraphDef() ); if ( format.equalsIgnoreCase("png") ) graph.saveAsPNG( imageName, width, height ); else if ( format.equalsIgnoreCase("gif") ) graph.saveAsGIF( imageName, width, height ); else if ( format.equalsIgnoreCase("jpg") ) graph.saveAsJPEG( imageName, width, height, quality ); long stop = System.currentTimeMillis(); System.out.println( ">>> Graph generated and saved in " + (stop - start) + " milliseconds" ); } catch ( RrdException rrde ) { die( "RrdException occurred: " + rrde.getMessage() ); } catch ( IOException ioe ) { die( "IOException occurred: " + ioe.getMessage() ); } } } --- NEW FILE: SwingDemoPanel.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...); * * (C) Copyright 2003, by Sasa Markovic. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.demo.graph; import javax.swing.*; import java.awt.*; import org.jrobin.graph.RrdGraph; /** * <p>Extended JPanel for use in the GUI demo application.</p> * * @author Arne Vandamme (cob...@jr...) */ public class SwingDemoPanel extends JPanel { private RrdGraph graph; SwingDemoPanel( RrdGraph graph ) { this.graph = graph; } public void paintComponent( Graphics g ) { try { // Render the image directly on the Graphics object of the JPanel // Width and height of 0 means autoscale the graph graph.renderImage( (Graphics2D) g, 0, 0 ); } catch ( Exception e ) { e.printStackTrace(); } } } |