From: Sasa M. <sa...@us...> - 2004-06-09 12:09:35
|
Update of /cvsroot/jrobin/src/org/jrobin/demo In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21595/org/jrobin/demo Added Files: ComplexDemo.java Demo.java PlottableDemo.java StressTest.java TemplatesDemo.java Log Message: Demo files shifted to org.jrobin.demo package --- NEW FILE: Demo.java --- package org.jrobin.demo; /* ============================================================ * 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. * * 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. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * 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. */ import org.jrobin.core.*; import org.jrobin.graph.RrdGraph; import org.jrobin.graph.RrdGraphDef; import java.awt.*; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; class Demo { // static final String FACTORY_NAME = "NIO"; static final long SEED = 1909752002L; static final Random RANDOM = new Random(SEED); static final String FILE = "demo"; static final long START = Util.getTimestamp(2003, 4, 1); static final long END = Util.getTimestamp(2003, 5, 1); static final int MAX_STEP = 300; public static void main(String[] args) throws RrdException, IOException { // RrdDb.setDefaultFactory(FACTORY_NAME); // setup println("== Starting demo"); RrdDb.setLockMode(RrdDb.NO_LOCKS); long startMillis = System.currentTimeMillis(); long start = START; long end = END; String rrdPath = Util.getJRobinDemoPath(FILE + ".rrd"); String xmlPath = Util.getJRobinDemoPath(FILE + ".xml"); String rrdRestoredPath = Util.getJRobinDemoPath(FILE + "_restored.rrd"); String pngPath = Util.getJRobinDemoPath(FILE + ".png"); String jpegPath = Util.getJRobinDemoPath(FILE + ".jpeg"); String gifPath = Util.getJRobinDemoPath(FILE + ".gif"); String logPath = Util.getJRobinDemoPath(FILE + ".log"); PrintWriter log = new PrintWriter( new BufferedOutputStream(new FileOutputStream(logPath, false)) ); // creation println("== Creating RRD file " + rrdPath); RrdDef rrdDef = new RrdDef(rrdPath, start - 1, 300); rrdDef.addDatasource("sun", "GAUGE", 600, 0, Double.NaN); rrdDef.addDatasource("shade", "GAUGE", 600, 0, Double.NaN); rrdDef.addArchive("AVERAGE", 0.5, 1, 600); rrdDef.addArchive("AVERAGE", 0.5, 6, 700); rrdDef.addArchive("AVERAGE", 0.5, 24, 775); rrdDef.addArchive("AVERAGE", 0.5, 288, 797); rrdDef.addArchive("MAX", 0.5, 1, 600); rrdDef.addArchive("MAX", 0.5, 6, 700); rrdDef.addArchive("MAX", 0.5, 24, 775); rrdDef.addArchive("MAX", 0.5, 288, 797); println(rrdDef.dump()); log.println(rrdDef.dump()); println("Estimated file size: " + rrdDef.getEstimatedSize()); RrdDb rrdDb = new RrdDb(rrdDef); println("== RRD file created."); rrdDb.close(); println("== RRD file closed."); // update database GaugeSource sunSource = new GaugeSource(1200, 20); GaugeSource shadeSource = new GaugeSource(300, 10); println("== Simulating one month of RRD file updates with step not larger than " + MAX_STEP + " seconds (* denotes 1000 updates)"); long t = start; int n = 0; rrdDb = new RrdDb(rrdPath); Sample sample = rrdDb.createSample(); while(t <= end + 86400L) { //rrdDb = new RrdDb(rrdPath); //Sample sample = rrdDb.createSample(); sample.setTime(t); sample.setValue("sun", sunSource.getValue()); sample.setValue("shade", shadeSource.getValue()); log.println(sample.dump()); sample.update(); t += RANDOM.nextDouble() * MAX_STEP + 1; if(((++n) % 1000) == 0) { System.out.print("*"); }; //rrdDb.close(); } println(""); println("== Finished. RRD file updated " + n + " times"); println("== Last update time was: " + rrdDb.getLastUpdateTime()); rrdDb.close(); // test read-only access! rrdDb = new RrdDb(rrdPath, true); println("File reopen in read-only mode"); // fetch data println("== Fetching data for the whole month"); FetchRequest request = rrdDb.createFetchRequest("AVERAGE", start, end); println(request.dump()); log.println(request.dump()); FetchData fetchData = request.fetchData(); println("== Data fetched. " + fetchData.getRowCount() + " points obtained"); for(int i = 0; i < fetchData.getRowCount(); i++) { println(fetchData.getRow(i).dump()); } println("== Dumping fetch data to XML format"); println(fetchData.exportXml()); println("== Fetch completed"); // dump to XML file println("== Dumping RRD file to XML file " + xmlPath + " (can be restored with RRDTool)"); rrdDb.exportXml(xmlPath); println("== Creating RRD file " + rrdRestoredPath + " from XML file " + xmlPath); RrdDb rrdRestoredDb = new RrdDb(rrdRestoredPath, xmlPath); // close files println("== Closing both RRD files"); rrdDb.close(); println("== First file closed"); rrdRestoredDb.close(); println("== Second file closed"); // create graph println("== Creating graph from the second file"); RrdGraphDef gDef = new RrdGraphDef(); gDef.setTimePeriod(start, end); gDef.setTitle("Temperatures in May 2003"); gDef.setVerticalLabel("temperature"); gDef.datasource("sun", rrdRestoredPath, "sun", "AVERAGE"); gDef.datasource("shade", rrdRestoredPath, "shade", "AVERAGE"); gDef.datasource("median", "sun,shade,+,2,/"); gDef.datasource("diff", "sun,shade,-,ABS,-1,*"); gDef.datasource("sine", "TIME," + start + ",-," + (end - start) + ",/,2,PI,*,*,SIN,1000,*"); gDef.line("sun", Color.GREEN, "sun temp"); gDef.line("shade", Color.BLUE, "shade temp"); gDef.line("median", Color.MAGENTA, "median value@L"); gDef.area("diff", Color.YELLOW, "difference@r"); gDef.line("diff", Color.RED, null); gDef.line("sine", Color.CYAN, "sine function demo@L"); gDef.gprint("sun", "MAX", "maxSun = @3@s"); gDef.gprint("sun", "AVERAGE", "avgSun = @3@S@r"); gDef.gprint("shade", "MAX", "maxShade = @3@S"); gDef.gprint("shade", "AVERAGE", "avgShade = @3@S@r"); // create graph finally RrdGraph graph = new RrdGraph(gDef); println("== Graph created"); graph.saveAsPNG(pngPath, 400, 250); println("== Graph saved as a PNG file " + pngPath); graph.saveAsJPEG(jpegPath, 400, 250, 0.5F); println("== Graph saved as a JPEG file " + jpegPath); graph.saveAsGIF(gifPath, 400, 250); println("== Graph saved as a GIF file " + gifPath); // demo ends log.close(); println("== Demo completed in " + ((System.currentTimeMillis() - startMillis) / 1000.0) + " sec"); } static void println(String msg) { //System.out.println(msg + " " + Util.getLapTime()); System.out.println(msg); } static void print(String msg) { System.out.print(msg); } static class GaugeSource { private double value; private double step; GaugeSource(double value, double step) { this.value = value; this.step = step; } long getValue() { double oldValue = value; double increment = RANDOM.nextDouble() * step; if(RANDOM.nextDouble() > 0.5) { increment *= -1; } value += increment; if(value <= 0) { value = 0; } return Math.round(oldValue); } } } --- NEW FILE: StressTest.java --- package org.jrobin.demo; /* ============================================================ * 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. * * 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. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * 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. */ import org.jrobin.core.*; import org.jrobin.graph.RrdGraphDef; import org.jrobin.graph.RrdGraph; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.util.Date; import java.awt.*; class StressTest { static final String FACTORY_NAME = "NIO"; static final String RRD_PATH = Util.getJRobinDemoPath("stress.rrd"); static final long RRD_START = 946710000L; static final long RRD_STEP = 30; static final String RRD_DATASOURCE_NAME = "T"; static final int RRD_DATASOURCE_COUNT = 6; static final long TIME_START = 1060142010L; static final long TIME_END = 1080013472L; static final String PNG_PATH = Util.getJRobinDemoPath("stress.png"); static final int PNG_WIDTH = 400; static final int PNG_HEIGHT = 250; static void printLapTime(String message) { System.out.println(message + " " + Util.getLapTime()); } public static void main(String[] args) throws RrdException, IOException { if(args.length == 0) { System.out.println("Usage: StressTest [path to stress-test.txt file]"); System.out.println("You can download separate stress-test.txt file from:"); System.out.println("http://www.sourceforge.net/projects/jrobin"); System.exit(-1); } System.out.println("********************************************************************"); System.out.println("* JRobinStressTest *"); System.out.println("* *"); System.out.println("* This demo creates single RRD file and tries to update it *"); System.out.println("* more than 600.000 times. Real data (> 20Mb) is obtained from the *"); System.out.println("* stress-test.txt file provided by Vadim Tkachenko *"); System.out.println("* (http://diy-zoning.sourceforge.net). *"); System.out.println("* *"); System.out.println("* Finally, a single PNG graph will be created from the RRD file. *"); System.out.println("* The stress test takes about one hour to complete on a 1.6GHz *"); System.out.println("* computer with 256MB of RAM. *"); System.out.println("********************************************************************"); printLapTime("Starting demo at " + new Date()); RrdDb.setDefaultFactory(FACTORY_NAME); printLapTime("Backend factory set to " + FACTORY_NAME); // create RRD database printLapTime("Creating RRD definition"); RrdDef def = new RrdDef(RRD_PATH); def.setStartTime(RRD_START); def.setStep(RRD_STEP); for(int i = 0; i < RRD_DATASOURCE_COUNT; i++) { def.addDatasource(RRD_DATASOURCE_NAME + i, "GAUGE", 90, -60, 85); } def.addArchive("LAST", 0.5, 1, 5760); def.addArchive("MIN", 0.5, 1, 5760); def.addArchive("MAX", 0.5, 1, 5760); def.addArchive("AVERAGE", 0.5, 5, 13824); def.addArchive("MIN", 0.5, 5, 13824); def.addArchive("MAX", 0.5, 5, 13824); def.addArchive("AVERAGE", 0.5, 60, 16704); def.addArchive("MIN", 0.5, 60, 16704); def.addArchive("MAX", 0.5, 60, 16704); def.addArchive("AVERAGE", 0.5, 1440, 50000); def.addArchive("MIN", 0.5, 1440, 50000); def.addArchive("MAX", 0.5, 1440, 50000); printLapTime("Definition created, creating RRD file"); RrdDb rrd = RrdDbPool.getInstance().requestRrdDb(def); printLapTime("RRD file created: " + RRD_PATH); BufferedReader r = new BufferedReader(new FileReader(args[0])); printLapTime("Buffered reader created, processing data"); int count = 0; Date updateStart = new Date(); for(String line; (line = r.readLine()) != null;) { Sample sample = rrd.createSample(); try { sample.setAndUpdate(line); if(++count % 1000 == 0) { Date now = new Date(); long speed = (long)(count * 1000.0 / (now.getTime() - updateStart.getTime())); printLapTime(count + " samples stored, " + speed + " updates/sec"); } } catch(RrdException e) { printLapTime("RRD ERROR: " + line); } } RrdDbPool.getInstance().release(rrd); printLapTime("FINISHED: " + count + " samples stored"); // GRAPH printLapTime("Creating composite graph definition"); RrdGraphDef gdef = new RrdGraphDef(TIME_START, TIME_END); gdef.setTitle("Temperatures"); gdef.setVerticalLabel("Fahrenheits"); final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.MAGENTA, Color.CYAN, Color.ORANGE }; // datasources for(int i = 0; i < RRD_DATASOURCE_COUNT; i++) { String name = RRD_DATASOURCE_NAME + i; gdef.datasource(name, RRD_PATH, name, "AVERAGE"); } // lines for(int i = 0; i < RRD_DATASOURCE_COUNT; i++) { String name = RRD_DATASOURCE_NAME + i; gdef.line(name, colors[i], name); } gdef.comment("@c"); gdef.comment("\nOriginal data provided by diy-zoning.sf.net@c"); printLapTime("Graph definition created"); RrdGraph g = new RrdGraph(gdef, true); g.saveAsPNG(PNG_PATH, PNG_WIDTH, PNG_HEIGHT); printLapTime("Graph saved: " + PNG_PATH); printLapTime("Finished at " + new Date()); } } --- NEW FILE: TemplatesDemo.java --- package org.jrobin.demo; /* ============================================================ * 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. * * 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. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * 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. */ import org.jrobin.core.*; import org.jrobin.graph.RrdGraph; import org.jrobin.graph.RrdGraphDef; import org.jrobin.graph.RrdGraphDefTemplate; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; class TemplatesDemo { static final String RRD_TEMPLATE = "<rrd_def> " + " <path>${path}</path> " + " <start>${start}</start> " + " <step>300</step> " + " <datasource> " + " <name>sun</name> " + " <type>GAUGE</type> " + " <heartbeat>600</heartbeat> " + " <min>0</min> " + " <max>U</max> " + " </datasource> " + " <datasource> " + " <name>shade</name> " + " <type>GAUGE</type> " + " <heartbeat>600</heartbeat> " + " <min>0</min> " + " <max>U</max> " + " </datasource> " + " <archive> " + " <cf>AVERAGE</cf> " + " <xff>0.5</xff> " + " <steps>1</steps> " + " <rows>600</rows> " + " </archive> " + " <archive> " + " <cf>AVERAGE</cf> " + " <xff>0.5</xff> " + " <steps>6</steps> " + " <rows>700</rows> " + " </archive> " + " <archive> " + " <cf>AVERAGE</cf> " + " <xff>0.5</xff> " + " <steps>24</steps> " + " <rows>775</rows> " + " </archive> " + " <archive> " + " <cf>AVERAGE</cf> " + " <xff>0.5</xff> " + " <steps>288</steps> " + " <rows>797</rows> " + " </archive> " + " <archive> " + " <cf>MAX</cf> " + " <xff>0.5</xff> " + " <steps>1</steps> " + " <rows>600</rows> " + " </archive> " + " <archive> " + " <cf>MAX</cf> " + " <xff>0.5</xff> " + " <steps>6</steps> " + " <rows>700</rows> " + " </archive> " + " <archive> " + " <cf>MAX</cf> " + " <xff>0.5</xff> " + " <steps>24</steps> " + " <rows>775</rows> " + " </archive> " + " <archive> " + " <cf>MAX</cf> " + " <xff>0.5</xff> " + " <steps>288</steps> " + " <rows>797</rows> " + " </archive> " + "</rrd_def> " ; static final String GRAPH_TEMPLATE = "<rrd_graph_def> " + " <span> " + " <start>${start}</start> " + " <end>${end}</end> " + " </span> " + " <options> " + " <title>${title}</title> " + " <vertical_label>temperature</vertical_label> " + " </options> " + " <datasources> " + " <def> " + " <name>sun</name> " + " <rrd>${rrd}</rrd> " + " <source>sun</source> " + " <cf>AVERAGE</cf> " + " </def> " + " <def> " + " <name>shade</name> " + " <rrd>${rrd}</rrd> " + " <source>shade</source> " + " <cf>AVERAGE</cf> " + " </def> " + " <def> " + " <name>median</name> " + " <rpn>sun,shade,+,2,/</rpn> " + " </def> " + " <def> " + " <name>diff</name> " + " <rpn>sun,shade,-,ABS,-1,*</rpn> " + " </def> " + " <def> " + " <name>sine</name> " + " <rpn>${sine}</rpn> " + " </def> " + " </datasources> " + " <graph> " + " <line> " + " <datasource>sun</datasource> " + " <color>#00FF00</color> " + " <legend>sun temp</legend> " + " </line> " + " <line> " + " <datasource>shade</datasource> " + " <color>#0000FF</color> " + " <legend>shade temp</legend> " + " </line> " + " <line> " + " <datasource>median</datasource> " + " <color>#FF00FF</color> " + " <legend>median value@L</legend> " + " </line> " + " <area> " + " <datasource>diff</datasource> " + " <color>#FFFF00</color> " + " <legend>difference@r</legend> " + " </area> " + " <line> " + " <datasource>diff</datasource> " + " <color>#FF0000</color> " + " <legend/> " + " </line> " + " <line> " + " <datasource>sine</datasource> " + " <color>#00FFFF</color> " + " <legend>sine function demo@L</legend> " + " </line> " + " <gprint> " + " <datasource>sun</datasource> " + " <cf>MAX</cf> " + " <format>maxSun = @3@s</format> " + " </gprint> " + " <gprint> " + " <datasource>sun</datasource> " + " <cf>AVERAGE</cf> " + " <format>avgSun = @3@S@r</format> " + " </gprint> " + " <gprint> " + " <datasource>shade</datasource> " + " <cf>MAX</cf> " + " <format>maxShade = @3@S</format> " + " </gprint> " + " <gprint> " + " <datasource>shade</datasource> " + " <cf>AVERAGE</cf> " + " <format>avgShade = @3@S@r</format> " + " </gprint> " + " </graph> " + "</rrd_graph_def> " ; static final long SEED = 1909752002L; static final Random RANDOM = new Random(SEED); static final String FILE = "templates_demo"; static final long START = Util.getTimestamp(2003, 4, 1); static final long END = Util.getTimestamp(2003, 5, 1); static final int MAX_STEP = 300; public static void main(String[] args) throws RrdException, IOException { // setup println("== Starting demo"); RrdDb.setLockMode(RrdDb.NO_LOCKS); long startMillis = System.currentTimeMillis(); long start = START; long end = END; String rrdPath = Util.getJRobinDemoPath(FILE + ".rrd"); String xmlPath = Util.getJRobinDemoPath(FILE + ".xml"); String rrdRestoredPath = Util.getJRobinDemoPath(FILE + "_restored.rrd"); String pngPath = Util.getJRobinDemoPath(FILE + ".png"); String jpegPath = Util.getJRobinDemoPath(FILE + ".jpeg"); String gifPath = Util.getJRobinDemoPath(FILE + ".gif"); String logPath = Util.getJRobinDemoPath(FILE + ".log"); PrintWriter log = new PrintWriter( new BufferedOutputStream(new FileOutputStream(logPath, false)) ); // creation from the template println("== Creating RRD file " + rrdPath); RrdDefTemplate defTemplate = new RrdDefTemplate(RRD_TEMPLATE); defTemplate.setVariable("path", rrdPath); defTemplate.setVariable("start", start - 1); RrdDef rrdDef = defTemplate.getRrdDef(); println(rrdDef.dump()); log.println(rrdDef.dump()); RrdDb rrdDb = new RrdDb(rrdDef); rrdDb.close(); println("== RRD file created and closed."); // update database GaugeSource sunSource = new GaugeSource(1200, 20); GaugeSource shadeSource = new GaugeSource(300, 10); println("== Simulating one month of RRD file updates with step not larger than " + MAX_STEP + " seconds (* denotes 1000 updates)"); long t = start; int n = 0; rrdDb = new RrdDb(rrdPath); Sample sample = rrdDb.createSample(); while(t <= end + 86400L) { sample.setTime(t); sample.setValue("sun", sunSource.getValue()); sample.setValue("shade", shadeSource.getValue()); log.println(sample.dump()); sample.update(); t += RANDOM.nextDouble() * MAX_STEP + 1; if(((++n) % 1000) == 0) { System.out.print("*"); }; } System.out.println(""); println("== Finished. RRD file updated " + n + " times"); println("== Last update time was: " + rrdDb.getLastUpdateTime()); // fetch data println("== Fetching data for the whole month"); FetchRequest request = rrdDb.createFetchRequest("AVERAGE", start, end); println(request.dump()); log.println(request.dump()); FetchData fetchData = request.fetchData(); println("== Data fetched. " + fetchData.getRowCount() + " points obtained"); for(int i = 0; i < fetchData.getRowCount(); i++) { println(fetchData.getRow(i).dump()); } println("== Dumping fetch data to XML format"); println(fetchData.exportXml()); println("== Fetch completed"); // dump to XML file println("== Dumping RRD file to XML file " + xmlPath + " (can be restored with RRDTool)"); rrdDb.exportXml(xmlPath); println("== Creating RRD file " + rrdRestoredPath + " from XML file " + xmlPath); RrdDb rrdRestoredDb = new RrdDb(rrdRestoredPath, xmlPath); // close files println("== Closing both RRD files"); rrdDb.close(); rrdRestoredDb.close(); // create graph println("== Creating graph from the second file"); RrdGraphDefTemplate graphTemplate = new RrdGraphDefTemplate(GRAPH_TEMPLATE); graphTemplate.setVariable("start", start); graphTemplate.setVariable("end", end); graphTemplate.setVariable("title", "Temperatures in May 2003"); graphTemplate.setVariable("rrd", rrdRestoredPath); // RPN expressions can be created at runtime and put into a template graphTemplate.setVariable("sine", "TIME," + start + ",-," + (end - start) + ",/,2,PI,*,*,SIN,1000,*"); // create graph finally RrdGraphDef gDef = graphTemplate.getRrdGraphDef(); RrdGraph graph = new RrdGraph(gDef); println("== Graph created"); println("== Saving graph as PNG file " + pngPath); graph.saveAsPNG(pngPath, 400, 250); println("== Saving graph as JPEG file " + jpegPath); graph.saveAsJPEG(jpegPath, 400, 250, 0.5F); println("== Saving graph as GIF file " + gifPath); graph.saveAsGIF(gifPath, 400, 250); // demo ends log.close(); println("== Demo completed in " + ((System.currentTimeMillis() - startMillis) / 1000.0) + " sec"); } static void println(String msg) { System.out.println(msg); } static class GaugeSource { private double value; private double step; GaugeSource(double value, double step) { this.value = value; this.step = step; } long getValue() { double oldValue = value; double increment = RANDOM.nextDouble() * step; if (RANDOM.nextDouble() > 0.5) { increment *= -1; } value += increment; if (value <= 0) { value = 0; } return Math.round(oldValue); } } } --- NEW FILE: ComplexDemo.java --- package org.jrobin.demo; /* ============================================================ * 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. * * 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. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * 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. */ import java.awt.Color; import java.util.Date; import java.util.GregorianCalendar; import java.text.SimpleDateFormat; import java.io.IOException; import org.jrobin.core.*; import org.jrobin.graph.*; /** * <p>Extended graphing demo for JRobin.</p> * * @author Arne Vandamme (cob...@jr...) */ class ComplexDemo { private static final String filename = "complexdemo"; private static String getPath(String ext) { return Util.getJRobinDemoPath(filename + "." + ext); } private static String getPath(int version, String ext) { return Util.getJRobinDemoPath(filename + version + "." + ext); } private static void println(String msg) { System.out.println(msg); } private static void createDatabase(String xmlPath) throws IOException, RrdException { // Import database from XML String rrdPath = getPath("rrd"); println("-- Importing XML file: " + xmlPath); println("-- to RRD file: " + rrdPath); RrdDbPool pool = RrdDbPool.getInstance(); RrdDb rrd = pool.requestRrdDb(rrdPath, xmlPath); println("-- RRD file created"); pool.release(rrd); } private static void createGraphs() throws RrdException, IOException { GregorianCalendar start, stop; RrdGraph graph = new RrdGraph(true); String rrdPath = getPath("rrd"); // Create traffic overview of a week println("-- Creating graph 1"); start = new GregorianCalendar(2003, 7, 20); stop = new GregorianCalendar(2003, 7, 27); RrdGraphDef def = new RrdGraphDef(start, stop); def.setImageBorder(Color.GRAY, 1); def.setTitle("JRobinComplexDemo@Ldemo graph 1@r\nNetwork traffic overview"); def.setVerticalLabel("bits per second"); def.datasource("ifInOctets", rrdPath, "ifInOctets", "AVERAGE"); def.datasource("ifOutOctets", rrdPath, "ifOutOctets", "AVERAGE"); def.datasource("bitIn", "ifInOctets,8,*"); def.datasource("bitOut", "ifOutOctets,8,*"); def.comment(" "); def.area("bitIn", new Color(0x00, 0xFF, 0x00), "Incoming traffic "); def.line("bitOut", new Color(0x00, 0x00, 0x33), "Outgoing traffic\n\n"); def.gprint("bitIn", "MAX", "Max: @6.1 @sbit/s"); def.gprint("bitOut", "MAX", " @6.1 @sbit/s\n"); def.gprint("bitIn", "MIN", "Min: @6.1 @sbit/s"); def.gprint("bitOut", "MIN", " @6.1 @sbit/s"); def.comment(" Connection: 100 Mbit/s\n"); def.gprint("bitIn", "AVG", "Avg: @6.1 @sbit/s"); def.gprint("bitOut", "AVG", " @6.1 @sbit/s"); def.comment(" Duplex mode: FD - fixed\n\n"); def.gprint("bitIn", "LAST", "Cur: @6.1 @sbit/s"); def.gprint("bitOut", "LAST", " @6.1 @sbit/s\n\n"); def.comment("[ courtesy of www.cherrymon.org ]@L"); def.comment("Generated: " + timestamp() + " @r"); graph.setGraphDef(def); String pngFile = getPath(1, "png"); graph.saveAsPNG(pngFile); String gifFile = getPath(1, "gif"); graph.saveAsGIF(gifFile); String jpgFile = getPath(1, "jpg"); graph.saveAsJPEG(jpgFile, 0.6F); // Create server load and cpu usage of a day println("-- Creating graph 2"); start = new GregorianCalendar(2003, 7, 19); stop = new GregorianCalendar(2003, 7, 20); def = new RrdGraphDef(start, stop); def.setImageBorder(Color.GRAY, 1); def.setTitle("JRobinComplexDemo@Ldemo graph 2@r\nServer load and CPU utilization"); def.datasource("load", rrdPath, "serverLoad", "AVERAGE"); def.datasource("user", rrdPath, "serverCPUUser", "AVERAGE"); def.datasource("nice", rrdPath, "serverCPUNice", "AVERAGE"); def.datasource("system", rrdPath, "serverCPUSystem", "AVERAGE"); def.datasource("idle", rrdPath, "serverCPUIdle", "AVERAGE"); def.datasource("total", "user,nice,+,system,+,idle,+"); def.datasource("busy", "user,nice,+,system,+,total,/,100,*"); def.datasource("p25t50", "busy,25,GT,busy,50,LE,load,0,IF,0,IF"); def.datasource("p50t75", "busy,50,GT,busy,75,LE,load,0,IF,0,IF"); def.datasource("p75t90", "busy,75,GT,busy,90,LE,load,0,IF,0,IF"); def.datasource("p90t100", "busy,90,GT,load,0,IF"); def.comment("CPU utilization (%)\n "); def.area("load", new Color(0x66, 0x99, 0xcc), " 0 - 25%"); def.area("p25t50", new Color(0x00, 0x66, 0x99), "25 - 50%@L"); def.gprint("busy", "MIN", "Minimum:@5.1@s%"); def.gprint("busy", "MAX", "Maximum:@5.1@s% @r "); def.area("p50t75", new Color(0x66, 0x66, 0x00), "50 - 75%"); def.area("p75t90", new Color(0xff, 0x66, 0x00), "75 - 90%"); def.area("p90t100", new Color(0xcc, 0x33, 0x00), "90 - 100%@L"); def.gprint("busy", "AVG", " Average:@5.1@s%"); def.gprint("busy", "LAST", "Current:@5.1@s% @r "); def.comment("\nServer load\n "); def.line("load", new Color(0x00, 0x00, 0x00), "Load average (5 min)@L"); def.gprint("load", "MIN", "Minimum:@5.2@s%"); def.gprint("load", "MAX", "Maximum:@5.2@s% @r "); def.gprint("load", "AVG", "Average:@5.2@s%"); def.gprint("load", "LAST", "Current:@5.2@s% @r"); def.comment("\n\n[ courtesy of www.cherrymon.org ]@L"); def.comment("Generated: " + timestamp() + " @r"); graph.setGraphDef(def); pngFile = getPath(2, "png"); graph.saveAsPNG(pngFile); gifFile = getPath(2, "gif"); graph.saveAsGIF(gifFile); jpgFile = getPath(2, "jpg"); graph.saveAsJPEG(jpgFile, 0.6F); // Create ftp graph for a month println("-- Creating graph 3"); start = new GregorianCalendar(2003, 7, 19, 12, 00); stop = new GregorianCalendar(2003, 7, 20, 12, 00); def = new RrdGraphDef(start, stop); def.setImageBorder(Color.GRAY, 1); def.setFrontGrid(false); def.setTitle("JRobinComplexDemo@Ldemo graph 3@r\nFTP Usage"); def.datasource("ftp", rrdPath, "ftpUsers", "AVERAGE"); def.line("ftp", new Color(0x00, 0x00, 0x33), "FTP connections"); def.gprint("ftp", "AVG", "( average: @0,"); def.gprint("ftp", "MIN", "never below: @0 )\n\n"); def.comment(" Usage spread:"); def.area(new GregorianCalendar(2003, 7, 19, 17, 00), Double.MIN_VALUE, new GregorianCalendar(2003, 7, 19, 23, 00), Double.MAX_VALUE, Color.RED, "peak period"); def.area(new GregorianCalendar(2003, 7, 20, 5, 00), Double.MIN_VALUE, new GregorianCalendar(2003, 7, 20, 8, 30), Double.MAX_VALUE, Color.LIGHT_GRAY, "quiet period\n"); def.comment(" Rise/descend:"); def.area("ftp", new Color(0x00, 0x00, 0x33), null); def.line(new GregorianCalendar(2003, 7, 19, 12, 00), 110, new GregorianCalendar(2003, 7, 19, 20, 30), 160, Color.PINK, "climb slope", 2); def.line(new GregorianCalendar(2003, 7, 19, 20, 30), 160, new GregorianCalendar(2003, 7, 20, 8, 00), 45, Color.CYAN, "fall-back slope\n", 2); def.vrule(new GregorianCalendar(2003, 7, 20), Color.YELLOW, null); def.comment("\n\n[ courtesy of www.cherrymon.org ]@L"); def.comment("Generated: " + timestamp() + " @r"); graph.setGraphDef(def); pngFile = getPath(3, "png"); graph.saveAsPNG(pngFile, 500, 300); gifFile = getPath(3, "gif"); graph.saveAsGIF(gifFile, 500, 300); jpgFile = getPath(3, "jpg"); graph.saveAsJPEG(jpgFile, 500, 300, 0.6F); println("-- Finished"); println("**************************************"); println("Check your " + Util.getJRobinDemoDirectory() + " directory."); println("You should see nine nice looking graphs starting with [" + filename + "],"); println("three different graphs, each in three different image formats"); println("**************************************"); } private static String timestamp() { SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm"); return df.format(new Date()); } public static void main(String[] args) throws IOException, RrdException { if(args.length == 0) { println("Usage: ComplexDemo [path to rrdtool_dump.xml file]"); println("You can download separate rrdtool_dump.xml file from:"); println("http://www.sourceforge.net/projects/jrobin"); System.exit(-1); } long start = System.currentTimeMillis(); println("********************************************************************"); println("* JRobinComplexDemo *"); println("* *"); println("* This demo creates 3 separate graphs and stores them under *"); println("* several formats in 9 files. Values are selected from a large *"); println("* RRD file that will be created by importing an XML dump *"); println("* of approx. 7 MB. *"); println("* *"); println("* Graphs are created using real-life values, original RRD file *"); println("* provided by www.cherrymon.org. See the ComplexDemo *"); println("* sourcecode on how to create the graphs generated by this demo. *"); println("********************************************************************"); createDatabase(args[0]); createGraphs(); long stop = System.currentTimeMillis(); println("-- Demo finished in " + ((stop - start) / 1000.0) + " seconds."); } } --- NEW FILE: PlottableDemo.java --- package org.jrobin.demo; /* ============================================================ * 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. * * 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. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * 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. */ import org.jrobin.graph.*; import org.jrobin.core.RrdException; import org.jrobin.core.Util; import java.awt.*; import java.io.IOException; import java.util.Date; import java.util.GregorianCalendar; import java.util.Calendar; class PlottableDemo { static final double[] SF_DOWNLOAD_COUNT = { 0, 0, 13, 34, 76, 72, 255, 144, 135, 194, 358, 304, 293 }; static final double[] SF_PAGE_HITS = { 0, 1072, 517, 979, 2132, 2532, 5515, 3519, 3500, 4942, 7858, 7797, 6570 }; static final GregorianCalendar[] SF_TIMESTAMPS = new GregorianCalendar[SF_DOWNLOAD_COUNT.length]; static final Date SF_START_DATE = new GregorianCalendar(2003, 4, 1).getTime(); // May 1st 2004. static { for(int i = 0; i < SF_TIMESTAMPS.length; i++) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(SF_START_DATE); gc.add(Calendar.MONTH, i); SF_TIMESTAMPS[i] = gc; } } private PlottableDemo() throws RrdException, IOException { createGraph1(); createGraph2(); createGraph3(); createGraph4(); createGraph5(); createGraph6(); createGraph7(); createGraph8(); createGraph9(); createGraph10(); createGraph11(); createGraph12(); createGraph13(); createGraph14(); createGraph15(); createGraph16(); } private void createGraph1() throws RrdException, IOException { final long t0 = new Date().getTime() / 1000L, dt = 86400L; final int n = 10; final long t1 = t0 + (n - 1) * dt; Plottable p = new Plottable() { public double getValue(long t) { double x = (t - t0) / (double) (t1 - t0); return Math.exp(-x * 2) * Math.cos(x * 7 * Math.PI); } }; long t[] = new long[n]; double x[] = new double[n]; for (int i = 0; i < n; i++) { t[i] = t0 + i * dt; x[i] = p.getValue(t[i]); } LinearInterpolator i1 = new LinearInterpolator(t, x); // defaults to INTERPOLATE_LINEAR CubicSplineInterpolator i2 = new CubicSplineInterpolator(t, x); // graph definition RrdGraphDef gdef = new RrdGraphDef(t0, t1); gdef.setTitle("Plottable demonstration"); gdef.setTimeAxisLabel("days of our lives"); gdef.setVerticalLabel("inspiration"); gdef.datasource("real", p); gdef.datasource("linear", i1); gdef.datasource("spline", i2); gdef.line("real", Color.BLUE, "Real values", 1); gdef.line("linear", Color.RED, "Linear interpolation", 1); gdef.line("spline", Color.MAGENTA, "Spline interpolation@r", 1); gdef.setTimeAxis(TimeAxisUnit.DAY, 1, TimeAxisUnit.DAY, 1, "dd", true); RrdGraph g = new RrdGraph(gdef); String filename = Util.getJRobinDemoPath("plottable1.png"); g.saveAsPNG(filename, 400, 200); System.out.println("Graph1 saved to " + filename); } private void createGraph2() throws RrdException, IOException { GregorianCalendar[] timestamps = { new GregorianCalendar(2004, 2, 1, 0, 0, 0), new GregorianCalendar(2004, 2, 1, 2, 0, 0), new GregorianCalendar(2004, 2, 1, 7, 0, 0), new GregorianCalendar(2004, 2, 1, 14, 0, 0), new GregorianCalendar(2004, 2, 1, 17, 0, 0), new GregorianCalendar(2004, 2, 1, 19, 0, 0), new GregorianCalendar(2004, 2, 1, 23, 0, 0), new GregorianCalendar(2004, 2, 1, 24, 0, 0) }; double[] values = {100, 250, 230, 370, 350, 300, 340, 350}; LinearInterpolator linear = new LinearInterpolator(timestamps, values); linear.setInterpolationMethod(LinearInterpolator.INTERPOLATE_LEFT); CubicSplineInterpolator spline = new CubicSplineInterpolator(timestamps, values); RrdGraphDef gDef = new RrdGraphDef(timestamps[0], timestamps[timestamps.length - 1]); gDef.setTitle("Plottable demonstration"); gDef.setTimeAxisLabel("time"); gDef.setVerticalLabel("water level [inches]"); gDef.datasource("linear", linear); gDef.datasource("spline", spline); gDef.area("spline", Color.ORANGE, "Spline interpolation"); gDef.line("linear", Color.RED, "Linear inteprolation@r", 2); gDef.gprint("spline", "AVERAGE", "Average spline value: @0 inches@r"); gDef.gprint("linear", "AVERAGE", "Average linear value: @0 inches@r"); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable2.png"); graph.saveAsPNG(filename, 300, 100); System.out.println("Graph2 saved to " + filename); } private void createGraph3() throws RrdException, IOException { LinearInterpolator linear = new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("JRobin page hits per month"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("page hits"); gDef.datasource("linear", linear); gDef.area("linear", Color.GREEN, null); gDef.line("linear", Color.RED, "page hits@L", 2); gDef.vrule(new GregorianCalendar(2004, 0, 1), Color.BLUE, null, 3); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); gDef.comment("Data provided by SourceForge.net@r"); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable3.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph3 saved to " + filename); } private void createGraph4() throws RrdException, IOException { LinearInterpolator linear = new LinearInterpolator(SF_TIMESTAMPS, SF_DOWNLOAD_COUNT); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("JRobin download count per month"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("download count"); gDef.datasource("linear", linear); gDef.area("linear", Color.GREEN, null); gDef.line("linear", Color.RED, "download count@L", 2); gDef.vrule(new GregorianCalendar(2004, 0, 1), Color.BLUE, null, 3); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); gDef.comment("Data provided by SourceForge.net@r"); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable4.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph4 saved to " + filename); } private void createGraph5() throws RrdException, IOException { LinearInterpolator hitsInterpolator = new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); LinearInterpolator downloadsInterpolator = new LinearInterpolator(SF_TIMESTAMPS, SF_DOWNLOAD_COUNT); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("JRobin statistics at SourceForge"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits/downloads"); gDef.datasource("hits", hitsInterpolator); gDef.datasource("downloads", downloadsInterpolator); gDef.datasource("ratio", "downloads,0,EQ,UNKN,hits,downloads,/,IF"); gDef.area("hits", Color.GREEN, null); gDef.line("hits", Color.RED, "page hits", 2); gDef.area("downloads", Color.MAGENTA, "downloads@L"); gDef.vrule(new GregorianCalendar(2004, 0, 1), Color.BLUE, null, 3); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); gDef.gprint("ratio", "AVERAGE", "Average number of page hits per download: @0@r"); gDef.comment("Data provided by SourceForge.net@r"); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable5.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph5 saved to " + filename); } private void createGraph6() throws RrdException, IOException { CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 1"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); gDef.datasource("hits2", "hits,1000,-"); gDef.datasource("invisible", "hits2,0,GE,hits2,0,IF"); gDef.datasource("margin", "hits,invisible,-"); gDef.area("invisible", null, null); gDef.stack("margin", Color.YELLOW, "yellow margin"); gDef.line("hits", Color.RED, "page hits", 3); gDef.line("hits", Color.WHITE, null, 1); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable6.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph6 saved to " + filename); } private void createGraph7() throws RrdException, IOException { CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 2"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); gDef.datasource("avg", "hits", "AVERAGE"); gDef.datasource("diff", "avg,hits,-"); gDef.datasource("diffpos", "diff,0,GE,diff,0,IF"); gDef.datasource("diffneg", "diff,0,LT,diff,0,IF"); gDef.area("hits", null, null); gDef.stack("diffpos", Color.RED, "bad"); gDef.stack("diffneg", Color.GREEN, "good"); gDef.line("hits", Color.BLUE, "hits", 3); gDef.line("hits", Color.WHITE, null, 1); gDef.line("avg", Color.MAGENTA, "average@L", 3); gDef.line("avg", Color.WHITE, null, 1); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); gDef.gprint("hits", "AVERAGE", "Average: @0@r"); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable7.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph7 saved to " + filename); } private void createGraph8() throws RrdException, IOException { GregorianCalendar[] times = { SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1] }; double[] values = { SF_PAGE_HITS[0], SF_PAGE_HITS[SF_PAGE_HITS.length - 1] }; LinearInterpolator trendLine = new LinearInterpolator(times, values); CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 3"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); gDef.datasource("trend", trendLine); gDef.datasource("diff", "trend,hits,-"); gDef.area("hits", null, null); gDef.stack("diff", Color.YELLOW, "difference"); gDef.line("hits", Color.BLUE, "hits"); gDef.line("trend", Color.RED, "trend@L"); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable8.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph8 saved to " + filename); } private void createGraph9() throws RrdException, IOException { final int GRADIENT_STEPS = 30; final Color color1 = Color.RED, color2 = Color.YELLOW; CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 4"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); for(int i = 0; i <= GRADIENT_STEPS; i++) { gDef.datasource("hits" + i, "hits," + i + ",*," + GRADIENT_STEPS + ",/"); } for(int i = GRADIENT_STEPS; i >=0 ; i--) { Color c = interpolateColor(color1, color2, i / (double) GRADIENT_STEPS); gDef.area("hits" + i, c, null); } gDef.line("hits", Color.BLACK, "Number of page hits"); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable9.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph9 saved to " + filename); } private void createGraph10() throws RrdException, IOException { final int GRADIENT_STEPS = 30; final Color color1 = Color.RED, color2 = Color.YELLOW; CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 5"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); for(int i = 0; i <= GRADIENT_STEPS; i++) { gDef.datasource("hits" + i, "hits," + i + ",*," + GRADIENT_STEPS + ",/"); } for(int i = GRADIENT_STEPS; i >= 0 ; i--) { Color c = interpolateColor(color1, color2, i / (double) GRADIENT_STEPS); gDef.area("hits" + i, c, null); } gDef.line("hits", color2, "Estimated number of page hits"); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); gDef.setCanvasColor(color1); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable10.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph10 saved to " + filename); } private void createGraph11() throws RrdException, IOException { final int GRADIENT_STEPS = 30; final Color color1 = Color.YELLOW, color2 = Color.RED; CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 6"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); gDef.datasource("top", "hits", "MAX"); for(int i = 1; i <= GRADIENT_STEPS; i++) { gDef.datasource("hits" + i, "hits,top," + i + ",*," + GRADIENT_STEPS + ",/,MIN"); } for(int i = GRADIENT_STEPS; i >= 1 ; i--) { Color c = i % 2 == 0? color1: color2; gDef.area("hits" + i, c, null); } gDef.line("hits", color2, "Estimated number of page hits"); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable11.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph11 saved to " + filename); } private void createGraph12() throws RrdException, IOException { final int GRADIENT_STEPS = 15; final Color color1 = Color.LIGHT_GRAY, color2 = Color.WHITE; CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 7"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); for(int i = GRADIENT_STEPS; i >= 1 ; i--) { Color c = interpolateColor(color1, color2, i / (double) GRADIENT_STEPS); gDef.line("hits", c, null, i); } gDef.line("hits", color1, "Estimated number of page hits"); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable12.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph12 saved to " + filename); } private void createGraph13() throws RrdException, IOException { final int GRADIENT_STEPS = 20; final double GRADIENT_WIDTH = 2000.0; final Color color1 = Color.RED, color2 = Color.WHITE; CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 8"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); for(int i = 0; i <= GRADIENT_STEPS; i++) { gDef.datasource("hits" + i, "hits," + GRADIENT_WIDTH + "," + i + ",*," + GRADIENT_STEPS + ",/,-,0,MAX"); } for(int i = 0; i <= GRADIENT_STEPS; i++) { gDef.area("hits" + i, interpolateColor(color1, color2, i / (double) GRADIENT_STEPS), null); } gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable13.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph13 saved to " + filename); } private void createGraph14() throws RrdException, IOException { final int STEPS = 20; final Color color1 = Color.BLACK, color2 = Color.RED; CubicSplineInterpolator hitsInterpolator = new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 9"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); gDef.datasource("maxhits", "hits", "MAX"); for(int i = 1; i <= STEPS; i++) { gDef.datasource("hits" + i, "maxhits," + i + ",*," + STEPS + ",/,hits,GE,hits,0,IF"); } for(int i = STEPS; i >= 1; i--) { gDef.area("hits" + i, interpolateColor(color1, color2, i / (double) STEPS), null); } gDef.line("hits", Color.BLUE, "page hits", 2); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable14.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph14 saved to " + filename); } private void createGraph15() throws RrdException, IOException { final int STEPS = 20; final Color color1 = Color.BLACK, color2 = Color.RED; LinearInterpolator hitsInterpolator = new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); hitsInterpolator.setInterpolationMethod(LinearInterpolator.INTERPOLATE_LEFT); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trick graph 10"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); gDef.datasource("maxhits", "hits", "MAX"); for(int i = 1; i <= STEPS; i++) { gDef.datasource("hits" + i, "maxhits," + i + ",*," + STEPS + ",/,hits,GE,hits,0,IF"); } for(int i = STEPS; i >= 1; i--) { gDef.area("hits" + i, interpolateColor(color1, color2, i / (double) STEPS), null); } gDef.line("hits", Color.BLUE, "page hits", 2); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable15.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph15 saved to " + filename); } private void createGraph16() throws RrdException, IOException { LinearInterpolator hitsInterpolator = new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); LinearInterpolator trendInterpolator = new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); trendInterpolator.setInterpolationMethod(LinearInterpolator.INTERPOLATE_REGRESSION); RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); gDef.setTitle("Trend report"); gDef.setTimeAxisLabel("month"); gDef.setVerticalLabel("hits"); gDef.datasource("hits", hitsInterpolator); gDef.datasource("trend", trendInterpolator); gDef.datasource("diff", "hits,trend,-"); gDef.datasource("absdiff", "diff,ABS"); gDef.area("trend", null, null); gDef.stack("diff", Color.YELLOW, "difference"); gDef.line("hits", Color.RED, "real page hits"); gDef.line("trend", Color.BLUE, "trend@L"); gDef.gprint("absdiff", "AVERAGE", "Average difference: @0@r"); gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); RrdGraph graph = new RrdGraph(gDef); String filename = Util.getJRobinDemoPath("plottable16.png"); graph.saveAsPNG(filename, 400, 200); System.out.println("Graph16 saved to " + filename); } private Color interpolateColor(Color c1, Color c2, double factor) { int r = c1.getRed() + (int)((c2.getRed() - c1.getRed()) * factor); int g = c1.getGreen() + (int)((c2.getGreen() - c1.getGreen()) *... [truncated message content] |