天天看點

SNMP之JRobin畫圖

[color=red]blog遷移至[/color]:[url=http://www.micmiu.com]http://www.micmiu.com[/url]

本文主要記錄下學習如何運用JRboin畫圖的一些測試代碼。

本次測試畫圖用到的測試資料demo_flow.rrd檔案,是來自之前寫的[url=http://sjsky.iteye.com/admin/blogs/811393]SNMP系列之(一)JRobin Core學習[/url]中的方法生成的,可供大家參考。JRboin的畫圖主要從下面兩方面:

[list]

[*][color=blue]直接定義RrdGraphDef對象畫圖[/color]

[*][color=blue]根據定義好的模闆XML檔案生成圖檔[/color][/list]

先看下兩中方法生成的圖檔效果圖:

[img]http://dl.iteye.com/upload/attachment/351073/dfeadeaa-1b79-38f5-a78b-ca867e38de45.png[/img]

[img]http://dl.iteye.com/upload/attachment/351075/14d7e2f5-92a0-3a88-8469-b996a04532ab.png[/img]

下面是我學習時生成圖檔的測試代碼以及定義的模闆檔案:

package com.snmp.jrobin;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.jrobin.core.Util;
import org.jrobin.graph.RrdGraph;
import org.jrobin.graph.RrdGraphDef;
import org.jrobin.graph.RrdGraphDefTemplate;
import org.xml.sax.InputSource;

/**
 * @author Michael
 * 
 */
public class TestGraphCommon {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String rootPath = "d:/test/jrobin/";
        String imgFileName = "demo_graph_rrd.png";

        TestGraphCommon test = new TestGraphCommon();

        // 測試直接定義畫圖模闆生成圖檔
        test.graphByGraphDef(rootPath, imgFileName);

        String tempName = "graph-def-template.xml";
        // 測試根據定義的XML模闆檔案生成圖檔
        test.graphByTemplate(rootPath, tempName);
    }

    /**
     * 直接定義畫圖模闆生成圖檔
     * @param rootPath
     * @param imgFileName
     */
    private void graphByGraphDef(String rootPath, String imgFileName) {
        try {
            System.out.println("[rrd graph by RrdGraphDef start...]");
            // 2010-10-01:1285862400L 2010-11-01:1288540800L
            long startTime = Util.getTimestamp(2010, 10 - 1, 1);
            long endTime = Util.getTimestamp(2010, 10 - 1, 31);

            RrdGraphDef rgdef = new RrdGraphDef();
            // If the filename is set to '-' the image will be created only in
            // memory (no file will be created).
            // rgdef.setFilename("-");
            rgdef.setFilename(rootPath + imgFileName);

            // "PNG", "GIF" or "JPG"
            rgdef.setImageFormat("PNG");
            // rgdef.setTimeSpan(startTime, endTime);
            rgdef.setStartTime(startTime);
            rgdef.setEndTime(endTime);

            rgdef.setAntiAliasing(false);
            rgdef.setSmallFont(new Font("Monospaced", Font.PLAIN, 11));
            rgdef.setLargeFont(new Font("SansSerif", Font.BOLD, 14));

            rgdef.setTitle("JRobin graph by RrdGraphDef demo");
            // default 400
            rgdef.setWidth(500);
            // default 100
            rgdef.setHeight(200);

            // 一般不需要設定這個參數
            // rgdef.setStep(86400);

            rgdef.setVerticalLabel("transfer speed [bits/sec]");

            rgdef.datasource("in", rootPath + "demo_flow.rrd", "input",
                    "AVERAGE");
            rgdef.datasource("out", rootPath + "demo_flow.rrd", "output",
                    "AVERAGE");
            rgdef.datasource("in8", "in,8,*");
            rgdef.datasource("out8", "out,8,*");
            // PS:先畫域的再畫線的,否則線會被域遮蓋
            rgdef.area("out8", new Color(0, 206, 0), "output traffic");
            rgdef.line("in8", Color.BLUE, "input traffic\\l");

            // \\l->左對齊 \\c->中間對齊 \\r->右對齊 \\j->自适應
            // \\s-> \\g->glue \\J->
            rgdef.gprint("in8", "MAX", "maxIn=%.2f %sbits/sec");
            rgdef.gprint("out8", "MAX", "maxOut=%.2f %sbits/sec\\l");
            rgdef.gprint("in8", "AVERAGE", "avgIn=%.2f %sbits/sec");
            rgdef.gprint("out8", "AVERAGE", "avgOut=%.2f %sbits/sec\\l");
            rgdef.gprint("in8", "TOTAL", "totalIn=%.2f %sbits/sec");
            rgdef.gprint("out8", "TOTAL", "totalOut=%.2f %sbits/sec\\s");
            rgdef.comment("畫圖測試");

            RrdGraph graph = new RrdGraph(rgdef);
            System.out.println("[rrd graph info:]"
                    + graph.getRrdGraphInfo().dump());
            // 如果filename沒有設定,隻是在記憶體中,可以調用下面的方法再次生成圖檔檔案
            if ("-".equals(graph.getRrdGraphInfo().getFilename())) {
                createImgFile(graph, rootPath + imgFileName);
            }
            System.out.println("[rrd graph by RrdGraphDef success.]");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * ImageIO 生成圖檔檔案
     */
    private void createImgFile(RrdGraph graph, String imgFileFullName) {

        try {
            BufferedImage image = new BufferedImage(graph.getRrdGraphInfo()
                    .getWidth(), graph.getRrdGraphInfo().getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            graph.render(image.getGraphics());
            File imgFile = new File(imgFileFullName);
            ImageIO.write(image, "PNG", imgFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根據定義的XML模闆生成圖檔
     * @param rootPath
     * @param imgFileName
     */
    private void graphByTemplate(String rootPath, String tempName) {
        try {
            System.out.println("[rrd graph by xml template start...]");
            RrdGraphDefTemplate defTemplate = new RrdGraphDefTemplate(
                    new InputSource(rootPath + tempName));
            // setVariable 設定XML template的變量
            defTemplate.setVariable("startTime", "20101001 00:00");
            defTemplate.setVariable("endTime", "20101031 23:59");
            defTemplate.setVariable("in_rrd_file", rootPath + "demo_flow.rrd");
            defTemplate.setVariable("out_rrd_file", rootPath + "demo_flow.rrd");
            RrdGraph graph = new RrdGraph(defTemplate.getRrdGraphDef());

            BufferedImage image = new BufferedImage(graph.getRrdGraphInfo()
                    .getWidth(), graph.getRrdGraphInfo().getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            graph.render(image.getGraphics());
            File imgFile = new File(rootPath + "demo_graph_tmp.PNG");
            ImageIO.write(image, "PNG", imgFile);//
            System.out.println("[rrd graph by xml template success.]");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}