⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rrdgraph.java

📁 httptunnel.jar httptunnel java 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		// Set the compression quality
		ImageWriteParam iwparam = new JpegImageWriteParam();
		iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
		iwparam.setCompressionQuality(quality);

		// Write the image
		writer.write(null, new IIOImage(rndImage, null, null), iwparam);

		// Cleanup
		ios.flush();
		writer.dispose();
		ios.close();
	}
	
	/**
	 * Returns graph with default chart dimensions (400 by 100) as an array of PNG bytes.
	 * @return Array of PNG bytes.
	 * @throws IOException Thrown in case of I/O error.
	 */
	public byte[] getPNGBytes() throws IOException, RrdException
	{
		return getPNGBytes( 0, 0 );
	}
	
	/**
	 * Returns graph with custom chart dimensions as an array of PNG bytes.
	 * @param width Width of the chart area in pixels.
	 * @param height Height of the chart area in pixels.
	 * @return Array of PNG bytes.
	 * @throws IOException Thrown in case of I/O error.
	 */
	public byte[] getPNGBytes( int width, int height ) throws IOException, RrdException
	{
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		
		ImageIO.write(getBufferedImage(width, height, BufferedImage.TYPE_INT_RGB), "png", outputStream );
				
		return outputStream.toByteArray();
	}

	/**
	 * Returns graph with default chart dimensions (400 by 100) as an array of JPEG bytes.
	 * @param quality JPEG quality, between 0 (= low) and 1.0f (= high).
	 * @return Array of PNG bytes.
	 * @throws IOException Thrown in case of I/O error.
	 */
	public byte[] getJPEGBytes( float quality ) throws IOException, RrdException
	{
		return getJPEGBytes( 0, 0, quality );
	}
	
	/**
	 * Returns graph with custom chart dimensions as an array of JPEG bytes.
	 * @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).
	 * @return Array of JPEG bytes.
	 * @throws IOException Thrown in case of I/O error.
	 */
	public byte[] getJPEGBytes( int width, int height, float quality ) throws IOException, RrdException
	{
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		
		// 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(outputStream);
		writer.setOutput(ios);

		// Set the compression quality
		ImageWriteParam iwparam = new JpegImageWriteParam();
		iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
		iwparam.setCompressionQuality(quality);

		// Write the image
		writer.write(null, new IIOImage(rndImage, null, null), iwparam);

		// Cleanup
		ios.flush();
		writer.dispose();
		ios.close();
		
		return outputStream.toByteArray();
	}

	/**
	 * Returns graph with default chart dimensions (400 by 100) as an array of GIF bytes.
	 * @return Array of GIF bytes.
	 * @throws IOException Thrown in case of I/O error.
	 */
	public byte[] getGIFBytes() throws RrdException, IOException
	{
		return getGIFBytes( 0, 0 );	
	}
	
	/**
	 * Returns graph with custom chart dimensions as an array of GIF bytes.
	 * @param width Width of the chart area in pixels.
	 * @param height Height of the chart area in pixels.
	 * @return Array of GIF bytes.
	 * @throws IOException Thrown in case of I/O error.
	 */
	public byte[] getGIFBytes(int width, int height) throws RrdException, IOException
	{
		BufferedImage image 			= getBufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED);
		ByteArrayOutputStream bStream 	= new ByteArrayOutputStream();
	
		GifEncoder gifEncoder 			= new GifEncoder( image );
		gifEncoder.encode( bStream );
		
		return bStream.toByteArray();
	}

	/**
	 * Returns the underlying BufferedImage of a graph with custom dimensions.
	 * Specifying 0 for both width and height will result in a auto-sized graph.
	 * @param width Width of the chart area in pixels.
	 * @param height Height of the chart area in pixels.
	 * @return BufferedImage containing the graph.
	 * @throws IOException Thrown in case of I/O error.
	 * @throws RrdException Thrown in case of JRobin specific error.
	 */
	public BufferedImage getBufferedImage( int width, int height ) throws IOException, RrdException
	{
		return getBufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
	}

	/**
	 * Returns panel object so that graph can be easily embedded in swing applications.
	 * @return Swing JPanel object with graph embedded in panel.
	 */
	public ChartPanel getChartPanel() throws RrdException, IOException
	{
		ChartPanel p = new ChartPanel();
		p.setChart( getBufferedImage(0, 0, BufferedImage.TYPE_INT_RGB) );
		
		return p;
	}

	/**
	 * Renders the graph onto a specified Graphics2D object.
	 * Specifying 0 for both width and height will result in a auto-sized graph.
	 * @param graphics Handle to a Graphics2D object to render the graph on.
	 * @param width Width of the chart area in pixels.
	 * @param height Height of the chart area in pixels.
	 * @throws RrdException Thrown in case of JRobin specific error.
	 * @throws IOException Thrown in case of I/O error
	 */
	public void renderImage( Graphics2D graphics, int width, int height ) throws RrdException, IOException
	{
		if ( useImageSize )
			grapher.renderImage( width, height, graphics, true );
		else
			grapher.renderImage( width, height, graphics, false );
	}

	/**
	 * This retrieves the ExportData object associated with the reduced dataset of this Graph.
	 * This method assumes the graph or at least the dataset has already been calculated.
	 *
	 * @return ExportData object containing the reduced dataset.
	 * @throws RrdException Thrown in case of JRobin specific error.
	 */
	public ExportData getExportData() throws RrdException {
		return grapher.createExportData();
	}

	/**
	 * This retrieves the ExportData object associated with the reduced dataset of this Graph,
	 * by calculating the dataset on the spot.  Use this if you want to retrieve the associated
	 * ExportData without generating the actual graph.
	 *
	 * @return ExportData object containing the reduced dataset.
	 * @throws RrdException Thrown in case of JRobin specific error.
	 * @throws IOException Thrown in case of I/O error
	 */
	public ExportData fetchExportData() throws RrdException, IOException {
		return grapher.fetch( Grapher.DEFAULT_WIDTH );
	}

	/**
	 * This retrieves the ExportData object associated with the reduced dataset of this Graph,
	 * by calculating the dataset on the spot.  Use this if you want to retrieve the associated
	 * ExportData without generating the actual graph, or if you wish to re-calculate the
	 * associated dataset for a different number of rows.
	 *
	 * @param maxRows Ballpark figure 'maximum number of rows' that the dataset can contain.
	 * 				  Note that this is not an absolute maximum and can be overruled in some cases.
	 * @return ExportData object containing the reduced dataset.
	 * @throws RrdException Thrown in case of JRobin specific error.
	 * @throws IOException Thrown in case of I/O error
	 */
	public ExportData fetchExportData( int maxRows ) throws RrdException, IOException {
		return grapher.fetch( maxRows );
	}

	// ================================================================
	// -- Private methods
	// ================================================================
	/**
	 * This method checks if the graph should be generated.  This would be the case if the requested
	 * image file does not yet exist, or (in case the generation is set to be lazy) the last modified
	 * timestamp of the image file is before the last updated timestamp of the used datasources.
	 * @param imgFile Image file to check against.
	 * @return True if graph generation should be done, false if not.
	 * @throws IOException Thrown in case of I/O error.
	 * @throws RrdException Thrown in case of JRobin specific error.
	 */
	private boolean shouldGenerate( File imgFile ) throws RrdException, IOException
	{
		if ( !imgFile.exists() )
			return true;

		return grapher.shouldGenerate( imgFile.lastModified() );
	}

	private BufferedImage getBufferedImage(int width, int height, int colorType) throws RrdException, IOException
	{
		// Always regenerate graph
		if ( useImageSize )
			img = grapher.createImageGlobal( width, height, colorType );
		else
			img = grapher.createImage( width, height, colorType );
		
		return img;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -