📄 rrdgraphdef.java
字号:
/* ============================================================
* JRobin : Pure java implementation of RRDTool's functionality
* ============================================================
*
* Project Info: http://www.jrobin.org
* Project Lead: Sasa Markovic (saxon@jrobin.org)
*
* Developers: Sasa Markovic (saxon@jrobin.org)
*
*
* (C) Copyright 2003-2005, 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 org.jrobin.core.RrdException;
import org.jrobin.core.Util;
import org.jrobin.data.Plottable;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
* Class which should be used to define new JRobin graph. Once constructed and populated with data
* object of this class should be passed to the constructor of the {@link RrdGraph} class which
* will actually create the graph.
* <p/>
* The text printed below the actual graph can be formated by appending
* special escaped characters at the end of a text. When ever such a
* character occurs, all pending text is pushed onto the graph according to
* the character specified.
* <p/>
* Valid markers are: \j for justified, \l for left aligned, \r for right
* aligned and \c for centered.
* <p/>
* Normally there are two space characters inserted between every two
* items printed into the graph. The space following a string can be
* suppressed by putting a \g at the end of the string. The \g also squashes
* any space inside the string if it is at the very end of the string.
* This can be used in connection with %s to suppress empty unit strings.
* <p/>
* A special case is COMMENT:\s this inserts some additional vertical
* space before placing the next row of legends.
* <p/>
* When text has to be formated without special instructions from your
* side, RRDTool will automatically justify the text as soon as one string
* goes over the right edge. If you want to prevent the justification
* without forcing a newline, you can use the special tag \J at the end of
* the string to disable the auto justification.
*/
public class RrdGraphDef implements RrdGraphConstants {
boolean poolUsed = false; // ok
boolean antiAliasing = false; // ok
String filename = RrdGraphConstants.IN_MEMORY_IMAGE; // ok
long startTime, endTime; // ok
TimeAxisSetting timeAxisSetting = null; // ok
ValueAxisSetting valueAxisSetting = null; // ok
boolean altYGrid = false; // ok
boolean noMinorGrid = false; // ok
boolean altYMrtg = false; // ok
boolean altAutoscale = false; // ok
boolean altAutoscaleMax = false; // ok
int unitsExponent = Integer.MAX_VALUE; // ok
int unitsLength = DEFAULT_UNITS_LENGTH; // ok
String verticalLabel = null; // ok
int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; // ok
boolean interlaced = false; // ok
String imageInfo = null; // ok
String imageFormat = DEFAULT_IMAGE_FORMAT; // ok
float imageQuality = DEFAULT_IMAGE_QUALITY; // ok
String backgroundImage = null; // ok
String overlayImage = null; // ok
String unit = null; // ok
boolean lazy = false; // ok
double minValue = Double.NaN; // ok
double maxValue = Double.NaN; // ok
boolean rigid = false; // ok
double base = DEFAULT_BASE; // ok
boolean logarithmic = false; // ok
Paint[] colors = new Paint[] {
// ok
DEFAULT_CANVAS_COLOR,
DEFAULT_BACK_COLOR,
DEFAULT_SHADEA_COLOR,
DEFAULT_SHADEB_COLOR,
DEFAULT_GRID_COLOR,
DEFAULT_MGRID_COLOR,
DEFAULT_FONT_COLOR,
DEFAULT_FRAME_COLOR,
DEFAULT_ARROW_COLOR
};
boolean noLegend = false; // ok
boolean onlyGraph = false; // ok
boolean forceRulesLegend = false; // ok
String title = null; // ok
long step = 0; // ok
Font smallFont = RrdGraphConstants.DEFAULT_SMALL_FONT; // ok
Font largeFont = RrdGraphConstants.DEFAULT_LARGE_FONT; // ok
boolean drawXGrid = true; // ok
boolean drawYGrid = true; // ok
int firstDayOfWeek = FIRST_DAY_OF_WEEK; // ok
boolean showSignature = true;
List<Source> sources = new ArrayList<Source>();
List<CommentText> comments = new ArrayList<CommentText>();
List<PlotElement> plotElements = new ArrayList<PlotElement>();
/**
* Creates RrdGraphDef object and sets default time span (default ending time is 'now',
* default starting time is 'end-1day'.
*/
public RrdGraphDef() {
try {
setTimeSpan(Util.getTimestamps(DEFAULT_START, DEFAULT_END));
}
catch (RrdException e) {
throw new RuntimeException(e);
}
}
/**
* Sets the time when the graph should begin. Time in seconds since epoch
* (1970-01-01) is required. Negative numbers are relative to the current time.
*
* @param time Starting time for the graph in seconds since epoch
*/
public void setStartTime(long time) {
this.startTime = time;
if (time <= 0) {
this.startTime += Util.getTime();
}
}
/**
* Sets the time when the graph should end. Time in seconds since epoch
* (1970-01-01) is required. Negative numbers are relative to the current time.
*
* @param time Ending time for the graph in seconds since epoch
*/
public void setEndTime(long time) {
this.endTime = time;
if (time <= 0) {
this.endTime += Util.getTime();
}
}
/**
* Sets starting and ending time for the for the graph. Timestamps in seconds since epoch are
* required. Negative numbers are relative to the current time.
*
* @param startTime Starting time in seconds since epoch
* @param endTime Ending time in seconds since epoch
*/
public void setTimeSpan(long startTime, long endTime) {
setStartTime(startTime);
setEndTime(endTime);
}
/**
* Sets starting and ending time for the for the graph. Timestamps in seconds since epoch are
* required.
*
* @param timestamps Array of timestamps. The first array item will be chosen for the starting
* timestamp. The last array item will be chosen for the ending timestamp.
*/
public void setTimeSpan(long[] timestamps) {
setTimeSpan(timestamps[0], timestamps[timestamps.length - 1]);
}
/**
* Sets RrdDbPool usage policy (defaults to true). If set to true,
* {@link org.jrobin.core.RrdDbPool RrdDbPool} will be used to
* access individual RRD files. If set to false, RRD files will be accessed directly.
*
* @param poolUsed true, if RrdDbPool class should be used. False otherwise.
*/
public void setPoolUsed(boolean poolUsed) {
this.poolUsed = poolUsed;
}
/**
* Sets the name of the graph to generate. Since JRobin outputs GIFs, PNGs,
* and JPEGs it's recommended that the filename end in either .gif,
* .png or .jpg. JRobin does not enforce this, however. If the filename is
* set to '-' the image will be created only in memory (no file will be created).
* PNG and GIF formats are recommended but JPEGs should be avoided.
*
* @param filename Path to the image file
*/
public void setFilename(String filename) {
this.filename = filename;
}
/**
* Configures x-axis grid and labels. The x-axis label is quite complex to configure.
* So if you don't have very special needs, you can rely on the autoconfiguration to
* get this right.
* <p/>
* Otherwise, you have to configure three elements making up the x-axis labels
* and grid. The base grid, the major grid and the labels.
* The configuration is based on the idea that you first specify a well
* known amount of time and then say how many times
* it has to pass between each minor/major grid line or label. For the label
* you have to define two additional items: The precision of the label
* in seconds and the format used to generate the text
* of the label.
* <p/>
* For example, if you wanted a graph with a base grid every 10 minutes and a major
* one every hour, with labels every hour you would use the following
* x-axis definition.
* <p/>
* <pre>
* setTimeAxis(RrdGraphConstants.MINUTE, 10,
* RrdGraphConstants.HOUR, 1,
* RrdGraphConstants.HOUR, 1,
* 0, "%H:%M")
* </pre>
* <p/>
* The precision in this example is 0 because the %X format is exact.
* If the label was the name of the day, we would have had a precision
* of 24 hours, because when you say something like 'Monday' you mean
* the whole day and not Monday morning 00:00. Thus the label should
* be positioned at noon. By defining a precision of 24 hours or
* rather 86400 seconds, you make sure that this happens.
*
* @param minorUnit Minor grid unit. Minor grid, major grid and label units
* can be one of the following constants defined in
* {@link RrdGraphConstants}: {@link RrdGraphConstants#SECOND SECOND},
* {@link RrdGraphConstants#MINUTE MINUTE}, {@link RrdGraphConstants#HOUR HOUR},
* {@link RrdGraphConstants#DAY DAY}, {@link RrdGraphConstants#WEEK WEEK},
* {@link RrdGraphConstants#MONTH MONTH}, {@link RrdGraphConstants#YEAR YEAR}.
* @param minorUnitCount Number of minor grid units between minor grid lines.
* @param majorUnit Major grid unit.
* @param majorUnitCount Number of major grid units between major grid lines.
* @param labelUnit Label unit.
* @param labelUnitCount Number of label units between labels.
* @param labelSpan Label precision
* @param simpleDateFormat Date format (SimpleDateFormat pattern of strftime-like pattern)
*/
public void setTimeAxis(int minorUnit, int minorUnitCount, int majorUnit, int majorUnitCount,
int labelUnit, int labelUnitCount, int labelSpan, String simpleDateFormat) {
timeAxisSetting = new TimeAxisSetting(minorUnit, minorUnitCount, majorUnit, majorUnitCount,
labelUnit, labelUnitCount, labelSpan, simpleDateFormat);
}
/**
* Sets vertical axis grid and labels. Makes vertical grid lines appear
* at gridStep interval. Every labelFactor*gridStep, a major grid line is printed,
* along with label showing the value of the grid line.
*
* @param gridStep Minor grid step
* @param labelFactor Specifies how many minor minor grid steps will appear between labels
* (major grid lines)
*/
public void setValueAxis(double gridStep, int labelFactor) {
valueAxisSetting = new ValueAxisSetting(gridStep, labelFactor);
}
/**
* Places Y grid dynamically based on graph Y range. Algorithm ensures
* that you always have grid, that there are enough but not too many
* grid lines and the grid is metric. That is grid lines are placed
* every 1, 2, 5 or 10 units.
*
* @param altYGrid true, if Y grid should be calculated dynamically (defaults to false)
*/
public void setAltYGrid(boolean altYGrid) {
this.altYGrid = altYGrid;
}
/**
* Use this method to turn off minor grid lines (printed by default)
*
* @param noMinorGrid true, to turn off, false to turn on (default)
*/
public void setNoMinorGrid(boolean noMinorGrid) {
this.noMinorGrid = noMinorGrid;
}
/**
* Use this method to request MRTG-like graph (false by default)
*
* @param altYMrtg true, to create MRTG-like graph, false otherwise (default)
*/
public void setAltYMrtg(boolean altYMrtg) {
this.altYMrtg = altYMrtg;
}
/**
* Computes Y range based on function absolute minimum and maximum
* values. Default algorithm uses predefined set of ranges. This is
* good in many cases but it fails miserably when you need to graph
* something like 260 + 0.001 * sin(x). Default algorithm will use Y
* range from 250 to 300 and on the graph you will see almost straight
* line. With --alt-autoscale Y range will be from slightly less the
* 260 - 0.001 to slightly more then 260 + 0.001 and periodic behavior
* will be seen.
*
* @param altAutoscale true to request alternative autoscaling, false otherwise
* (default).
*/
public void setAltAutoscale(boolean altAutoscale) {
this.altAutoscale = altAutoscale;
}
/**
* Computes Y range based on function absolute minimum and maximum
* values. Where setAltAutoscale(true) will modify both the absolute maximum AND
* minimum values, this option will only affect the maximum value. The
* minimum value, if not defined elsewhere, will be 0. This
* option can be useful when graphing router traffic when the WAN line
* uses compression, and thus the throughput may be higher than the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -