📄 rrdtoolgraph.java
字号:
package net.jumperz.ext.org.jrobin.graph;
import net.jumperz.ext.org.jrobin.core.RrdException;
import java.awt.*;
/**
* Created by IntelliJ IDEA.
* User: cbld
* Date: 25-mei-2004
* Time: 21:02:56
* To change this template use File | Settings | File Templates.
*/
public class RrdtoolGraph
{
// ================================================================
// -- Constants
// ================================================================
private static final int SAVE_PNG = 0;
private static final int SAVE_GIF = 1;
private static final int DS_DEF = 0;
private static final int DS_CDEF = 1;
private static final int TXT_COMMENT = 0;
private static final int TXT_GPRINT = 1;
private static final int GRAPH_LINE = 0;
private static final int GRAPH_AREA = 1;
private static final int GRAPH_STACK = 2;
private static final int TKN_IGNORE = -2;
private static final int TKN_UNKNOWN = -1;
private static final int TKN_RRDTOOL = 0;
private static final int TKN_GRAPH = 1;
private static final int TKN_START = 2;
private static final int TKN_END = 3;
private static final int TKN_COMMENT = 4;
private static final int TKN_LINE = 5;
private static final int TKN_AREA = 6;
private static final int TKN_STACK = 7;
private static final int TKN_CDEF = 8;
private static final int TKN_DEF = 9;
private static final int TKN_GPRINT = 11;
private static final int TKN_HRULE = 12;
private static final int TKN_VRULE = 13;
private static final int TKN_STEP = 14;
private static final int TKN_TITLE = 15;
private static final int TKN_NOLEGEND = 16;
private static final int TKN_COLOR = 17;
private static final int TKN_RIGID = 18;
private static final int TKN_LOWERLIMIT = 19;
private static final int TKN_UPPERLIMIT = 20;
private static final int TKN_LAZY = 21;
private static final int TKN_OVERLAY = 23;
private static final int TKN_BACKGROUND = 24;
private static final int TKN_IMGFORMAT = 25;
private static final int TKN_WIDTH = 26;
private static final int TKN_HEIGHT = 27;
private static final int TKN_VERT_LABEL = 28;
private static final int TKN_UNITS_EXP = 29;
private static final int TKN_NOMINOR = 30;
private static final int TKN_XGRID = 31;
private static final int TKN_YGRID = 32;
private static final int TKN_BASE = 33;
// ================================================================
// -- Members
// ================================================================
private String token = "";
private String script = null;
private RrdGraphDef graphDef = null;
private int tokenPos = 0;
private char[] parseCmd = new char[0];
private boolean gridRigid = false;
private double gridLower = Double.MAX_VALUE;
private double gridUpper = Double.MIN_VALUE;
private int width = 0;
private int height = 0;
private int fileType = SAVE_PNG;
private String fileName = "";
// ================================================================
// -- Constructors
// ================================================================
public RrdtoolGraph( String script )
{
this.script = script;
}
// ================================================================
// -- Public methods
// ================================================================
public RrdGraphDef getRrdGraphDef() throws RrdException
{
parseRrdtoolScript();
return graphDef;
}
// ================================================================
// -- Private methods
// ================================================================
/**
*
* @return
* @throws RrdException
*/
private boolean parseRrdtoolScript() throws RrdException
{
long startTime = 0, stopTime = 0;
graphDef = new RrdGraphDef();
// First replace all special whitespace chars by a space
parseCmd = script.replace( '\n', ' ' ).replace( '\r', ' ' ).replace( '\t', ' ' ).toCharArray();
while ( nextToken() > 0 )
{
System.err.println( token );
switch ( parseToken( token ) )
{
case TKN_RRDTOOL: // We don't care about this token
break;
case TKN_GRAPH: // Next token is the filename
nextToken();
break;
case TKN_START: // Next token is the start time
nextToken();
startTime = Long.parseLong( token );
break;
case TKN_END: // Next token is the end time
nextToken();
stopTime = Long.parseLong( token );
break;
case TKN_NOMINOR: // Hide the entire minor grid
graphDef.setMinorGridX( false );
graphDef.setMinorGridY( false );
break;
case TKN_WIDTH: // Next token is graph pixel width
nextToken();
width = Integer.parseInt( token );
break;
case TKN_HEIGHT: // Next token is graph pixel height
nextToken();
height = Integer.parseInt( token );
break;
case TKN_UNITS_EXP: // Next token is the units exponent value
nextToken();
graphDef.setUnitsExponent( Integer.parseInt( token ) );
break;
case TKN_VERT_LABEL: // Next token is the actual vertical label text
nextToken();
graphDef.setVerticalLabel( unescape(token) );
break;
case TKN_TITLE: // Next token is the actual title text
nextToken();
graphDef.setTitle( unescape(token) );
break;
case TKN_IMGFORMAT: // Next token is the file type
nextToken();
if ( token.equalsIgnoreCase("gif") )
fileType = SAVE_GIF;
break;
case TKN_BACKGROUND: // Next token is the filename of background image
nextToken();
graphDef.setBackground( unescape(token) );
break;
case TKN_OVERLAY: // Next token is the filename of background image
nextToken();
graphDef.setOverlay( unescape(token) );
break;
case TKN_NOLEGEND: // Hide the legend
graphDef.setShowLegend( false );
break;
case TKN_LOWERLIMIT: // Next token is the lower limit value
nextToken();
gridLower = Double.parseDouble(token);
break;
case TKN_UPPERLIMIT: // Next token is the upper limit value
nextToken();
gridUpper = Double.parseDouble(token);
break;
case TKN_RIGID: // Set rigid grid
gridRigid = true;
break;
case TKN_BASE: // Set base value
nextToken();
graphDef.setBaseValue( Double.parseDouble(token) );
break;
case TKN_COMMENT:
parseTextCommand( TXT_COMMENT );
break;
case TKN_GPRINT:
parseTextCommand( TXT_GPRINT );
break;
case TKN_LINE:
parseGraphCommand( GRAPH_LINE );
break;
case TKN_AREA:
parseGraphCommand( GRAPH_AREA );
break;
case TKN_STACK:
parseGraphCommand( GRAPH_STACK );
break;
case TKN_DEF:
parseDatasource( DS_DEF );
break;
case TKN_CDEF:
parseDatasource( DS_CDEF );
break;
case TKN_IGNORE: // Do nothing
break;
case TKN_UNKNOWN:
throw new RrdException( "Unknown token: " + token );
}
}
// Set grid range if necessary
if ( gridRigid || ( gridLower == Double.MAX_VALUE ) || ( gridUpper == Double.MIN_VALUE ) )
graphDef.setGridRange( gridLower, gridUpper, gridRigid );
return true;
}
/**
*
* @param type
* @throws RrdException
*/
private void parseGraphCommand( int type ) throws RrdException
{
if ( type == GRAPH_LINE )
{
int w = Integer.parseInt( "" + token.charAt( 4 ) );
// Get the datasource
int pos = token.indexOf( '#', 6 );
int npos = token.indexOf( ':', 6 );
if ( pos < 0 ) pos = npos;
String ds = ( pos > 0 ? token.substring( 6, pos ) : token.substring( 6 ) );
Color color = null;
String legend = null;
// Get the color
if ( pos > 0 && token.charAt(pos) == '#' )
color = Color.decode( npos > 0 ? token.substring( pos, npos ) : token.substring( pos ) );
// Get the legend (if there is one)
if ( npos > 0 )
legend = unescape( token.substring( npos + 1 ) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -