📄 rrdgraph.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)
* Arne Vandamme (cobralord@jrobin.org)
*
* (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 net.jumperz.ext.org.jrobin.graph;
import java.util.Iterator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import javax.imageio.IIOImage;
import javax.imageio.ImageWriter;
import javax.imageio.ImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.RenderedImage;
import java.awt.image.BufferedImage;
import java.awt.*;
import net.jumperz.ext.org.jrobin.core.RrdException;
import net.jumperz.ext.org.jrobin.core.RrdOpener;
/**
* <p>Class to represent JRobin graphs. This class needs an appropriate RrdGraphDef to generate graphs.</p>
*
* @author Arne Vandamme (cobralord@jrobin.org)
* @author Sasa Markovic (saxon@jrobin.org)
*/
public class RrdGraph extends RrdOpener implements Serializable
{
// ================================================================
// -- Members
// ================================================================
private Grapher grapher;
private BufferedImage img;
private boolean useImageSize = false;
// ================================================================
// -- Constructors
// ================================================================
/**
* Constructs a new JRobin graph object, without a shared database pool.
*/
public RrdGraph()
{
super( false, true );
}
/**
* Constructs a new JRobin graph object.
* @param usePool True if this object should use RrdDbPool
*/
public RrdGraph( boolean usePool )
{
super( usePool, true );
}
/**
* Constructs a new JRobin graph object from the supplied definition.
* @param graphDef Graph definition.
*/
public RrdGraph( RrdGraphDef graphDef )
{
this( graphDef, false );
}
/**
* Constructs a new JRobin graph from the supplied definition.
* @param graphDef Graph definition.
* @param usePool True if this should object should use RrdDbPool
*/
public RrdGraph( RrdGraphDef graphDef, boolean usePool )
{
super( usePool, true );
grapher = new Grapher( graphDef, this );
}
// ================================================================
// -- Public mehods
// ================================================================
/**
* Determines if graph creation should specify dimensions for the chart graphing
* are, of for the entire image size. Default is the only the chart graphing
* area, this has an impact on the entire image size.
* @param specImgSize True if the dimensions for the entire image will be specified, false if only for the chart area.
*/
public void specifyImageSize( boolean specImgSize )
{
this.useImageSize = specImgSize;
}
/**
* Sets the graph definition to use for the graph construction.
* @param graphDef Graph definition.
*/
public void setGraphDef( RrdGraphDef graphDef )
{
img = null;
grapher = new Grapher( graphDef, this );
}
/**
* Creates and saves a graph image with default dimensions as a PNG file.
* By default the chart area is 400 by 100 pixels, the size of the entire image is dependant
* on number of title/legend/comment lines and some other settings.
* @param path Path to the PNG file to be created.
* @throws IOException Thrown in case of I/O error.
* @throws RrdException Thrown in case of JRobin specific error.
*/
public void saveAsPNG( String path ) throws RrdException, IOException
{
saveAsPNG( path, 0, 0 );
}
/**
* Creates and saves a graph image with custom chart dimensions as a PNG file.
* The resulting size of the entire image is also influenced by many other settings like number of comment lines.
* @param path Path to the PNG file to be created.
* @param width Width of the chart area in pixels.
* @param height Height of the chart area in pixels.
* @throws IOException Thrown in case of I/O error.
* @throws RrdException Thrown in case of JRobin specific error.
*/
public void saveAsPNG( String path, int width, int height ) throws RrdException, IOException
{
File imgFile = new File( path );
if ( shouldGenerate(imgFile) )
ImageIO.write( getBufferedImage(width, height, BufferedImage.TYPE_INT_RGB), "png", imgFile );
}
/**
* Creates and saves a graph image with default dimensions as a GIF file.
* By default the chart area is 400 by 100 pixels, the size of the entire image is dependant
* on number of title/legend/comment lines and some other settings.
* @param path Path to the GIF file to be created.
* @throws IOException Thrown in case of I/O error.
* @throws RrdException Thrown in case of JRobin specific error.
*/
public void saveAsGIF( String path ) throws RrdException, IOException
{
saveAsGIF( path, 0, 0 );
}
/**
* Creates and saves a graph image with custom chart dimensions as a GIF file.
* The resulting size of the entire image is also influenced by many other settings like number of comment lines.
* @param path Path to the GIF file to be created.
* @param width Width of the chart area in pixels.
* @param height Height of the chart area in pixels.
* @throws IOException Thrown in case of I/O error.
* @throws RrdException Thrown in case of JRobin specific error.
*/
public void saveAsGIF(String path, int width, int height) throws RrdException, IOException
{
File imgFile = new File( path );
if ( shouldGenerate(imgFile) )
{
GifEncoder gifEncoder = new GifEncoder( getBufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED) );
FileOutputStream stream = new FileOutputStream( path, false );
gifEncoder.encode(stream);
stream.close();
}
}
/**
* Creates and saves a graph image with default dimensions as a JPEG file.
* By default the chart area is 400 by 100 pixels, the size of the entire image is dependant
* on number of title/legend/comment lines and some other settings.
* @param path Path to the JPEG file to be created.
* @param quality JPEG quality, between 0 (= low) and 1.0f (= high).
* @throws IOException Thrown in case of I/O error.
*/
public void saveAsJPEG( String path, float quality ) throws RrdException, IOException
{
saveAsJPEG( path, 0, 0, quality );
}
/**
* Creates and saves a graph image with custom chart dimensions as a JPEG file.
* The resulting size of the entire image is also influenced by many other settings like number of comment lines.
* @param path Path to the JPEG file to be created.
* @param width Width of the chart area in pixels.
* @param height Height of the chart area in pixels.
* @param quality JPEG quality, between 0 (= low) and 1.0f (= high).
* @throws IOException Thrown in case of I/O error.
*/
public void saveAsJPEG( String path, int width, int height, float quality ) throws RrdException, IOException
{
File imgFile = new File( path );
if ( !shouldGenerate(imgFile) )
return;
// Based on http://javaalmanac.com/egs/javax.imageio/JpegWrite.html?l=rel
// Retrieve jpg image to be compressed
RenderedImage rndImage = getBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Find a jpeg writer
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
writer = (ImageWriter)iter.next();
}
// Prepare output file
ImageOutputStream ios = ImageIO.createImageOutputStream(new File(path));
writer.setOutput(ios);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -