Update of /cvsroot/jrobin/src/org/jrobin/graph In directory sc8-pr-cvs1:/tmp/cvs-serv3268/src/org/jrobin/graph Modified Files: Legend.java RrdGraphDef.java Area.java Grapher.java Comment.java Line.java Cdef.java FetchSource.java Gprint.java PlotDef.java TimeAxisLabel.java Source.java CustomArea.java Stack.java Added Files: XmlGraphDefConvertor.java Plottable.java Pdef.java Log Message: JRobin 1.2.3-dev - Added Plottable interface, support for custom graph sources - Added basic XML input for RrdGraphDef - Added basic XML output for RrdGraphDef (getJRobinXml()) --- NEW FILE: XmlGraphDefConvertor.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...) * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@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. * * 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.graph; import javax.xml.parsers.*; import java.io.*; import java.awt.Color; import org.xml.sax.*; import org.xml.sax.helpers.*; import org.jrobin.core.RrdException; /** * <p>JRobin</p> * * @author Arne Vandamme <cob...@ch...> */ class XmlGraphDefConvertor extends DefaultHandler { // ---------------------------------------- private final static Byte BLOCK_ROOT = new Byte((byte)0); private final static Byte BLOCK_GENERAL = new Byte((byte)1); private final static Byte BLOCK_DATASOURCES = new Byte((byte)2); private final static Byte BLOCK_GRAPHING = new Byte((byte)3); private final static Byte EL_NONE = new Byte((byte)0); private final static Byte EL_DEF = new Byte((byte)1); private final static Byte EL_CDEF = new Byte((byte)2); private final static Byte EL_GENERAL = new Byte((byte)3); private final static Byte EL_DATASOURCES = new Byte((byte)4); private final static Byte EL_GRAPHING = new Byte((byte)5); private final static Byte EL_DEF_FILE = new Byte((byte)6); private final static Byte EL_DEF_DSNAME = new Byte((byte)7); private final static Byte EL_DEF_CF = new Byte((byte)8); private final static Byte EL_COMMENT = new Byte((byte)9); private final static Byte EL_LINE = new Byte((byte)10); private final static Byte EL_LINE_DS = new Byte((byte)11); private final static Byte EL_PERIOD = new Byte((byte)12); private final static Byte EL_LINE_LEGEND = new Byte((byte)13); private final static Byte EL_LINE_WIDTH = new Byte((byte)14); private final static Byte EL_PERIOD_START = new Byte((byte)15); private final static Byte EL_PERIOD_STOP = new Byte((byte)16); private final static Byte EL_LINE_COLOR = new Byte((byte)17); private final static Byte EL_AREA = new Byte((byte)18); private final static Byte EL_TITLE = new Byte((byte)19); private final static Byte EL_IMGBORDER = new Byte((byte)20); private final static Byte EL_BORDERWIDTH = new Byte((byte)21); private final static Byte EL_COLORS = new Byte((byte)22); private final static Byte EL_CLR_BACKGROUND = new Byte((byte)23); private final static Byte EL_CLR_CANVAS = new Byte((byte)24); private final static Byte EL_SIGNATURE = new Byte((byte)25); private RrdGraphDef def; private String[] params = new String[6]; private Color colorParam = null; private int intParam = 0; private int numParams = 1; private java.util.Stack tags = new java.util.Stack(); private int level = 0; private Byte block = BLOCK_ROOT; private Byte element = EL_NONE; XmlGraphDefConvertor( String xml, RrdGraphDef def ) throws RrdException { this.def = def; tags.push( EL_NONE ); try { SAXParserFactory factory = SAXParserFactory.newInstance(); // Parse the input SAXParser saxParser = factory.newSAXParser(); saxParser.parse( new ByteArrayInputStream( xml.getBytes() ), this ); } catch ( Exception e ) { //throw new RrdException( "Could not parse XML string." ); e.printStackTrace(); } } /** * */ public void startElement(String namespaceURI, String lName, String qName, Attributes attrs) throws SAXException { level++; Byte parent = (Byte) tags.peek(); if ( qName.equals("color") || ( parent == EL_COLORS && ( qName.equals("background") || qName.equals("canvas") ) ) ) { colorParam = new Color( Integer.parseInt(attrs.getValue("r")), Integer.parseInt(attrs.getValue("g")), Integer.parseInt(attrs.getValue("b")) ); numParams |= 2; } if ( parent == EL_COLORS ) { if ( qName.equals("background") ) tags.push( EL_CLR_BACKGROUND ); else if ( qName.equals("canvas") ) tags.push( EL_CLR_CANVAS ); } if ( parent == EL_DEF ) { if ( qName.equals("file") ) tags.push( EL_DEF_FILE ); else if ( qName.equals("ds-name") ) tags.push( EL_DEF_DSNAME ); else if ( qName.equals("cf") ) tags.push( EL_DEF_CF ); } else if ( parent == EL_LINE || parent == EL_AREA ) { if ( qName.equals("datasource") ) tags.push( EL_LINE_DS ); else if ( qName.equals("legend") ) tags.push( EL_LINE_LEGEND ); else if ( qName.equals("width") ) tags.push( EL_LINE_WIDTH ); else if ( qName.equals("color") ) tags.push( EL_LINE_COLOR ); } else if ( parent == EL_IMGBORDER ) { if ( qName.equals("width") ) tags.push( EL_BORDERWIDTH ); } else { if ( qName.equals("general") ) tags.push( EL_GENERAL ); else if ( qName.equals("datasources") ) tags.push( EL_DATASOURCES ); else if ( qName.equals("graphing") ) tags.push( EL_GRAPHING ); else if ( qName.equals("title") ) tags.push( EL_TITLE ); else if ( qName.equals("period") ) tags.push( EL_PERIOD ); else if ( qName.equals("start") ) tags.push( EL_PERIOD_START ); else if ( qName.equals("end") ) tags.push( EL_PERIOD_STOP ); else if ( qName.equals("def") ) // DEF { params[0] = "def"; params[1] = attrs.getValue("name"); tags.push( EL_DEF ); } else if ( qName.equals("cdef") ) // CDEF { params[0] = "cdef"; params[1] = attrs.getValue("name"); tags.push( EL_CDEF ); } else if ( qName.equals("comment") ) tags.push( EL_COMMENT ); else if ( qName.equals("line") ) tags.push( EL_LINE ); else if ( qName.equals("area") ) tags.push( EL_AREA ); else if ( qName.equals("colors") ) tags.push( EL_COLORS ); else if ( qName.equals("signature") ) tags.push( EL_SIGNATURE ); else if ( qName.equals("image-border") ) { tags.push( EL_IMGBORDER ); intParam = 1; // Default border of 1 } else tags.push( EL_NONE ); } } /** * */ public void endElement(String namespaceURI, String sName, String qName) throws SAXException { Byte tag = (Byte) tags.pop(); // Remove tag from the stack try { if ( qName.equals("general") || qName.equals("datasources") || qName.equals("graphing") ) block = BLOCK_ROOT; else if ( tag == EL_DEF || tag == EL_CDEF ) addDatasource(); else if ( tag == EL_LINE || tag == EL_AREA ) addPlotDef( tag ); else if ( tag == EL_PERIOD ) def.setTimePeriod( Long.parseLong(params[0]), Long.parseLong(params[1]) ); else if ( tag == EL_IMGBORDER ) def.setImageBorder( colorParam, intParam ); else if ( tag == EL_CLR_BACKGROUND ) def.setBackColor( colorParam ); else if ( tag == EL_CLR_CANVAS ) def.setCanvasColor( colorParam ); } catch ( RrdException e ) { throw new SAXException( e ); } level--; } /** * */ public void characters(char buf[], int offset, int len) throws SAXException { String value = new String(buf, offset, len); // remove whitespace Byte tag = (Byte) tags.peek(); if ( level < 3 ) return; try { if ( tag == EL_LINE_DS || tag == EL_PERIOD_START ) params[0] = value; else if ( tag == EL_LINE_LEGEND || tag == EL_PERIOD_STOP ) params[1] = value; else if ( tag == EL_CDEF || tag == EL_DEF_FILE ) // RPN expressions are param 2 params[2] = value; else if ( tag == EL_DEF_DSNAME ) params[3] = value; else if ( tag == EL_DEF_CF ) params[4] = value; else if ( tag == EL_COMMENT ) // Add comment def.comment( value ); else if ( tag == EL_TITLE ) // Set title def.setTitle( value ); else if ( tag == EL_SIGNATURE ) // Set signature visibility def.setShowSignature( !value.equalsIgnoreCase("no") ); else if ( tag == EL_LINE_WIDTH || tag == EL_BORDERWIDTH ) { intParam = Integer.parseInt( value ); numParams |= 4; } } catch ( Exception e ) { e.printStackTrace(); } } /** * * @throws RrdException */ private void addDatasource() throws RrdException { String dsType = params[0]; if ( dsType.equals("def") ) // name file dsname consolfunc def.datasource( params[1], params[2], params[3], params[4] ); else if ( dsType.equals("cdef") ) // name rpn def.datasource( params[1], params[2] ); } /** * * @throws RrdException */ private void addPlotDef( Byte graphType ) throws RrdException { int width = 1; Color color = null; if ( graphType == EL_LINE ) { if ( (numParams & 4) == 4 ) width = intParam; if ( (numParams & 2) == 2 ) color = colorParam; def.line( params[0], color, params[1], width ); } else if ( graphType == EL_AREA ) { if ( (numParams & 2) == 2 ) color = colorParam; def.area( params[0], color, params[1] ); } numParams = 0; } } --- NEW FILE: Plottable.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...) * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@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. * * 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.graph; /** * <p>Interface to be used for custom datasources. * If you wish to use a custom datasource in a graph, you should create a class implementing this interface * that represents that datasource, and then pass this class on to the RrdGraphDef. * </p> * * @author Arne Vandamme <cob...@ch...> */ public interface Plottable { /** * Retrieves datapoint value based on a given timestamp. * Use this method if you only have one series of data in this class. * @param timestamp Timestamp in seconds for the datapoint. * @return Double value of the datapoint. */ public double getValue( long timestamp ); /** * Retrieves datapoint value based on a given timestamp. * @param timestamp Timestamp in seconds for the datapoint. * @param index Integer referring to the series containing the specific datapoint. * @return Double value of the datapoint. */ public double getValue( long timestamp, int index ); /** * Retrieves datapoint value based on a given timestamp. * @param timestamp Timestamp in seconds for the datapoint. * @param fieldName String that refers to the series containing the datapoint. * @return Double value of the datapoint. */ public double getValue( long timestamp, String fieldName ); } --- NEW FILE: Pdef.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...) * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@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. * * 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.graph; /** * <p>Plottable Def, reprents a custom datasource that can be graphed by JRobin. * All the Pdef needs, is a reference to a Plottable class, and it will get the datapoint values (based * on timestamps) from that external class. Any class implementing the public Plottable interface will do, * meaning that the class could get its values from ANY source... like a RDBMS for example. * </p> * * @author Arne Vandamme <cob...@ch...> */ class Pdef extends Source { private Plottable plottable; private int index = 0; private String sourceName = null; private boolean indexed = false; private boolean named = false; /** * Constructs a new Plottable Def: a custom external datasource * (represented as a Plottable class) that can be graphed by JRobin. * @param name Name of the datasource in the graph definition. * @param plottable Reference to the class implementing the Plottable interface and providing the datapoints. */ Pdef( String name, Plottable plottable ) { super(name ); this.plottable = plottable; } /** * Constructs a new Plottable Def: a custom external datasource * (represented as a Plottable class) that can be graphed by JRobin. * @param name Name of the datasource in the graph definition. * @param plottable Reference to the class implementing the Plottable interface and providing the datapoints. * @param index Integer number used for referring to the series of datapoints to use in the Plottable class. */ Pdef( String name, Plottable plottable, int index ) { super(name ); this.plottable = plottable; this.index = index; indexed = true; } /** * Constructs a new Plottable Def: a custom external datasource * (represented as a Plottable class) that can be graphed by JRobin. * @param name Name of the datasource in the graph definition. * @param plottable Reference to the class implementing the Plottable interface and providing the datapoints. * @param sourceName String used for referring to the series of datapoints to use in the Plottable class. */ Pdef( String name, Plottable plottable, String sourceName) { super(name ); this.plottable = plottable; this.sourceName = sourceName; named = true; } /** * Prepares the array that will hold the values. * @param numPoints Number of datapoints that will be used. */ void prepare( int numPoints ) { // Create values table of correct size values = new double[numPoints]; } /** * Sets the value of a specific datapoint for this Pdef. The Pdef gets the datapoint by retrieving * the value from the Plottable interface using an appropriate getValue() method. * @param pos Position (index in the value table) of the new datapoint. * @param timestamp Timestamp of the new datapoint in number of seconds. */ void set( int pos, long timestamp ) { double val = Double.NaN; if ( indexed ) val = plottable.getValue( timestamp, index ); else if ( named ) val = plottable.getValue( timestamp, sourceName ); else val = plottable.getValue( timestamp ); super.set( pos, timestamp, val ); values[pos] = val; } } Index: Legend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Legend.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Legend.java 7 Nov 2003 08:23:19 -0000 1.1 --- Legend.java 15 Jan 2004 22:24:30 -0000 1.2 *************** *** 40,45 **** // -- Members // ================================================================ ! private Color color = Color.WHITE; ! // ================================================================ --- 40,45 ---- // -- Members // ================================================================ ! private Color color = Color.WHITE; ! private int refPlotDef = -1; // ================================================================ *************** *** 67,72 **** { super(text); ! this.commentType = Comment.CMT_LEGEND; ! this.color = color; } --- 67,89 ---- { super(text); ! if ( text == null ) ! this.commentType = Comment.CMT_NOLEGEND; ! else ! this.commentType = Comment.CMT_LEGEND; ! this.color = color; ! } ! ! /** ! * ! * @param text ! * @param color ! * @param referredPlotDef ! * @throws RrdException ! */ ! Legend( String text, Color color, int referredPlotDef ) throws RrdException ! { ! this( text, color ); ! ! refPlotDef = referredPlotDef; } *************** *** 78,80 **** --- 95,101 ---- return color; } + + int getPlofDefIndex() { + return refPlotDef; + } } Index: RrdGraphDef.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/RrdGraphDef.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdGraphDef.java 20 Nov 2003 09:28:00 -0000 1.3 --- RrdGraphDef.java 15 Jan 2004 22:24:30 -0000 1.4 *************** *** 33,36 **** --- 33,37 ---- import java.util.Vector; import java.util.HashMap; + import java.util.Iterator; import java.util.GregorianCalendar; import java.text.SimpleDateFormat; *************** *** 69,72 **** --- 70,74 ---- private Title title = null; // no title private String valueAxisLabel = null; // no vertical label + private TimeAxisLabel timeAxisLabel = null; // no horizontal label private boolean gridX = true; // hide entire X axis grid (default: no) *************** *** 115,118 **** --- 117,121 ---- private HashMap fetchSources = new HashMap(); // holds the list of FetchSources private Vector cdefList = new Vector(); // holds the list of Cdef datasources + private Vector pdefList = new Vector(); // holds the list of Plottable datasources private Vector plotDefs = new Vector(); // holds the list of PlotDefs private Vector comments = new Vector(); // holds the list of comment items *************** *** 127,130 **** --- 130,138 ---- public RrdGraphDef() { } + + public RrdGraphDef( String jrobinXml ) throws RrdException + { + new XmlGraphDefConvertor( jrobinXml, this ); + } /** *************** *** 170,173 **** --- 178,182 ---- /** * Sets time span to be presented on the graph using timestamps in number of seconds. + * An end time of 0 means JRobin will try to use the last available update time. * @param startTime Starting timestamp in seconds. * @param endTime Ending timestamp in secons. *************** *** 176,180 **** public void setTimePeriod( long startTime, long endTime ) throws RrdException { ! if ( startTime < 0 || endTime <= startTime ) throw new RrdException( "Invalid graph start/end time: " + startTime + "/" + endTime ); --- 185,189 ---- public void setTimePeriod( long startTime, long endTime ) throws RrdException { ! if ( startTime < 0 || ( endTime != 0 && endTime <= startTime ) ) throw new RrdException( "Invalid graph start/end time: " + startTime + "/" + endTime ); *************** *** 242,250 **** if ( label != null ) { ! TimeAxisLabel cmt = new TimeAxisLabel( label ); ! commentLines += cmt.getLineCount(); ! commentLineShift = (cmt.isCompleteLine() ? 0 : 1); ! comments.add( 0, cmt ); } } --- 251,259 ---- if ( label != null ) { ! timeAxisLabel = new TimeAxisLabel( label ); ! commentLines += timeAxisLabel.getLineCount(); ! commentLineShift = (timeAxisLabel.isCompleteLine() ? 0 : 1); ! comments.add( 0, timeAxisLabel ); } } *************** *** 651,654 **** --- 660,701 ---- /** + * <p>Adds a custom graph source with the given name to the graph definition. + * The datapoints should be made available by a class implementing the Plottable interface.</p> + * + * @param name Graph source name. + * @param plottable Class that implements plottable interface and is suited for graphing. + */ + public void datasource( String name, Plottable plottable ) + { + pdefList.add( new Pdef(name, plottable) ); + } + + /** + * <p>Adds a custom graph source with the given name to the graph definition. + * The datapoints should be made available by a class implementing the Plottable interface.</p> + * + * @param name Graph source name. + * @param plottable Class that implements plottable interface and is suited for graphing. + * @param index Integer referring to the datasource in the Plottable class. + */ + public void datasource( String name, Plottable plottable, int index ) + { + pdefList.add( new Pdef(name, plottable, index) ); + } + + /** + * <p>Adds a custom graph source with the given name to the graph definition. + * The datapoints should be made available by a class implementing the Plottable interface.</p> + * + * @param name Graph source name. + * @param plottable Class that implements plottable interface and is suited for graphing. + * @param sourceName String name referring to the datasource in the Plottable class. + */ + public void datasource( String name, Plottable plottable, String sourceName ) + { + pdefList.add( new Pdef(name, plottable, sourceName) ); + } + + /** * Adds line plot to the graph definition, using the specified color and legend. This method * takes exactly the same parameters as RRDTool's LINE1 directive (line width *************** *** 857,861 **** addComment( new Gprint(sourceName, consolFun, format) ); } ! // ================================================================ --- 904,1014 ---- addComment( new Gprint(sourceName, consolFun, format) ); } ! ! /** ! * ! * @return ! */ ! public String getJRobinXml() ! { ! StringBuffer xml = new StringBuffer( "" ); ! ! // -- Create the general block ! xml.append( "\t<general>\n" ); ! ! // Time period ! xml.append( "\t\t<period>\n" ); ! xml.append( "\t\t\t<start>" + startTime + "</start>\n" ); ! xml.append( "\t\t\t<end>" + endTime + "</end>\n" ); ! xml.append( "\t\t</period>\n" ); ! ! // Labels ! if ( title != null || valueAxisLabel != null || timeAxisLabel != null ) ! { ! xml.append( "\t\t<labels>\n" ); ! if ( title != null ) ! xml.append( "\t\t\t<title>" + title.text + "</title>\n" ); ! if ( valueAxisLabel != null ) ! xml.append( "\t\t\t<vertical-label>" + valueAxisLabel + "</vertical-label>\n" ); ! if ( timeAxisLabel != null ) ! xml.append( "\t\t\t<horizontal-label>" + timeAxisLabel.text + "</horizontal-label>\n" ); ! xml.append( "\t\t</labels>\n" ); ! } ! ! // General colors ! xml.append( "\t\t<colors>\n" ); ! xml.append( "\t\t\t<background r=\"" + backColor.getRed() + "\" g=\"" + backColor.getGreen() + "\" b=\"" + backColor.getBlue() + "\" />\n" ); ! xml.append( "\t\t\t<canvas r=\"" + canvasColor.getRed() + "\" g=\"" + canvasColor.getGreen() + "\" b=\"" + canvasColor.getBlue() + "\" />\n" ); ! xml.append( "\t\t</colors>\n" ); ! ! // General data information ! ! // General visual options ! xml.append( "\t\t<visual>\n" ); ! xml.append( "\t\t\t<show-legend>" + (showLegend ? "yes" : "no") + "</show-legend>\n" ); ! if ( background != null ) ! xml.append( "\t\t\t<background-image>" + background.getAbsolutePath() + "</background-image>\n" ); ! if ( overlay != null ) ! xml.append( "\t\t\t<overlay-image>" + overlay.getAbsolutePath() + "</overlay-image>\n" ); ! if ( borderStroke != null ) ! { ! xml.append( "\t\t\t<image-border>\n" ); ! xml.append( "\t\t\t\t<width>" + borderStroke.getLineWidth() + "</width>\n" ); ! xml.append( "\t\t\t\t<color r=\"" + borderColor.getRed() + "\" g=\"" + borderColor.getGreen() + "\" b=\"" + borderColor.getBlue() + "\" />\n" ); ! xml.append( "\t\t\t</image-border>\n" ); ! } ! if ( gridRange != null ) ! { ! xml.append( "\t\t\t<grid>\n" ); ! xml.append( "\t\t\t\t<lower>" + gridRange.getLowerValue() + "</lower>\n" ); ! xml.append( "\t\t\t\t<upper>" + gridRange.getUpperValue() + "</upper>\n" ); ! xml.append( "\t\t\t\t<rigid>" + (gridRange.isRigid() ? "yes" : "no") + "</rigid>\n" ); ! xml.append( "\t\t\t</grid>\n" ); ! } ! //xml.append( "\t\t\t<canvas r=\"" + canvasColor.getRed() + "\" g=\"" + canvasColor.getGreen() + "\" b=\"" + canvasColor.getBlue() + "\" />\n" ); ! xml.append( "\t\t</visual>\n" ); ! ! xml.append( "\t</general>\n" ); ! // ------------------------------------------------------------- ! ! ! // -- Create the datasources block ! xml.append( "\t<datasources>\n" ); ! ! // Add all defs ! Iterator fsIterator = fetchSources.values().iterator(); ! while ( fsIterator.hasNext() ) ! xml.append( ((FetchSource) fsIterator.next()).getXml() ); ! ! // Add all cdefs ! for ( int i = 0; i < cdefList.size(); i++ ) ! xml.append( ((Cdef) cdefList.elementAt(i)).getXml() ); ! ! xml.append( "\t</datasources>\n" ); ! // ------------------------------------------------------------- ! ! ! // -- Create the graphing block ! // Run through the comments, if its a legend or nolegend, get the plotdef xml ! // else get the comment or gprint xml ! xml.append( "\t<graphing>\n" ); ! for ( int i = 0; i < comments.size(); i++ ) ! { ! Comment cmt = (Comment) comments.elementAt(i); ! if ( cmt.commentType == Comment.CMT_LEGEND || cmt.commentType == Comment.CMT_NOLEGEND ) ! { ! PlotDef pDef = (PlotDef) plotDefs.elementAt( ((Legend) cmt).getPlofDefIndex() ); ! xml.append( pDef.getXml(cmt.text) ); ! } ! else ! xml.append( cmt.getXml() ); ! } ! xml.append( "\t</graphing>\n" ); ! // ------------------------------------------------------------- ! ! xml.insert( 0, "<jrobin-graph>\n"); ! xml.append( "</jrobin-graph>\n" ); ! ! return xml.toString(); ! } // ================================================================ *************** *** 1039,1042 **** --- 1192,1200 ---- } + protected Pdef[] getPdefs() + { + return (Pdef[]) pdefList.toArray( new Pdef[] {} ); + } + protected HashMap getFetchSources() { *************** *** 1057,1062 **** private void addLegend( String legend, Color color ) throws RrdException { ! if ( legend != null && color != null ) ! addComment( new Legend(legend, color) ); } } --- 1215,1221 ---- private void addLegend( String legend, Color color ) throws RrdException { ! // Always add the item, even if it's empty, always add the index ! // of the graph this legend is for ! addComment( new Legend(legend, color, plotDefs.size() - 1 ) ); } } Index: Area.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Area.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Area.java 20 Nov 2003 09:28:00 -0000 1.2 --- Area.java 15 Jan 2004 22:24:30 -0000 1.3 *************** *** 122,124 **** --- 122,139 ---- } } + + String getXml( String legend ) + { + StringBuffer xml = new StringBuffer(); + + xml.append( "\t\t<area>\n" ); + xml.append( "\t\t\t<datasource>" + sourceName + "</datasource>\n" ); + if ( color != null ) + xml.append( "\t\t\t<color r=\"" + color.getRed() + "\" g=\"" + color.getGreen() + "\" b=\"" + color.getBlue() + "\" />\n" ); + if ( legend != null ) + xml.append( "\t\t\t<legend>" + legend + "</legend>\n" ); + xml.append( "\t\t</area>\n" ); + + return xml.toString(); + } } Index: Grapher.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Grapher.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Grapher.java 20 Nov 2003 20:56:32 -0000 1.5 --- Grapher.java 15 Jan 2004 22:24:30 -0000 1.6 *************** *** 101,104 **** --- 101,106 ---- private TimeGrid tGrid; + private long calculatedEndTime; + // ================================================================ *************** *** 277,283 **** --- 279,288 ---- RrdDb rrd; String[] varList; + long finalEndTime = 0; + boolean changingEndTime = false; long startTime = graphDef.getStartTime(); long endTime = graphDef.getEndTime(); + changingEndTime = (endTime == 0); int numDefs = graphDef.getNumDefs(); *************** *** 285,292 **** Cdef[] cdefList = graphDef.getCdefs(); int numCdefs = cdefList.length; ! // Set up the array with all datasources (both Def and Cdef) ! sources = new Source[ numDefs + numCdefs ]; ! sourceIndex = new HashMap( numDefs + numCdefs ); int tblPos = 0; int vePos = 0; --- 290,300 ---- Cdef[] cdefList = graphDef.getCdefs(); int numCdefs = cdefList.length; + + Pdef[] pdefList = graphDef.getPdefs(); + int numPdefs = pdefList.length; ! // Set up the array with all datasources (both Def, Cdef and Pdef) ! sources = new Source[ numDefs + numCdefs + numPdefs ]; ! sourceIndex = new HashMap( numDefs + numCdefs + numPdefs ); int tblPos = 0; int vePos = 0; *************** *** 299,304 **** // Get the rrdDb src = (FetchSource) fetchSources.next(); ! rrd = rrdGraph.getRrd( src.getRrdFile() ); ! // Fetch all required datasources ve = src.fetch( rrd, startTime, endTime ); --- 307,321 ---- // Get the rrdDb src = (FetchSource) fetchSources.next(); ! rrd = rrdGraph.getRrd( src.getRrdFile() ); ! ! // If the endtime is 0, use the last time a database was updated ! if ( changingEndTime ) { ! endTime = rrd.getLastUpdateTime(); ! endTime -= (endTime % rrd.getHeader().getStep()); ! ! if ( endTime > finalEndTime ) ! finalEndTime = endTime; ! } ! // Fetch all required datasources ve = src.fetch( rrd, startTime, endTime ); *************** *** 313,316 **** --- 330,342 ---- } + // Add all Pdefs to the source table + for ( int i = 0; i < pdefList.length; i++ ) + { + pdefList[i].prepare( numPoints ); + + sources[tblPos] = pdefList[i]; + sourceIndex.put( pdefList[i].getName(), new Integer(tblPos++) ); + } + // Add all Cdefs to the source table // Reparse all RPN datasources to use indices of the correct variables *************** *** 328,332 **** // Fill the array for all datasources timestamps = new long[numPoints]; ! for (int i = 0; i < numPoints; i++) { --- 354,363 ---- // Fill the array for all datasources timestamps = new long[numPoints]; ! ! if ( changingEndTime ) { ! endTime = finalEndTime; ! calculatedEndTime = endTime; ! } ! for (int i = 0; i < numPoints; i++) { *************** *** 337,341 **** for (int j = 0; j < veList.length; j++) pos = veList[j].extract( t, sources, i, pos ); ! // Get all combined datasources for (int j = pos; j < sources.length; j++) --- 368,377 ---- for (int j = 0; j < veList.length; j++) pos = veList[j].extract( t, sources, i, pos ); ! ! // Get all custom datasources ! for (int j = pos; j < pos + numPdefs; j++) ! ((Pdef) sources[j]).set( i, t ); ! pos += numPdefs; ! // Get all combined datasources for (int j = pos; j < sources.length; j++) *************** *** 482,486 **** vGrid = new ValueGrid( range, lowerValue, upperValue, graphDef.getValueAxis() ); ! tGrid = new TimeGrid( graphDef.getStartTime(), graphDef.getEndTime(), graphDef.getTimeAxis() ); lowerValue = vGrid.getLowerValue(); --- 518,522 ---- vGrid = new ValueGrid( range, lowerValue, upperValue, graphDef.getValueAxis() ); ! tGrid = new TimeGrid( graphDef.getStartTime(), ( graphDef.getEndTime() != 0 ? graphDef.getEndTime() : calculatedEndTime ), graphDef.getTimeAxis() ); lowerValue = vGrid.getLowerValue(); *************** *** 741,744 **** --- 777,781 ---- // Plot the string if ( drawText ) { + graphString( g, tmpStr.toString(), posx, posy ); tmpStr = new StringBuffer(""); Index: Comment.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Comment.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Comment.java 7 Nov 2003 08:23:19 -0000 1.1 --- Comment.java 15 Jan 2004 22:24:30 -0000 1.2 *************** *** 42,45 **** --- 42,46 ---- protected static final int CMT_LEGEND = 1; protected static final int CMT_GPRINT = 2; + protected static final int CMT_NOLEGEND = 3; protected static final Byte TKN_ALF = new Byte( (byte) 1); // Align left with Linefeed *************** *** 78,82 **** { this.text = text; ! parseComment(); } --- 79,85 ---- { this.text = text; ! ! if ( text != null ) ! parseComment(); } *************** *** 246,248 **** --- 249,260 ---- return trimString; } + + String getXml() + { + StringBuffer xml = new StringBuffer(); + + xml.append( "\t\t<comment>" + text + "</comment>\n" ); + + return xml.toString(); + } } Index: Line.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Line.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Line.java 20 Nov 2003 09:28:00 -0000 1.2 --- Line.java 15 Jan 2004 22:24:30 -0000 1.3 *************** *** 131,133 **** --- 131,149 ---- return lineWidth; } + + String getXml( String legend ) + { + StringBuffer xml = new StringBuffer(); + + xml.append( "\t\t<line>\n" ); + xml.append( "\t\t\t<datasource>" + sourceName + "</datasource>\n" ); + if ( color != null ) + xml.append( "\t\t\t<color r=\"" + color.getRed() + "\" g=\"" + color.getGreen() + "\" b=\"" + color.getBlue() + "\" />\n" ); + if ( legend != null ) + xml.append( "\t\t\t<legend>" + legend + "</legend>\n" ); + xml.append( "\t\t\t<width>" + lineWidth + "</width>\n" ); + xml.append( "\t\t</line>\n" ); + + return xml.toString(); + } } Index: Cdef.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Cdef.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Cdef.java 7 Nov 2003 09:53:35 -0000 1.2 --- Cdef.java 15 Jan 2004 22:24:30 -0000 1.3 *************** *** 208,211 **** --- 208,232 ---- } + String getRpnString() + { + StringBuffer tmpStr = new StringBuffer(""); + for (int i = 0; i < strTokens.length - 1; i++) { + tmpStr.append( strTokens[i] ); + tmpStr.append( ',' ); + } + if ( strTokens.length > 0 ) + tmpStr.append( strTokens[strTokens.length - 1] ); + + return tmpStr.toString(); + } + + public String getXml() + { + StringBuffer xml = new StringBuffer( "" ); + + xml.append( "\t\t<cdef name=\"" + this.getName() + "\">" + getRpnString() + "</cdef>\n"); + + return xml.toString(); + } // ================================================================ Index: FetchSource.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/FetchSource.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FetchSource.java 7 Nov 2003 08:23:19 -0000 1.1 --- FetchSource.java 15 Jan 2004 22:24:30 -0000 1.2 *************** *** 92,95 **** --- 92,115 ---- } + public String getXml() + { + StringBuffer xml = new StringBuffer( "" ); + + for ( int i = 0; i < datasources.length; i++ ) { + for ( int j = 0; j < datasources[i].size(); j++ ) { + String[] pair = (String[]) datasources[i].elementAt(j); + + xml.append( "\t\t<def name=\"" + pair[1] + "\">\n" ); + xml.append( "\t\t\t<file>" + rrdFile + "</file>\n" ); + xml.append( "\t\t\t<ds-name>" + pair[0] + "</ds-name>\n" ); + xml.append( "\t\t\t<cf>" + cfNames[i] + "</cf>\n" ); + xml.append( "\t\t</def>\n" ); + //<file>test.rrd</file> + //<ds-name>ifInOctets</ds-name> + //<cf>AVERAGE</cf> + } + } + return xml.toString(); + } // ================================================================ Index: Gprint.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Gprint.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Gprint.java 7 Nov 2003 08:23:19 -0000 1.1 --- Gprint.java 15 Jan 2004 22:24:30 -0000 1.2 *************** *** 175,177 **** --- 175,193 ---- } + /** + * + */ + String getXml() + { + StringBuffer xml = new StringBuffer(); + + xml.append( "\t\t<gprint>\n" ); + xml.append( "\t\t\t<datasource>" + sourceName + "</datasource>\n" ); + xml.append( "\t\t\t<cf>" + Source.aggregates[aggregate] + "</cf>\n" ); + xml.append( "\t\t\t<text>" + text + "</text>\n" ); + xml.append( "\t\t</gprint>\n" ); + + return xml.toString(); + } + } Index: PlotDef.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/PlotDef.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PlotDef.java 7 Nov 2003 08:23:19 -0000 1.1 --- PlotDef.java 15 Jan 2004 22:24:30 -0000 1.2 *************** *** 129,132 **** --- 129,138 ---- abstract void draw( ChartGraphics g, int[] xValues, int[] stackValues, int lastPlotType ) throws RrdException; + /** + * Abstract getXml method, must be implemented in all child classes. + * This method is reponsible for returning a corresponding JRobin XML string for the PlotDef. + */ + abstract String getXml( String legend ); + Source getSource() { return source; Index: TimeAxisLabel.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/TimeAxisLabel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** TimeAxisLabel.java 20 Nov 2003 09:28:00 -0000 1.1 --- TimeAxisLabel.java 15 Jan 2004 22:24:30 -0000 1.2 *************** *** 60,62 **** --- 60,66 ---- } } + + String getXml() { + return ""; + } } Index: Source.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Source.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Source.java 24 Nov 2003 10:15:43 -0000 1.3 --- Source.java 15 Jan 2004 22:24:30 -0000 1.4 *************** *** 43,46 **** --- 43,47 ---- protected static final int AGG_LAST = 4; + protected static final String[] aggregates = { "MINIMUM", "MAXIMUM", "AVERAGE", "FIRST", "LAST" }; private String name; protected double[] values; Index: CustomArea.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/CustomArea.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** CustomArea.java 7 Nov 2003 08:23:19 -0000 1.1 --- CustomArea.java 15 Jan 2004 22:24:30 -0000 1.2 *************** *** 168,170 **** --- 168,185 ---- void setSource( Source[] sources, HashMap sourceIndex ) throws RrdException { } + + String getXml( String legend ) + { + StringBuffer xml = new StringBuffer(); + + xml.append( "\t\t<custom-area>\n" ); + xml.append( "\t\t\t<datasource>" + sourceName + "</datasource>\n" ); + if ( color != null ) + xml.append( "\t\t\t<color r=\"" + color.getRed() + "\" g=\"" + color.getGreen() + "\" b=\"" + color.getBlue() + "\" />\n" ); + if ( legend != null ) + xml.append( "\t\t\t<legend>" + legend + "</legend>\n" ); + xml.append( "\t\t</custom-area>\n" ); + + return xml.toString(); + } } Index: Stack.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/graph/Stack.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Stack.java 7 Nov 2003 08:23:19 -0000 1.1 --- Stack.java 15 Jan 2004 22:24:30 -0000 1.2 *************** *** 82,84 **** --- 82,102 ---- } + + /** + * + */ + String getXml( String legend ) + { + StringBuffer xml = new StringBuffer(); + + xml.append( "\t\t<stack>\n" ); + xml.append( "\t\t\t<datasource>" + sourceName + "</datasource>\n" ); + if ( color != null ) + xml.append( "\t\t\t<color r=\"" + color.getRed() + "\" g=\"" + color.getGreen() + "\" b=\"" + color.getBlue() + "\" />\n" ); + if ( legend != null ) + xml.append( "\t\t\t<legend>" + legend + "</legend>\n" ); + xml.append( "\t\t</stack>\n" ); + + return xml.toString(); + } } |